DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > How To Store and Resume HTML5 Canvas States

How To Store and Resume HTML5 Canvas States

Jakob Jenkov user avatar by
Jakob Jenkov
·
Jan. 13, 12 · Web Dev Zone · Interview
Like (0)
Save
Tweet
6.12K Views

Join the DZone community and get the full member experience.

Join For Free

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.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:

 




 

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

 

HTML

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Container Orchestration Tools Comparison
  • Datafaker: An Alternative to Using Production Data
  • My Sentiments, Erm… Not Exactly
  • What Is Data Analytics? Understanding Data Analytics Techniques

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo