canvas實現的圓形時鐘效果
分享一段程式碼例項,它實現了圓形時鐘效果。
程式碼例項如下:
[HTML] 純文字檢視 複製程式碼<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> </head> <body> <canvas id='box' width="500" height="500"></canvas> <script> var box = document.getElementById('box'); var cxt = box.getContext('2d'); // 時鐘動起來 var timer = null; function clock() { var date = new Date(); var h = date.getHours(); h = h + h / 60; h = h > 12 ? h - 12 : h; var m = date.getMinutes(); var s = date.getSeconds(); // 清畫布 cxt.clearRect(0, 0, 500, 500); // 畫表盤 cxt.strokeStyle = '#f0f'; cxt.lineWidth = 6; cxt.beginPath(); cxt.arc(250, 250, 100, 0, 2 * Math.PI); cxt.stroke(); // 畫時鐘刻度 for (var i = 0; i < 12; i++) { cxt.save(); cxt.translate(250, 250); cxt.rotate(30 * i * Math.PI / 180); cxt.lineWidth = 3; cxt.beginPath(); cxt.moveTo(0, -80); cxt.lineTo(0, -92); cxt.stroke(); cxt.restore(); } //畫分鐘刻度 for (var i = 0; i < 60; i++) { cxt.save(); cxt.translate(250, 250); cxt.rotate(6 * i * Math.PI / 180); cxt.lineWidth = 2; cxt.beginPath(); cxt.moveTo(0, -86); cxt.lineTo(0, -92); cxt.stroke(); cxt.restore(); } // 畫時針 cxt.save(); cxt.lineWidth = 5; cxt.translate(250, 250); cxt.rotate(h * 30 * Math.PI / 180); cxt.beginPath(); cxt.moveTo(0, 6); cxt.lineTo(0, -40); cxt.stroke(); cxt.restore(); // 畫分針 cxt.save(); cxt.lineWidth = 3; cxt.translate(250, 250); cxt.rotate(m * 6 * Math.PI / 180); cxt.beginPath(); cxt.moveTo(0, 8); cxt.lineTo(0, -60); cxt.stroke(); cxt.restore(); // 畫秒針 cxt.save(); cxt.lineWidth = 1; cxt.translate(250, 250); cxt.rotate(s * 6 * Math.PI / 180); cxt.beginPath(); cxt.moveTo(0, 10); cxt.lineTo(0, -75); cxt.stroke(); cxt.restore(); // 畫中心的小圓固定三根針 cxt.save(); cxt.beginPath(); cxt.fillStyle = '#0f0'; cxt.lineWidth = 2; cxt.translate(250, 250); cxt.arc(0, 0, 2, 0, 360, false); cxt.stroke(); cxt.fill(); cxt.restore(); // 畫秒針上的園 cxt.save(); cxt.fillStyle = '#f00'; cxt.lineWidth = 2; cxt.translate(250, 250); cxt.rotate(s * 6 * Math.PI / 180); cxt.beginPath(); cxt.arc(0, -60, 2, 0, 360, false); cxt.stroke(); cxt.fill(); cxt.restore(); } clock(); timer = setInterval(clock, 1000); </script> </body> </html>
相關文章
- canvas圓形時鐘效果Canvas
- canvas實現的圓形走動鐘錶效果Canvas
- canvas實現的圓形鐘錶效果程式碼例項Canvas
- jquery和css實現的圓形鐘錶效果jQueryCSS
- js和css3實現的圓形鐘錶效果JSCSSS3
- SVG圓形鐘錶效果SVG
- JavaScript圓形鐘錶效果JavaScript
- CSS3圓形時鐘效果程式碼CSSS3
- canvas圓形隨機漂浮效果Canvas隨機
- canvas實現的絢麗的電子時鐘效果Canvas
- canvas動態時鐘效果Canvas
- css3和js實現的圓形鐘錶效果程式碼例項CSSS3JS
- canvas繪製圓盤走動鐘錶效果Canvas
- canvas繪製圓形鐘錶程式碼例項Canvas
- canvas實現的鐘表效果程式碼例項Canvas
- JavaScript動態圓形鐘錶效果詳解JavaScript
- css3動態圓形鐘錶效果CSSS3
- canvas繪製圓形框效果不填充內部Canvas
- css實現圓形、橢圓和半圓效果程式碼例項CSS
- Flutter實現圓形波浪進度球【canvas+animation】FlutterCanvas
- 直播網站原始碼,Canvas實現圓形時間倒數計時進度條網站原始碼Canvas
- canvas 繪製圓形Canvas
- 【譯】使用CSS Transitions實現圓形懸停效果CSS
- canvas實現的動態心形效果程式碼例項Canvas
- javascript圓形電子鐘錶效果程式碼例項JavaScript
- 圓形放大的hover效果
- 多角度實現有趣的時鐘效果!
- 圓形 ImageView 的實現方法View
- canvas實現波浪效果Canvas
- jquery自動走動的圓形鐘錶效果程式碼例項jQuery
- css3和javascript實現的時鐘效果CSSS3JavaScript
- canvas實現 漂亮的下雨效果Canvas
- 使用 HTML5 Canvas 實現圓形圖片裁剪並上傳HTMLCanvas
- canvas繪製實心圓形程式碼例項Canvas
- Android 圓角、圓形 ImageView 實現AndroidView
- canvas的簡單圓形進度條Canvas
- OneClock的翻頁時鐘效果是如何實現的
- CSS圓形圖片效果CSS