Chapter 47: Canvas Text Alignment

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

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

Canvas Text Alignment – From Zero to Confident

1. The single most important sentence about text alignment in Canvas

Text alignment in Canvas controls where the text is positioned relative to the (x, y) coordinates you give it.

You tell Canvas:

  • “I’m giving you an x coordinate — should the text be left-aligned, centered, or right-aligned to that x?”
  • “I’m giving you a y coordinate — should the text sit above, on, or below that y line?”

That’s it — two properties control everything:

  • textAlign → horizontal alignment (left/center/right)
  • textBaseline → vertical alignment (top/middle/bottom/etc.)

Both properties affect where the text is painted relative to the point (x, y) you pass to fillText() or strokeText().

2. The two properties you will use 95% of the time

Property What it controls Common values Default value Most useful for
textAlign Horizontal position relative to x ‘left’ / ‘center’ / ‘right’ ‘start’ (usually left) Centering text
textBaseline Vertical position relative to y ‘top’ / ‘middle’ / ‘alphabetic’ / ‘bottom’ ‘alphabetic’ True vertical centering

Quick rule of thumb (memorize this):

  • Want text perfectly centered both horizontally and vertically? → textAlign = ‘center’ → textBaseline = ‘middle’

3. Minimal complete example – see alignment in action

Copy-paste and run this — it shows every common alignment combination with labels.

HTML

What you should see & remember forever:

  • Left-aligned → text starts at x=300
  • Center-aligned → text is perfectly centered around x=300
  • Right-aligned → text ends at x=300
  • Alphabetic baseline → text sits on the line (like normal writing)
  • Top baseline → top of text touches the line
  • Middle baseline → true vertical center (most useful for centering)
  • Bottom baseline → bottom of text touches the line

4. Common real-world patterns you’ll reuse

Pattern 1 – Perfectly centered title

JavaScript

Pattern 2 – Outlined glowing text

JavaScript

Pattern 3 – Text on button

JavaScript

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

Task 1 – Centered heading Fill canvas with light blue Draw large centered text “Canvas Text Alignment” in dark blue Use center + middle

Task 2 – Baseline demo Draw one horizontal gray line across the middle Draw the same text “Baseline Test” three times on that line:

  • once with textBaseline = ‘top’
  • once with textBaseline = ‘middle’
  • once with textBaseline = ‘alphabetic’ Label each one

Task 3 – Button with text Draw a rounded gray rectangle Inside it draw white centered text “Submit” Make sure text is perfectly vertically centered in the button

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 textAlign vs textBaseline?
  • Why middle baseline is best for vertical centering?
  • How alphabetic baseline behaves differently?
  • Making text look centered in buttons/boxes?
  • Something else?

Tell me — we’ll stay on text alignment until you feel super confident placing text exactly where you want it.

You’re doing really well — mastering alignment is one of the biggest “aha” moments in Canvas text! 🚀

You may also like...

Leave a Reply

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