canvas钟表(加事件)

 

本文章转自IT老黑(strive) 地址:http://www.itstrive.com 温馨提示:亲……只兼容Chrome哦^_^!!

 

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

 

你可能感兴趣的:(html5,canvas,css3)