html5 state,HTML5 Canvas: State + State Stack

When drawing on an HTML5 canvas using its 2D Context, the 2D Context is in a certain state.

You set that state by manipulating the 2D Context properties, like for instance fillStyle

and strokeStyle. All these manipulations in total is called the 2D context state.

Often, when drawing on a canvas you need to change the state of the 2D Context during the drawing.

For instance, you may need one strokStyle for one line or rectangle, and another

strokeStyle for other lines or rectangles. Or a different rotation, or something else.

Since setting up the full state before drawing each shape can be quite cumbersome, you can push

states onto a state stack. From this state stack, earlier states can then be popped. This is a

quick way to resume an earlier state after temporary state changes.

HTML5 Canvas State Example

You push and pop the state of the 2D context using these methods:

context.save(); // state pushed onto stack.

context.restore(); // state popped from stack, and set on 2D Context.

Being saved on a stack, you can push multiple states onto this stack, and pop them later.

Here is a code example:

var canvas = document.getElementById("ex1");

var context = canvas.getContext("2d");

context.fillStyle ="#66ff66";

context.strokeStyle="#990000";

context.lineWidth = 5;

context.fillRect (5, 5, 50, 50);

context.strokeRect(5, 5, 50, 50);

context.save();

context.fillStyle = "#6666ff";

context.fillRect (65, 5, 50, 50);

context.strokeRect(65, 5, 50, 50);

context.save();

context.strokeStyle = "#000099";

context.fillRect (125, 5, 50, 50);

context.strokeRect(125, 5, 50, 50);

context.restore();

context.fillRect (185, 5, 50, 50);

context.strokeRect(185, 5, 50, 50);

context.restore();

context.fillRect (245, 5, 50, 50);

context.strokeRect(245, 5, 50, 50);

Here is the result of the above code when drawn on a canvas:

HTML5 Canvas not supported

State Stack Use Cases

The state stack is really useful if you change the canvas composition mode, or transformation

settings, and need to get back to the settings before the changes you made. By saving and restoring

the composition mode or transformation settings, you make sure that it is reset correctly. Otherwise

it can be a bit hard to get back to the exact settings you had before.

What is Part of the 2D Context State?

All 2D Context properties are part of the state that is saved and restored.

But, what is drawn on the canvas is not. That means, that when restoring a state

you do not restore the drawing area itself. You only restore the 2D Context

settings (property values). These settings include:

fillStyle

font

globalAlpha

globalCompositionOperation

lineCap

lineJoin

lineWidth

miterLimit

shadowBlur

shadowColor

shadowOffsetX

shadowOffsetY

strokeStyle

strokeStyle

textAlign

textBaseline

The clipping region

The transformation matrix (rotations + translations via context.rotate() + context.setTransform())

This list is not exhaustive. More properties may be part of the 2D Context state as the standard evolve.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值