Advertisement
avr39ripe

jsClockClass

May 10th, 2021
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 
  2. <!doctype html>
  3. <html>
  4. <head>
  5.     <title>Canvas example</title>
  6.     <meta charset='utf-8' />
  7. </head>
  8. <body>
  9.     <canvas id='draw' width="600" height="600"></canvas>
  10.     <script>
  11.         class Clock {
  12.             constructor(x, y, radius, ctx, secondsShift = 0, name = '') {
  13.                 this.x = x;
  14.                 this.y = y;
  15.                 this.radius = radius;
  16.                 this.ctx = ctx;
  17.                 this.secondsShift = secondsShift;
  18.                 this.name = name;
  19.             };
  20.  
  21.             _drawRay(radius, length, angle) {
  22.                 this.ctx.beginPath();
  23.                 this.ctx.moveTo(Math.cos(angle) * (radius - length), Math.sin(angle) * (radius - length));
  24.                 this.ctx.lineTo(Math.cos(angle) * radius, Math.sin(angle) * radius);
  25.                 this.ctx.stroke();
  26.             };
  27.  
  28.             draw() {
  29.                 this.ctx.save();
  30.                 this.ctx.translate(this.x, this.y);
  31.  
  32.                 this.ctx.moveTo(this.radius, 0);
  33.                 this.ctx.arc(0, 0, this.radius, 0, Math.PI * 2);
  34.                 this.ctx.clip();
  35.                 this.ctx.clearRect(-this.radius, -this.radius, this.radius * 2, this.radius * 2);
  36.                 this.ctx.beginPath();
  37.  
  38.                 this.ctx.lineWidth = 12;
  39.                 this.ctx.arc(0, 0, this.radius, 0, Math.PI * 2);
  40.                 this.ctx.stroke();
  41.  
  42.  
  43.                 let clockRadius = this.radius * 0.85;
  44.                 let radiusHourMark = clockRadius;
  45.                 let lengthHourMark = 10;
  46.                 let radiusMinuteMark = clockRadius;
  47.                 let lengthMinuteMark = 6;
  48.                 for (let i = 0, angle = 0; i < 60; angle += Math.PI / 30, ++i) {
  49.                     if (i % 5 == 0) {
  50.                         this.ctx.save();
  51.                         this.ctx.lineWidth = 8;
  52.                         this.ctx.lineCap = 'round';
  53.                         this._drawRay(radiusHourMark, lengthHourMark, angle);
  54.                         this.ctx.restore();
  55.                     }
  56.                     else {
  57.                         this.ctx.save();
  58.                         this.ctx.lineWidth = 4;
  59.                         this._drawRay(radiusMinuteMark, lengthMinuteMark, angle);
  60.                         this.ctx.restore();
  61.                     }
  62.                 }
  63.  
  64.                 if (this.name) {
  65.                     this.ctx.save();
  66.                     this.ctx.font = `${this.radius * 0.2}px sans-serif`;
  67.                     this.ctx.textAlign = 'center';
  68.                     this.ctx.fillText(this.name, 0, this.radius * 0.30);
  69.                     this.ctx.restore();
  70.                 }
  71.  
  72.  
  73.                 let now = new Date();
  74.  
  75.                 let sec = (!this.secondsShift ? now.getSeconds() : now.setSeconds(now.getSeconds() + this.secondsShift), now.getSeconds());
  76.                 let min = now.getMinutes();
  77.                 let hour = now.getHours();
  78.                 hour = hour > 12 ? hour - 12 : hour;
  79.  
  80.                 let angleSec = (Math.PI * 2 / 60 * sec) - Math.PI / 2;
  81.                 let radiusSec = clockRadius;
  82.                 let lengthSec = clockRadius;
  83.  
  84.                 let angleMin = (Math.PI * 2 / 60 * min + Math.PI * 2 / 3600 * sec) - Math.PI / 2;
  85.                 let radiusMin = clockRadius * 0.95;
  86.                 let lengthMin = clockRadius * 0.95;
  87.  
  88.                 let angleHour = (Math.PI * 2 / 12 * hour + Math.PI * 2 / 720 * min + Math.PI * 2 / 43200 * sec) - Math.PI / 2;
  89.                 let radiusHour = clockRadius * 0.7;
  90.                 let lengthHour = clockRadius * 0.7;
  91.  
  92.                 this.ctx.save();
  93.                 this.ctx.lineWidth = 5;
  94.                 this._drawRay(radiusMin, lengthMin, angleMin);
  95.                 this._drawRay(radiusMin * 0.15, radiusMin * 0.15, angleMin - Math.PI);
  96.                 this.ctx.lineWidth = 7;
  97.                 this._drawRay(radiusHour, lengthHour, angleHour);
  98.                 this._drawRay(radiusHour * 0.15, radiusHour * 0.15, angleHour - Math.PI);
  99.                 this.ctx.restore();
  100.  
  101.                 this.ctx.save();
  102.                 this.ctx.lineWidth = 2;
  103.                 this.ctx.strokeStyle = 'red';
  104.                 this._drawRay(radiusSec, lengthSec, angleSec);
  105.                 this._drawRay(radiusSec * 0.15, radiusSec * 0.15, angleSec - Math.PI);
  106.                 this.ctx.restore();
  107.  
  108.                 this.ctx.restore();
  109.                 requestAnimationFrame(() => { this.draw() });
  110.             };
  111.         }
  112.  
  113.         let canvas = document.getElementById('draw');
  114.         canvas.height = 600;
  115.         canvas.width = 600;
  116.         let ctx = canvas.getContext('2d');
  117.  
  118.         let img = new Image();
  119.         img.src = 'https://cdn2.hubspot.net/hubfs/4783129/fish-1.jpg';
  120.         img.addEventListener('load', () => {
  121.             ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
  122.             ctx.save();
  123.             ctx.font = '48px serif';
  124.             ctx.fillStyle = 'white';
  125.             //let text = ctx.measureText('Clock Example');
  126.             //ctx.fillText('Clock Example', canvas.width / 2 - text.width / 2, 500);
  127.             ctx.textAlign = 'center'
  128.             ctx.fillText('Clock Example', canvas.width / 2, 500);
  129.             ctx.restore();
  130.         });
  131.  
  132.  
  133.         document.addEventListener('DOMContentLoaded', () => {
  134.             let clocks = [
  135.                 new Clock(canvas.width / 4, canvas.height / 4, canvas.height / 8 * 1.2, ctx, 0, 'Uzhgorod'),
  136.                 new Clock(canvas.width / 2, canvas.height / 2, canvas.height / 8 * 1.2, ctx, 3600, 'Moscow'),
  137.                 new Clock(canvas.width * 0.75, canvas.height / 4, canvas.height / 8 * 1.2, ctx, -18000, 'New-York')
  138.             ];
  139.  
  140.             function animate() {
  141.                 for (clock of clocks) {
  142.                     clock.draw();
  143.                 }
  144.                 //requestAnimationFrame(animate);
  145.             };
  146.  
  147.             animate();
  148.         });
  149.  
  150.  
  151.     </script>
  152. </body>
  153. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement