CanvasRenderingContext2D: save()-Methode

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.

Die CanvasRenderingContext2D.save()-Methode der Canvas 2D API speichert den gesamten Zustand der Leinwand, indem sie den aktuellen Zustand auf einen Stapel legt.

Der Zeichenstatus

Der Zeichenstatus, der auf einen Stapel gespeichert wird, besteht aus:

Syntax

js
save()

Parameter

Keine.

Rückgabewert

Keiner (undefined).

Beispiele

Speichern des Zeichenstatus

Dieses Beispiel verwendet die save()-Methode, um den aktuellen Zustand zu speichern, und restore(), um ihn später wiederherzustellen, damit Sie später ein Rechteck mit dem aktuellen Zustand zeichnen können.

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

Ergebnis

Spezifikationen

Specification
HTML
# dom-context-2d-save-dev

Browser-Kompatibilität

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
save

Legend

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

Full support
Full support

Siehe auch