H5 canvas学习----案例(二)钟表

H5 canvas学习----案例(二)钟表

一.钟表刻度的制作:
1.

<canvas id='myCanvas'></canvas>
    <script>
        var myCanvas = document.getElementById('myCanvas');
        myCanvas.width = 1000;
        myCanvas.height = 700;
        var x = 200;
        var y = 200;
        var r = 200;
        var cvs = myCanvas.getContext('2d');
        for (var i = 0; i < 60; i++) {
            cvs.moveTo(x, y);
            //此处可以画出半径,使圆心与弧的起始点相连
            cvs.arc(x, y, r, i * 6 * Math.PI / 180, (i + 1) * 6 * Math.PI / 180)
        }
        cvs.strokeStyle = "#000";
        cvs.stroke();

在这里插入图片描述

cvs.beginPath();//用圆覆盖
        cvs.arc(x, y, r * (19 / 20), 0, 2 * Math.PI)
        cvs.fillStyle = '#fff';
        cvs.fill();
        cvs.strokeStyle = '#fff'
        cvs.stroke();

在这里插入图片描述

cvs.beginPath();
        for (var i = 0; i < 60; i++) {
            cvs.moveTo(x, y);
            cvs.arc(x, y, r, i * 30 * Math.PI / 180, (i + 1) * 30 * Math.PI / 180)
        }
        cvs.lineWidth = 3;
        cvs.strokeStyle = "#000";
        cvs.stroke();

在这里插入图片描述

cvs.beginPath();
        cvs.beginPath();
        cvs.arc(x, y, r * (18 / 20), 0, 2 * Math.PI)
        cvs.fillStyle = '#fff';
        cvs.fill();
        cvs.strokeStyle = '#fff'
        cvs.stroke();

在这里插入图片描述
二.时针,分针和秒针:

1.绘制时针分针,秒针

        //时针
        cvs.beginPath();
        cvs.moveTo(x, y);
        cvs.arc(x, y, r * (8 / 20), 50, 50);
        cvs.lineWidth = 7;
        cvs.strokeStyle = "#000";
        cvs.stroke();


        //分针
        cvs.beginPath();
        cvs.moveTo(x, y);
        cvs.arc(x, y, r * (13 / 20), 86, 86);
        cvs.lineWidth = 4;
        cvs.strokeStyle = "#000";
        cvs.stroke();


        //秒针
        cvs.beginPath();
        cvs.moveTo(x, y);
        cvs.arc(x, y, r * (17 / 20), 20, 20);
        cvs.lineWidth = 1;
        cvs.strokeStyle = "#000";
        cvs.stroke();

在这里插入图片描述
2.具体确定时针分针秒针的位置:

        var x = 200,
            y = 200,
            r = 200,
            date = new Date(),
            hours = date.getHours(),
            minutes = date.getMinutes(),
            seconds = date.getSeconds(),
            H = (-90 + hours * 30) * Math.PI / 180,
            M = (-90 + minutes * 6) * Math.PI / 180,
            S = (-90 + seconds * 6) * Math.PI / 180;
            //为什么要减90度呢,因为我们做圆的时候,起始点在正右方,而钟表的0点在正上方,所以要在基础上逆时针旋转90度,才能达到目标效果

        //时针
        cvs.beginPath();
        cvs.moveTo(x, y);
        cvs.arc(x, y, r * (8 / 20), H, H);
        cvs.lineWidth = 7;
        cvs.strokeStyle = "#000";
        cvs.stroke();


        //分针
        cvs.beginPath();
        cvs.moveTo(x, y);
        cvs.arc(x, y, r * (13 / 20), M, M);
        cvs.lineWidth = 4;
        cvs.strokeStyle = "#000";
        cvs.stroke();


        //秒针
        cvs.beginPath();
        cvs.moveTo(x, y);
        cvs.arc(x, y, r * (17 / 20), S, S);
        cvs.lineWidth = 1;
        cvs.strokeStyle = "#000";
        cvs.stroke();

在这里插入图片描述
3.配置定时器,实时更新:

function timer() {
  //秒针
            cvs.beginPath();
            cvs.moveTo(x, y);
            cvs.arc(x, y, r * (17 / 20), S, S);
            cvs.lineWidth = 1;
            cvs.strokeStyle = "#000";
            cvs.stroke();
        };
        timer();
        setInterval(timer, 1000);

4.调整时针,让其位置更准确:

H = (-90 + hours*30 + minutes/2)*Math.PI/180;

在这里插入图片描述

完整代码如下:!!!

<body>

<canvas id='myCanvas'></canvas>

<script>
var myCanvas = document.getElementById('myCanvas');
myCanvas.width = 1000;
myCanvas.height = 700;
var cvs = myCanvas.getContext("2d");


function timer(){

var x = 200,
	y = 200,
	r = 200,
	date = new Date(),
	hours = date.getHours(),
	minutes = date.getMinutes(),
	seconds = date.getSeconds();

var H = (-90 + hours*30 + minutes/2)*Math.PI/180;
var M = (-90 + minutes*6)*Math.PI/180;
var S = (-90 + seconds*6)*Math.PI/180;

cvs.beginPath();
cvs.arc(x,y,r,0,360*Math.PI/180);
cvs.fillStyle="#fff";
cvs.fill();
cvs.strokeStyle="#CCC";
cvs.stroke();

cvs.beginPath();
for(var i=0;i<60;i++){

	cvs.moveTo(x,y);
	cvs.arc(x,y,r,i*6*Math.PI/180,(i+1)*6*Math.PI/180);

}
cvs.strokeStyle="#000";
cvs.stroke();


cvs.beginPath();
cvs.arc(x,y,r*(19/20),0,2*Math.PI);
cvs.fillStyle="#fff";
cvs.fill();
cvs.strokeStyle="#fff";
cvs.stroke();


cvs.beginPath();
for(var i=0;i<12;i++){

	cvs.moveTo(x,y);
	cvs.arc(x,y,r,i*30*Math.PI/180,(i+1)*30*Math.PI/180);

}
cvs.lineWidth = 3;
cvs.strokeStyle="#000";
cvs.stroke();


cvs.beginPath();
cvs.arc(x,y,r*(18/20),0,2*Math.PI);
cvs.fillStyle="#fff";
cvs.fill();
cvs.strokeStyle="#fff";
cvs.stroke();





//时针
cvs.beginPath();
cvs.moveTo(x,y);
cvs.arc(x,y,r*(8/20),H,H);
cvs.lineWidth = 7;
cvs.strokeStyle="#000";
cvs.stroke();


//分针
cvs.beginPath();
cvs.moveTo(x,y);
cvs.arc(x,y,r*(13/20),M,M);
cvs.lineWidth = 4;
cvs.strokeStyle="#000";
cvs.stroke();


//秒针
cvs.beginPath();
cvs.moveTo(x,y);
cvs.arc(x,y,r*(17/20),S,S);
cvs.lineWidth = 1;
cvs.strokeStyle="#000";
cvs.stroke();

}
timer();
setInterval(timer,1000);
</script>

</body>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值