canvas繪製鐘錶效果程式碼例項
本章節分享一段程式碼例項,它利用canvas實現了繪製鐘錶的效果。
並且鐘錶能夠實時走動,程式碼例項如下:
[HTML] 純文字檢視 複製程式碼<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <script type="text/javascript"> window.onload = function() { var canvas = document.getElementById('canvas'); canvas.width = 600; canvas.height = 600; var context = canvas.getContext('2d'); context.lineWidth = 5; context.strokeStyle = '#666'; context.beginPath(); context.arc(300, 300, 200, 0, 2 * Math.PI); context.closePath(); context.stroke(); drawMark(300, 300, context, 200, 180); setInterval(function() { drawHand(300, 300, context, 100, 120, 140); }, 500); } function drawMark(x, y, cxt, r1, r2) { cxt.beginPath(); for (var i = 0; i < 12; i++) { cxt.moveTo(x + r1 * Math.cos((i * 30) / 180 * Math.PI), y - r1 * Math.sin((i * 30) / 180 * Math.PI)); cxt.lineTo(x + r2 * Math.cos((i * 30) / 180 * Math.PI), y - r2 * Math.sin((i * 30) / 180 * Math.PI)); } cxt.closePath(); cxt.strokeStyle = '#777'; cxt.lineWidth = 3; cxt.stroke(); } function drawHand(x, y, cxt, rh, rm, rs) { cxt.clearRect(160, 160, 280, 280); var now = new Date(); var h = now.getHours() + now.getMinutes() / 60; var m = now.getMinutes(); var s = now.getSeconds(); cxt.beginPath(); cxt.moveTo(300, 300); cxt.lineTo(300+rh * (Math.sin(h/24*720/180 * Math.PI)), 300-rh * (Math.cos(h / 24 * 720/180 * Math.PI))); cxt.moveTo(300, 300); cxt.lineTo(300+rm * (Math.sin(m/60*360/180 * Math.PI)), 300-rm * (Math.cos(m/60 * 360/180 * Math.PI))); cxt.moveTo(300, 300); cxt.lineTo(300 + rs * (Math.sin(s/60*360 / 180 * Math.PI)), 300-rs * (Math.cos(s / 60 * 360 / 180 * Math.PI))); cxt.closePath(); cxt.strokeStyle = '#777'; cxt.lineWidth = 3; cxt.stroke(); } </script> </head> <body> <canvas id='canvas' style='display:block;margin:50px auto'> 當前瀏覽器不支援canvas </canvas> </body> </html>
相關文章
- 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
- canvas繪製米字旗程式碼例項Canvas
- canvas繪製拋物線程式碼例項Canvas線程
- canvas繪製機器貓程式碼例項Canvas
- canvas繪製圓角矩形程式碼例項Canvas
- canvas繪製熊貓頭像程式碼例項Canvas
- canvas實現的鐘表效果程式碼例項Canvas
- canvas繪製實心圓形程式碼例項Canvas
- canvas繪製網狀圓圈程式碼例項Canvas
- jQuery繪製網格效果程式碼例項jQuery
- javascript圓形電子鐘錶效果程式碼例項JavaScript
- canvas繪製機器貓頭像程式碼例項Canvas
- canvas繪製矩形並填充顏色程式碼例項Canvas
- canvas繪製貝濟埃曲線程式碼例項Canvas線程
- canvas繪製憤怒小鳥形象程式碼例項Canvas
- HTML5利用canvas繪製矩形程式碼例項HTMLCanvas
- canvas繪製五角星程式碼例項Canvas
- canvas火焰效果程式碼例項Canvas
- css3繪製月牙效果程式碼例項CSSS3
- js秒錶效果程式碼例項JS
- canvas字母雨效果程式碼例項Canvas
- canvas載入效果程式碼例項Canvas
- canvas擺動效果程式碼例項Canvas