Chapter 37: Canvas Fill and Stroke

Canvas Fill, Stroke, and Lines in a very clear, patient, step-by-step way.

Imagine I’m right next to you with the code editor open. We’ll build understanding slowly, with tiny complete examples you can copy-paste immediately. No rushing, no magic — just plain English explanations, visual thinking, and repeated patterns until everything feels natural.

Three most important mental models before we start coding

  1. Canvas is a painter, not a scene graph You don’t create “a line object” or “a filled circle object” that lives forever (like in SVG). You tell the painter: “use this color, this thickness… now paint pixels right now”. After fill() or stroke() the pixels are just pixels — there is no editable object anymore.
  2. Fill = inside the shapeStroke = the outline / border of the shape
  3. Every interesting shape in Canvas is a path Even a simple rectangle or circle is internally a path. You define the path first → then decide whether to fill() it, stroke() it, or both.

1. The absolute smallest Fill + Stroke example

Copy-paste and run this — it shows everything in one tiny picture.

HTML

What you should see & remember forever:

  • Green rectangle → filled only (fillRect = fill + auto path)
  • Red rectangle → outlined only (strokeRect = stroke + auto path)
  • Blue circle → filled AND outlined (we defined path manually → fill() → stroke())

Important order rule Usually do fill() before stroke() — because stroke draws on top of fill.

2. Lines – the foundation of almost everything

Lines are the simplest path — but they teach you the pattern you’ll use for every shape.

Four-step recipe (you will type this 1000 times):

JavaScript

3. Full line properties example (copy-paste)

HTML

Key lessons from this example:

  • lineCap = how line ends look
    • ‘butt’ → flat cut (default)
    • ’round’ → half-circle (best for dots)
    • ‘square’ → square cap
  • setLineDash([dashLength, gapLength]) → creates dashed/dotted lines Common patterns:
    • [10, 5] → classic dashed
    • [2, 8] → dotted
    • [20, 10, 5, 10] → dash-dot pattern
  • Always reset with ctx.setLineDash([]) after using dashes — otherwise next lines become dashed too!

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

Task 1 – Four lines showcase Draw four horizontal lines across the canvas:

  • thin solid black at y=80
  • thick blue with round caps at y=160
  • medium green dashed at y=240
  • orange dotted with round caps at y=320

Task 2 – Coordinate axes Draw a gray cross through the center of the canvas

  • label the center “(300, 200)” with text

Task 3 – Zigzag arrow Start at left side → zigzag 5–6 times → end with an arrowhead (Use thick line + round caps + lineJoin=’round’)

Paste any of them here when you finish — I’ll review it like we’re pair-programming together.

Which part still feels a little confusing?

  • Why do we need beginPath() every single time?
  • Difference between stroke() and fill()?
  • How lineCap actually changes the look?
  • Making nice dashed/dotted lines?
  • Something else?

Tell me — we’ll stay on fill/stroke/lines until you feel super confident drawing anything with them.

You’re doing really well — this is the foundation of 90% of Canvas drawings! 🚀

You may also like...

Leave a Reply

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