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.
Save, then set globalAlpha = 0.35 for the yellow rect.
Restore returns alpha to 1.
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.
Compatibility
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 ChromeAll modern
Full support
Mozilla FirefoxAll modern
Full support
Apple SafariAll modern
Full support
Microsoft EdgeAll modern
Full support
OperaAll modern
Full support
Samsung InternetAll modern
Full support
Internet Explorer9+
Supported
save() / restore()Widely supported
Bottom line: Safe for production isolation of styles, transforms, shadows, and clips on Canvas 2D everywhere modern browsers run.
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.