Chapter 38: Canvas Shapes

Canvas Shapes in the clearest, most patient, step-by-step way possible.

Imagine I’m sitting right next to you. We’re going to build every single example together — slowly, with tiny complete code snippets you can copy-paste and run immediately. No skipping steps, no magic, just plain English explanations, visual thinking, common mistakes highlighted, and repeated patterns until everything feels natural and confident.

Canvas Shapes – Complete Beginner-to-Confident Guide

1. The single most important mental model about shapes in Canvas

In Canvas, a “shape” is not a permanent object like in SVG (where you can later say “move this rectangle” or “change its color”).

A shape in Canvas is just a path you describe once, then you tell the painter:

  • “fill it” → paint the inside pixels
  • “stroke it” → paint the outline pixels
  • or both

After fill() or stroke() the pixels are done — there is no shape object you can edit later. If you want to move it, change color, or resize it → you have to erase the old pixels and draw a new shape at the new position/color/size.

That’s why almost every Canvas program redraws everything every frame (animation loop).

2. The universal 4-step recipe for any shape in Canvas

You will type this pattern thousands of times — memorize it:

JavaScript

Important rule: beginPath() is almost always needed before a new shape — otherwise the new shape connects to the previous unfinished path.

3. All basic Canvas shapes – one complete example

Copy-paste and run this — it shows every common shape in one canvas with labels.

HTML

What you should see & remember forever:

  • Rectangle → easiest (built-in rect() / fillRect / strokeRect)
  • Rounded rectangle → manual path with arc() + lineTo()
  • Circle → arc(cx, cy, r, 0, Math.PI*2)
  • Ellipse → ellipse(cx, cy, rx, ry, rotation, start, end)
  • Line / polyline → moveTo + multiple lineTo

4. Quick reference – All common shape commands

Shape / Goal Code pattern (after beginPath())
Rectangle ctx.rect(x, y, w, h) or ctx.fillRect(x,y,w,h)
Rounded rectangle ctx.roundRect(x, y, w, h, radius) (modern browsers 2024+)
Circle ctx.arc(cx, cy, r, 0, Math.PI*2)
Ellipse ctx.ellipse(cx, cy, rx, ry, 0, 0, Math.PI*2)
Simple line ctx.moveTo(x1,y1); ctx.lineTo(x2,y2);
Polyline / polygon ctx.moveTo(…); ctx.lineTo(…); … ctx.closePath();
Quadratic curve ctx.quadraticCurveTo(controlX, controlY, endX, endY)
Cubic Bézier curve ctx.bezierCurveTo(c1x,c1y, c2x,c2y, endX,endY)

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

Task 1 – Flag Draw a simple flag:

  • big rectangle background
  • circle in the center
  • one diagonal line crossing through the circle

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

Task 3 – Traffic light Three stacked circles (red on top, yellow middle, green bottom)

  • black outlines on all three

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

Which part still feels a little slippery?

  • Difference between fillRect vs rect + fill()?
  • Why we need beginPath() before every new shape?
  • How to make rounded rectangles?
  • Ellipse vs circle?
  • Something else?

Tell me — we’ll stay on shapes until you feel super confident drawing anything you imagine.

You’re doing really well — shapes are the foundation of 95% of Canvas art! 🚀

You may also like...

Leave a Reply

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