Chapter 55: Clock Intro

A clock with Canvas from the very beginning.

I’m going to teach this like your favorite patient teacher who wants you to really understand — not just copy-paste and move on. We’ll go slowly, explain why every line exists, show you the thinking process, and build a beautiful, live, analog clock together — step by step.

By the end of this lesson you’ll have:

  • A complete, elegant analog clock that shows real time
  • Clear understanding of how Canvas animation, rotation, arcs, text, and time math work together
  • Code you can immediately run, modify, and be proud of

Canvas Clock – Complete Introduction & Build-Along Tutorial

Step 0: Why build a clock in Canvas?

A clock is the perfect first real project because it forces you to combine almost everything you’ve learned:

  • Drawing circles & arcs (face, hands)
  • Rotating things around a center point
  • Updating every second (animation/time loop)
  • Text positioning & alignment (numbers)
  • Math with angles (hours → degrees)
  • Keeping state clean (save/restore)
  • Redrawing only what changes (hands) while keeping static parts

And the result looks impressive and useful — something you can actually show people.

Step 1: Basic HTML + Canvas structure

HTML

Why this structure?

  • Square canvas (420×420) → perfect for round clock
  • Dark theme + thick border + shadow → looks premium & modern
  • border-radius: 50% → makes square canvas look circular

Step 2: Get context & define helpers

JavaScript

Step 3: Draw the static clock face (only once!)

We draw numbers, ticks, and background circle once — not every second.

JavaScript

Step 4: Draw moving hands (updated every second)

JavaScript

Step 5: Make it live (update every second)

JavaScript

Full final code (everything together)

HTML

Summary – What we learned / reused

  • arc() for face & hands
  • save() / restore() + translate + rotate for correct hand rotation
  • Static face drawn once → hands redrawn every second (efficient)
  • lineCap = ’round’ for smooth hand ends
  • Real-time Date object + angle math
  • Nice styling (gradients, shadows, numbers)

Your mini homework (try tonight)

  1. Add small second ticks (thin lines every 6°)
  2. Make 12, 3, 6, 9 numbers bigger & bolder
  3. Show current date below the clock (use fillText with new Date().toLocaleDateString())

Paste your modified version here if you want feedback — I’ll review it like we’re sitting together improving it.

Any part confusing?

  • Rotating hands around center?
  • Why we don’t clear the whole canvas every second?
  • Angle math (why -Math.PI/2)?
  • 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 something to be proud of! 🚀

You may also like...

Leave a Reply

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