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.
In this chapter, we're going to look at some pretty advanced canvas operations, and in fact some of these are not necessarily routines or features that cause any drawing to happen, but they certainly do have a profound effect on the results of drawing operations that you learned about earlier on in the course. And so we're going to start off by looking at transformations. Transformations are essentially a way that affect how objects are drawn to the canvas to achieve some pretty common, but yet difficult to program, effects.
There's three basic transformations that the canvas provides. There's translate, scale and rotate, along with a way to define your own free-form transforms. Transforms affect all of the drawing operations that come after them, and they are additive. Each transform is added to the effects of the previous one. So just like other global canvas properties, like line width and so on, transforms affect everything.
You define a transform, you use it, and then you perform some drawing operations. Now, because they are additive, this is where saving and restoring the canvas state can really come in handy. Let's start by taking a look at the translate transform. It's the simplest one to use, and all it really does is moves the canvas origin to a new location. And the way you do it is by calling the translate function with an amount to move the origin in the X direction and an amount to move the origin in the Y direction.
So let's imagine for a moment you have a canvas and at the upper left of the canvas, that's the current origin point. That's where 0,0 is. If you use the translate function to move the origin by an amount X and Y, then all you have done is move the location of the origin. So now, anything drawn at 0,0 will be drawn at this point on the canvas instead of this point. So let's take a look at how that works. I'll go into the code. So here we are in my editor, and in the snippets file, I'm under the Translate Transform section.
So let's open up our example, and we're going to go to the Advanced folder, and we're going to go to the translate_start example. So what we're going to do is copy some code, and we're going to come in here and copy this over, copy, and we're going to paste. So before I show you what the example does, let me just comment out this call to translate, and we will save and we will go over the browser and look at the results.
So, let's bring this up. So you can see that I'm drawing a blue rectangle at the origin point 0,0 right here. So if we go look in the code, you can see that there it is. It's at 0,0. It's 100 pixels wide and 50 pixels high. Now, if I uncomment the call to translate, what I'm doing here is calling translate and I'm getting the width of the canvas and dividing it in half, and I'm getting the height of the canvas and dividing it in half. So this will pretty much move the origin of the canvas right to the middle, half the width and half the height.
So when I call fillRect again, even though I'm calling it to draw at the 0,0 location, the origin has now been moved to the middle of the canvas. So the second rectangle will be drawn at the middle point of the canvas. So let's save, and let's go back to the browser, and let's refresh. So you can see, I've got two rectangles. They are both drawn at 0,0. But in the first example, 0,0 is up here, and then after the call the translate, 0,0 moves to here.
So this can be pretty useful when you are creating a whole bunch of objects to be drawn on the canvas at different locations and the results of those locations are arrived at via various calculations and so on. Rather than having to keep track of all of those points, you can just simply move the origin to where you need it to move to, do your drawing, and then set it back to its previous state. So in this example, what we've seen is how to use a basic translate transform to move the origin around and affect where objects are drawn.
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.