canvas 钟表

7 篇文章 0 订阅

<!doctype html>

<html>

<head>

<meta charset="utf-8">

<title>无标题文档</title>

<script>

/*arc(x,y,半径,起始弧度,结束弧度,旋转方向)*/

window.onload = function(){

var oC = document.querySelector('#box');

var oGC = oC.getContext('2d');



var x = 200.5;

var y = 200.5;

var r = 200;



function timer(){

oGC.clearRect(0,0,oC.width,oC.height)

var time = new Date();

var h = time.getHours();

var m = time.getMinutes();

var s = time.getSeconds();



var h_val = (h*30-90+m/60*30)*Math.PI/180;

var m_val = (m*6-90+s/60*6)*Math.PI/180;

var s_val = (s*6-90)*Math.PI/180;





/*生成小刻度*/

oGC.beginPath();

oGC.moveTo(x,y);

oGC.lineWidth = 1;

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

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

oGC.closePath();

}

oGC.stroke();



/*生成一个白色遮罩层*/

oGC.beginPath();

oGC.fillStyle = '#fff';

oGC.moveTo(x,y);

oGC.arc(x,y,r*0.95,0,2*Math.PI,false);

oGC.closePath();

oGC.fill();



/*生成大刻度*/

oGC.beginPath();

oGC.lineWidth = 2;

oGC.moveTo(x,y);

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

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

oGC.closePath();

}

oGC.stroke();



/*生成一个白色遮罩层*/

oGC.beginPath();

oGC.fillStyle = '#fff';

oGC.moveTo(x,y);

oGC.arc(x,y,r*0.9,0,2*Math.PI,false);

oGC.closePath();

oGC.fill();





/*时针*/

oGC.beginPath();

oGC.lineWidth = 4;

oGC.moveTo(x,y);

oGC.arc(x,y,r*0.5,h_val,h_val);

oGC.closePath();

oGC.stroke();





/*分针*/

oGC.save();

oGC.beginPath();

oGC.strokeStyle = '#00f';

oGC.lineWidth = 2;

oGC.moveTo(x,y);

oGC.arc(x,y,r*0.6,m_val,m_val);

oGC.closePath();

oGC.stroke();

oGC.restore();



/*秒针*/

oGC.save();

oGC.beginPath();

oGC.strokeStyle = '#f00';

oGC.lineWidth = 1;

oGC.moveTo(x,y);

oGC.arc(x,y,r*0.7,s_val,s_val);

oGC.closePath();

oGC.stroke();

oGC.restore();



/*刻度钉*/

oGC.fillStyle = '#f00';

oGC.beginPath();

oGC.moveTo(x,y)

oGC.arc(x,y,r*0.03,0,2*Math.PI)

oGC.closePath();

oGC.fill();

}

timer();

setInterval(timer,500);

}

</script>

</head>



<body>

<canvas id="box" height="400" width="400" style="border:1px solid #000"></canvas>

</body>

</html>


 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值