javascript canvas Implementation Simple Clock Example

  • 2021-08-10 06:46:01
  • OfStack

In this paper, we share the specific code of javascript canvas to realize simple clock for your reference. The specific contents are as follows


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title> Clocks and watches </title>
  <script type="text/javascript">
  window.onload=function(){
    //1. Get canvas 
    var canvas=document.getElementById("canvas");
    //2. Get the context object (get the brush) 
    var cx=canvas.getContext("2d");
    //3. Set the brush style 
    function clock(){
      cx.fillStyle="tomato";
    //4. Draw a Graphic 
      // Draw the dial 
      cx.beginPath(); // Draw a circle 
      cx.arc(300,300,200,0,Math.PI*2);// Set circle  arc ( x,y,r,begin,end,c ) ;x,y: Center coordinates of a circle of a circle  r Radius of a circle  begin , end Start angle and end angle; Math.PI=180 Math.PI/180=1 Degrees  c Draw counterclockwise true According to inverse time  false Clockwise 
      cx.closePath();// Close the path 
      cx.fill();//
      // Drawing time degree 
        cx.lineWidth=2;
        cx.strokeStyle="black";
        for(var i=0;i<12;i++){
          cx.save();
          cx.translate(300,300);// Shape shift 
          cx.rotate(i*(Math.PI/6));
          
          cx.beginPath();
          cx.moveTo(0,-180);
          cx.lineTo(0,-200);
          cx.stroke();
          cx.closePath();

          cx.fillStyle="black";// Draw numbers 
          cx.font="16px blod";
          cx.rotate(Math.PI/6);
          cx.fillText(i+1,-6,-220);// Text 

          cx.restore();
        }

      // Draw sub-scale 
      for(var i=0;i<60;i++){
        cx.save();
        cx.translate(300,300);
        cx.rotate(i*(Math.PI/30));

        cx.beginPath();
        cx.moveTo(0,-190);
        cx.lineTo(0,-200);
        cx.stroke();
        cx.closePath();

        cx.restore();
      }
      
      // Get the current time 
      var today=new Date();
      var hour=today.getHours();
      var min=today.getMinutes();
      var sec=today.getSeconds();

      hour=hour+min/60;
      
      // Draw an hour hand 
      cx.lineWidth=4;
      cx.save();
      cx.translate(300,300);
      cx.beginPath();
      cx.rotate(hour*(Math.PI/6));// Rotate 
      cx.moveTo(0,10);
      cx.lineTo(0,-130);
      cx.closePath();
      cx.stroke();
      cx.restore();   // Rollback 

      // Draw the minute hand 
      cx.lineWidth=2;
      cx.save();
      cx.translate(300,300);
      cx.beginPath();
      cx.rotate(min*(Math.PI/30));
      cx.moveTo(0,10);
      cx.lineTo(0,-160);
      cx.closePath();
      cx.stroke();
      cx.restore();   // Rollback 

      // Draw the second hand 
      cx.lineWidth=1;
      cx.strokeStyle="black";
      cx.save();
      cx.translate(300,300);
      cx.beginPath();
      cx.rotate(sec*(Math.PI/30));
      cx.moveTo(0,10);
      cx.lineTo(0,-160);
      cx.closePath();
      cx.stroke();
      cx.restore();  

      // Draw an intersection 
      cx.fillStyle='#ccc';
      cx.strokeStyle="red";
      cx.save();
      cx.translate(300,300);
      cx.beginPath();
      cx.beginPath();
      cx.arc(0,0,4,0,Math.PI*2);
      cx.closePath();
      cx.fill();
      cx.closePath();
      cx.stroke();
      cx.restore();
      
      setTimeout(clock,1000);
    }
   
    // setInterval(clock,1000);
    clock();
  }
  </script>
</head>
<body>
  <canvas id="canvas" width="1300px" height="600px" style="background-color: #ccc;"></canvas>
</body>
</html>

Related articles: