This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
The CanvasRenderingContext2D.save() method of the Canvas 2D API saves the entire state of the canvas by pushing the current state onto a stack.
The drawing state that gets saved onto a stack consists of:
directionfillStylefilterfontfontKerningfontStretchfontVariantCapsglobalAlphaglobalCompositeOperationimageSmoothingEnabledimageSmoothingQualityletterSpacinglineCaplineDashOffsetlineJoinlineWidthmiterLimitshadowBlurshadowColorshadowOffsetXshadowOffsetYstrokeStyletextAligntextBaselinetextRenderingwordSpacingsave()
None.
None (undefined).
This example uses the save() method to save the current state and restore() to restore it later, so that you are able to draw a rect with the current state later.
<canvas id="canvas"></canvas>
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
// Save the current state
ctx.save();
ctx.fillStyle = "green";
ctx.fillRect(10, 10, 100, 100);
// Restore to the state saved by the most recent call to save()
ctx.restore();
ctx.fillRect(150, 40, 100, 100);
| Specification |
|---|
| HTML> # dom-context-2d-save-dev> |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | |
save |
1 | 12 | 1.5 | ≤12.1 | 2 | 18 | 4 | ≤12.1 | 1 | 1.0 | 4.4 | 1 |
CanvasRenderingContext2D
CanvasRenderingContext2D.restore()
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/save