1. Web
  2. Web APIs
  3. CanvasRenderingContext2D
  4. save()

CanvasRenderingContext2D: save() 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.save() method of the Canvas 2D API saves the entire state of the canvas by pushing the current state onto a stack.

Syntax

js
save()

Parameters

None.

Return value

None (undefined).

Description

The drawing state that gets saved onto a stack consists of:

Examples

Saving the drawing 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
# dom-context-2d-save-dev

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on by MDN contributors.

AltStyle によって変換されたページ (->オリジナル) /