I have to make an analog clock in canvas. Wrote some code and I need help with how to adjust speed of the hands (hours, mins and seconds). When I start the app hands are just spinning around really fast. I am ending up with hands line allover the clock. Is there any way to remove those lines?
I tried adjusting the rotation but that didn't help. I just started learning about canvas and am really not sure how to fix this problem.
window.onload = draw;
function draw() {
let myCanvas = document.getElementById("my-canvas");
if (myCanvas.getContext) {
let ctx = myCanvas.getContext('2d');
var img = new Image();
img.addEventListener('load', function() {
ctx.drawImage(img, 0, 0, 400, 400);
}, false);
img.src = 'image.png';
update(ctx);
} else {
alert("Canvas is not supported.");
}
}
let angle = 0;
function update(ctx) {
ctx.save();
ctx.translate(200, 200);
ctx.rotate((Math.PI / 180) * angle);
ctx.translate(-200, -200);
ctx.beginPath();
ctx.moveTo(200, 200);
ctx.lineTo(200, 150);
ctx.closePath();
ctx.stroke();
ctx.beginPath();
ctx.moveTo(200, 200);
ctx.lineTo(200, 100);
ctx.closePath();
ctx.stroke();
ctx.beginPath();
ctx.moveTo(200, 150);
ctx.lineTo(200, 30);
ctx.closePath();
ctx.stroke();
ctx.restore();
angle++;
requestAnimationFrame(function() {
update(ctx);
});
}
#my-canvas {
border: #cacaca 1px solid;
}
<canvas id="my-canvas" width="400" height="400">
Your web browser does not support canvas element.
</canvas>