Chapter 34: Canvas Drawing

Canvas Drawing in a very clear, step-by-step, human-teacher way.

Imagine I’m sitting next to you with my laptop open and we’re building everything together from the smallest piece to more interesting things. No magic, no skipping steps, lots of tiny complete examples you can copy-paste right now.

1. Canvas Drawing – the most honest mental model

When you draw on Canvas you are not creating objects that live forever (like in SVG). You are painting pixels on a bitmap screen.

Every time you want something to change (move, grow, disappear, change color), you basically have to erase the old picture and paint a new one.

That is why almost every Canvas program has this repeating pattern:

text

This loop usually runs ~60 times per second (thanks to requestAnimationFrame).

2. The absolute smallest drawing example (copy → run)

HTML

What we just learned in plain English:

  • getContext(‘2d’) → gives you the paintbrush object (99.9% of Canvas code uses ‘2d’)
  • fillStyle / strokeStyle → current paint color (stays until you change it)
  • fillRect(x, y, width, height) → paint a filled rectangle
  • strokeRect(x, y, width, height) → paint only the outline

Coordinates: (0,0) is top-left, y grows downward.

3. The second most important concept: Paths

Almost everything interesting (circles, curves, polygons, stars, arrows…) is built with paths.

Basic path recipe (you will write this pattern thousands of times):

JavaScript

4. Drawing a circle / arc (the most common shape after rectangles)

JavaScript

Quick angle cheat-sheet (radians)

  • 0 → right (3 o’clock)
  • Math.PI/2 → down (6 o’clock)
  • Math.PI → left (9 o’clock)
  • Math.PI*2 → full circle

5. Drawing text

JavaScript

6. Quick summary table – 80/20 most used drawing commands

Goal Code example
Filled rectangle ctx.fillRect(x, y, w, h)
Outlined rectangle ctx.strokeRect(x, y, w, h)
Circle / arc ctx.arc(cx, cy, r, startAngle, endAngle)
Line ctx.moveTo(x1,y1); ctx.lineTo(x2,y2); ctx.stroke()
Change color ctx.fillStyle = ‘#FF5722’ or ‘rgba(255,87,34,0.7)’
Line thickness & cap style ctx.lineWidth = 10; ctx.lineCap = ’round’
Save / restore state ctx.save(); … ctx.restore();
Clear whole canvas ctx.clearRect(0, 0, canvas.width, canvas.height)

Your first three tiny practice tasks (10–15 minutes each)

Task 1 – Smiley face Yellow circle + two black eyes + red smiling mouth (arc)

Task 2 – House Rectangle base + triangle roof + rectangle door + circle window

Task 3 – Traffic light Three stacked circles (red, yellow, green) with black outlines

Copy any of them into a file, run it, then come back and paste your code here if you want feedback. I’ll review it exactly like we’re pair-programming together.

Which part feels most confusing or strange right now?

  • The fact that Canvas is “stateful” and remembers colors/line width?
  • Why we need beginPath()?
  • Angles in radians for arcs?
  • Difference between fill and stroke?
  • Something else?

Tell me honestly — we’ll slow down and fix exactly the piece that’s fuzzy. Canvas feels weird at first, but once it clicks it becomes very logical and powerful.

You’re doing great — let’s keep going! 🚀

You may also like...

Leave a Reply

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