Canvas State & Styling

Definition
State & styling covers how you keep drawing styles under control and how you add soft depth: save() / restore() for the drawing-state stack, and shadowColor / shadowBlur / shadow offsets for drop shadows and glows.
After text and blending from Text & Composition, open States so temporary styles do not leak, then Shadow for elevated shapes and labels.
Learn save/restore first → then shadow props wrapped in that stack → then gradients in Color & Effects.
Syntax
Isolate a shadowed draw, then continue with clean styles:
const canvas = document.getElementById("demo");
const ctx = canvas.getContext("2d");
ctx.fillStyle = "#e2e8f0";
ctx.fillRect(24, 24, 120, 80);
ctx.save();
ctx.shadowColor = "rgba(15, 23, 42, 0.45)";
ctx.shadowBlur = 16;
ctx.shadowOffsetY = 8;
ctx.fillStyle = "#2563eb";
ctx.fillRect(160, 40, 140, 90);
ctx.restore(); // shadow gone — next draw is clean
ctx.fillStyle = "#0f172a";
ctx.fillRect(24, 140, 276, 24); Topics
Guides in the State & Styling section (2 tutorials).
Drawing state
1 topicIsolate temporary style and transform changes with the state stack.
Shadows
1 topicSoft drop shadows and glows on filled, stroked, and text draws.
Live Preview
Adjust blur on a saved shadow block. The gray bar after restore() stays sharp — styles did not leak.
How to Learn State & Styling
A practical order for this hub:
- Finish labels and alpha from Text & Composition.
- Practice
save()before a temporary style andrestore()right after. - Change fill, stroke, or alpha inside a save/restore pair and confirm later draws stay clean.
- Add
shadowColor,shadowBlur, and offsets for one shape only. - Apply the same shadow pattern to text, then clear it with restore.
- Continue to Color & Effects for gradients and patterns.
Frequently Asked Questions
Start with Canvas States
Open the save/restore guide — stack rules, live preview, and examples.
8 people found this page helpful
