Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we use canvas.save or canvas.restore?

Tags:

android

I know what it does. If I dont use sometimes I can see the difference in alignments, sometimes I don't. My question is: if canvas.restore restores the state previous to when it was saved, why aren't the changes made after save and before restore undone? The changes remain. Why?

like image 432
Prabhat Avatar asked Jun 16 '10 09:06

Prabhat


People also ask

What does canvas save do?

It removes the change in settings, but not the drawing itself. Settings can include scaling the canvas etc., and it restores the scaling to the initial state when you called canvas. save().

What is canvas restore?

This allows you to restore the state before the most recent entry on the save stack, i.e. it "Pops the current save stack".

How do you save a canvas drawing?

Save the canvas contents as a base64 encoded PNG image by calling canvas. toDataURL() and store the encoded string in the page's localStorage. When you want to restore the canvas, you would create an image, set the src to be the value previously stored locally and then draw that image on the canvas.

What is saveLayer in flutter?

saveLayer method Null safety blendMode applied. This lets you create composite effects, for example making a group of drawing commands semi-transparent. Without using saveLayer, each part of the group would be painted individually, so where they overlap would be darker than where they do not.


1 Answers

canvas.save and canvas.restore undo things like rotation and translation. They don't undo drawing on the canvas. The android canvas works similarly to the HTML5 canvas, so you can look at https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Canvas_tutorial/Transformations if you need more clarification.

like image 156
Ben L. Avatar answered Oct 03 '22 16:10

Ben L.