HTML Canvas States

Beginner
8 min read
Updated: Nov 2026
5 examples
Live preview

Definition

On an <canvas>, ctx.save() pushes the current drawing state onto a stack. ctx.restore() pops the top entry and puts those settings back.

Saved state includes the transform, clipping region, dash list, and style attributes such as fillStyle, strokeStyle, lineWidth, font, globalAlpha, shadows, and composite mode. Wrap temporary changes so later draws do not inherit them.

Baseline · Widely available.

save and restore are stable Canvas 2D features across modern browsers since about July 2015.

Path is not saved.

The current path (beginPath, moveTo, lineTo, …) is not part of the drawing state. Rebuild paths after restore() if you still need them.

Syntax

states.js
ctx.save();
// temporary styles / transforms / shadows
ctx.restore();

// Nested (stack)
ctx.save();
  ctx.save();
  ctx.restore(); // inner
ctx.restore();   // outer

If the stack is empty, restore() does nothing.

Key Methods

Core state tools:

Method / ideaDescriptionTry it
save()Push current drawing state onto the stackTry it
restore()Pop and apply the most recent saved stateTry it
Isolate transformRotate/scale inside save/restoreTry it
Isolate shadowLimit shadow props to one drawTry it
Nested stackMultiple save levels like bracketsTry it
Alpha / styleTemporary globalAlpha or colorsTry it

Global Attributes

The <canvas> tag also supports the Global Attributes in HTML.

Live Preview

Toggle isolation. With save/restore, the second rect stays default blue; without it, the green style leaks.

Left rect always uses a temporary green fillStyle.

Canvas

save → green → restore → second draw

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Isolate fillStyle

Change color temporarily; restore the previous style.

HTML
<canvas id="demo" width="300" height="160" aria-label="save restore fillStyle"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");
  ctx.fillStyle = "#2563eb";

  ctx.save();
  ctx.fillStyle = "#16a34a";
  ctx.fillRect(30, 40, 100, 80);
  ctx.restore();

  ctx.fillRect(170, 40, 100, 80);
</script>
Try It Yourself

How It Works

  1. Default fill is blue.
  2. save() remembers that blue state.
  3. Temporary green draws the left rect.
  4. restore() brings blue back for the right rect.

Example 2 — Isolate a Rotation

Rotate one shape without spinning later draws.

HTML
<canvas id="demo" width="300" height="160" aria-label="save restore around rotate"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");

  ctx.save();
  ctx.translate(90, 80);
  ctx.rotate((-35 * Math.PI) / 180);
  ctx.fillStyle = "#ea580c";
  ctx.fillRect(-45, -18, 90, 36);
  ctx.restore();

  ctx.fillStyle = "#2563eb";
  ctx.fillRect(170, 62, 90, 36);
</script>
Try It Yourself

How It Works

  1. Save before changing the matrix.
  2. Translate + rotate only for the orange rect.
  3. Restore resets the transform to identity (for this page’s prior state).
  4. Blue rect stays upright and uses canvas coordinates again.

Example 3 — Isolate a Shadow

Apply a drop shadow to one shape only.

HTML
<canvas id="demo" width="300" height="160" aria-label="save restore around shadow"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");

  ctx.save();
  ctx.shadowColor = "rgba(0, 0, 0, 0.4)";
  ctx.shadowBlur = 12;
  ctx.shadowOffsetX = 6;
  ctx.shadowOffsetY = 6;
  ctx.fillStyle = "#7c3aed";
  ctx.fillRect(40, 40, 90, 80);
  ctx.restore();

  ctx.fillStyle = "#2563eb";
  ctx.fillRect(170, 40, 90, 80);
</script>
Try It Yourself

How It Works

  1. Shadow props are part of state and get saved/restored.
  2. Purple rect draws with a drop shadow.
  3. Restore clears blur and offsets.
  4. Blue rect has no shadow.

Example 4 — Nested save / restore

Stack levels like nested brackets.

HTML
<canvas id="demo" width="300" height="160" aria-label="Nested canvas save restore"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");
  ctx.fillStyle = "#94a3b8";

  ctx.save();                 // level 1
  ctx.fillStyle = "#2563eb";
  ctx.fillRect(20, 50, 70, 60);

  ctx.save();                 // level 2
  ctx.fillStyle = "#16a34a";
  ctx.fillRect(110, 50, 70, 60);
  ctx.restore();              // back to blue

  ctx.fillRect(200, 50, 70, 60);
  ctx.restore();              // back to gray
</script>
Try It Yourself

How It Works

  1. Outer save remembers gray.
  2. Inner save remembers blue while green draws.
  3. Inner restore returns to blue for the third rect.
  4. Outer restore would return to gray for any later draws.

Example 5 — Isolate globalAlpha

Fade one shape without making everything translucent.

HTML
<canvas id="demo" width="300" height="160" aria-label="save restore globalAlpha"></canvas>
<script>
  const ctx = document.getElementById("demo").getContext("2d");

  ctx.fillStyle = "#0f172a";
  ctx.fillRect(0, 0, 300, 160);

  ctx.save();
  ctx.globalAlpha = 0.35;
  ctx.fillStyle = "#facc15";
  ctx.fillRect(40, 40, 100, 80);
  ctx.restore();

  ctx.fillStyle = "#22d3ee";
  ctx.fillRect(160, 40, 100, 80);
</script>
Try It Yourself

How It Works

  1. Dark background shows transparency clearly.
  2. Save, then set globalAlpha = 0.35 for the yellow rect.
  3. Restore returns alpha to 1.
  4. Cyan rect is fully opaque.

Edge Cases & Pitfalls

Most state bugs are missing restores, empty-stack restores, or assuming the path is saved.

path

Current path is not saved

Only styles, transform, clip, dash, and related attributes are stacked. Rebuild paths after restore.

match

Pair every save

Unmatched save() calls leave a growing stack. Unmatched restore() on an empty stack is a no-op and does not fix leaked styles.

leak

Forgot restore = leaked style

A temporary rotate, shadow, or fillStyle will affect every later draw until you restore or overwrite it.

clip

Clipping is restored

Clip regions are part of state. Save before clip() so you can restore the previous (often full-canvas) clip.

Browser Support

save() and restore() are part of the Canvas 2D API in every modern browser.

✓ Baseline · Widely available

save / restore

Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support the drawing state stack. Internet Explorer 9+ also supports it.

98% Global support (approx.)
Google Chrome All modern
Full support
Mozilla Firefox All modern
Full support
Apple Safari All modern
Full support
Microsoft Edge All modern
Full support
Opera All modern
Full support
Samsung Internet All modern
Full support
Internet Explorer 9+
Supported
save() / restore() Widely supported

Bottom line: Safe for production isolation of styles, transforms, shadows, and clips on Canvas 2D everywhere modern browsers run.

Key Takeaways

  • Push: save() before temporary changes.
  • Pop: restore() when done.
  • Includes: styles, transform, clip, shadows, alpha.
  • Not included: the current path.

One line: save → change → draw → restore.

Default CSS Settings

Drawing state is a Canvas API concern. The <canvas> element still uses typical user-agent CSS:

default.css
canvas {
  display: inline;
}

Set bitmap size with HTML width/height. CSS sizing does not replace save/restore.

Frequently Asked Questions

save() pushes the current drawing state onto a stack. restore() pops the most recent saved state and applies it again. If the stack is empty, restore() does nothing.
The transform matrix, clipping region, dash list, and many attributes — fillStyle, strokeStyle, lineWidth, font, globalAlpha, shadows, composite mode, and more. The current path is not saved.
Without save/restore, later draws inherit your fillStyle, rotate, shadow, and alpha. Wrapping temporary changes keeps the rest of the frame predictable.
Yes. Each save pushes another level. Match them like brackets: innermost restore undoes the innermost save.
No. beginPath / moveTo / lineTo path data is not part of the drawing state stack. Rebuild paths after restore if needed.
setTransform(1,0,0,1,0,0) only resets the matrix. save/restore restores styles, shadows, clip, alpha, and more in one step.

Next: Canvas Shadow

Learn shadowColor, shadowBlur, and offsets — often wrapped in save/restore.

Canvas Shadow →

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