canvas圓形時鐘效果
本章節分享一段程式碼例項,它利用canvas實現了圓形時鐘效果。
程式碼例項如下:
[HTML] 純文字檢視 複製程式碼執行程式碼<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> </head> <body> <canvas id="clock" width="500" height="500"></canvas> <script> var clock = document.getElementById('clock'); var cxt = clock.getContext('2d'); function drawClock() { //獲取時間 cxt.clearRect(0, 0, 500, 500); var now = new Date(); var sec = now.getSeconds(); var min = now.getMinutes(); var hour = now.getHours(); //規格化 hour = hour + min / 60; hour = hour > 12 ? hour - 12 : hour; //錶盤 cxt.lineWidth = 10; cxt.strokeStyle = "blue"; cxt.beginPath(); cxt.arc(250, 250, 200, 0, 360, false); cxt.closePath(); cxt.stroke(); //錶盤格 for (var i = 0; i < 12; i++) { cxt.save(); cxt.lineWidth = 7; cxt.strokeStyle = "#000"; cxt.translate(250, 250); cxt.rotate(i * 30 * Math.PI / 180); cxt.beginPath(); cxt.moveTo(0, -170); cxt.lineTo(0, -190); cxt.closePath(); cxt.stroke(); cxt.restore(); } for (var i = 0; i < 60; i++) { cxt.save(); cxt.lineWidth = 5; cxt.strokeStyle = "#000"; cxt.translate(250, 250); cxt.rotate(i * 6 * Math.PI / 180); cxt.beginPath(); cxt.moveTo(0, -180); cxt.lineTo(0, -190); cxt.closePath(); cxt.stroke(); cxt.restore(); } //時針 cxt.save(); cxt.lineWidth = 7; cxt.strokeStyle = "#000"; cxt.translate(250, 250); cxt.rotate(hour * 30 * Math.PI / 180); cxt.beginPath(); cxt.moveTo(0, -140); cxt.lineTo(0, 10); cxt.closePath(); cxt.stroke(); cxt.restore(); //分針 cxt.save(); cxt.lineWidth = 5; cxt.strokeStyle = "#000"; cxt.translate(250, 250); cxt.rotate(min * 6 * Math.PI / 180); cxt.beginPath(); cxt.moveTo(0, -160); cxt.lineTo(0, 15); cxt.closePath(); cxt.stroke(); cxt.restore(); //秒針 cxt.save(); cxt.strokeStyle = "red"; cxt.lineWidth = 3; cxt.translate(250, 250); cxt.rotate(sec * 6 * Math.PI / 180); cxt.beginPath(); cxt.moveTo(0, -170); cxt.lineTo(0, 20); cxt.closePath(); cxt.stroke(); cxt.beginPath(); cxt.arc(0, 0, 5, 0, 360, false); cxt.closePath(); cxt.fillStyle = "gray"; cxt.fill(); cxt.stroke(); cxt.beginPath(); cxt.arc(0, -150, 5, 0, 360, false); cxt.closePath(); cxt.fillStyle = "gray"; cxt.fill(); cxt.stroke(); cxt.restore(); } drawClock(); setInterval(drawClock, 1000); </script> </body> </html>
相關文章
- canvas實現的圓形時鐘效果Canvas
- canvas實現的圓形走動鐘錶效果Canvas
- SVG圓形鐘錶效果SVG
- JavaScript圓形鐘錶效果JavaScript
- canvas實現的圓形鐘錶效果程式碼例項Canvas
- CSS3圓形時鐘效果程式碼CSSS3
- canvas圓形隨機漂浮效果Canvas隨機
- canvas動態時鐘效果Canvas
- canvas繪製圓盤走動鐘錶效果Canvas
- canvas繪製圓形鐘錶程式碼例項Canvas
- JavaScript動態圓形鐘錶效果詳解JavaScript
- css3動態圓形鐘錶效果CSSS3
- canvas繪製圓形框效果不填充內部Canvas
- jquery和css實現的圓形鐘錶效果jQueryCSS
- canvas 繪製圓形Canvas
- javascript圓形電子鐘錶效果程式碼例項JavaScript
- js和css3實現的圓形鐘錶效果JSCSSS3
- CSS圓形圖片效果CSS
- 圓形放大的hover效果
- canvas環形資料效果Canvas
- JavaScript 動態圓形鐘錶JavaScript
- jquery自動走動的圓形鐘錶效果程式碼例項jQuery
- canvas實現的絢麗的電子時鐘效果Canvas
- canvas繪製多個圓圈效果Canvas
- canvas環形進度條效果Canvas
- 使用canvas繪製圓形進度條Canvas
- canvas的簡單圓形進度條Canvas
- html5中canvas繪製圓形HTMLCanvas
- css3和js實現的圓形鐘錶效果程式碼例項CSSS3JS
- canvas圓形進度條註釋超全Canvas
- javascript圓形鐘錶程式碼例項JavaScript
- css實現圓形、橢圓和半圓效果程式碼例項CSS
- canvas繪製橢圓效果程式碼例項Canvas
- 直播網站原始碼,Canvas實現圓形時間倒數計時進度條網站原始碼Canvas
- CSS3圓形進度條效果CSSS3
- canvas原型鐘錶效果程式碼例項Canvas原型
- canvas繪製實心圓形程式碼例項Canvas
- HTML5 Canvas使用路徑——繪製圓形HTMLCanvas