files/en-us/web/api/canvasrenderingcontext2d/restore/index.md
{{APIRef("Canvas API")}}
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 {{domxref("CanvasRenderingContext2D.save()")}}.
restore()
None.
None ({{jsxref("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);
{{ EmbedLiveSample('Restoring_a_saved_state', 700, 180) }}
{{Specifications}}
{{Compat}}