Start learning with our library of video tutorials taught by experts. Get started
Viewers: in countries Watching now:
One of the most exciting additions that HTML5 offers to designers is the ability to draw free-form graphics on a drawing surface known as the Canvas. In this course, author Joe Marini introduces the technical concepts behind Canvas and shows how to perform drawing operations directly in a web page. The course covers drawing basic and complex shapes, setting colors and styles, adding shadows, patterns, and gradients, more advanced techniques such as scaling, rotating, and compositing objects, and how to incorporate Canvas elements in a slideshow and an animation.
So let's see how we built this. So I am going to open up my start version of the file, and what we are going to do is we are going to take a look at the snippets code. So for the simple animation, let's go ahead and copy over the first few lines which initialize everything, and we will paste that in there. So I have got some initialization logic here. I have got some variables for the rectangle. The Y value is 200, the width is 40, and then I've got an X position which is equal to the initial value of the rectWidth, but I negate that so it's -40.
And that so the rect appears completely off the screen when it first gets drawn. So let's go back to the snippets. So let's copy the initialization function over. We will copy this here. So in the initialization function, we get a reference to the canvas and then the context, and then we call a function called blank. Blank draws the background which we will copy over in a moment. Then we have the logic which draws the rectangle. So we have a fillStyle of yellow, and then we fill the rect with the yellow content right here.
Let's go ahead and copy that over. So I am going to copy over the blank function. So you can see that the blank function simply fills the canvas. I have got a nice light blue color right here, and it fills the rectangle which is from the upper left to the width and height of the canvas. And notice, by the way, that I am using the built-in canvas properties, like width and height, rather than hard-coding the values which are down here in the document.
That way if I ever want to change the height of the canvas or make any other changes, I can just change it in one place and then my code automatically picks up the difference; I don't have to worry about changing numbers in more than one spot. So that draws the blank canvas background, and then we've got drawing the rectangle. Let's go back and copy some more code over. This is the function where the animation happens. So let's go ahead and copy that.
So the animation function, you can see up here, we set an interval every 30 milliseconds, which is about 30-ish or so frames per second, to call the function labeled anim, which is down here. So every 30 milliseconds this function will be called, and we check to see if the X position of the rectangle is less than the width of the canvas, and if it is, we then draw the blank background, we move the rectangle by five pixels, and then fill it again.
So we fill it with yellow, we stroke it with red and a lineWidth of 3, and then we fill and stroke the rectangle. Now if the rectangle has moved beyond the canvas width, if the X and Y position is greater than the width--in this case, it is just the X position-- we then reset it back to being a negative value of the rectWidth, which will place the rectangle, again, back off the screen. And the reason we do negative width is because we want the X position to be far enough to the left that none of the rectangle peeks through on the left side of the canvas.
So to recap, we've got some initialization logic. The first thing we do in the initialization function is draw the blank background, set up the initial rectangle, and then start the animation. The blank function just fills in the background, and then every time through the loop, we draw the background, draw the rectangle. And each time we were drawing the rectangle, we are moving in over by five pixels. So the effect that the user sees, because this is happening so quickly, is the blank background gets drawn, which paints over the rect. We move the rect by five pixels and then we fill it again.
Now the example of animation that I am showing here is called single buffering, and the reason it is called that is because all the animation is taking place directly on the canvas that's in the page. There's no buffer where we are drawing everything and copying it onto the canvas. So let's run this in the browser, save, and we'll go to start. And you can see that what's happening is the rectangle is moving across the screen. So it's happening too fast for you to see it, but the blank background is being drawn each time. The rectangle is moved and then painted in its new location.
Now this looks pretty smooth because there's only one moving object, but if this animation contained a lot of moving objects, we might start seeing things like flicker, because all of the objects are being erased, the background is being drawn each time, and then everything is being drawn on the canvas right here directly in the page where the user can see it. So what we see in this movie is an example of creating simple animation with what's called single buffering. But later on in this chapter, we will see an example of double buffering, which makes it easier to draw multiple objects on a canvas, and makes the animation appear much smoother to the user.
There are currently no FAQs about HTML5: Graphics and Animation with Canvas.
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.