利用H5中canvas畫布繪制一個時鍾(動態)


注意:Internet Explorer 9、Firefox、Opera、Chrome 以及 Safari 支持 <canvas> 及其屬性和方法。Internet Explorer 8 以及更早的版本不支持 <canvas> 元素

效果圖如下:

   時鍾效果圖

實現代碼:(代碼中有相應注釋介紹,比較好懂,如果實在有不懂得地方,歡迎私信,或者參考W3C文檔)

  1 <!DOCTYPE html>
  2 <html>
  3     <head>
  4         <meta charset="utf-8" />
  5         <title>時鍾</title>
  6         <style type="text/css">
  7             div{
  8                 text-align: center;
  9             }
 10         </style>
 11     </head>
 12     <body>
 13         <!--該div作用:讓時鍾在屏幕中間-->
 14         <div>
 15             <canvas width="800px" height="800px" id="myCanvas"></canvas>
 16         </div>
 17         
 18         <script>
 19             var myCanvas = document.getElementById("myCanvas");
 20             var ctx  = myCanvas.getContext("2d");
 21             //獲取畫布寬和高
 22             var w = myCanvas.width;
 23             var h = myCanvas.height;
 24             
 25 
 26             //時鍾表盤大小
 27             var r = w/2;
 28             
 29             
 30             //時鍾表盤上的時間及刻度
 31             function fun(){
 32                 ctx.save();//保存當前環境的狀態
 33                 //填充的漸變色
 34                 var r1 = Math.floor(Math.random()*256);
 35                 var g1 = Math.floor(Math.random()*256);
 36                 var b1 = Math.floor(Math.random()*256);
 37                 var grd=ctx.createLinearGradient(0,0,0,r);
 38                 grd.addColorStop(0,"rgb("+r1+","+g1+","+b1+")");
 39                 grd.addColorStop(0.5,"rgb("+b1+","+g1+","+r1+")");
 40                 grd.addColorStop(1,"rgb("+g1+","+b1+","+r1+")");
 41                 
 42                 //重新映射畫布上的 (0,0) 位置,即改變畫布原點位置
 43                 ctx.translate(r,r);
 44                 ctx.beginPath();
 45                 ctx.lineWidth = 10;
 46                 ctx.arc(0,0,r-12,0,2*Math.PI,false);
 47                 ctx.strokeStyle = grd;
 48                 ctx.stroke();
 49                 ctx.closePath();
 50                 //設置時間刻度字體大小,對其方式,基線
 51                 ctx.font = "22px Arial";
 52                 ctx.textAlign = "center";
 53                 ctx.textBaseline = "middle"
 54                 //定義一個數組用於保存時間刻度
 55                 var num = [3,4,5,6,7,8,9,10,11,12,1,2];
 56                 //循環將時間顯示在表盤中
 57                 for(var i = 0;i < num.length; i++){
 58                     //定義時間刻度在表盤中相應弧度,根據弧度利用正弦余弦公式求出該刻度橫坐標和縱坐標
 59                     //數字3為數組第一個數字刻度是因為,繪制圓時(0弧度)是從3對應刻度位置開始的
 60                     var rad = Math.PI*2/12*i;
 61                     var x = Math.cos(rad)*(r-60);
 62                     var y = Math.sin(rad)*(r-60);
 63                     ctx.beginPath();
 64                     //填充文本
 65                     ctx.fillText(num[i],x,y);
 66                     ctx.fillStyle = grd;
 67                     ctx.closePath();
 68                 }
 69                 
 70                 for(var i = 0;i < 60; i++){
 71                     var rad = Math.PI*2/60*i;
 72                     var x = Math.cos(rad)*(r-40);
 73                     var y = Math.sin(rad)*(r-40);
 74                     ctx.beginPath();
 75                     ctx.arc(x,y,6,0,2*Math.PI,false);
 76                     //時間刻度點取余,與現實表盤相對應
 77                     if(i%5==0){
 78                         ctx.fillStyle = grd;
 79                         ctx.arc(x,y,10,0,2*Math.PI,false);
 80                     }else{
 81                         ctx.fillStyle = grd;
 82                     }
 83                     ctx.fill()
 84                     ctx.closePath();
 85                 }
 86             }
 87             
 88             //繪制時針
 89             function drawHour(hour,minute){
 90                 ctx.save();
 91                 //填充的漸變色
 92                 var r1 = Math.floor(Math.random()*256);
 93                 var g1 = Math.floor(Math.random()*256);
 94                 var b1 = Math.floor(Math.random()*256);
 95                 var grd=ctx.createLinearGradient(0,0,0,r);
 96                 grd.addColorStop(0,"rgb("+g1+","+r1+","+b1+")");
 97                 grd.addColorStop(0.5,"rgb("+r1+","+b1+","+g1+")");
 98                 grd.addColorStop(1,"rgb("+r1+","+g1+","+r1+")");
 99                 
100                 //設置時針旋轉角度
101                 var rad = Math.PI*2/12*hour;
102                 //分針旋轉角度會對時針起影響,將對應分針旋轉角度加上
103                 var rad_min = Math.PI*2/12/60*minute
104                 //旋轉
105                 ctx.rotate(rad+rad_min);
106                 ctx.lineWidth = 8;
107                 ctx.beginPath();
108                 ctx.moveTo(0,10);
109                 ctx.lineTo(0,-r/2)
110                 ctx.closePath();
111                 ctx.strokeStyle = grd;
112                 ctx.stroke();
113                 ctx.restore();//返回之前保存過的路徑狀態和屬性
114             }
115             
116             //繪制分針,繪制過程和時針相似
117             function drawMinute(minute,second){
118                 ctx.save();
119                 
120                 var rad = Math.PI*2/60*minute;
121                 var rad_seco = Math.PI*2/3600*second
122                 ctx.rotate(rad + rad_seco);
123                 ctx.lineWidth = 6;
124                 ctx.beginPath();
125                 ctx.moveTo(0,20);
126                 ctx.lineTo(0,-r+150)
127                 ctx.lineCap = "round";
128                 ctx.stroke();
129                 ctx.restore();
130             }
131             
132             //繪制秒針
133             function drawScond(second){
134                 ctx.save();
135                 //填充的漸變色
136                 var r1 = Math.floor(Math.random()*256);
137                 var g1 = Math.floor(Math.random()*256);
138                 var b1 = Math.floor(Math.random()*256);
139                 var grd=ctx.createLinearGradient(0,0,0,r);
140                 grd.addColorStop(0,"rgb("+r1+","+g1+","+b1+")");
141                 grd.addColorStop(0.5,"rgb("+b1+","+g1+","+r1+")");
142                 grd.addColorStop(1,"rgb("+g1+","+b1+","+r1+")");
143                 
144                 var rad = Math.PI*2/60*second;
145                 ctx.rotate(rad);
146                 ctx.beginPath();
147                 ctx.moveTo(-2,30);
148                 ctx.lineTo(2,10)
149                 ctx.lineTo(1,-r+50);
150                 ctx.lineTo(-1,-r+50);
151                 ctx.fillStyle = grd;
152                 ctx.fill();
153                 //繪制秒針中間點綴
154                 ctx.beginPath();
155                 ctx.strokeStyle = grd;
156                 ctx.arc(0,-r/2,3,0,2*Math.PI,false);
157                 ctx.stroke();
158                 ctx.arc(0,-r/2,2,0,2*Math.PI,false);
159                 ctx.fillStyle = grd;
160                 ctx.fill()
161                 ctx.restore();
162             }
163             
164             //繪制固定時針分針秒針的中心點
165             function drawDot(){
166                 ctx.beginPath();
167                 ctx.fillStyle = "white";
168                 ctx.arc(0,0,4,0,2*Math.PI,false);
169                 ctx.closePath();
170                 ctx.fill();
171             }
172             
173             //獲取系統當前時間,調用函數,傳參數
174             function drawClock(){
175                 ctx.clearRect(0,0,w,h);
176                 var now = new Date();
177                 var hour = now.getHours();
178                 var minute = now.getMinutes();
179                 var second = now.getSeconds();
180                 fun();
181                 drawHour(hour,minute);
182                 drawMinute(minute,second);
183                 drawScond(second);
184                 drawDot();
185                 ctx.restore();
186             }
187             
188             //調用函數顯示表盤
189             drawClock();
190             //設置定時器,跟隨系統讓時鍾動起來
191             setInterval(drawClock,1000);
192         </script>
193     </body>
194 </html>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM