Using H5api to Realize Clock Drawing of javascript

  • 2021-08-21 19:32:29
  • OfStack

The canvas tag of HTML5 is used to draw images (through scripts, usually JavaScript). However, the canvas element itself does not have the ability to draw (it is merely a container for graphics) and must use scripts to do the actual drawing task.

Below, the steps of using canvas canvas under 1 are summarized in detail:

Canvas:

canvas

Plan a space on the page, canvas label, and control the canvas through javascript to complete the drawing

1. Get the canvas


var canvas=document.getElementById("");

2. Get the context object (get the brush)


var cx=canvas.getContext( " 2d " );

3. Set the brush style


cx.fillStyle= ' red';
cx.strokeStyle= ' blue';

Step 4 Start drawing

The following is a small example of drawing a simple clock for canvas:


<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>Document</title>
 <script>
 window.onload=function(){
  //1. Get canvas 
  var canvas=document.getElementById("canvas");
  //2. Get a brush 
  var cx=canvas.getContext("2d");
  
  function clock(){
  //3. Set the brush style 
  cx.fillStyle="orange";
  //4. Draw a Graphic 
  // Draw the dial 
  cx.beginPath();
  cx.arc(300,300,200,0,Math.PI*2)
  cx.closePath();
  cx.fill();
  // Drawing time degree 
  cx.lineWidth=2;
  cx.strokeStyle="black";
  for(var i=0;i<12;i++){
   cx.save();
   cx.translate(300,300);
   cx.rotate(i*(Math.PI/6));
   // cx.beginPath();
   cx.moveTo(0,-180);
   cx.lineTo(0,-200);
   // cx.closePath();
   cx.stroke();
   cx.fillStyle="black";
   cx.font="16px blod";
   cx.rotate(Math.PI/6)
   cx.fillText(i+1,-5,-220);
   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.closePath();
   cx.stroke();
   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=5;
  cx.save();
  cx.beginPath();
  cx.translate(300,300);
  cx.rotate(hour*(Math.PI/6));
  cx.moveTo(0,10);
  cx.lineTo(0,-100);
  cx.closePath();
  cx.stroke();
  cx.restore();
  // Draw the minute hand 
  cx.lineWidth=3;
  cx.save();
  cx.beginPath();
  cx.translate(300,300);
  cx.rotate(min*(Math.PI/30));
  cx.moveTo(0,10);
  cx.lineTo(0,-120);
  cx.closePath();
  cx.stroke();
  cx.restore();
  // Draw the second hand 
  cx.lineWidth=1;
  cx.strokeStyle="red";
  cx.save();
  cx.beginPath();
  cx.translate(300,300);
  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.arc(0,0,5,0,Math.PI*2);
  cx.closePath();
  cx.fill();
  cx.stroke();
  cx.restore();
   setTimeout(clock,1000);
  }
  clock()
 }
 </script>
</head>
<body>
 <canvas id="canvas" width="600px" height="600px" style="background-color: #ccc;"></canvas>
</body>
</html>

Related articles: