天天看點

用HTML5 Canvas畫的時鐘 用HTML5 Canvas畫的時鐘

用HTML5 Canvas畫的時鐘

用HTML5 Canvas畫的時鐘 用HTML5 Canvas畫的時鐘

效果圖

  1. <!doctype html> 
  2. <html> 
  3. <head> 
  4. <meta charset="utf-8"> 
  5. <title>canvas鐘表_by http://www.sitejs.cn</title> 
  6. <style> 
  7. #c1{ background:-webkit-radial-gradient(#ccc,#930); display:block; margin:100px auto;} 
  8. body{ background:#000;}  
  9. </style> 
  10. <script> 
  11. function Rect(x,y,w,h,r){ 
  12.     this.x=x; 
  13.     this.y=y; 
  14.     this.w=w; 
  15.     this.h=h; 
  16.     this.background='#36383a'; 
  17.     this.borderColor='black'; 
  18.     thisthis.originX=this.w/2; 
  19.     thisthis.originY=this.h/2; 
  20. Rect.prototype.draw=function(gd){ 
  21.     gd.save(); 
  22.     gd.beginPath(); 
  23.     gd.translate(this.x,this.y); 
  24.     gd.rotate(d2a(this.r)); 
  25.     gd.fillStyle=this.background; 
  26.     gd.strokeStyle=this.borderColor; 
  27.     gd.fillRect(-this.originX,-this.originY,this.w,this.h); 
  28.     gd.strokeRect(-this.originX,-this.originY,this.w,this.h); 
  29.     gd.closePath(); 
  30.     gd.restore(); 
  31. function d2a(n){ 
  32.     return n*Math.PI/180; 
  33. function mouseInCicle(cx,cy,cr,x,y){ 
  34.     var d=Math.sqrt(Math.pow(x-cx,2)+Math.pow(cy-y,2));  
  35.     return d<cr; 
  36. window.onload=function(){ 
  37.     if(!/Chrom/g.test(navigator.userAgent)){alert('您的浏覽器不支援此項功能,請您下載下傳并安裝chrome'); document.write('<a href="http://www.google.cn/chrome">下載下傳chrome浏覽器</a>');} 
  38.     var oC=document.getElementById('c1'); 
  39.     var gd=oC.getContext('2d'); 
  40.     var clockX=100,clockY=100,clockR=100,timer=null; 
  41.     var hour=new Rect(clockX,clockY,6,50); 
  42.     var min=new Rect(clockX,clockY,4,70); 
  43.     var sec=new Rect(clockX,clockY,2,90); 
  44.     hour.background='#111315'; 
  45.     hour.borderColor='#111315'; 
  46.     hour.originY=hour.h; 
  47.     min.background='#111315'; 
  48.     min.borderColor='#111315'; 
  49.     min.originY=min.h; 
  50.     sec.background='#111315'; 
  51.     sec.borderColor='#111315'; 
  52.     sec.originY=sec.h; 
  53.     oC.onmousedown=function(ev){ 
  54.         if(!mouseInCicle(clockX,clockY,clockR,ev.offsetX,ev.offsetY))return; 
  55.         var disX=ev.clientX-clockX; 
  56.         var disY=ev.clientY-clockY; 
  57.         document.onmousemove=function(ev){ 
  58.             var l=ev.clientX-disX; 
  59.             var t=ev.clientY-disY; 
  60.             ll=l<=105?105:l; 
  61.             tt=t<=105?105:t; 
  62.             ll=l>=690?690:l; 
  63.             tt=t>=490?490:t; 
  64.             clockX=l; 
  65.             clockY=t; 
  66.             tick(); 
  67.         } 
  68.         document.onmouseup=function(){ 
  69.             document.onmousemove=null; 
  70.             document.onmouseup=null;     
  71.         } 
  72.         return false; 
  73.     } 
  74.     function tick(){ 
  75.         gd.clearRect(0,0,oC.width,oC.height); 
  76.         gd.save() 
  77.         gd.beginPath(); 
  78.         gd.lineWidth='10'; 
  79.         gd.shadowOffsetX='0'; 
  80.         gd.shadowOffsetY='0'; 
  81.         gd.shadowBlur='20'; 
  82.         gd.shadowColor='#999999'; 
  83.         gd.strokeStyle='#36383a'; 
  84.         gd.arc(clockX,clockY,clockR,0,d2a(360)); 
  85.         gd.stroke(); 
  86.         gd.restore(); 
  87.         hour.x=clockX; 
  88.         hour.y=clockY; 
  89.         min.x=clockX; 
  90.         min.y=clockY; 
  91.         sec.x=clockX; 
  92.         sec.y=clockY; 
  93.         var oDate=new Date(); 
  94.         hour.r=oDate.getHours()*30; 
  95.         hour.draw(gd); 
  96.         min.r=oDate.getMinutes()*6; 
  97.         min.draw(gd); 
  98.         sec.r=oDate.getSeconds()*6; 
  99.         sec.draw(gd); 
  100.         gd.beginPath(); 
  101.         gd.fillStyle='#424345'; 
  102.         gd.arc(clockX,clockY,10,0,d2a(360)); 
  103.         gd.fill(); 
  104.         //四個表盤針 
  105.         gd.beginPath(); 
  106.         var sTop=new Rect(clockX,clockY,3,5); 
  107.         var sBottom=new Rect(clockX,clockY,3,5); 
  108.         var sLeft=new Rect(clockX,clockY,5,3); 
  109.         var sRight=new Rect(clockX,clockY,5,3); 
  110.         sTop.originY=sTop.h+90; 
  111.         sTop.draw(gd); 
  112.         sBottom.originY=sTop.h-95; 
  113.         sBottom.draw(gd); 
  114.         sLeft.originX=sTop.w+90; 
  115.         sLeft.draw(gd); 
  116.         sRight.originX=sTop.w-92; 
  117.         sRight.draw(gd); 
  118.         gd.closePath(); 
  119.         gd.font='20px 宋體'; 
  120.         gd.fillText('http://www.sitejs.cn',300,520,500); 
  121.         gd.font='50px 黑體';   
  122.         gd.fillText('拖拽的試試……',220,580,500); 
  123.     } 
  124.     timer=setInterval(tick,1000); 
  125.     tick(); 
  126. </script> 
  127. </head> 
  128. <body> 
  129. <canvas id="c1" width="800" height="600"> 
  130.     您的浏覽器不支援canvas! 
  131. </canvas> 
  132. </body> 
  133. </html> 

來自:http://www.w3cfuns.com

繼續閱讀