Chapter 47: Canvas Text Alignment

Canvas Text Color and Canvas Text Alignment in the most detailed, slow, step-by-step, human-teacher way possible. 😊

I’m going to explain everything exactly like I’m sitting right next to you in a Hyderabad computer lab or coaching center — 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. Two Main Topics Today

  1. Canvas Text Color → how to control the fill color (inside of letters) and stroke color (outline/border of letters)
  2. Canvas Text Alignment → how to control where the text sits relative to the (x, y) position you give it

Both are controlled through properties of the 2D context (ctx).

2. Canvas Text Color – Full Details

In Canvas, text color is not set with CSS color property. Instead you use two special properties:

  • fillStyle → color of the inside of the letters (most common)
  • strokeStyle → color of the outline/border around the letters (optional, very useful for emphasis)

You also control thickness of the outline with lineWidth.

Basic rules everyone must remember:

  • fillText() uses fillStyle
  • strokeText() uses strokeStyle
  • You can use both at the same time on the same text (fill first, then stroke on top)
  • Colors work the same way as fill/stroke on shapes: named colors, hex, rgb/rgba, hsl, gradients, patterns

3. Your First Text Color & Alignment Example (Copy-Paste & Run)

Create canvas-text-color-alignment.html:

HTML

What you see:

  • Red filled text
  • Blue outlined text
  • Yellow filled + orange outlined text
  • Rainbow gradient filled text
  • Vertical center line + explanation (shows where the text is anchored)

4. All Important Text Color & Alignment Properties

A. Text Color – fillStyle & strokeStyle

JavaScript

B. textAlign – Horizontal Alignment

Value Meaning Where text sits relative to x coordinate
start Left side (default in LTR languages) x = left edge of text
end Right side x = right edge of text
center Center (most useful) x = middle of text
left Same as start x = left edge
right Same as end x = right edge

C. textBaseline – Vertical Alignment (Very Confusing for Beginners)

Value Meaning (where y coordinate points) Best use case
top Top edge of text When you want y = top
hanging Special for some scripts Rarely used
middle Middle of text (most useful) Center text vertically
alphabetic Default – bottom of normal letters Standard writing
ideographic For Asian scripts Rarely used
bottom Bottom of entire text (including descenders) When you want y = bottom

Most useful combo students should memorize:

JavaScript

5. Teacher’s Quick Tips (Hyderabad Student Style šŸ˜„)

  • Always set font before drawing text — default is tiny 10px sans-serif
  • textAlign & textBaseline are very powerful — use center + middle for most UI/game text
  • Common mistake #1: Forget to set fillStyle → black text (default)
  • Common mistake #2: Wrong textBaseline → text looks shifted up/down unexpectedly
  • Common mistake #3: Draw text but forget fillText() or strokeText() → invisible
  • Pro tip: Measure text width for perfect centering
JavaScript

Understood Canvas Text Color & Text Alignment fully now? These two features control how almost every piece of text looks and sits in Canvas — game scores, chart labels, buttons, UI overlays, loaders percentage, everything.

Tell me what you want next — we can build on this:

  • Animated typing text effect?
  • Text with gradient fill + shadow + outline?
  • Text inside circle / curved text demo?
  • Canvas dashboard with aligned labels & numbers?
  • Or 15-question Canvas text 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 *