Canvas Text & Composition

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.
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:
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 topicDraw labels and titles on the bitmap with font styling and metrics.
Composition
1 topicControl transparency and how new drawings blend with existing pixels.
| Topic | Description | Tutorial |
|---|---|---|
Composition | Drawing 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.
How to Learn Text & Composition
A practical order for this hub:
- Finish a few shape tutorials from Core Drawing.
- Draw a label with
fillTextand setfont/textAlign. - Try
strokeTextandmeasureTextfor outlined or measured labels. - Overlap two shapes and change
globalAlpha. - Experiment with blend modes such as
multiplyanddestination-out. - Reset styles with
save()/restore()(see State & Styling).
Frequently Asked Questions
Start with Canvas Text
Open the text guide — font, alignment, live preview, and examples.
8 people found this page helpful
