Chapter 45: Canvas Text

1. What is “Canvas Text” actually?

Canvas Text means using JavaScript on the <canvas> element to draw text (words, sentences, titles, labels, numbers, signatures, game scores, chart labels, buttons text, etc.) directly on the canvas.

In Canvas, text is not HTML text (no <p>, <h1>, no CSS hover effects on individual letters). Instead, text is drawn like paint — once you place it, it becomes pixels. You control:

  • What text to write
  • Where to place it
  • Font family, size, weight, style
  • Color (fill & stroke)
  • Alignment (left, center, right)
  • Baseline (top, middle, bottom of letters)
  • Shadows, rotation, gradients, patterns on text

Very important from day one:

  • Canvas text is pixel-based — zoom too much = can look pixelated (unlike SVG text)
  • You draw text with fillText() or strokeText() (or both)
  • Text uses the currentfont, fillStyle, strokeStyle, textAlign, textBaseline

2. Your First Canvas Text – Hello World Style (Copy-Paste & Run)

Create canvas-text-hello.html:

HTML

What you see:

  • Big red filled text “Hello Canvas!”
  • Green outlined text “Stroke Text”
  • Yellow filled + orange outlined text “Fill + Stroke”
  • Small explanation text below

3. All Important Text Properties & Methods (With Copy-Paste Snippets)

A. font (size + family + style + weight)

JavaScript

B. fillStyle & strokeStyle (color)

JavaScript

C. textAlign (horizontal alignment)

JavaScript

D. textBaseline (vertical alignment – very confusing for beginners)

JavaScript

E. Shadows on Text (very common in UI)

JavaScript

F. Text on Path / Curve (advanced – not native, but possible with libraries or manual math)

Canvas does not have <textPath> like SVG. You either:

  • Use a library (like opentype.js + manual placement)
  • Or draw letter-by-letter with rotation

Simple manual example (not perfect, just idea):

JavaScript

4. Teacher’s Quick Tips (Hyderabad Student Style 😄)

  • textAlign & textBaseline together control where the text sits relative to (x,y)
  • Most useful combo: textAlign = ‘center’ + textBaseline = ‘middle’
  • Common mistake #1: Forget to set font → default tiny 10px font appears
  • Common mistake #2: Wrong textBaseline → text looks shifted up/down
  • Common mistake #3: Draw text but forget fillText() or strokeText() → invisible!
  • Pro tip: Use ctx.measureText(text) to get width → center text perfectly
JavaScript

Understood Canvas Text fully now? Text is one of the most used features in Canvas — charts labels, game scores, UI buttons, loaders percentage, watermarks, everything.

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

  • Animated typing text effect?
  • Text with gradient fill + shadow?
  • Text inside circle / curved text demo?
  • Canvas dashboard with 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 *