canvas繪製圓盤走動鐘錶效果
分享一段程式碼例項,利用canvas實現繪製鐘錶的功能,並且鐘錶指標能夠自動走動,實時的獲取本機時間。
程式碼例項如下:
[HTML] 純文字檢視 複製程式碼執行程式碼<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <style> * { padding: 0; margin: 0; } body { background: #ccc; } #c { background: #fff; } </style> <script> window.onload = () => { let oC = document.getElementById('c'); let oC1 = oC.getContext('2d'); let toDraw = () => { let x = 200; // x軸座標 let y = 200; // y軸座標 let r = 150; // r 半徑 // 清空畫布 oC1.clearRect(0, 0, oC.width, oC.height) // 獲取時間 let oDate = new Date(); let oHours = oDate.getHours(); //時 let oMin = oDate.getMinutes(); //分 let oSen = oDate.getSeconds(); //秒 let oHoursVal = (oHours * 30 - 90 + oMin / 2) * Math.PI / 180; let oMinVal = (oMin * 6 - 90) * Math.PI / 180; let oSenVal = (oSen * 6 - 90) * Math.PI / 180; // 繪製秒針錶盤 oC1.beginPath(); for (let i = 0; i < 60; i++) { oC1.moveTo(x, y); oC1.arc(x, y, r, 6 * i * Math.PI / 180, 6 * (i + 1) * Math.PI / 180, false); } oC1.closePath(); oC1.stroke(); // 繪製大圓盤 (覆蓋) oC1.fillStyle = "#fff"; oC1.beginPath(); oC1.moveTo(x, y); oC1.arc(x, y, r * 0.95, 0, 360 * Math.PI / 180, false); oC1.closePath(); oC1.fill(); // 繪製分針錶盤 oC1.lineWidth = 3; oC1.beginPath(); for (let i = 0; i < 12; i++) { oC1.moveTo(x, y); oC1.arc(x, y, r, 30 * i * Math.PI / 180, 30 * (i + 1) * Math.PI / 180, false); } oC1.closePath(); oC1.stroke(); // 繪製大圓盤 (覆蓋) oC1.fillStyle = "#fff"; oC1.beginPath(); oC1.moveTo(x, y); oC1.arc(x, y, r * 0.9, 0, 360 * Math.PI / 180, false); oC1.closePath(); oC1.fill(); // 繪製時針 oC1.lineWidth = 5; oC1.beginPath(); oC1.moveTo(x, y); oC1.arc(x, y, r * 0.5, oHoursVal, oHoursVal, false); oC1.closePath(); oC1.stroke(); // 繪製分針 oC1.lineWidth = 3; oC1.beginPath(); oC1.moveTo(x, y); oC1.arc(x, y, r * 0.7, oMinVal, oMinVal, false); oC1.closePath(); oC1.stroke(); // 繪製秒針 oC1.lineWidth = 1; oC1.beginPath(); oC1.moveTo(x, y); oC1.arc(x, y, r * 0.8, oSenVal, oSenVal, false); oC1.closePath(); oC1.stroke(); // 中心點 // 繪製大圓盤 (覆蓋) oC1.fillStyle = "red"; oC1.beginPath(); oC1.moveTo(x, y); oC1.arc(x, y, r * 0.05, 0, 360 * Math.PI / 180, false); oC1.closePath(); oC1.fill(); } setInterval(toDraw, 1000) toDraw() } </script> </head> <body> <canvas id="c" height="400" width="400"></canvas> </body> </html>
相關文章
- canvas繪製圓形鐘錶程式碼例項Canvas
- canvas繪製多個圓圈效果Canvas
- SVG圓形鐘錶效果SVG
- canvas圓形時鐘效果Canvas
- 使用canvas繪製圓弧動畫Canvas動畫
- canvas繪製圓形框效果不填充內部Canvas
- JavaScript動態圓形鐘錶效果詳解JavaScript
- canvas 繪製圓形Canvas
- canvas繪製流星效果Canvas
- canvas 繪製圓角矩形Canvas
- canvas原型鐘錶效果程式碼例項Canvas原型
- canvas繪製風車效果Canvas
- canvas繪製不重合的圓Canvas
- canvas 繪製立體圓環Canvas
- QT繪製簡易錶盤QT
- JavaScript 動態圓形鐘錶JavaScript
- canvas繪製網格射線效果Canvas
- canvas繪製卡通人臉形象效果Canvas
- canvas動態時鐘效果Canvas
- Canvas 繪製 3d 圓柱體Canvas3D
- 使用canvas繪製圓形進度條Canvas
- Vue canvas繪製圓形進度條動畫載入VueCanvas動畫
- canvas繪製經典星空連線效果Canvas
- Canvas繪製一個類似老版支付寶信用分儀表盤效果Canvas
- 打造真實感十足的速度錶盤:WPF實現動態效果與刻度繪製
- canvas繪製箭頭效果程式碼例項Canvas
- 微信小程式 canvas圓角矩形的繪製微信小程式Canvas
- canvas繪製動畫的技巧Canvas動畫
- canvas繪製帶有漸變效果的直線Canvas
- 原來 Canvas 也能直接繪製圓角矩形了Canvas
- 一分鐘教程-超橢圓快速繪製
- canvas實現手動繪製矩形Canvas
- canvas 繪製扇形Canvas
- canvas 繪製文字Canvas
- canvas 繪製矩形Canvas
- canvas 奇巧淫技(二)繪製箭頭路徑效果Canvas
- 【Web前端基礎知識】如何使用Canvas繪製圓形Web前端Canvas
- canvas繪圖之鐘表Canvas繪圖