Chapter 36: Canvas Lines

Canvas Lines.

I’m going to explain this the way a patient, slightly excited teacher would: slowly, clearly, with small complete examples, visual thinking, common mistakes highlighted, and lots of copy-paste code you can run immediately.

We will build from the tiniest line to more interesting patterns — no skipping steps.

Canvas Lines – Complete Beginner-to-Confident Guide

1. The absolute most important mental model about lines in Canvas

A line in Canvas is not an object that lives forever (like in SVG).

It is a path instruction you give to the painter:

  • You tell him: “pick up the pen, move to this point, draw to that point”
  • He paints the pixels immediately when you say stroke()
  • After that the pixels are just pixels — there is no “line object” you can move or click later

That is why almost every line-drawing code follows this exact 4-step pattern:

JavaScript

2. Minimal complete line example (copy → run)

HTML

What we just learned in plain English:

  • beginPath() → tells Canvas “forget any previous unfinished path — start fresh”
  • moveTo(x, y) → lift pen and jump to starting point (no line drawn yet)
  • lineTo(x, y) → draw a straight line to this new point
  • stroke() → paint the line using current strokeStyle and lineWidth

3. Most important line properties (copy these cheats)

Property What it controls Common values Default Very useful?
strokeStyle Color of the line ‘#2196F3’, ‘rgba(33,150,243,0.7)’, ‘red’ black ★★★★★
lineWidth Thickness in pixels 1, 4, 12, 0.5 1 ★★★★★
lineCap Shape at line ends ‘butt’ / ’round’ / ‘square’ ‘butt’ ★★★★☆
lineJoin Shape at corners / joins ‘miter’ / ’round’ / ‘bevel’ ‘miter’ ★★★★☆
setLineDash([a,b]) Creates dashed / dotted pattern [10,5], [2,8], [20,10,5,10] solid ★★★★★
lineDashOffset Shifts where dash pattern starts number (positive or negative) 0 ★★★☆☆

4. Example 2 – Different line endings (lineCap)

HTML

Quick tip: lineCap = ’round’ is very popular for dotted lines and organic feel.

5. Example 3 – Dashed & dotted lines (setLineDash)

HTML

Tip: To make marching-ants selection effect, animate lineDashOffset.

6. Example 4 – Multiple connected lines (polyline style)

HTML

Note: This is exactly how you draw polylines / connected line charts in Canvas.

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

Task 1 – Traffic lines Draw three horizontal lines across the canvas:

  • thick solid black at y=100
  • medium dashed blue at y=200
  • thin dotted red at y=300 (use round caps)

Task 2 – Zigzag lightning Start at (100,50), draw a zigzag down to (500,350) with 6–8 sharp turns, thick orange line, round caps.

Task 3 – Coordinate cross Draw a thin gray cross through the center + label the center point “(300,200)” with text.

Paste any of them here when you’re done — I’ll review them like we’re sitting together debugging.

Which part still feels a little slippery?

  • Why we need beginPath() every time?
  • Difference between stroke() and fill()?
  • How lineCap and lineJoin actually look?
  • Making nice dashed lines?
  • Something else?

Tell me — we’ll stay on lines until you feel 100% comfortable drawing anything with them.

You’re doing really well — lines are the foundation of almost every interesting Canvas drawing. 🚀

You may also like...

Leave a Reply

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