Over a million developers have joined DZone.

How To Store and Resume HTML5 Canvas States

DZone's Guide to

How To Store and Resume HTML5 Canvas States

· Web Dev Zone ·
Free Resource

Learn how to add document editing and viewing to your web app on .Net (C#), Node.JS, Java, PHP, Ruby, etc.

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.

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.lineWidth  = 5;

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


context.fillStyle = "#6666ff";

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


context.strokeStyle = "#000099";

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


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


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:



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.


Source: http://tutorials.jenkov.com/html5-canvas/state.html


Extend your web service functionality with docx, xlsx and pptx editing. Check out ONLYOFFICE document editors for integration.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}