The Art of clipping
Like any modern drawing editor so does the canvas API provide a means to create masking in canvas. Masking is done using the clip method. In this lecture we will learn how to work with the clip method.
Keynotes:
- Creating a grid.
- Using the rect method.
- Learn to clip and restore.
Video Transcript
00:00This lesson's main topic will be masks or as it's known in canvas as
00:05Clipping now what we're gonna do actually we're gonna create a grid which is be will be a practice really a relatively complicated
00:12Practice and we're also gonna look at the rect method a method
00:15We didn't meet until now so far we worked with the fill rect or the stroke rect
00:20Well this time around we're gonna look at the rect method and see how to work with it
00:24but really the main topic will be is how to create a
00:27Mask and how to restore back after we're done with the mask to return back to drawing normally by the end of this lesson
00:34You'll understand what clipping is and restoring is so let's jump right into it
00:39We have a lot to do. So we're gonna jump right into it. And what we're gonna do is we're gonna create a clipping
00:45Scenario where you're clipping items. It's really cool. We're doing a lot
00:48So we're gonna just go for it and you're gonna get what's going on as we tag along
00:53Now I'm gonna create a size because what I'm trying to do is I'm trying to create a checker
00:57It's kind of view on our canvas. So I want each each each a square to be 20 by 20
01:04Now that I've got that configured the next thing I really need to do is I need to decide or find out
01:09How many how many squares because I could I fit within our width to that?
01:13I'm gonna just divide my width by the size, but I really want this to be an integer
01:17So I'm gonna floor that value and I'm gonna no matter what just add one to it
01:22I could also seal it but I want to make sure that I have one extra ish no matter what so I'm using a floor
01:28I'm gonna go ahead and also do the exact same thing for our height and that's gonna give us the amount of items really
01:35That we want to have inside of our element. Okay, so this is gonna be the index width or index height variables
01:41Beautiful now that we have that configured the next step is we really want to start creating a pattern now
01:48I'm gonna do it in a really really complicated way just for the sake of practice because we're getting close to the end
01:54So let's go ahead here and let's set a context. We're gonna set a begin path and
01:59I'm gonna go to my context and I'm gonna set a fill style and I'm gonna go ahead and start only with the black parts
02:061 2 3 4 5 6 we got ourselves a nice black fill
02:10The next step is I I'm gonna create a function that does exactly that it will just draw out patterns because I'm gonna call it
02:17Twice. So let's go ahead there. I'm gonna called it. I'm gonna call this generate pattern and it's gonna get a context
02:24It's gonna get the amount in width amount and height is gonna get the size of the items
02:28And I'm also gonna send one item that's gonna be used as an offset because when I'm gonna draw it in another color
02:34I'm gonna want to have an offset of zero or one. I'm gonna go ahead and also finish this up
02:39I'm gonna go to the context and close that path and I'm also gonna go ahead and context fill it out
02:45All that's left for me to do is to go ahead and generate this function. So let's go ahead and do that function
02:51and
02:54What will we call this? Let's go ahead here. Let's figure out a good naming convention. So I'm gonna just gonna call this width
03:01Height even though it's not exactly that it's numbers that represent and we're gonna call this offset
03:06Okay. So now that we got all our variables configured
03:10I'm gonna go ahead and call the for loop and loop through all the items in index I
03:15Okay, so as long as it's smaller than our width
03:17I'm gonna grow by two because I want to skip one item each time and then for our height
03:23I'm just gonna go ahead and go on onto every single item inside of the height
03:27So I'm just gonna go ahead here to our var J
03:31equals 0 as
03:32Long as our J is smaller than our height
03:35We're gonna go ahead and get our J to be larger by one each time
03:40Beautiful the next step for me to do was what I is literally go ahead and start drawing out our rectangle instead of using the fill
03:49Wrecked or stroked wreck. I'm gonna go ahead and use something new that we haven't seen which is the wreck
03:54Which is literally drawing out just the lines in theory without actually committing until we call the fill or stroke commands
04:01Which we're gonna do later after the generate pattern which is done literally right after that when we call the fill
04:07So the wreck gets this a similar type of construct the x and y coordinates and then the width and height
04:13So let's start with the x coordinate, which is probably the most complicated one
04:18Let's start with I times the size
04:21That's gonna be our x coordinate and our J times the size would probably our y coordinate
04:27And then we're gonna set the size and size for the actual size of our item and width and height now
04:32If I just do this on its own, it's gonna work, but it's not really gonna work
04:36It's gonna work, but it's not gonna really gonna work cuz we're not offsetting at all
04:40And what we need to do is each time we're going into an odd line
04:43We really want to push the item one of the items a little bit out of shape to do that
04:48I'm gonna go ahead and focus on this eye area on our x coordinate
04:52And I literally want to basically add inside of here not only this eye
04:56I'm gonna go ahead here and kind of put this in a bracket. I also want to add into it something
05:01What do I want to add?
05:02I want to add something that will be every time it will run. I want it to be 0 1 0 1 0 1 to do that
05:08I decided to do something very weird, which will be taking our J, and I'm gonna just take the J itself and
05:16Well, that's all I need
05:17I'm gonna take the J itself and I'm gonna mold to making sure that the value it's gonna is always gonna be
05:22Basically because our J is going up by one each time is gonna be 0 or 1 0 1 0 1 just by doing this
05:28On its own. I'm already gonna get a really interesting construct
05:32That is gonna be very very similar to what I'm actually looking for which is that
05:36Checker its pattern now the only problem I have is if I go back into my code right here
05:41And I just grab this whole thing and just duplicate it and this time around
05:46I want to set it to be with a color that is white and I want to set here an offset
05:51Although I didn't configure the place where that offset would go if I go ahead and run this we'll see that the white
05:56Area will just go on top of the black area, which is not what I want
05:59So to be able to solve that I'm gonna have to use that extra variable of offset and with this extra variable of offset
06:06What I want to do is I'm gonna add it into that J
06:08Because I'm basically I want to account that offset forever whenever I'm working inside of a line only
06:13So when I only when I'm controlling the X not when I'm controlling the Y as well now by doing that
06:18I'm gonna get your 0 1 0 1 but I'm gonna get a start off
06:21That's gonna be offset by 1 if 1 is being sent making sure that it's the opposite of if it would be sent without that
06:28So if I go ahead here and run this just one more time
06:30We'll see now we're gonna get a full checkers board the way we planned and wanted it to be from the beginning
06:36Now before we finish this up this whole lecture was about clipping and although we didn't really do any clipping yet
06:42Let's go ahead and do clipping because that's really the point of this lecture
06:46So I'm gonna go ahead and what I want to do is I'm gonna go ahead and I'm just gonna come here just
06:51Before I start all these drawings. I want to define what is gonna be the clipping region to do that
06:57I'm gonna go ahead and draw a triangle and
07:00I'm drawing this triangle. I'm gonna use the context. Remember that function we created long ago
07:04It's gonna start from our 0 0 coordinates and I'm gonna go ahead here and just set here
07:10The next coordinate will be the width and half of the height and the next coordinate
07:16I'm gonna use is gonna be 0 and the full height
07:19So I'm literally going through and creating a triangle last but not least it expects that to get a color
07:25So I'm gonna send there 1 2 3 4. I'm gonna send there the color white now if I run this
07:30It's not gonna be seen right now
07:31So I'm not gonna even run it because everything that happens below is gonna override it
07:35But what I want to do is I want to go right after I draw it out and I want to define a clipping area
07:41Now once I defined a clipping area if I go ahead here and click here on refresh
07:45You'll see that now the drawing will only happen within the region of the triangle that we created before
07:51So all the things that we drew previously
07:54Once I call clip turn in to be the mask that then controls everything that happens afterwards now if you're asking yourself
08:01Well, that's lovely
08:03But I want this clip to only happen to this part and once we get here
08:06I really want to just draw things out completely to do that
08:11I need to have a reference to our original context to do that before I draw a clip before I do anything
08:18I'm gonna go to my context and save it what that does
08:21It saves a reference to how it's supposed to be before we created a clip
08:26Now that I have a reference to that original point because I saved it
08:29I could at any stage after the clipping just call the context and ask to restore and then create another clip if I wanted to
08:37Once I restored my item everything is gonna happen here is gonna be inside of the larger larger clipping area
08:44So if I go ahead here and click here on refresh, you'll see the black areas are gonna be within that
08:50Flag area, but the white areas are gonna bleed out outside because they're not bound by a clip anymore
08:58In this lesson we were introduced to clipping and from this point onwards
09:02We're really talking about manipulating the canvas itself to create the elements that we want in the next lecture
09:09We're gonna take a look at transforming the whole canvas based on a configuration that we're gonna look into in the next lesson
