使用canvas画一个可以动的时钟

先给个效果图,画的比较丑,大家可以自己美化一下,




直接上代码:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <!DOCTYPE html>  
  2. <meta charset="utf-8">  
  3. <html>  
  4.    <body>  
  5.       <canvas  width="500" height="500" id="clock" >    
  6.              您的浏览器不支持canvas  
  7.       </canvas>  
  8.       <script>  
  9.            //获取画布  
  10.            var clock=document.getElementById('clock');  
  11.            //设置绘图环境  
  12.            var cxt=clock.getContext('2d');    
  13.   
  14.        function drawClock(){      
  15.            //清除画布  
  16.            cxt.clearRect(0,0,500,500);  
  17.            //获取时间,  
  18.             var now =new Date();  
  19.             var second =now.getSeconds();  
  20.             var minute =now.getMinutes();  
  21.             var hour1 =now.getHours();  
  22.             //将24小时进制转为12小时,且为浮点型  
  23.             var hour=hour1+minute/60;  
  24.             hour=hour>12 ?hour-12:hour;  
  25.             //获取全部时间  
  26.             var time=now.toLocaleString( );  
  27.               
  28.            //表盘  
  29.                 //开始新路径  
  30.                 cxt.beginPath();  
  31.                 cxt.lineWidth=8;  
  32.                 cxt.strokeStyle="blue";  
  33.                 //路径函数 x,y,r,角度范围,顺时针/逆时针  
  34.                 cxt.arc(250,250,200,0,360,false);  
  35.                 cxt.stroke();   
  36.                 cxt.closePath();  
  37.             //刻度,利用旋转  
  38.                //时刻度  
  39.                 for(var i=0;i<12;i++){  
  40.                     //保存当前状态  
  41.                     cxt.save();  
  42.                     //刻度粗细  
  43.                      cxt.lineWidth=5;  
  44.                     //刻度颜色  
  45.                     cxt.strokeStyle="black"  
  46.                     //设置00点,以画布中心为00  
  47.                     cxt.translate(250,250);  
  48.                     //设置旋转角度 参数是弧度,角度 0--360 弧度角度*Math.PI/180  
  49.                     cxt.rotate(i*30*Math.PI/180);  
  50.                     cxt.beginPath();  
  51.                     //刻度起始点  
  52.                     cxt.moveTo(0,-180);  
  53.                     //刻度结束点  
  54.                     cxt.lineTo(0,-195);  
  55.                     cxt.closePath();  
  56.                     cxt.stroke();  
  57.                     //将旋转后的图片返回原画布  
  58.                     cxt.restore();  
  59.                 }  
  60.   
  61.                //分刻度  
  62.                  for(var i=0;i<60;i++){  
  63.                     //保存当前状态  
  64.                     cxt.save();  
  65.                     //刻度粗细  
  66.                     cxt.lineWidth=3;  
  67.                     //刻度颜色  
  68.                     cxt.strokeStyle="black"  
  69.                     //设置00点,以画布中心为00  
  70.                     cxt.translate(250,250);  
  71.                     //设置旋转角度 参数是弧度,角度 0--360 弧度角度*Math.PI/180  
  72.                     cxt.rotate(i*6*Math.PI/180);  
  73.                     cxt.beginPath();  
  74.                     //起始点  
  75.                     cxt.moveTo(0,-188);  
  76.                     cxt.lineTo(0,-195);  
  77.                     cxt.closePath();  
  78.                     cxt.stroke();  
  79.                     //将旋转后的图片返回原画布  
  80.                     cxt.restore();  
  81.                 }  
  82.             //表盘中心  
  83.                 cxt.beginPath();  
  84.                 cxt.lineWidth=1;  
  85.                 cxt.strokeStyle="red";  
  86.                 cxt.fillStyle="red";  
  87.                 //路径函数 x,y,r,角度范围,顺时针/逆时针  
  88.                 cxt.arc(250,250,4,0,360,false);  
  89.                 cxt.fill();  
  90.                 cxt.stroke();   
  91.                 cxt.closePath();  
  92.             //时针  
  93.                 //保存当前状态  
  94.                  cxt.save();  
  95.                  cxt.lineWidth=5;  
  96.                  cxt.strokeStyle="black";  
  97.                  //设置异次元空间00点  
  98.                  cxt.translate(250,250);  
  99.                  //设置旋转角度 参数是弧度,角度 0--360 弧度角度*Math.PI/180  
  100.                  cxt.rotate(hour*30*Math.PI/180);  
  101.                  cxt.beginPath();       
  102.                  cxt.moveTo(0,-120);  
  103.                  cxt.lineTo(0,10);  
  104.                  cxt.closePath();  
  105.                  cxt.stroke();  
  106.                  cxt.restore();  
  107.             //分针  
  108.                  cxt.save();  
  109.                  cxt.lineWidth="3";  
  110.                  cxt.strokeStyle="black";  
  111.                  cxt.translate(250,250);  
  112.                  cxt.rotate(minute*6*Math.PI/180);  
  113.                  cxt.beginPath();       
  114.                  cxt.moveTo(0,-150);  
  115.                  cxt.lineTo(0,15);  
  116.                  cxt.closePath();  
  117.                  cxt.stroke();  
  118.                  cxt.restore();  
  119.             //秒针  
  120.                  cxt.save();  
  121.                  cxt.lineWidth="1.5";  
  122.                  cxt.strokeStyle="red";  
  123.                  cxt.translate(250,250);  
  124.                  cxt.rotate(second*6*Math.PI/180);  
  125.                  cxt.beginPath();       
  126.                  cxt.moveTo(0,-160);  
  127.                  cxt.lineTo(0,20);  
  128.                  cxt.closePath();  
  129.                  cxt.stroke();  
  130.                  //秒针前端小点  
  131.                  cxt.beginPath();  
  132.                  //外环为红色  
  133.                  cxt.strokeStyle="red";  
  134.                  //灰色填充  
  135.                  cxt.fillStyle="gray";  
  136.                  cxt.arc(0,-145,4,0,360,false);  
  137.                    cxt.fill();  
  138.                  cxt.closePath();  
  139.                  cxt.stroke();  
  140.                  cxt.restore();  
  141.             //表盘中心  
  142.                 cxt.beginPath();  
  143.                 cxt.lineWidth=1;  
  144.                 //外环为红色  
  145.                 cxt.strokeStyle="red";  
  146.                 //灰色填充  
  147.                 cxt.fillStyle="gray";  
  148.                 //路径函数 x,y,r,角度范围,顺时针/逆时针  
  149.                 cxt.arc(250,250,4,0,360,false);  
  150.                 cxt.fill();  
  151.                 cxt.stroke();   
  152.                 cxt.closePath();  
  153.             //写时间      
  154.                 cxt.font="15px 黑体 ";  
  155.                  cxt.fillStyle="black";  
  156.                 //实心字  
  157.                 cxt.fillText(time,160,150);  
  158.            }        
  159.            //使用setInterval(代码,毫秒时间)使时钟转起来;  
  160.            drawClock();  
  161.            setInterval(drawClock,1000);  
  162.   
  163.   
  164.       </script>  
  165.    </body>  
  166. </html>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值