Html5繪製時鐘

來源:互聯網
上載者:User

標籤:and   對象   start   begin   canvas   弧度   clock   doc   end   

最近在對Html5比較感興趣,就用空閑時間做一些小例子進行練習,今天繪製一個走動的時鐘,具體如所示:

具體思路在已有說明,代碼如下:

  1 <script type="text/javascript">  2   3         //繪製圓形的弧度,ctx 為繪製環境,x,y為繪製中心,r為繪製半徑,st為起始弧度,end為結束弧度  4         function circle(ctx, x, y, r, st, end, w) {  5             ctx.lineWidth = w;  6             ctx.beginPath();  7             ctx.moveTo(x, y);  8             ctx.arc(x, y, r, st, end, true);  9             ctx.stroke(); 10         } 11  12         //畫一個白色的圓,用以覆蓋 13         function circle0(ctx, x, y, r, st, end) { 14             ctx.fillStyle = "#ffffff"; 15             ctx.beginPath(); 16             ctx.arc(x, y, r, st, end, true); 17             ctx.fill(); 18         } 19         function circle1(ctx, x, y, r, st, end, w) { 20             ctx.strokeStyle = "gray"; 21             ctx.lineWidth = w; 22             ctx.beginPath(); 23             ctx.arc(x, y, r, st, end, true); 24             ctx.stroke(); 25         } 26  27         //繪製時鐘用的線 28         function line(ctx, x1, y1, x2, y2, w) { 29             if (w == 1) { 30                 ctx.strokeStyle = "red"; 31             } else if (w == 2) { 32                 ctx.strokeStyle = "blue"; 33             } else { 34                 ctx.strokeStyle = "black"; 35             } 36             ctx.lineWidth = w; 37             ctx.beginPath(); 38             ctx.moveTo(x1, y1); 39             ctx.lineTo(x2, y2); 40             ctx.stroke(); 41         } 42         function drawClock() { 43             var c = document.getElementById("myCanvas"); 44             var ctx = c.getContext("2d"); 45             ctx.clearRect(0, 0, c.width, c.height); 46             //繪製一個鐘錶 47             var cX = 300; 48             var cY = 200; 49             var radius = 100; 50  51             //繪製分與秒的刻度 52             for (var i = 0; i < 60; i++) { 53                 circle(ctx, cX, cY, radius, i * 6 * Math.PI / 180, (i + 1) * 6 * Math.PI / 180, 1); 54             } 55             circle0(ctx, cX, cY, radius * 9 / 10, 0, 2 * Math.PI, true); 56             //繪製時刻度 57             for (var i = 0; i < 12; i++) { 58                 circle(ctx, cX, cY, radius, i * 30 * Math.PI / 180, (i + 1) * 30 * Math.PI / 180, 2); 59             } 60             circle0(ctx, cX, cY, radius * 8 / 10, 0, 2 * Math.PI, true); 61  62             //外圍再畫一圈 63             circle1(ctx, cX, cY, radius * 11 / 10, 0, 2 * Math.PI, 1); 64             ctx.save(); 65             ctx.fillStyle = "black"; 66             ctx.font = "10px Arial"; 67             //在鐘錶的周圍畫上數字 68             for (var i = 0; i < 12; i++) { 69                 var fX = cX + Math.cos((i + 1) * 5 * 6 * Math.PI / 180 - Math.PI / 2) * radius * 7.5 / 10; 70                 var fY = cY + Math.sin((i + 1) * 5 * 6 * Math.PI / 180 - Math.PI / 2) * radius * 7.5 / 10; 71                 ctx.fillText((i + 1), fX, fY); 72             } 73             var date = new Date(); 74             var second = date.getSeconds(); 75             var minute = date.getMinutes(); 76             var hour = date.getHours(); 77             hour = hour % 12; //採用12小時制 78  79  80             var secondX = cX + Math.cos(second * 6 * Math.PI / 180 - Math.PI / 2) * radius * 9 / 10; 81             var secondY = cY + Math.sin(second * 6 * Math.PI / 180 - Math.PI / 2) * radius * 9 / 10; 82             var minuteX = cX + Math.cos(minute * 6 * Math.PI / 180 - Math.PI / 2) * radius * 8 / 10; 83             var minuteY = cY + Math.sin(minute * 6 * Math.PI / 180 - Math.PI / 2) * radius * 8 / 10; 84             //小時為了準確起見,應該要加上分的弧度 85             var hourX = cX + Math.cos(hour * 30 * Math.PI / 180 + (minute * 6 * Math.PI / 180) / 12 - Math.PI / 2) * radius * 6.5 / 10; 86             var hourY = cY + Math.sin(hour * 30 * Math.PI / 180 + (minute * 6 * Math.PI / 180) / 12 - Math.PI / 2) * radius * 6.5 / 10; 87             //畫秒針,紅色 88             line(ctx, cX, cY, secondX, secondY, 1); 89             //畫分針,藍色 90             line(ctx, cX, cY, minuteX, minuteY, 2); 91             //畫時針,黑色 92             line(ctx, cX, cY, hourX, hourY, 3); 93             ctx.fillStyle = "black"; 94             ctx.font = "15px Arial"; 95             ctx.fillText("CurrentTime is : " + date.toLocaleString(), 150, 450); 96             ctx.save(); 97         } 98         window.onload = function () { 99             //迴圈,1s一次,且要在載入完才可以開始,否則會出現找不到對象的異常100             setInterval(drawClock, 1000);101         }102     </script>
View Code

 

Html5繪製時鐘

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.