Canvas State & Styling

Beginner
6 min read
Updated: Nov 2026
2 topic guides
Appearance hub

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.

Beginner path.

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:

JavaScript
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 topic

Isolate temporary style and transform changes with the state stack.

TopicDescriptionTutorial
Statessave(), restore(), and what the drawing-state stack stores.Open

Shadows

1 topic

Soft drop shadows and glows on filled, stroked, and text draws.

TopicDescriptionTutorial
ShadowshadowColor, shadowBlur, shadowOffsetX, and shadowOffsetY.Open

Live Preview

Adjust blur on a saved shadow block. The gray bar after restore() stays sharp — styles did not leak.

16
8

Blue card uses save/restore + shadow; bottom bar draws after restore.

Canvas

blur="16" offsetY="8"

How to Learn State & Styling

A practical order for this hub:

  1. Finish labels and alpha from Text & Composition.
  2. Practice save() before a temporary style and restore() right after.
  3. Change fill, stroke, or alpha inside a save/restore pair and confirm later draws stay clean.
  4. Add shadowColor, shadowBlur, and offsets for one shape only.
  5. Apply the same shadow pattern to text, then clear it with restore.
  6. Continue to Color & Effects for gradients and patterns.

Frequently Asked Questions

Two related skills: pushing and popping the drawing state with save()/restore(), and applying soft shadows with shadowColor, shadowBlur, and shadow offsets.
Styles (fill, stroke, alpha, composite mode, font, shadows), the current transformation matrix, and the clipping region. It does not save the bitmap pixels themselves.
Temporary style or transform changes are easy to forget. save() before a local effect and restore() after keeps later draws from inheriting blur, color, or transforms you no longer want.
Large shadowBlur values are more expensive to paint. Prefer modest blur for UI chrome, and wrap shadow settings in save()/restore() so they do not leak onto every later shape.
Yes. Shadow properties apply to fill, stroke, and text draws. Set them before the draw call that should cast the shadow.
This page is the index for the State & Styling sidebar section. Each topic page (States, Shadow) has full syntax, live demos, examples, and FAQs.

Start with Canvas States

Open the save/restore guide — stack rules, live preview, and examples.

Canvas States →

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