DDR爱好者之家 Design By 杰米
本文实例讲述了JS+canvas绘制的动态机械表动画效果。分享给大家供大家参考,具体如下:
先来看看运行效果:
完整实例代码:
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>www.jb51.net canvas时钟</title> <style> canvas { border: 1px solid red; } </style> </head> <body> <canvas width="800" height="600"></canvas> </body> <script> function Clock(opt) { for (var key in opt) { this[key] = opt[key]; } this.init(); } Clock.prototype = { init: function () { var self = this; var ctx = this.ctx; this.timer = setInterval(function(){ ctx.clearRect(0,0,ctx.canvas.width,ctx.canvas.height); self.drawDial(); self.drawDegreeScale(); self.drawNumber(); self.drawPointers(); },1000); }, drawDial: function () { var ctx = this.ctx; ctx.save(); ctx.beginPath(); ctx.lineWidth = this.clockDialW; ctx.strokeStyle = this.clockDialColor; ctx.arc(this.clockX, this.clockY, this.clockRadius, 0, 2 * Math.PI); ctx.stroke(); ctx.restore(); }, drawDegreeScale: function () { var ctx = this.ctx; var clockRadius = this.clockRadius; var clockX = this.clockX; var clockY = this.clockY; var bigDegreeScaleL = this.bigDegreeScaleL; var smallDegreeScale = this.smallDegreeScale; var startX, startY, endX, endY, radian; ctx.save(); for (var i = 0; i < 12; i++) { radian = i * Math.PI / 6; endX = clockX + clockRadius * Math.cos(radian); endY = clockY + clockRadius * Math.sin(radian); if (radian % (Math.PI / 2) == 0) { startX = clockX + (clockRadius - bigDegreeScaleL) * Math.cos(radian); startY = clockY + (clockRadius - bigDegreeScaleL) * Math.sin(radian); ctx.lineWidth = this.bigDCWidth; } else { startX = clockX + (clockRadius - smallDegreeScale) * Math.cos(radian); startY = clockY + (clockRadius - smallDegreeScale) * Math.sin(radian); ctx.lineWidth = this.smallDCWidth; } ctx.beginPath(); ctx.moveTo(startX, startY); ctx.lineTo(endX, endY); ctx.stroke(); ctx.restore(); } }, drawNumber: function () { var ctx = this.ctx; var textX, textY, textRadian; var clockX = this.clockX; var clockY = this.clockY; var clockRadius = this.clockRadius; ctx.font = '20px 微软雅黑'; ctx.fillStyle = 'red'; ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; ctx.save(); for (var i = 0; i < 12; i++) { textRadian = i * Math.PI / 6 - Math.PI/3; textX = clockX + (clockRadius - 40) * Math.cos(textRadian); textY = clockY + (clockRadius - 40) * Math.sin(textRadian); ctx.beginPath(); ctx.fillText(i + 1, textX, textY); } ctx.restore(); }, drawPointers: function () { var date = new Date(); var h = date.getHours(); var m = date.getMinutes(); var s = date.getSeconds(); h = h % 12; var hRadian = 2 * Math.PI / 12 * h - Math.PI / 2; this.drawPoint(hRadian,30,'red',8); var mRadian = 2 * Math.PI / 60 * m - Math.PI / 2; this.drawPoint(mRadian,50,'blue',6); var sRadian = 2 * Math.PI / 60 * s - Math.PI / 2; this.drawPoint(sRadian,70,'green',2); }, drawPoint: function (radian, length,color,lineWidth) { var x = this.clockX + Math.cos(radian) * length; var y = this.clockY + Math.sin(radian) * length; var ctx = this.ctx; ctx.save(); ctx.beginPath(); ctx.strokeStyle = color; ctx.lineWidth = lineWidth; ctx.moveTo(this.clockX,this.clockY); ctx.lineTo(x,y); ctx.stroke(); ctx.restore(); } }; </script> <script> var canvas = document.querySelector('canvas'); var ctx = canvas.getContext('2d'); var clock = new Clock({ ctx: this.ctx, clockRadius: 150, clockX: 300, clockY: 300, clockDialW: 6, clockDialColor: 'blue', bigDegreeScaleL: 20, bigDCWidth: 6, smallDegreeScale: 10, smallDCWidth: 4 }); </script> </html>
更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《JavaScript+HTML5特效与技巧汇总》、《JavaScript动画特效与技巧汇总》、《JavaScript图形绘制技巧总结》、《JavaScript切换特效与技巧总结》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》及《JavaScript数学运算用法总结》
希望本文所述对大家JavaScript程序设计有所帮助。
DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米
暂无评论...
更新日志
2024年11月27日
2024年11月27日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]