Ready to watch this entire course?
Become a member and get unlimited access to the entire skills library of over 4,900 courses, including more Developer and personalized recommendations.Start Your Free Trial Now
- View Offline
- Understanding the differences between Canvas and SVG Graphics
- Drawing shapes
- Drawing arcs and paths
- Rendering text
- Using clipping paths
- Drawing images and video
- Transforming objects with the translate tag
- Manipulating raw pixels
- Applying a custom transformation
- Creating an animation or slideshow control with Canvas
Skill Level Intermediate
The HTML5 canvas element makes it easy to draw using patterns. In fact, drawing operations that use fill and stroke styles can take patterns, as well as gradients, as settings. The way you create a pattern is from an image, a video, or another canvas element. So if you use an image that's an animated image, the pattern that you create will use the poster frame of the animated image as the pattern. And if there is no poster frame in the animated image, it will use the first frame of the animation.
For video, the current playback frame is used as the pattern. It's not the live video; it will actually grab a frame and it will use it as the pattern. So at whatever point you create the pattern, then it will choose whatever frame happens to be drawn at that point and use it. Patterns can be set to repeat. They can either repeat not at all, or in both dimensions, or only in X or Y dimensions. So to create a pattern, it's pretty simple; you basically use the createPattern function.
So createPattern takes two arguments. The first one is an element and that has to be either an image, video, or canvas element. The second argument can be one of these four strings: no-repeat, repeat, repeat-x, or repeat- y--pretty much just like any other CSS background property you would set on an image. All right, let's jump over to the code and actually take a look at patterns in real life. Okay, so here I am in the code. I'm at my Patterns section in my Snippets file, and I've got my patterns_start file open from chapter 06.
So let's start making some patterns. The first thing I want to do is create a pattern from an image. So I'm going to copy this example right here and paste it in, and I'll save. So to create a pattern from an image, I'm actually going to create a new image object using the DOM and then load the image in. Now, because images might load slowly over the network, if I try to just simply create a pattern right away, that might give me an error because the image hasn't loaded yet.
So to prevent that, I'm going to use an onload function here. So when the onload function completes, that's when my createPattern method will be called, and then we'll fill the rect of the canvas with a rectangle that uses its fillStyle as this image pattern. So the image we're going to use is this one right here. And if we look at the examples, it's this little image right here. We're going to tile the background on the canvas with this image. So let's go ahead and do that. So what we do is we create the image. Then we set the onload function. Then we set the source of this image to be the path to that image. That will cause the image to load. And then when the load completes, this function will be called.
Then we tell the context to go ahead and use the fillStyle, set the results of the createPattern function into the fillStyle, and then we fill the rectangle, starting at the upper left of the canvas and all the way across the canvas to the width and height. So let's save this and try it out. And you can see that we've got a nice little repeating tiled image here, and now if you just say repeat-x, save, refresh, right, we only get the X direction.
Let's go back and change that, and let's try it in one other browser. We'll try it in Firefox. And you can see the results are the same. So, now let's make another pattern and let's get rid of this one to avoid competition. So in this case, now we're going to create a pattern from a video. So I'll copy this over, and I'll show you what we're doing. So down here in the document, I've got a video. This is an HTML5 video element with an ID, and it's pointing at this source right here, and it's hidden from view.
So let's take a look at the source. It's this video right here. So I'll bring this up in the player. Let's take a quick look at it. Okay, so this is the video that we're going to use as our pattern. Now to use the video as the pattern, remember that when we create the pattern, it's going to grab the current playback frame. So what we need to do is set a timeout function in order to grab a pattern that's a few seconds into the video. So I'm setting a timeout that's waiting 3000 milliseconds, or 3 seconds, and when the timeout function completes, my resulting function is going to go get the video element from the document, get the canvas, and get the context, and then create a pattern from the video element and set that as the current fillStyle of the context.
And then when I call the fillRect function, again, covering the entire canvas, the rectangle will be filled with repeating images of whatever frame was playing at 3 seconds into the video. So let's save, and let's put a little autoplay on the video right there. That way it plays automatically. And we'll bring it up in the browser. Okay, the video is playing. 1, 2, 3. All right! So, at 3 seconds, you can see that what happened was the frame got grabbed at the 3- second index and was filled into the entire canvas right here. And because of the repeat, the image is repeating across.
So it's creating a pattern from video. Let's go back and let's get rid of this one, and we'll take that off as well. The last example, I'm going to create a pattern from another canvas, and that's this one right here. So we'll copy that, and we will paste. So let me explain what's going on here. Once again, we look down at the document, and I've actually got two canvasses.
Here is the example canvas that we've been using throughout the course that we've been drawing our results into. I also have another canvas down here with a width and height of 25. And I'm going to draw some content into that canvas and then use it as a pattern. So the way I do that is I get a reference to the patcanvas and its context and I set the strokeStyle to red and the lineWidth to 1. I'm drawing a 1-pixel line from the top left of that canvas down to the bottom right.
So I've got a diagonal red line, and then I stroke it. That way we get to the actual drawing of the line. So now I've done that. I can use that canvas as a pattern. So I have a variable here called strokePat, and I tell my regular canvas to go ahead and create a pattern out of the patcanvas. And I'm passing in repeat. Then I'm going to set the strokeStyle--no fillStyle this time, this time it's a stroke--to the strokePattern. I am going to set the lineWidth to be 25 to make sure it shows up. And then I'm going to stroke a rectangle with these dimensions, and it's going to have a stroke width of 25.
So let's go ahead and save and let's refresh, and you can see here is the little source canvas over here, right, with its red line in it, and you can see that when I stroked the rectangle, that pattern is now there. Let's do the same thing in Firefox, and we'll see it works. All right, so we've seen three examples of how to create patterns, using images, video, and even another canvas as our pattern source.