Advertisement
Guest User

HTML5 clock

a guest
Oct 13th, 2015
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.04 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.     <head>
  4.         <title>Title</title>
  5.         <meta charset="utf-8" />
  6.         <style>
  7.             #sekunde {
  8.                 visibility: hidden;
  9.             }
  10.         </style>
  11.     </head>
  12.     <body>
  13.         <canvas id="canvas" width="300" height="300">Upgrade your browser!</canvas>
  14.         <script>
  15.             var fps = 60;
  16.             var canvas = document.getElementById("canvas");
  17.             var ctx = canvas.getContext("2d");
  18.             var time = new Date();
  19.             var hour = 0;
  20.             var min = 0;
  21.             var sec = 0;
  22.            
  23.             function frame(){
  24.                 setTimeout(frame, 1000 / fps);
  25.                 calc();
  26.                 window.requestAnimationFrame(render);
  27.             }
  28.            
  29.             function calc(){
  30.                 time = new Date();
  31.                 hour = time.getHours();
  32.                 min = time.getMinutes();
  33.                 sec = time.getSeconds();
  34.             }
  35.            
  36.             function render(){
  37.                 ctx.clearRect(0, 0, canvas.width, canvas.height);
  38.                 ctx.save();
  39.                     ctx.translate(canvas.width / 2, canvas.height / 2);
  40.                     ctx.rotate(-Math.PI / 2);
  41.                     ctx.beginPath();
  42.                     ctx.arc(0,0, 100, 0, Math.PI * 2);
  43.                     ctx.closePath();
  44.                     ctx.stroke();
  45.                     ctx.save();
  46.                         ctx.rotate(Math.PI / 6 * hour);
  47.                         ctx.fillRect(0, -3, 40, 6);
  48.                     ctx.restore();
  49.                     ctx.save();
  50.                         ctx.rotate(Math.PI / 30 * min);
  51.                         ctx.fillRect(0, -2, 80, 4);
  52.                     ctx.restore();
  53.                     ctx.save();
  54.                         ctx.rotate(Math.PI / 30 * sec);
  55.                         ctx.fillRect(0, -1, 95, 2);
  56.                     ctx.restore();
  57.                 ctx.restore();
  58.             }
  59.            
  60.             frame();
  61.         </script>
  62.     </body>
  63. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement