CanvasRenderingContext2D: restore() method

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

The CanvasRenderingContext2D.restore() method of the Canvas 2D API restores the most recently saved canvas state by popping the top entry in the drawing state stack. If there is no saved state, this method does nothing.

For more information about the drawing state, see CanvasRenderingContext2D.save().

Syntax

js
restore()

Parameters

None.

Return value

None (undefined).

Examples

Restoring a saved state

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.

HTML

html
<canvas id="canvas"></canvas>

JavaScript

js
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);

Result

Specifications

Specification
HTML Standard
# dom-context-2d-restore-dev

Browser compatibility

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
restore

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

See also