欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

用HTML5的canvas实现一个炫酷时钟效果

程序员文章站 2023-11-28 22:25:16
下面小编就为大家带来一篇用HTML5的canvas实现一个炫酷时钟效果。小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧... 16-05-20...

对于h5来说,canvas可以说是它最有特色的一个地方了,有了它之后我们可以随意的在网页上画各种各样的图形,做一些小游戏啊什么的。canvas这个标签的用法,在网上也有特别多的教程了,这里就不作介绍了。今天我们就用canvas来做一个小小的时钟。

那么首先在这个页面里面我使用了两个canvas,一个用来绘制静态的时钟表盘和刻度,另一个用来绘制时钟的三个指针,然后用定位让他们重合到一起。然后这里没什么好说的,下面附上代码。

javascript code复制内容到剪贴板
  1. <canvas id="plate">   
  2.         画表盘   
  3. </canvas>   
  4. <canvas id="needles">   
  5.         画时针   
  6. </canvas>  
javascript code复制内容到剪贴板
  1. var plate=document.getelementbyid('plate');   
  2. var needles=document.getelementbyid('needles');   
  3. needles.setattribute('style','position:absolute;top:8px;left:8px;');  //这里因为chrome里面,body的magin值为8px,所以我这里就没设为0了。   
  4. var cntp=plate.getcontext('2d');   
  5. var cnth=needles.getcontext('2d');   
  6. plate.width=800;   
  7. plate.height=500;   
  8. needles.width=800;   
  9. needles.height=500;  

到了这里准备工作就做完了,下面就准备绘制时钟了。我先定义了一个绘制时钟表盘的构造函数。

javascript code复制内容到剪贴板
  1. function drawclock(cnt,radius,platelen,linewidth,numlen,numlen){   
  2.             this.cnt=cnt;   
  3.             this.radius=radius;   
  4.             this.platelen=platelen;   
  5.             this.linewidth=linewidth;   
  6.             this.numlen=numlen;   
  7.             this.numlen=numlen;   
  8.             this.getcalibcoor=function(i){     
  9.                 //获得表盘刻度两端的坐标   
  10.                 var x=200+this.radius*math.sin(6*i*math.pi/180);   
  11.                 var y=200-this.radius*math.cos(6*i*math.pi/180);   
  12.                 var x=200+(this.radius-this.platelen)*math.sin(6*i*math.pi/180);   
  13.                 var y=200-(this.radius-this.platelen)*math.cos(6*i*math.pi/180);   
  14.   
  15.                 // 获得分钟数字的坐标   
  16.                 var numx=200+(this.radius-this.platelen-this.numlen)*math.sin(6*i*math.pi/180);   
  17.                 var numy=200-(this.radius-this.platelen-this.numlen)*math.cos(6*i*math.pi/180);   
  18.                 //获得小时数字的坐标   
  19.                 var numx=200+(this.radius-this.platelen-this.numlen)*math.sin(6*i*math.pi/180);     
  20.                 var numy=200-(this.radius-this.platelen-this.numlen)*math.cos(6*i*math.pi/180);   
  21.                 return {x:x,y:y,x:x,y:y,numx:numx,numy:numy,numx:numx,numy:numy};   
  22.             };   
  23.             this.drawcalibration=function(){ //画刻度   
  24.                 for(var i=0,coorobj;i<60;i++){   
  25.                     coorobj=this.getcalibcoor(i);   
  26.                     this.cnt.beginpath();   
  27.                     this.cnt.moveto(coorobj.x,coorobj.y);   
  28.                     this.cnt.lineto(coorobj.x,coorobj.y);   
  29.                     this.cnt.closepath();   
  30.   
  31.                     this.cnt.linewidth=this.linewidth;   
  32.                     this.cnt.strokestyle='#ddd';   
  33.                     i%5==0&&(this.cnt.strokestyle='#aaa')   
  34.                     &&(this.cnt.linewidth=this.linewidth*2);   
  35.                     i%15==0&&(this.cnt.strokestyle='#999')   
  36.                     &&(this.cnt.linewidth=this.linewidth*3);   
  37.                     this.cnt.stroke();   
  38.   
  39.                     this.cnt.font='10px arial';   
  40.                     this.cnt.fillstyle='rgba(0,0,0,.2)';   
  41.                     this.cnt.filltext(i,coorobj.numx-7,coorobj.numy+3);   
  42.                     i%5==0&&(this.cnt.fillstyle='rgba(0,0,0,.5)')   
  43.                         &&(this.cnt.font='18px arial')   
  44.                         &&(this.cnt.filltext(i/5,coorobj.numx-5,coorobj.numy+5));   
  45.                 }   
  46.             };   
  47.         }   
  48.   
  49.       var clock=new drawclock(cntp,200,5,1,10,25); //实例化一个表盘对象   
  50.       clock.drawcalibration();  

这里最重要的部分就应该是获得刻度和数字绘制的坐标了。我把绘制刻度的起始点放在了表盘的边缘上,然后从表盘的半径上减去刻度的长度,就可以得到刻度终点的位置,然后利用角度和三角函数得到两个点的坐标。最后就可以绘制出表盘的刻度了。下面绘制出表盘上的数字也是一样的方法。我这里吧表盘的中心放在了(200,200)这里位置。到了这里我们就已经绘制好了一个静态的时钟表盘。

下面我又定义了一个绘制时钟指针的构造函数。

javascript code复制内容到剪贴板
  1. function clockneedle(cnt,r,linewidth,strokestyle,linecap,obj){   
  2.             this.r=r;   
  3.             this.cnt=cnt;   
  4.             this.linewidth=linewidth;   
  5.             this.strokestyle=strokestyle;   
  6.             this.linecap=linecap;   
  7.             this.obj=obj;   
  8.             this.getneedlecoor=function(i){   
  9.                 var x=200+this.r*0.8*math.sin(i); //起点的坐标   
  10.                 var y=200-this.r*0.8*math.cos(i);   
  11.   
  12.                 var x=200-20*math.sin(i); //终点的坐标   
  13.                 var y=200+20*math.cos(i);   
  14.                 return {x:x,y:y,x:x,y:y};   
  15.             };   
  16.             this.drawneedle=function(){   
  17.                 var d=new date().gettime();   
  18.                 var angle;   
  19.                 switch(this.obj){   
  20.                     case 0:   
  21.                     angle=(d/3600000%24+8)/12*360*math.pi/180;   
  22.                     break;   
  23.                     case 1:   
  24.                     angle=d/60000%60/60*360*math.pi/180;   
  25.                     break;   
  26.                     case 2:   
  27.                     angle=d/1000%60/60*360*math.pi/180;   
  28.                     break;   
  29.                 }   
  30.                 var coorobj=this.getneedlecoor(angle);   
  31.                 this.cnt.beginpath();   
  32.                 this.cnt.moveto(coorobj.x,coorobj.y);   
  33.                 this.cnt.lineto(coorobj.x,coorobj.y);   
  34.                 // this.cnt.closepath();   
  35.   
  36.                 this.cnt.linewidth=this.linewidth;   
  37.                 this.cnt.strokestyle=this.strokestyle;   
  38.                 this.cnt.linecap=this.linecap;   
  39.                 this.cnt.stroke();   
  40.             }   
  41.         }  

这里有两个地方需要说一下:1、在我们获得当前时间的的毫秒数,然后转换为小时的时候,对24取模计算出当天的小时数的时候,这里需要加上8。2、如果想要使用linecap这个属性吗,那么上面在设置路径的时候,不要用closepath()。

到了这里我们还需要一个来绘制指针的方法,并且让指针看起来能够转动:

javascript code复制内容到剪贴板
  1. function draw(){   
  2.             cnth.clearrect(0,0,needles.width,needles.height);   
  3.             var mzneedle=new clockneedle(cnth,200,1,'rgba(0,0,0,.5)','round',2);   
  4.             //最后一个参数0代表画时针,1画分针,2画秒针   
  5.             var fzneedle=new clockneedle(cnth,80,3,'rgba(0,0,0,.4)','round',0);   
  6.             var szneedle=new clockneedle(cnth,140,2,'rgba(0,0,0,.3)','round',1);   
  7.             mzneedle.drawneedle();   
  8.             fzneedle.drawneedle();   
  9.             szneedle.drawneedle();   
  10.             cnth.arc(200,200,5,0,2*math.pi);   
  11.             cnth.fillstyle='rgba(0,0,0,.5)';   
  12.             cnth.fill();   
  13.  }   
  14.  setinterval(draw,1);  

下面附上该时钟的图片:

用HTML5的canvas实现一个炫酷时钟效果

以上这篇用html5的canvas实现一个炫酷时钟效果就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

原文地址: