Javascript

canvas仿iwatch时钟效果

本文主要是介绍canvas仿iwatch时钟效果,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

效果图:

图(1)

图(2)

代码如下:

<!doctype html>
<html>
<head>
 <meta charset="UTF-8">
 <title>Title</title>
<style>
*{margin:0; padding:0;}
body{ background:#000; text-align:center;}
canvas{ background:#fff;}
</style>
<script>
function toDou(iNum){
 return iNum<10?'0'+iNum:''+iNum;
}
function d2a(n){
 return n*Math.PI/180;
}
window.onload = function(){
 var oC = document.getElementById('c1');
 var gd = oC.getContext('2d');
 var cx = oC.width/2,
 cy = oC.height/2;
 function clock(){
 gd.clearRect(0,0,oC.width,oC.height);
 var oDate = new Date();
 var H = oDate.getHours()%12;
 var M = oDate.getMinutes();
 var S = oDate.getSeconds();
 var MS = oDate.getMilliseconds();
 drawArc(60,0,(H*30+M/60*30),'orange');
 drawArc(80,0,(M*6+S/60*6),'purple');
 drawArc(100,0,(S*6+MS/1000*6),'aqua');
 var str = toDou(H)+':'+toDou(M)+':'+toDou(S);
 gd.font = '20px 微软雅黑';
 gd.textAlign = 'center';
 gd.textBaseline = 'middle';
 gd.fillText(str,cx,cy);
 }
 clock()
 setInterval(clock,1);
 function drawArc(r,s,e,color){
 color = color||'black';
 gd.beginPath();
 gd.arc(cx,cy,r,d2a(s-90),d2a(e-90),false);
 gd.strokeStyle = color;
 gd.lineWidth = 20;
 gd.stroke();
 }
};
</script>
</head>
<body>
 <canvas id="c1" width="800" height="600"></canvas>
</body>
</html>

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持找一找教程网!

这篇关于canvas仿iwatch时钟效果的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!