Advertisement
avr39ripe

clockFunction

Jun 2nd, 2020
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2.  
  3. <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5.     <meta charset="utf-8" />
  6.     <title></title>
  7.     <style>
  8.         canvas {
  9.             /*border: 1px solid black;*/
  10.         }
  11.     </style>
  12. </head>
  13. <body>
  14.     <canvas id="draw" width="640" height="480">
  15.     </canvas>
  16.     <script>
  17.         let canvas = document.getElementById('draw');
  18.         let ctx = canvas.getContext('2d');
  19.         let width = 640;
  20.         let height = 480;
  21.         let size = 200;
  22. /*
  23.         let radius = 150;
  24.  
  25.         ctx.beginPath();
  26.         ctx.moveTo((width / 2 ) - radius *0.8, (height / 2 ));
  27.         ctx.arc((width / 2 ), (height / 2 ), radius, - Math.PI/8, Math.PI/8, true );
  28.         ctx.closePath();
  29.         //ctx.stroke();
  30.         ctx.fillStyle = 'red';
  31.         ctx.fill();
  32.         ctx.beginPath();
  33.         ctx.arc((width / 2 ) + (radius/2), (height / 2 ) - (radius/2), radius/8, 0, Math.PI*2);
  34.         ctx.fillStyle = 'black';
  35.         ctx.closePath();
  36.         ctx.fill();
  37.        
  38. */
  39.  
  40.  /*       ctx.save()
  41.         ctx.translate((width / 2 ), (height / 2 ));
  42.         ctx.beginPath();
  43.         ctx.moveTo(0, 0);
  44.  
  45.         ctx.strokeStyle = 'red';
  46.         //ctx.fillStyle = 'red';
  47.         ctx.lineWidth = 5;
  48.         ctx.lineTo(-200,0);
  49.         ctx.lineTo(-200,-200);
  50.         ctx.stroke();
  51.  
  52.         ctx.save();
  53.         ctx.beginPath();
  54.         ctx.strokeStyle = 'blue';
  55.         ctx.rotate(-Math.PI/2);
  56.         ctx.moveTo(0, 0);
  57.         ctx.lineTo(-200,0);
  58.         ctx.lineTo(-200,-200);
  59.         ctx.stroke();
  60.         ctx.restore()
  61.  
  62.         ctx.beginPath();
  63.         ctx.moveTo(0, 0);
  64.         ctx.lineTo(200,0);
  65.         ctx.lineTo(200,-200);
  66.         ctx.stroke();
  67.         ctx.restore();
  68.  
  69.         ctx.beginPath();
  70.         ctx.moveTo(0, 0);
  71.         ctx.lineTo(100,100);
  72.         ctx.stroke();
  73.         //ctx.restore();*/
  74.  
  75. /*        ctx.save()
  76.         ctx.translate((width / 2 ), (height / 2 ));
  77.         ctx.rotate(-Math.PI/2);
  78.         ctx.scale(1.5,1.5);
  79.  
  80.         //for(let angle=0; angle <= Math.PI*2; angle += Math.PI / 4)
  81.  
  82.         const raysCnt = 12
  83.         for (let i = 0; i < raysCnt; ++i)
  84.         {
  85.             ctx.rotate(Math.PI / (raysCnt/ 2));
  86.             ctx.moveTo(0,0);
  87.             ctx.lineTo(100,0);
  88.             ctx.stroke();
  89.         }
  90.         ctx.beginPath();
  91.         ctx.moveTo(0, 0);
  92.  
  93.         ctx.restore()
  94.     */
  95.         function drawRay(radius, length, angle, ctx)
  96.         {    
  97.             ctx.beginPath();
  98.             ctx.moveTo(Math.cos(angle) * (radius - length), Math.sin(angle) * (radius - length));
  99.             ctx.lineTo(Math.cos(angle) * radius, Math.sin(angle) * radius);
  100.             ctx.stroke();
  101.         }
  102.  
  103.         function drawClock(ctx)
  104.         {
  105.             ctx.save();
  106.             ctx.translate((width / 2 ), (height / 2 ));
  107.    
  108.             let radius = 150;
  109.  
  110.             ctx.clearRect(-radius,-radius, radius*2, radius*2);
  111.  
  112.             let clockRadius = radius * 0.85;
  113.             let radiusHourMark = clockRadius;
  114.             let lengthHourMark = 10;
  115.             let radiusMinuteMark = clockRadius;
  116.             let lengthMinuteMark = 6;
  117.  
  118.             for(let i=0, angle=0; i<60; angle+=Math.PI/30, ++i)
  119.             {
  120.                 if ( i % 5 == 0)
  121.                 {
  122.                     ctx.save();
  123.                     ctx.lineWidth = 8;
  124.                     ctx.lineCap= 'round';
  125.                     drawRay(radiusHourMark,lengthHourMark,angle,ctx);
  126.                     ctx.restore();
  127.                 }
  128.                 else
  129.                 {
  130.                     ctx.save();
  131.                     drawRay(radiusMinuteMark,lengthMinuteMark,angle,ctx);
  132.                     ctx.restore();
  133.                 }
  134.            
  135.             }
  136.  
  137.             let now = new Date();
  138.             let sec = now.getSeconds();
  139.             let min = now.getMinutes();
  140.             let hour = now.getHours();
  141.  
  142.             hour = hour > 12 ? hour - 12 : hour;
  143.  
  144.             let angleSec = ( Math.PI * 2 / 60 * sec ) - ( Math.PI / 2 );
  145.             let radiusSec = clockRadius;
  146.             let lengthSec = clockRadius;
  147.  
  148.             let angleMin = ( Math.PI * 2 / 60 * min + Math.PI * 2 / 3600 * sec) - ( Math.PI / 2 );
  149.             let radiusMin = clockRadius * 0.95;
  150.             let lengthMin = clockRadius * 0.95;
  151.  
  152.             let angleHour = ( Math.PI * 2 / 12 * hour + Math.PI * 2 / 720 * min + Math.PI * 2 / 43200 * sec ) - ( Math.PI / 2 );
  153.             let radiusHour = clockRadius * 0.7;
  154.             let lengthHour = clockRadius * 0.7;
  155.  
  156.             ctx.save();
  157.             ctx.lineWidth = 5;
  158.             drawRay(radiusMin, lengthMin, angleMin, ctx);
  159.             drawRay(radiusMin*0.15, lengthMin*0.15, angleMin-Math.PI, ctx);
  160.  
  161.             ctx.lineWidth = 7;
  162.             drawRay(radiusHour, lengthHour, angleHour, ctx);
  163.             drawRay(radiusHour*0.15, lengthHour*0.15, angleHour-Math.PI, ctx);
  164.             ctx.restore();
  165.  
  166.             ctx.save();
  167.             ctx.lineWidth = 2;
  168.             ctx.strokeStyle='red';
  169.             drawRay(radiusSec, lengthSec, angleSec, ctx);
  170.             drawRay(radiusSec*0.15, lengthSec*0.15, angleSec-Math.PI, ctx);
  171.             ctx.restore();
  172.  
  173.             ctx.restore();
  174.         }
  175.  
  176.         function animate()
  177.         {
  178.             drawClock(ctx);
  179.             requestAnimationFrame(animate);
  180.         };
  181.  
  182.         animate();
  183.        
  184.     </script>
  185.  
  186. </body>
  187. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement