Write your own ctx.* commands, run them line by line on a real canvas, and see the state the context holds: brush colours, transform, the path not painted…
Why it matters
Canvas powers charts, games, image editors and visualisations. Two beginner puzzles, "why is everything one colour" and "why can I not move my shape", go away once you see the context as a state machine painting pixels, line by line.
The key idea
The context is a state machine, and drawing turns straight into pixels
Common mistakes
Every shape comes out one colour, though their fillStyles differfillStyle sticks to every draw after it. Set it again right before each shape, or wrap the part that changes state in save()/restore(). A mistyped colour is ignored silently and the old brush stays in use.
You want to move a drawn shape, but the canvas has no reference to it There is none: a canvas only keeps pixels. Keep the data yourself (x, y, colour), then every frame clearRect() and draw everything again in its new place.
A new shape joins the old path, so it gets filled toofill() and stroke() do not empty the path. Start every new shape with beginPath(), or the next fill() repaints every shape since the last beginPath().
Try it in this playground
Step back to just before ctx.fill() The dashed violet line is the path built but not painted. Not a single pixel yet: arc() only draws a plan.
Read a pixel where two shapes overlapgetImageData only gives that pixel’s colour. There is no answer "this is a box": a canvas remembers no shapes.
Delete the line ctx.fillStyle = '#38bdf8' The box comes out orange too: it uses the brush the context still holds from the circle.
Add ctx.rotate(Math.PI / 8) before the box’s fillRect The red and green axes turn around the top left corner, and everything drawn after them tilts too.