公司为何要开发小程序_js Canvas绘制圆形时钟教程

2021-01-12 14:28 jianzhan
js Canvas绘制圆形时钟教程       这篇文章主要为大家详细介绍了js Canvas绘制圆形时钟教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
canvas id="canvas" width="500px" height="500px" 你的浏览器不支持该元素!赶紧下载最新版本浏览器或使用其他浏览器! /canvas script //获取到canvas元素 var canvas = document.getElementById('canvas'); //获取canvas中的画图环境 var context = canvas.getContext('2d');
//绘制表盘圆圈 context.arc(canvas.width/2,canvas.height/2,clockDimensions,0,Math.PI*2,false); context.stroke();//描边绘制 //绘制表盘的刻度线 for(var i=1;i 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

效果图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持凡科。