Chapter 54: Canvas Clock

A live analog clock using Canvas.

I’m going to teach this the way a patient, enthusiastic teacher would — slowly, clearly, step by step, explaining why we do every single thing, with complete runnable code, visual thinking, and comments so you understand every line.

We will build a classic analog clock that:

  • Shows hour, minute, second hands
  • Updates every second
  • Looks nice (with shadows, gradients, numbers)
  • Is fully self-contained in one HTML file

Canvas Analog Clock – Complete Step-by-Step Tutorial

Step 1: Basic HTML + Canvas setup

HTML

Why this setup?

  • Square canvas (400×400) → perfect for round clock
  • Dark background + shadow → clock looks like it’s floating
  • border-radius: 50% → makes canvas look circular even though it’s square

Step 2: Get context & helper functions

Add this inside <script>:

JavaScript

Step 3: Draw the clock face (once – not in loop)

We only need to draw numbers, ticks, and background circle once (or when resizing), not every second.

JavaScript

Step 4: Draw hands (updated every second)

JavaScript

Step 5: Animation loop (update every second)

JavaScript

Full final code (put everything together)

HTML

Summary – What we learned / reused in this project

  • Full canvas setup with centered clock
  • arc() for face & hands
  • save() / restore() + translate + rotate for hand rotation around center
  • strokeStyle, lineWidth, lineCap for nice hand look
  • setInterval for time update (simple & reliable)
  • Static face drawn once → hands redrawn every second (efficient)

Your mini homework (try tonight)

  1. Add small second ticks (thin lines every 6°) around the clock face
  2. Make the hour numbers bigger & bolder for 12, 3, 6, 9
  3. Add a date display below the clock using fillText (e.g. “March 03, 2026”)

Paste your modified code here if you want feedback — I’ll review it like we’re pair-programming together 😄

Any part confusing?

  • Rotating hands around center?
  • Why we don’t clear the whole canvas every second?
  • Angles in radians?
  • Something else?

Tell me — we’ll zoom in until the clock feels perfect to you.

You just built a real, live analog clock in pure Canvas — that’s huge! 🚀

You may also like...

Leave a Reply

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