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 のメソッドで、現在の状態をスタックにプッシュすることで、キャンバス全体の状態を保存します。
描画状態
スタックに保存される描画状態には、以下のものが含まれます。
- 現在の変形行列。
- 現在のクリッピング領域。
- 現在の点線リスト。
strokeStyle
、fillStyle
、globalAlpha
、lineWidth
、lineCap
、lineJoin
、miterLimit
、lineDashOffset
、shadowOffsetX
、shadowOffsetY
、shadowBlur
、shadowColor
、globalCompositeOperation
、font
、textAlign
、textBaseline
、direction
、imageSmoothingEnabled
の現在の値。
構文
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 GitHubdesktop | mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
save |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
関連情報
- このメソッドを定義しているインターフェイス:
CanvasRenderingContext2D
CanvasRenderingContext2D.restore()