canvas實現的鐘表效果程式碼例項
本章節分享一段程式碼例項,它實現了獲取本地時間的鐘表效果。
感興趣的朋友可以可以做一下參考,程式碼如下:
[HTML] 純文字檢視 複製程式碼<!DOCTYPE html> <html> <head> <title>模擬select</title> <meta charset="utf-8" /> <script type="text/javascript"> window.onload=function(){ var myCanvas = document.getElementById('myCanvas'); var clock = myCanvas.getContext('2d'); drawClock(); setInterval(drawClock, 1000); function drawClock() { // 清理畫布 clock.clearRect(0, 0, 400, 400); // 獲取系統時間的時、分、秒 var nowDate = new Date(); var sec = nowDate.getSeconds() > 9 ? nowDate.getSeconds() : '0' + nowDate.getSeconds(); var mins = nowDate.getMinutes(); var hours = nowDate.getHours(); // 設定時間讀取位置 clock.save(); clock.translate(0, 300); clock.font = '16px 微軟雅黑'; clock.textAlign = 'center'; clock.fillText('當前系統時間為:' + hours + '時' + mins + '分' + sec + '秒', 200, 40); // clock.strokeText('當前系統時間為:'+hours+'時'+mins+'分'+sec+'秒',200,60); clock.restore(); // 製作鐘盤 clock.beginPath(); clock.lineWidth = 8; clock.arc(200, 154, 150, 0, 2 * Math.PI, false); clock.stroke(); // 製作時刻度 for (var i = 12; i > 0; i--) { clock.save(); clock.translate(200, 154); clock.rotate(i * 30 * Math.PI / 180); clock.lineWidth = 5; clock.beginPath(); clock.moveTo(0, -146); clock.lineTo(0, -132); clock.stroke(); clock.font = '14px Arial'; clock.textAlign = 'center'; clock.textBaseline = 'top'; clock.fillText(i, 0, -132); clock.fill(); clock.restore(); } // 製作分刻度 for (var i = 60; i > 0; i--) { clock.save(); clock.translate(200, 154); clock.rotate(i * 6 * Math.PI / 180); clock.lineWidth = 3; clock.beginPath(); clock.moveTo(0, -146); clock.lineTo(0, -140); clock.stroke(); clock.restore(); } // 製作時針 clock.save(); clock.translate(200, 154); clock.lineWidth = 5; clock.rotate((hours * 30 + mins * 6 / 12) * Math.PI / 180); clock.beginPath(); clock.moveTo(0, -80); clock.lineTo(0, 20); clock.stroke(); clock.restore(); // 製作分針 clock.save(); clock.translate(200, 154); clock.lineWidth = 3; clock.rotate((mins * 6 + sec * 6 / 60) * Math.PI / 180); clock.beginPath(); clock.moveTo(0, -100); clock.lineTo(0, 20); clock.stroke(); clock.restore(); // 製作秒針 clock.save(); clock.translate(200, 154); clock.lineWidth = 2; clock.strokeStyle = '#f00'; clock.rotate(sec * 6 * Math.PI / 180); clock.beginPath(); clock.moveTo(0, -110); clock.lineTo(0, 20); clock.stroke(); // 製作秒針上的圓點 clock.fillStyle = '#eee'; clock.beginPath(); clock.arc(0, 0, 6, 0, 2 * Math.PI, false); clock.fill(); clock.stroke(); clock.beginPath(); clock.arc(0, -80, 6, 0, 2 * Math.PI, false); clock.fill(); clock.stroke(); clock.restore(); } } </script> </head> <body> <canvas width="400" height="400" id="myCanvas"></canvas> </body> </html>
相關文章
- canvas實現的圓形鐘錶效果程式碼例項Canvas
- canvas原型鐘錶效果程式碼例項Canvas原型
- CSS3 實現的鐘表效果程式碼例項CSSS3
- canvas繪製鐘錶效果程式碼例項Canvas
- canvas實現的鋸齒效果程式碼例項Canvas
- canvas實現的驗證碼效果程式碼例項Canvas
- 利用canvas實現的驗證碼效果程式碼例項Canvas
- canvas實現的雪花飄落效果程式碼例項Canvas
- canvas實現的藍色雨滴效果程式碼例項Canvas
- canvas實現的彈力小球效果程式碼例項Canvas
- canvas實現煙花燃放效果程式碼例項Canvas
- canvas實現的七巧板效果程式碼例項Canvas
- canvas實現的動態心形效果程式碼例項Canvas
- canvas實現的簡單畫板效果程式碼例項Canvas
- canvas火焰效果程式碼例項Canvas
- canvas實現的賽貝爾曲線效果程式碼例項Canvas
- canvas實現的小球四壁碰撞效果程式碼例項Canvas
- canvas實現文字線性漸變效果程式碼例項Canvas
- canvas實現的刮刮樂程式碼例項Canvas
- canvas實現的旋轉的太極圖效果程式碼例項Canvas
- canvas字母雨效果程式碼例項Canvas
- canvas載入效果程式碼例項Canvas
- canvas擺動效果程式碼例項Canvas
- canvas實現的美猴王程式碼例項Canvas
- 實時變化的時鐘效果程式碼例項
- js鐘錶效果程式碼例項JS
- canvas實現點選產生放射性效果程式碼例項Canvas
- canvas水位進度效果程式碼例項Canvas
- canvas氣泡上浮效果程式碼例項Canvas
- canvas田字格效果程式碼例項Canvas
- canvas繪製圓環效果程式碼例項Canvas
- canvas螞蟻線效果程式碼例項Canvas
- javascript動態實現的表單提交效果程式碼例項JavaScript
- canvas實現的變幻線程式碼例項Canvas線程
- canvas實現的多邊形程式碼例項Canvas
- canvas實現的千輪眼程式碼例項Canvas
- jquery實現的選項卡效果例項程式碼jQuery
- canvas模擬彈幕效果程式碼例項Canvas