CanvasRenderingContext2D.save()

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.

CanvasRenderingContext2D.save() はキャンバス 2D API のメソッドで、現在の状態をスタックにプッシュすることで、キャンバス全体の状態を保存します。

描画状態

スタックに保存される描画状態には、以下のものが含まれます。

構文

js
void ctx.save();

描画状態の保存

この例は、save() メソッドを使用して既定の状態を保存し、 restore() を使用して復元しているため、既定の状態で長方形を描画できます。

HTML

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

JavaScript

js
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");

// 既定の状態を保存
ctx.save();

ctx.fillStyle = "green";
ctx.fillRect(10, 10, 100, 100);

// 既定の状態を復元
ctx.restore();

ctx.fillRect(150, 40, 100, 100);

結果

仕様書

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

ブラウザーの互換性

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

関連情報