Managing Canvas State with save() and restore()

Think of save() and restore() as checkpoints for your canvas. save() pushes the current drawing state (styles, transforms) onto a stack, and restore() pops it back off. Use this to temporarily apply a transformation to just one element.
Why it exists
When drawing complex scenes, you often need to change settings like color, transparency, or the coordinate system itself (e.g., rotating for one object). Manually resetting every single property back to its original value after each change is tedious and error-prone. save() and restore() provide a robust mechanism to manage these temporary state changes.
The mental model
Treat the canvas drawing state as a stack of settings. save() is like pushing a new, clean plate onto a stack of plates. You can paint on this new plate by changing fillStyle, globalAlpha, or applying rotate(). When you're done, restore() takes that plate off the top, revealing the one underneath exactly as you left it. You can nest these operations as deep as you need.
How it works
The save() method pushes the entire current drawing state onto a memory stack. This state includes transformation matrices (from translate, rotate, scale), style attributes (strokeStyle, fillStyle), global settings (globalAlpha, globalCompositeOperation), font settings, and even the current clipping path. The restore() method pops the most recently saved state from the top of the stack and applies all its settings back to the canvas context.
When to use it
Use save() and restore() any time you are making a temporary change for a specific drawing operation. This is especially critical when applying transformations. A common pattern is: save(), apply transformations (translate, rotate), draw the object, then restore() to reset the coordinate system for the next object. It's also useful for temporarily changing colors or transparency for a single shape.
When not to use it
Avoid using save() and restore() for global, permanent changes. If you want to set a background for the entire canvas once, just set fillStyle and fillRect at the beginning without saving state. Overusing it for simple, non-conflicting style changes can add unnecessary complexity to your code.
One canonical example
A common pattern is nesting states. First, you might draw a large black square with default settings. Then, you call ctx.save() to remember this state. Next, you change ctx.fillStyle to blue and draw a smaller square. You can call ctx.save() again, change ctx.fillStyle to white and ctx.globalAlpha to 0.5, and draw a third, semi-transparent square. Calling ctx.restore() will pop the white/transparent state, returning you to the blue state. Another ctx.restore() pops the blue state, returning you to the original black state.
Interview question
When is using ctx.save() and ctx.restore() most beneficial in canvas drawing?
- a.To set a global background color for the canvas that should remain consistent for all future drawings.
- b.To temporarily apply a rotation to draw a single shape, then reset the canvas's coordinate system for subsequent shapes.Correct
- c.To store the current visual content (pixels) of the canvas to be retrieved and displayed again later.
- d.To change the fill color for every single shape drawn on the canvas, one after another, without any complex transformations.
Why? this is the answer
Option B correctly identifies the primary use case: managing temporary transformations for specific drawing operations, allowing the canvas to revert to its previous state. Option A describes a permanent global change, which the card advises against using save/restore for. Option C misunderstands that save/restore manage drawing state, not the actual pixel content. Option D, while possible, is an example of overusing save/restore for simple style changes, which the card suggests avoiding due to added complexity.
Just read this? Test yourself on what you have been reading.
Read the original → developer.mozilla.org
- #canvas
- #web apis
- #javascript
- #graphics
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.
See open roles