CanvasRenderingContext2D.save() - Web APIs 编辑
The CanvasRenderingContext2D
.save()
method of the Canvas 2D API saves the entire state of the canvas by pushing the current state onto a stack.
The drawing state
The drawing state that gets saved onto a stack consists of:
- The current transformation matrix.
- The current clipping region.
- The current dash list.
- The current values of the following attributes:
strokeStyle
,fillStyle
,globalAlpha
,lineWidth
,lineCap
,lineJoin
,miterLimit
,lineDashOffset
,shadowOffsetX
,shadowOffsetY
,shadowBlur
,shadowColor
,globalCompositeOperation
,font
,textAlign
,textBaseline
,direction
,imageSmoothingEnabled
.
Syntax
void ctx.save();
Examples
Saving the drawing state
This example uses the save()
method to save the default state and restore()
to restore it later, so that you are able to draw a rect with the default state later.
HTML
<canvas id="canvas"></canvas>
JavaScript
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
// Save the default state
ctx.save();
ctx.fillStyle = 'green';
ctx.fillRect(10, 10, 100, 100);
// Restore the default state
ctx.restore();
ctx.fillRect(150, 40, 100, 100);
Result
Specifications
Specification | Status | Comment |
---|---|---|
HTML Living Standard The definition of 'CanvasRenderingContext2D.save' in that specification. | Living Standard |
Browser compatibility
BCD tables only load in the browser
See also
- The interface defining this method:
CanvasRenderingContext2D
CanvasRenderingContext2D.restore()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论