DZone
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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Languages
  4. 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 · Interview
Like (0)
Save
Tweet
Share
6.31K 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

  • Scaling Your Testing Efforts With Cloud-Based Testing Tools
  • Introduction to NoSQL Database
  • 10 Most Popular Frameworks for Building RESTful APIs
  • What Are the Different Types of API Testing?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • 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: