自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

詳解通過(guò)Canvas + JS 實(shí)現(xiàn)簡(jiǎn)易的時(shí)鐘

開(kāi)發(fā) 后端
之前學(xué)習(xí)了下html5中的canvas元素,為了練練手就實(shí)現(xiàn)了一個(gè)簡(jiǎn)易的時(shí)鐘。時(shí)鐘本身并不復(fù)雜,也沒(méi)有使用圖片進(jìn)行美化,不過(guò)麻雀雖小五臟俱全。

之前學(xué)習(xí)了下html5中的canvas元素,為了練練手就實(shí)現(xiàn)了一個(gè)簡(jiǎn)易的時(shí)鐘。時(shí)鐘本身并不復(fù)雜,也沒(méi)有使用圖片進(jìn)行美化,不過(guò)麻雀雖小五臟俱全,下面就與大家分享一下:

實(shí)現(xiàn)效果:


 

詳解通過(guò)Canvas + JS 實(shí)現(xiàn)簡(jiǎn)易的時(shí)鐘

 

html代碼:

  1. <!DOCTYPE html> 
  2. <html> 
  3. <head> 
  4.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  5.     <title>Clock</title> 
  6.     <style type="text/css"
  7.     *{ 
  8.         margin: 0
  9.         padding: 0
  10.     } 
  11.     .canvas{ 
  12.         margin-left: 20px; 
  13.         margin-top: 20px; 
  14.         border: solid 1px; 
  15.     } 
  16.     </style> 
  17. </head> 
  18. <body onload= "main()"
  19.  
  20. <canvas class = "canvas" id="canvasId" width = '500px' height = '400px'></canvas> 
  21.  
  22. <script type= "text/javascript" src = "Clock.js"></script> 
  23. </body> 
  24. </html> 

 

JS代碼:

  1. var Canvas = {}; 
  2.  
  3. Canvas.cxt = document.getElementById('canvasId').getContext('2d'); 
  4.  
  5. Canvas.Point = function(x, y){ 
  6.     this.x = x; 
  7.     this.y = y; 
  8. }; 
  9.  
  10. /*擦除canvas上的所有圖形*/ 
  11. Canvas.clearCxt = function(){ 
  12.     var me = this
  13.     var canvas = me.cxt.canvas; 
  14.        me.cxt.clearRect(0,0, canvas.offsetWidth, canvas.offsetHeight); 
  15. }; 
  16.  
  17. /*時(shí)鐘*/ 
  18. Canvas.Clock = function(){ 
  19.     var me = Canvas, 
  20.         c = me.cxt, 
  21.         radius = 150/*半徑*/ 
  22.         scale = 20/*刻度長(zhǎng)度*/ 
  23.         minangle = (1/30)*Math.PI, /*一分鐘的弧度*/ 
  24.         hourangle = (1/6)*Math.PI, /*一小時(shí)的弧度*/ 
  25.         hourHandLength = radius/2/*時(shí)針長(zhǎng)度*/ 
  26.         minHandLength = radius/3*2/*分針長(zhǎng)度*/ 
  27.         secHandLength = radius/10*9/*秒針長(zhǎng)度*/ 
  28.         center = new me.Point(c.canvas.width/2, c.canvas.height/2); /*圓心*/ 
  29.     
  30.     /*繪制圓心(表盤(pán)中心)*/ 
  31.     function drawCenter(){ 
  32.         c.save(); 
  33.  
  34.         c.translate(center.x, center.y);  
  35.  
  36.         c.fillStyle = 'black'
  37.         c.beginPath(); 
  38.         c.arc(00, radius/2002*Math.PI); 
  39.         c.closePath(); 
  40.         c.fill(); 
  41.         c.stroke(); 
  42.  
  43.         c.restore(); 
  44.     }; 
  45.  
  46.     /*通過(guò)坐標(biāo)變換繪制表盤(pán)*/ 
  47.     function drawBackGround(){ 
  48.         c.save(); 
  49.  
  50.         c.translate(center.x, center.y); /*平移變換*/ 
  51.         /*繪制刻度*/ 
  52.         function drawScale(){ 
  53.            c.moveTo(radius - scale, 0); 
  54.            c.lineTo(radius, 0);  
  55.         }; 
  56.  
  57.         c.beginPath(); 
  58.         c.arc(00, radius, 02*Math.PI, true); 
  59.         c.closePath(); 
  60.      
  61.         for (var i = 1; i <= 12; i++) { 
  62.            drawScale(); 
  63.            c.rotate(hourangle); /*旋轉(zhuǎn)變換*/ 
  64.         }; 
  65.         /*繪制時(shí)間(3,6,9,12)*/ 
  66.         c.font = " bold 30px impack" 
  67.         c.fillText("3"11010); 
  68.         c.fillText("6", -7120); 
  69.         c.fillText("9", -12010); 
  70.         c.fillText("12", -16, -100); 
  71.         c.stroke(); 
  72.  
  73.         c.restore(); 
  74.     }; 
  75.  
  76.     /*繪制時(shí)針(h: 當(dāng)前時(shí)(24小時(shí)制))*/ 
  77.     this.drawHourHand = function(h){ 
  78.  
  79.         h = h === 024: h; 
  80.  
  81.         c.save(); 
  82.         c.translate(center.x, center.y);  
  83.         c.rotate(3/2*Math.PI); 
  84.  
  85.         c.rotate(h*hourangle); 
  86.  
  87.         c.beginPath(); 
  88.         c.moveTo(00); 
  89.         c.lineTo(hourHandLength, 0); 
  90.         c.stroke(); 
  91.         c.restore(); 
  92.     }; 
  93.  
  94.     /*繪制分針(m: 當(dāng)前分)*/ 
  95.     this.drawMinHand = function(m){ 
  96.  
  97.         m = m === 060: m; 
  98.  
  99.         c.save(); 
  100.         c.translate(center.x, center.y);  
  101.         c.rotate(3/2*Math.PI); 
  102.  
  103.         c.rotate(m*minangle); 
  104.  
  105.         c.beginPath(); 
  106.         c.moveTo(00); 
  107.         c.lineTo(minHandLength, 0); 
  108.         c.stroke(); 
  109.         c.restore(); 
  110.     }; 
  111.  
  112.     /*繪制秒針(s:當(dāng)前秒)*/ 
  113.     this.drawSecHand = function(s){ 
  114.  
  115.         s = s === 060: s; 
  116.  
  117.         c.save(); 
  118.         c.translate(center.x, center.y);  
  119.         c.rotate(3/2*Math.PI); 
  120.  
  121.         c.rotate(s*minangle); 
  122.  
  123.         c.beginPath(); 
  124.         c.moveTo(00); 
  125.         c.lineTo(secHandLength, 0); 
  126.         c.stroke(); 
  127.         c.restore(); 
  128.     }; 
  129.  
  130.     /*依據(jù)本機(jī)時(shí)間繪制時(shí)鐘*/ 
  131.     this.drawClock = function(){ 
  132.         var me = this
  133.   
  134.         function draw(){ 
  135.            var date = new Date(); 
  136.  
  137.            Canvas.clearCxt(); 
  138.  
  139.            drawBackGround(); 
  140.            drawCenter(); 
  141.            me.drawHourHand(date.getHours() + date.getMinutes()/60); 
  142.            me.drawMinHand(date.getMinutes() + date.getSeconds()/60); 
  143.            me.drawSecHand(date.getSeconds()); 
  144.         } 
  145.         draw(); 
  146.         setInterval(draw, 1000); 
  147.     };   
  148. }; 
  149.  
  150.  var main = function(){ 
  151.     var clock = new Canvas.Clock(); 
  152.     clock.drawClock(); 
  153. }; 

 

代碼中涉及到了一些簡(jiǎn)單的canvas元素API 大家google一下即可,祝大家學(xué)習(xí)愉快:-D

原文鏈接:http://www.cnblogs.com/liubingblog/archive/2015/03/10/4325063.html

責(zé)任編輯:王雪燕 來(lái)源: 博客園
相關(guān)推薦

2022-06-29 14:06:54

canvas鴻蒙

2022-03-23 14:57:48

canvasjavascript運(yùn)動(dòng)小球

2022-02-14 14:14:02

鴻蒙數(shù)據(jù)可視化JS

2009-05-21 10:08:49

SQL報(bào)表JSPHibernate

2022-07-11 16:19:22

css屬性鴻蒙

2009-12-29 10:06:09

WPF Canvas

2010-04-08 15:35:13

Oracle 簡(jiǎn)易客戶(hù)

2017-04-24 08:31:26

Node.jsExpress.jsHTTP

2023-12-29 08:31:49

Spring框架模塊

2024-01-07 16:42:32

C++編程開(kāi)發(fā)

2021-06-10 08:29:15

Rollup工具前端

2009-10-12 09:02:03

SmartRWLock

2016-12-12 13:41:37

iOS簡(jiǎn)易加法開(kāi)發(fā)

2022-03-11 20:31:35

canvasHarmony鴻蒙

2022-02-23 15:17:04

鴻蒙OpenHarmonJacascript

2012-02-23 13:59:00

IndexedDB

2017-04-05 16:30:09

Node.jsFFmpeg Canvas

2022-10-20 11:49:49

JS動(dòng)畫(huà)幀,CSS

2010-05-26 13:17:55

SVN簡(jiǎn)易使用手冊(cè)

2022-03-10 11:04:04

Vue3Canvas前端
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)