Chapter 54: Canvas Clock

What is a “Canvas Clock”?

A Canvas Clock means using the HTML <canvas> element + JavaScript to draw a classic analog clock (round face, hour/minute/second hands, numbers, ticks) that updates in real time every second, just like a real wall clock.

Why do we build this?

  • It is one of the most asked Canvas projects in college (BCA/B.Tech/MCA), interviews, and portfolio work.
  • It teaches almost everything important in Canvas: shapes (circle, lines, arcs), text, transformations (rotate), time/date handling, animation loop (requestAnimationFrame), clearing & redrawing, math (angles, trigonometry), shadows, gradients, etc.
  • Once you finish this, you can easily make digital clocks, countdown timers, progress clocks, world clocks, etc.

We will build it step by step, from empty canvas → final working clock.

Step 1 – Basic Canvas Setup (Copy-Paste & Run)

Create a file called canvas-clock.html and paste this:

HTML

What you see so far:

  • Nice round white clock face with thick dark border + thin inner border
  • Centered on page with shadow

This is our base clock face — we will add numbers, ticks, hands, and animation on top.

Step 2 – Draw Clock Numbers (12, 3, 6, 9, etc.)

Add this function below drawClockFace() and call it after:

JavaScript

Explanation:

  • 12 numbers × 30° = 360° (full circle)
  • -90 shifts so 12 is at top (0° is right in math, but clocks start at top)
  • radius – 40 places numbers inside the outer rim

Refresh → you now see numbers 1 to 12!

Step 3 – Draw Minute & Hour Ticks

Add this function:

JavaScript

Refresh → now you have 60 minute ticks (longer & thicker every 5 minutes for hours)

Step 4 – Draw Clock Hands (Hour, Minute, Second)

Add this function:

JavaScript

Step 5 – Make it Live (Animation Loop)

Replace the initial drawing calls at the bottom with this:

JavaScript

Refresh → now you have a live, ticking analog clock!

6. Teacher’s Quick Tips (Hyderabad Student Style 😄)

  • Angles: -90 * Math.PI / 180 to start at 12 o’clock (math 0° is right, clock 0 is top)
  • Hands length: hour shortest, minute longer, second longest
  • clearRect() every frame — otherwise old hands stay visible
  • Common mistake #1: Forget clearRect() → hands leave trails
  • Common mistake #2: Wrong angle calculation → hands point wrong direction
  • Pro tip: Add smooth second hand motion with requestAnimationFrame instead of setInterval for silky animation
  • Pro tip 2: Use Date.now() instead of new Date() for sub-second precision

Understood how to build a Canvas Clock fully now? This project teaches almost everything important in Canvas: shapes, arcs, text, transformations (rotate), animation loop, time handling, math (trigonometry), clearing & redrawing.

Tell me what you want next — we can build on this:

  • Add digital time display below?
  • Make second hand smooth (requestAnimationFrame)?
  • Add date & day of week?
  • World clock (multiple time zones)?
  • Animated clock with ticking sound?
  • Or 15-question Canvas clock quiz?

Just say — we can make it even better together! 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *