龙空技术网

js使用canvas制作时钟

北漂佳佳的生活 759

前言:

眼前你们对“js制作动态时钟特效教程”大体比较关心,你们都想要学习一些“js制作动态时钟特效教程”的相关知识。那么小编也在网摘上搜集了一些有关“js制作动态时钟特效教程””的相关资讯,希望你们能喜欢,兄弟们快快来了解一下吧!

<canvas id="myCan" width="400" height="500"></canvas>

//1.把一个圆分成60等份(for循环 60个刻度)

//2.空白圆(遮住中间的线)

//3.再画一个圆(时刻度 width=3)

//4.空白圆(遮住线)

//5.获取当前时间,每次旋转相应的刻度

//6.定时器,每秒都在变化

//间隔1秒钟执行一次setInterval(canvas,1000);function canvas(){	var oDate=new Date();	var oHour=oDate.getHours();	var oMin=oDate.getMinutes();	var oSec=oDate.getSeconds();	var oHourValue=(-90+oHour*30)*Math.PI/180;	var oMinValue=(-90+oMin*6)*Math.PI/180;	var oSecValue=(-90+oSec*6)*Math.PI/180; //获取canvas元素,并设置舞台	 var og=document.getElementById('myCan');	 var ogc=og.getContext('2d');	 //画一个大圆	 ogc.beginPath();	 for(var i=0;i<60;i++){	 	ogc.moveTo(200,200);	 	ogc.arc(200,200,150,6*i*Math.PI/180,6*(i+1)*Math.PI/180,false);	 }	ogc.stroke();	ogc.closePath();	//画空白圆	ogc.beginPath();	ogc.fillStyle="#fff";	ogc.moveTo(200,200);	ogc.arc(200,200,150*19/20,0,360*Math.PI/180,false);	ogc.fill();	ogc.closePath();	//分针刻度	ogc.beginPath();	ogc.lineWidth=3;	for(var j=0;j<12;j++){		ogc.moveTo(200,200);		ogc.arc(200,200,150,30*j*Math.PI/180,30*(j+1)*Math.PI/180,false);				}	ogc.stroke();	ogc.closePath();	//画空白圆	ogc.beginPath();	ogc.fillStyle="#fff";	ogc.moveTo(200,200);	ogc.arc(200,200,150*18/20,0,360*Math.PI/180,false);	ogc.fill();	ogc.closePath();	//时	ogc.beginPath();	ogc.lineWidth=5;	ogc.moveTo(200,200);	ogc.fillStyle="#fff";	ogc.arc(200,200,150*10/20,oHourValue,oHourValue,false);		ogc.stroke();	ogc.closePath();	//分	ogc.beginPath();	ogc.lineWidth=3;	ogc.moveTo(200,200);	ogc.fillStyle="#fff";	ogc.arc(200,200,150*13/20,oMinValue,oMinValue,false);		ogc.stroke();	ogc.closePath();	//秒	ogc.beginPath();	ogc.lineWidth=1;	ogc.moveTo(200,200);	ogc.fillStyle="#fff";	ogc.arc(200,200,150*17/20,oSecValue,oSecValue,false);		ogc.stroke();	ogc.closePath(); ogc.save();}

标签: #js制作动态时钟特效教程