Skip to content

Clock Start

This section explains how to animate the analog clock we’ve been building using HTML Canvas.

To make the clock hands move and update in real-time, we need to repeatedly call our main drawing function (drawClock) at regular intervals. The setInterval() method is suitable for this task, especially for a clock that updates every second.

Here’s how you integrate setInterval():

// Assuming 'canvas', 'ctx', and 'radius' are already defined and initialized
// Also assuming drawClock(), drawFace(), drawNumbers(), and drawTime() functions exist.
// const canvas = document.getElementById('myClockCanvas');
// const ctx = canvas.getContext('2d');
// let radius = canvas.height / 2;
// ctx.translate(radius, radius); // Move origin to canvas center
// radius = radius * 0.90; // Actual clock radius
// Main drawing function that orchestrates all parts of the clock
// function drawClock() {
// drawFace(ctx, radius); // Draw the clock face
// drawNumbers(ctx, radius); // Draw the hour numbers
// drawTime(ctx, radius); // Draw the hands based on current time
// }
// Instead of calling drawClock() once:
// drawClock();
// Call drawClock every 1000 milliseconds (1 second)
setInterval(drawClock, 1000);
// --- Full conceptual setup ---
// (You would have these functions defined elsewhere in your script)
// function drawFace(ctx, radius) { /* ... draws clock face ... */ }
// function drawNumbers(ctx, radius) { /* ... draws numbers ... */ }
// function drawTime(ctx, radius) { /* ... gets current time and draws hands ... */ }
// function setupClock() {
// const canvas = document.getElementById('myClockCanvas');
// const ctx = canvas.getContext('2d');
// let radius = canvas.height / 2;
// ctx.translate(radius, radius);
// radius = radius * 0.90;
// function drawClock() {
// // It's important to clear and redraw the hands,
// // but the face and numbers might only need to be drawn once
// // if they don't change. For simplicity, this example redraws all.
// // A more optimized approach would clear only the area of the hands.
// // Or, clear the whole clock area before redrawing face, numbers, and time for simplicity:
// // ctx.clearRect(-radius, -radius, radius * 2, radius * 2);
// drawFace(ctx, radius);
// drawNumbers(ctx, radius);
// drawTime(ctx, radius);
// }
// setInterval(drawClock, 1000);
// }
// document.addEventListener('DOMContentLoaded', setupClock);

The core change to animate the clock is replacing a single call to drawClock(); with setInterval(drawClock, 1000);.

  • setInterval(callbackFunction, delayInMilliseconds): This browser API repeatedly calls callbackFunction (in our case, drawClock) every delayInMilliseconds.
  • 1000: This value means the drawClock function will be executed every 1000 milliseconds, which is equal to 1 second. This is appropriate for an analog clock where the second hand moves each second.
  • Clearing the Canvas: In each drawClock call, especially before drawing moving elements like hands, you usually need to clear the previous frame. Otherwise, new drawings will appear on top of old ones, creating a messy trail. You can clear the entire canvas ctx.clearRect(0, 0, canvas.width, canvas.height); or, if you’ve translated the context, ctx.clearRect(-radius, -radius, radius*2, radius*2);. A more optimized approach is to clear only the parts of the canvas that change (e.g., the area where the hands were). For simplicity in the clock example, often the entire clock face area is redrawn.
  • Performance: For complex animations, requestAnimationFrame() is generally preferred over setInterval(). requestAnimationFrame() synchronizes with the browser’s repaint cycle, leading to smoother animations and better performance, especially on devices with different refresh rates. However, for a simple clock updating once per second, setInterval() is adequate and easier for beginners to understand.
  • Resource Management: If the clock is removed from the page or no longer needed, you should clear the interval using clearInterval() to prevent unnecessary processing. Store the ID returned by setInterval(): const intervalID = setInterval(drawClock, 1000); and then call clearInterval(intervalID); when needed.

The drawClock function itself (which calls drawFace, drawNumbers, and drawTime) will now be executed every second, ensuring that drawTime fetches the current time and redraws the hands in their new positions.