Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Before we go any further in learning how to draw on the canvas, we should take a moment and learn about the canvas state and understand how it works. Each context that you get on a canvas maintains what's called a drawing state which your code can manage. You can save the current state and you can restore a previous one. States are basically pushed onto a stack of saved states. So each time you save a drawing state, it goes on a stack and then when you restore it, the one that's currently on the top of the stack gets popped off and becomes the current drawing state.
The canvas drawing state keeps track of several properties of the canvas. It keeps track of the current values of the lineWidth, the strokeStyle, the fillStyle, the lineCaps, etc.--all the stuff that we've seen so far in how to draw shapes like rectangles and lines. It also keeps track of what's called a current transformation matrix, and we'll see that later on in the course when we use transformations. It also keeps track of the current clipping region, which also we'll see a little bit later.
So it keeps track of each of these three sets of values and you can save these and restore these. So that begs the question, well, why would you want to do this? Well, your code may have set of whole bunch of drawing settings and during the course of drawing, you might want to just make a minor tweak to one of those settings, but you don't want to have to keep track of those manually because you will have to remember, oh I changed the lineWidth here, and I can set it back to it was. Over here I changed the strokeStyle. Now I've got to set it back to what it was.
So, saving and restoring the canvas does all of that for you. Now to save and restore the state, all you need to do is use the Save and Restore functions. Essentially, you call context.save and then you can perform a new set of drawing operations, any one of which might change any of the save properties: the current lineWidth, the current fillStyle or strokeStyle, so on and so forth. And when you're done, all you have to do is called context.restore and that will put the drawing state back into the state that it was before you called Save and made any changes.
So let's actually take a look at how this works in real code because that's usually the easiest way to understand it. So let's will switch over to my editor, and in the Snippets code, I am on to the Drawing State section here in chapter 5, so let's open the example file, and we'll open drawingstate_start. So what we're going to do is go back to my snippets and we're going to copy these lines over here. For now we'll just copy those and we'll paste.
So let's see what we've got so far in the browser. Let's go over here and go to drawingstart, so we've got a rectangle that has a yellow interior and a red stroke. Now, let's go back to the snippets and copy the next group of lines over. I'll copy these and we'll paste below this. So for the moment, I'm going to comment out the calls to save and restore, and let's take a look at the effect.
So here we draw the first rectangle. It's got a red stroke in the yellow interior. Then we'll go and we change the strokeStyle to be green and the fillStyle to be blue and the lineWidth to be five. And then we create a new rectangle, and then we create a third rectangle, but we don't have any properties that were being set, so let's go ahead and save, and let's go back to the browser and refresh. So now we have three rectangles: one is yellow inside in red, and then we have two rectangles that are blue interiors and green strokes.
So let's go back to the code now. Let's uncomment those two lines. I'm going to uncomment Restore. I'm going to uncomment Save. So the call to the save function will save the current properties of the context--in this case the stroke, fill and line. Then we reset them over here and draw a new rectangle, and then we restore them. So let's now save and let's go back to the browser. So now when we refresh, you can see that the call to the restore function is restoring the drawing properties that were set originally for this rectangle before they were changed for this one.
So again back in the code. So we start off with red and yellow. Then we save that. We do a whole bunch of things here that changes the current drawing context, and then we call restore. And that pops off the stack that original context that we saved which restores the red and yellow settings, allowing us to draw with them again. So we can see how using the Save and Restore functions to manage the canvas state can save us a lot of work when we're drawing things and making minor changes all over the place.
Now, this may seem like a trivial example, but later on when you're drawing really complex drawings and you're making minor changes to the drawing context property, this really comes in handy and relieves your code from having to do a lot of manual work to keep track of drawing properties, of which there are a lot.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98382 Viewers
61 Video lessons · 85669 Viewers
71 Video lessons · 69603 Viewers
56 Video lessons · 101928 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.