Chapter 45: Canvas Text

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

Imagine I’m sitting right next to you with my screen open. We’re going to build every concept together — slowly, with tiny complete examples you can copy-paste and run immediately. No skipping steps, no magic, just plain English explanations, visual thinking, common beginner traps, and repeated patterns until drawing and controlling text on Canvas feels completely natural and easy.

Canvas Text – Complete Beginner-to-Confident Guide

1. The single most important sentence about text in Canvas

Text in Canvas is painted pixels — not live editable DOM text like in HTML or SVG.

Once you call fillText() or strokeText(), those letters become just pixels on the bitmap. You cannot later say “change this word’s color” or “make this letter bigger” without redrawing everything. That is why text is usually drawn last in the rendering order (after background/shapes), and why animations involving changing text usually require a full redraw loop.

2. The two main commands you will use 99% of the time

Command What it does Filled? Outlined? Most common use case
fillText(text, x, y) Fills the text with current fillStyle Yes No Normal solid-color text
strokeText(text, x, y) Strokes (outlines) the text No Yes Outline / border text, glowing letters

Both commands use the current state:

  • ctx.font
  • ctx.fillStyle / ctx.strokeStyle
  • ctx.textAlign
  • ctx.textBaseline
  • ctx.lineWidth (for strokeText)

3. Minimal complete text example (copy → run)

HTML

What you should see & remember forever:

  • Blue text centered horizontally (textAlign = ‘center’) and vertically (textBaseline = ‘middle’)
  • Red outlined text below it
  • Thin gray line shows the baseline — text sits on this line, not centered inside it

4. Most important text properties (copy this cheat sheet)

Property What it controls Common values Default Very useful?
font Size, weight, family ‘bold 48px Arial’, ‘italic 36px serif’ ’10px sans-serif’ ★★★★★
fillStyle Color of filled text ‘#2196F3’, ‘rgba(33,150,243,0.8)’ black ★★★★★
strokeStyle Color of outlined text ‘#F44336’, ‘gold’ black ★★★★☆
lineWidth Thickness of stroke outline 2, 6, 12 1 ★★★★☆
textAlign Horizontal alignment to x coordinate ‘left’ / ‘center’ / ‘right’ / ‘start’ / ‘end’ ‘start’ ★★★★★
textBaseline Vertical alignment to y coordinate ‘top’ / ‘hanging’ / ‘middle’ / ‘alphabetic’ / ‘ideographic’ / ‘bottom’ ‘alphabetic’ ★★★★★
direction Text direction (rare) ‘ltr’ / ‘rtl’ / ‘inherit’ ‘inherit’ ★☆☆☆☆

Most common combination for centered text:

JavaScript

5. Example 2 – Text styles showcase (copy-paste & play)

HTML

Key lessons from this example:

  • textBaseline controls where the y coordinate points
    • ‘alphabetic’ (default) → bottom of letters (like normal text)
    • ‘middle’ → true vertical center — most useful for centering
    • ‘top’ → top of the text block
  • strokeText + fillText together = outlined filled text (stroke first or fill first changes look slightly)

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

Task 1 – Centered title Fill canvas with light gray background Draw big bold centered text “Welcome to Canvas” in dark blue Use textAlign = ‘center’ and textBaseline = ‘middle’

Task 2 – Outlined glowing text Draw large text “GLOW” in yellow fill Add thick red stroke around it Add very thin white stroke on top (draw twice)

Task 3 – Baseline experiment Draw one horizontal gray line across center Draw same text three times on that line using different textBaseline values: ‘top’, ‘middle’, ‘alphabetic’ Label each one

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 fillText vs strokeText?
  • How textBaseline actually moves the text relative to y?
  • Why centering text is tricky without middle baseline?
  • Making outlined text look nice (stroke width / order)?
  • Something else?

Tell me — we’ll stay on text until you feel super confident placing, styling, and centering any text you want.

You’re doing really well — text is one of the trickier parts of Canvas, but once you master alignment and baseline it becomes very powerful! 🚀

You may also like...

Leave a Reply

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