Chapter 37: Canvas Fill and Stroke

Canvas Fill and Stroke in the most detailed, slow, step-by-step, human-teacher way possible. 😊

I’m going to explain everything like I’m sitting next to you in a Hyderabad computer lab β€” no rushing, full classroom style, simple everyday English, lots of copy-paste examples you can run right now, and all the little tips & common mistakes students always make.

1. What are Fill and Stroke actually? (Clear definition first)

In Canvas (the <canvas> element + JavaScript drawing), fill and stroke are the two main ways you color what you draw.

Think of it like real painting:

  • Fill = the inside color / paint bucket / solid color that fills the shape completely (like filling a circle with red paint)
  • Stroke = the border / outline / pen stroke / line around the shape (like drawing the edge of the circle with a black marker)

Key rules everyone must remember from day one:

  • Fill colors the area inside a closed shape (rectangle, circle, path, polygon…)
  • Stroke colors the edge / line (whether the shape is closed or open)
  • You can use both at the same time on the same shape
  • You can use only fill (no border)
  • You can use only stroke (no inside color β€” very common for lines, borders, outlines)
  • If you forget both β†’ shape is invisible!

2. Your First Fill & Stroke Example – Side-by-Side Comparison (Copy-Paste & Run)

Create canvas-fill-stroke.html:

HTML

What you see:

  • Four shapes side by side
    1. Solid blue rectangle (only fill)
    1. Red border rectangle (only stroke)
    1. Green rounded rectangle with border (both fill + stroke)
    1. Purple circle with thick border (semi-transparent fill + stroke)

3. All Important Fill & Stroke Properties (With Examples)

A. fillStyle & strokeStyle (Color)

JavaScript

B. lineWidth (Stroke thickness)

JavaScript

C. lineCap (How line ends look)

JavaScript

D. lineJoin (How corners look)

JavaScript

E. Global transparency (affects both fill & stroke)

JavaScript

Better way β€” use fill-opacity style (not global):

JavaScript

4. Teacher’s Quick Tips (Hyderabad Student Style πŸ˜„)

  • Always set fillStyle or strokeStylebefore drawing β€” they don’t apply retroactively
  • If you want no fill β†’ fillStyle = ‘transparent’ or skip fill()
  • If you want no stroke β†’ skip stroke() or set strokeStyle = ‘transparent’
  • Common mistake #1: Draw shape but forget fill() or stroke() β†’ invisible!
  • Common mistake #2: Use fillStyle for stroke or vice versa β†’ wrong color
  • Pro tip: Use ctx.save() / ctx.restore() when changing styles temporarily
JavaScript

Understood Canvas Fill and Stroke fully now? These two properties control almost every color decision in canvas drawing β€” master them and you’re 50% there.

Tell me what you want next β€” we can build on this:

  • Full paint app (click to fill, drag to stroke)?
  • Animated color-changing shapes?
  • Gradient fill + stroke combo?
  • Dashed stroke patterns?
  • Or 15-question Fill & Stroke quiz?

Just say β€” we keep going step by step together! πŸš€

You may also like...

Leave a Reply

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