This article has shared the specific code for using canvas to draw the clock for your reference. The specific content is as follows
1. clock.html
<!doctype html><html lang="en"> <head> <meta charset="UTF-8"> <meta name="Description" content=""> <title>canvas clock</title> </head> <body> <canvas id="canvas"></canvas> <script> var canvas=document.getElementById("canvas"); var context=canvas.getContext('2d'); //draw(); function draw(){ //Get the time, minute and second of the current system var now=new Date(); var sec=now.getSeconds(); var min=now.getMinutes(); var hour=now.getHours(); hour>=12&&(hour=hour-12); var radius = Math.min(canvas.width/2,canvas.height/2); //Initialize canvas context.save(); context.clearRect(0,0,canvas.width,canvas.height); context.translate(canvas.width/2,canvas.height/2); context.rotate(-Math.PI/2); context.save(); //Table box//Hour scale context.strokeStyle="black"; context.fillStyle="black"; context.lineWidth=3; context.lineCap="round"; context.beginPath(); for(var i=0;i<12;i++){ context.rotate(Math.PI/6); context.moveTo(radius-30,0); context.lineTo(radius-10,0); } context.stroke(); context.restore(); context.save(); //Minute scale context.lineWidth=2; context.beginPath(); for(var i=0;i<60;i++){ if(!i%5==0){ context.moveTo(radius-15,0); context.lineTo(radius-10,0); } context.rotate(Math.PI/30); } context.stroke(); context.restore(); context.save(); //Draw the hour hand context.rotate((Math.PI/6)*hour+(Math.PI/360)*min+(Math.PI/21600)*sec); context.lineWidth=6; context.beginPath(); context.moveTo(-10,0); context.lineTo(radius*0.5,0); context.stroke(); context.restore(); context.save(); context.rotate((Math.PI/30)*min+(Math.PI/1800)*sec); context.strokeStyle="#29A8DE"; context.lineWidth=4; context.lineCap="butt"; context.beginPath(); context.moveTo(-20,0); context.lineTo(radius*0.7,0); context.stroke(); context.restore(); context.save(); context.rotate(sec*Math.PI/30); context.strokeStyle="red"; context.lineWidth=2; context.lineCap="butt"; context.beginPath(); context.moveTo(-30,0); context.lineTo(radius*0.9,0); context.stroke(); context.restore(); context.save(); context.lineWidth=4; context.strokeStyle="gray"; context.beginPath(); context.arc(0,0,radius,0,Math.PI*2,true); context.stroke(); context.restore(); context.restore(); context.restore(); } window.onload=function(){ setInterval(draw,1000) } </script> </body></html>2. JavaScript code
<script> var canvas=document.getElementById("canvas"); var context=canvas.getContext('2d'); //draw(); function draw(){ //Get the time and minutes of the current system var now=new Date(); var sec=now.getSeconds(); var min=now.getMinutes(); var hour=now.getHours(); hour>=12&&(hour=hour-12); var radius = Math.min(canvas.width/2,canvas.height/2); //Initialize the canvas context.save(); context.clearRect(0,0,canvas.width,canvas.height); context.translate(canvas.width/2,canvas.height/2); context.rotate(-Math.PI/2); context.save(); //Table box//Hour scale context.strokeStyle="black"; context.fillStyle="black"; context.lineWidth=3; context.lineCap="round"; context.beginPath(); for(var i=0;i<12;i++){ context.rotate(Math.PI/6); context.moveTo(radius-30,0); context.lineTo(radius-10,0); } context.stroke(); context.restore(); context.save(); //Minute scale context.lineWidth=2; context.beginPath(); for(var i=0;i<60;i++){ if(!i%5==0){ context.moveTo(radius-15,0); context.lineTo(radius-10,0); } context.rotate(Math.PI/30); } context.stroke(); context.restore(); context.save(); //Draw the hour hand context.rotate((Math.PI/6)*hour+(Math.PI/360)*min+(Math.PI/21600)*sec); context.lineWidth=6; context.beginPath(); context.moveTo(-10,0); context.lineTo(radius*0.5,0); context.stroke(); context.restore(); context.save(); context.rotate((Math.PI/30)*min+(Math.PI/1800)*sec); context.strokeStyle="#29A8DE"; context.lineWidth=4; context.lineCap="butt"; context.beginPath(); context.moveTo(-20,0); context.lineTo(radius*0.7,0); context.stroke(); context.restore(); context.save(); context.rotate(sec*Math.PI/30); context.strokeStyle="red"; context.lineWidth=2; context.lineCap="butt"; context.beginPath(); context.moveTo(-30,0); context.lineTo(radius*0.9,0); context.stroke(); context.restore(); context.save(); context.lineWidth=4; context.strokeStyle="gray"; context.beginPath(); context.arc(0,0,radius,0,Math.PI*2,true); context.stroke(); context.restore(); context.restore(); context.restore(); } window.onload=function(){ setInterval(draw,1000) } </script>The above is all about this article, I hope it will be helpful for everyone to learn JavaScript programming.