PayneLess_Designs

PayneLess Designs - HTML 5 Clock

Aug 3rd, 2013
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 4.33 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en"><head>
  3. <meta charset="utf-8">
  4. <title>Clock Animation - HTML5 Canvas (Basic Layout)</title>
  5. <meta name="generator" content="CoffeeCup HTML Editor (www.coffeecup.com)">
  6. <meta name="dcterms.created" content="Sat, 03 Aug 2013 05:12:24 GMT">
  7. <meta name="description" content="">
  8. <meta name="keywords" content="">
  9.  
  10. <style>
  11. h1 { text-align: center; }
  12.  
  13. #clockPostion {
  14. width: 300px; height: 300px;
  15. margin: 0 auto;
  16. }
  17.  
  18. </style>
  19.    
  20. <!--[if IE]>
  21. <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
  22. <![endif]-->
  23. </head><body>
  24. <h1>Clock Animation - HTML5 Canvas (Basic Layout)</h1>
  25.  
  26. <div id="clockPostion"><canvas id="clock" width="300" height="300">The &lt;canvas&gt; element is not supported by your browser!</canvas></div>
  27.  
  28. <br><br>
  29.  
  30. <script>
  31.     draw();
  32.    
  33.     function draw() {
  34.       var canvas = document.getElementById('clock');
  35.       if (canvas.getContext) {
  36.         var c2d=canvas.getContext('2d');
  37.         c2d.clearRect(0,0,300,300);
  38.         //Define gradients for 3D / shadow effect
  39.         var grad1=c2d.createLinearGradient(0,0,300,300);
  40.         grad1.addColorStop(0,"#D83040");
  41.         grad1.addColorStop(1,"#801020");
  42.         var grad2=c2d.createLinearGradient(0,0,300,300);
  43.         grad2.addColorStop(0,"#801020");
  44.         grad2.addColorStop(1,"#D83040");
  45.         c2d.font="Bold 20px Arial";
  46.         c2d.textBaseline="middle";
  47.         c2d.textAlign="center";
  48.         c2d.lineWidth=1;
  49.         c2d.save();
  50.         //Outer bezel
  51.         c2d.strokeStyle=grad1;
  52.         c2d.lineWidth=10;
  53.         c2d.beginPath();
  54.         c2d.arc(150,150,138,0,Math.PI*2,true);
  55.         c2d.shadowOffsetX=4;
  56.         c2d.shadowOffsetY=4;
  57.         c2d.shadowColor="rgba(0,0,0,0.6)";
  58.         c2d.shadowBlur=6;
  59.         c2d.stroke();
  60.         //Inner bezel
  61.         c2d.restore();
  62.         c2d.strokeStyle=grad2;
  63.         c2d.lineWidth=10;
  64.         c2d.beginPath();
  65.         c2d.arc(150,150,129,0,Math.PI*2,true);
  66.         c2d.stroke();
  67.         c2d.strokeStyle="#222";
  68.         c2d.save();
  69.         c2d.translate(150,150);
  70.         //Markings/Numerals
  71.         for (i=1;i<=60;i++) {
  72.          ang=Math.PI/30*i;
  73.          sang=Math.sin(ang);
  74.          cang=Math.cos(ang);
  75.          //If modulus of divide by 5 is zero then draw an hour marker/numeral
  76.          if (i % 5 == 0) {
  77.            c2d.lineWidth=8;
  78.            sx=sang*95;
  79.            sy=cang*-95;
  80.            ex=sang*120;
  81.            ey=cang*-120;
  82.            nx=sang*80;
  83.            ny=cang*-80;
  84.            c2d.fillText(i/5,nx,ny);
  85.          //Else this is a minute marker
  86.          } else {
  87.            c2d.lineWidth=2;
  88.            sx=sang*110;
  89.            sy=cang*110;
  90.            ex=sang*120;
  91.            ey=cang*120;
  92.          }
  93.          c2d.beginPath();
  94.          c2d.moveTo(sx,sy);
  95.          c2d.lineTo(ex,ey);
  96.          c2d.stroke();
  97.        }
  98.        //Fetch the current time
  99.        var ampm="AM";
  100.        var now=new Date();
  101.        var hrs=now.getHours();
  102.        var min=now.getMinutes();
  103.        var sec=now.getSeconds();
  104.        c2d.strokeStyle="#000";
  105.        //Draw AM/PM indicator
  106.        if (hrs>=12) ampm="PM";
  107.         c2d.lineWidth=1;
  108.         c2d.strokeRect(21,-14,44,27);
  109.         c2d.fillText(ampm,43,0);
  110.        
  111.         c2d.lineWidth=6;
  112.         c2d.save();
  113.         //Draw clock pointers but this time rotate the canvas rather than
  114.         //calculate x/y start/end positions.
  115.         //
  116.         //Draw hour hand
  117.         c2d.rotate(Math.PI/6*(hrs+(min/60)+(sec/3600)));
  118.         c2d.beginPath();
  119.         c2d.moveTo(0,10);
  120.         c2d.lineTo(0,-60);
  121.         c2d.stroke();
  122.         c2d.restore();
  123.         c2d.save();
  124.         //Draw minute hand
  125.         c2d.rotate(Math.PI/30*(min+(sec/60)));
  126.         c2d.beginPath();
  127.         c2d.moveTo(0,20);
  128.         c2d.lineTo(0,-110);
  129.         c2d.stroke();
  130.         c2d.restore();
  131.         c2d.save();
  132.         //Draw second hand
  133.         c2d.rotate(Math.PI/30*sec);
  134.         c2d.strokeStyle="#E33";
  135.         c2d.beginPath();
  136.         c2d.moveTo(0,20);
  137.         c2d.lineTo(0,-110);
  138.         c2d.stroke();
  139.         c2d.restore();
  140.        
  141.         //Additional restore to go back to state before translate
  142.         //Alternative would be to simply reverse the original translate
  143.         c2d.restore();
  144.         setTimeout(draw,1000);
  145.       }
  146.     }
  147. </script>
  148. </body></html>
Advertisement
Add Comment
Please, Sign In to add comment