canvas教程

js Canvas绘制圆形时钟教程

字号+ 作者:H5之家 来源:H5之家 2017-02-11 18:04 我要评论( )

这篇文章主要为大家详细介绍了js Canvas绘制圆形时钟教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>canvas-clock</title> </head> <body> <canvas> 你的浏览器不支持该元素!赶紧下载最新版本浏览器或使用其他浏览器! </canvas> <script> //获取到canvas元素 var canvas = document.getElementById('canvas'); //获取canvas中的画图环境 var context = canvas.getContext('2d'); //时钟的大小 function drowClock(){ //钟表的大小:初始值设置 var clockDimensions = 150; //清理当前画布,以便后期绘制 context.clearRect(0,0,canvas.width,canvas.height); //绘制表盘 context.beginPath(); //开启新路径 context.lineWidth = clockDimensions/15; context.strokeStyle = "#A7C0DC"; //绘制表盘圆圈 context.arc(canvas.width/2,canvas.height/2,clockDimensions,0,Math.PI*2,false); context.stroke();//描边绘制 //绘制表盘的刻度线 for(var i=1;i<=60;i++){ if(i%5==0){ context.save();//保存当前绘制环境 context.beginPath(); context.lineWidth =clockDimensions/30; context.strokeStyle = "#9AABB1"; //重置坐标原点(0,0) context.translate(canvas.width/2,canvas.height/2); //绘制环境旋转方法,以(0,0)为参考点进行旋转 context.rotate(Math.PI*2/60 * i); context.moveTo(0,clockDimensions-clockDimensions/30); context.lineTo(0,clockDimensions-clockDimensions/8); context.stroke(); context.beginPath(); context.textAlign = 'center'; context.textBaseline = 'middle'; context.font = 'bold '+Math.floor(clockDimensions/10)+'px 宋体'; context.fillStyle = "#03671F"; context.fillText(i/5,0,0-(clockDimensions-clockDimensions/5)); context.fill(); context.restore();//恢复当前保存的绘制环境 }else { context.save(); context.beginPath(); context.lineWidth = Math.floor(clockDimensions/100); context.strokeStyle = "#8EA5AB"; //重置坐标原点(0,0) context.translate(canvas.width / 2, canvas.height / 2); //绘制环境旋转方法,以(0,0)为参考点进行旋转 context.rotate(Math.PI * 2 / 60 * i); context.moveTo(0, clockDimensions-clockDimensions/20); context.lineTo(0, clockDimensions-clockDimensions/10); context.stroke(); context.restore(); } } //获取当前windows的时间 var now = new Date(); var sec = now.getSeconds(); var min = now.getMinutes(); var hour = now.getHours(); //获取精准的小时数 hour = hour +min/60 + sec/3600; //转换为12进制 hour = hour>12?(hour-12):hour; //获取精准的分钟数 min = min + sec/60; //绘制时针 context.save(); context.beginPath(); context.lineWidth = clockDimensions/30; context.strokeStyle = "#596C74"; //重置坐标原点(0,0) context.translate(canvas.width / 2, canvas.height / 2); //绘制环境旋转方法,以(0,0)为参考点进行旋转 context.rotate(Math.PI * 2 / 12 * hour); context.moveTo(0, clockDimensions/10); context.lineTo(0, 0-clockDimensions/2); context.stroke(); context.restore(); //绘制分针 context.save(); context.beginPath(); context.lineWidth = clockDimensions/40; context.strokeStyle = "#596C74"; //重置坐标原点(0,0) context.translate(canvas.width / 2, canvas.height / 2); //绘制环境旋转方法,以(0,0)为参考点进行旋转 context.rotate(Math.PI * 2 / 60 * min); context.moveTo(0, clockDimensions/8); context.lineTo(0, 0-(clockDimensions-clockDimensions/5)); context.stroke(); context.restore(); //绘制秒针 context.save(); //重置坐标原点(0,0) context.translate(canvas.width / 2, canvas.height / 2); context.beginPath(); context.lineWidth = clockDimensions/50; context.strokeStyle = "#738B93"; //绘制环境旋转方法,以(0,0)为参考点进行旋转 context.rotate(Math.PI * 2 / 60 * sec); context.moveTo(0, clockDimensions/6); context.lineTo(0, 0-(clockDimensions-clockDimensions/10)); context.stroke(); //修饰秒针 context.beginPath(); context.arc(0,0-(clockDimensions-clockDimensions/3),clockDimensions/20,0,Math.PI*2,true); context.fillStyle = "#2FFC14"; context.fill(); context.lineWidth = clockDimensions/50; context.stroke(); //修饰圆心 context.beginPath(); context.fillStyle = "#738B93"; context.arc(0,0,clockDimensions/20,0,Math.PI*2,true); context.fill(); context.restore(); } drowClock(); setInterval(drowClock,1000); </script> </body> </html>

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • canvas的神奇用法

    canvas的神奇用法

    2017-02-10 15:04

  • PHP源码免费下载,分享源码,网页特效,源码分享,html网页模板

    PHP源码免费下载,分享源码,网页特效,源码分享,html网页模板

    2017-02-09 08:00

  • javascript结合html5 canvas实现(可调画笔颜色/粗细/橡皮)的涂

    javascript结合html5 canvas实现(可调画笔颜色/粗细/橡皮)的涂

    2017-02-08 18:02

  • Bitmap,bitmap,src,Canvas,return,保存,watermark,代码,null,cre

    Bitmap,bitmap,src,Canvas,return,保存,watermark,代码,null,cre

    2017-02-07 13:03

网友点评