Canvas Text & Composition

Beginner
7 min read
Updated: Nov 2026
2 topic guides
Text hub

Definition

Text & composition covers two Canvas skills that often work together: drawing readable labels with fillText / strokeText, and controlling how new pixels mix with what is already on the bitmap (globalAlpha, globalCompositeOperation).

After shapes from Core Drawing, open Text for typography, then Composition for opacity and blend modes.

Beginner path.

Draw shapes first → add text labels → learn alpha and blend modes when layers overlap.

Syntax

A minimal text draw, then a soft overlay with alpha:

JavaScript
const canvas = document.getElementById("demo");
const ctx = canvas.getContext("2d");

// Text
ctx.font = "24px sans-serif";
ctx.fillStyle = "#0f172a";
ctx.textAlign = "left";
ctx.fillText("Hello Canvas", 24, 48);

// Composition — next draw is 50% opaque
ctx.globalAlpha = 0.5;
ctx.fillStyle = "#2563eb";
ctx.fillRect(40, 70, 160, 80);
ctx.globalAlpha = 1; // reset

Topics

Guides in the Text & Composition section (2 tutorials).

Text

1 topic

Draw labels and titles on the bitmap with font styling and metrics.

TopicDescriptionTutorial
TextfillText, strokeText, font, textAlign, textBaseline, measureText.Open

Composition

1 topic

Control transparency and how new drawings blend with existing pixels.

TopicDescriptionTutorial
CompositionDrawing order, globalAlpha, and globalCompositeOperation modes.Open

Live Preview

Change alpha and blend mode while a label sits over a blue rectangle — the same ideas as the topic pages.

0.75

Amber bar is destination; blue circle + text are source (drawn second).

Canvas

mode="source-over" alpha="0.75"

How to Learn Text & Composition

A practical order for this hub:

  1. Finish a few shape tutorials from Core Drawing.
  2. Draw a label with fillText and set font / textAlign.
  3. Try strokeText and measureText for outlined or measured labels.
  4. Overlap two shapes and change globalAlpha.
  5. Experiment with blend modes such as multiply and destination-out.
  6. Reset styles with save() / restore() (see State & Styling).

Frequently Asked Questions

Two related skills: drawing labels with fillText / strokeText, and controlling how new pixels mix with existing ones using globalAlpha and globalCompositeOperation.
Learn text first so you can label shapes. Then open composition to control opacity and blend modes when shapes and text overlap.
No. Canvas does not auto-wrap. Split lines yourself, use maxWidth to shrink a single line, or measure widths with measureText.
globalAlpha sets opacity for later draws: 0 is transparent, 1 is opaque (default). It stays until you change it — wrap changes in save()/restore() when needed.
It chooses how the next draw combines with pixels already on the canvas. Default source-over paints on top. Modes like multiply, destination-out, and xor change the mix.
This page is the index for the Text & Composition sidebar section. Each topic page (Text, Composition) has full syntax, live demos, examples, and FAQs.

Start with Canvas Text

Open the text guide — font, alignment, live preview, and examples.

Canvas Text →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful