Start learning with our library of video tutorials taught by experts. Get started
Viewed by members. in countries. members currently watching.
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.
Creating gradients using the HTML5 canvas is pretty easy. There is two kinds of gradients: there is linear and there is a radial. The way you create gradients is there are two steps involved. First, you have to create the gradient using the appropriate function to create a gradient of the right type, either linear or radial. Now once you created the gradient, you have to add color stops to the gradient at certain positions in order to create the necessary color transitions. Once you've created the gradient, it can be used anywhere a stroke or a fill style can be used, pretty much like patterns as well.
Let's take a look at how gradients work. Linear gradients are defined along a path. There is two points that define a linear gradient. The linear gradient then travels along that path from the first point to the second point. So in this example, we have a gradient defined by the starting point x0, y0, and the ending point, x1, y1. In this case, we've got a light blue color traveling along that line to the lower-right point, x1, y1. Radial gradients work a little bit differently.
Radial gradients are defined by two circles. The radial gradient travels from the edge of the inner circle to the edge of the outer circle. And in this case, we've got the inner circle of a darker blue color, and the gradient travels from the edge of the inner circle to the outer one. To create gradients, you use the createLinearGradient or createRadialGradient functions. The createLinearGradient function takes four arguments, two points. This is the first point. This is the second point, which I showed earlier, and that's the line that the gradient travels along.
The createRadialGradient function takes six arguments, which define the two circles. The first circle is centered at x0, y0 and has a radius of r0. And the second circle is at x1, y1 and has a radius of r1. So that defines the two circles, which define how the radial gradient travels. Regardless of which gradient you create, you use the addColorStop function to add color stops at various positions. The first argument, position, is a floating point number going from 0 all the way up to 1.0. You can kind of think of that as a percentage.
So, for example, a gradient at the 80% position would be at 0.8. You could add multiple color stops at different positions, and you simply define the color using any standard CSS color string to create the color. Let's go see gradients in action. So I'm here in my code. This is the gradients_start file, and this is in chapter 06. And now I'm going to go to my gradients section in my snippets. That's right here. So the first example we are going to do is create a linear gradient.
So let's just copy this example right here, and we'll paste. We've got the call to createLinearGradient right here, and this is the first point. This is the second point. Both of these points start at x of 20 and go from y 20 to 280. This is going to be a straight-line gradient, and then we need to add some color stops. We're going to go from red to blue and then blue to green. So at the 0 position, we're going to start with red and then at the halfway mark, we'll put blue in, and then we'll end up with green at the 100% mark.
Then to display the gradient, we'll simply set the fillStyle of the context to the linear gradient we just created, and then we'll fill the rectangle with the gradient. So let's save, and let's go up and look at the finished result, and you can see that it worked. So in this case, the linear gradient is going from this point here straight down to this point here. Let's see what happens when we change those two points. So back here in the code, I'm going to change this point here to be 220. So now I've got x of 20, 20, and the second point is 220, 280.
So we'll save, and we'll go back to the browser and we'll refresh. You can see now that I've got the gradient going from the top left down to the lower right. So that's pretty good for a linear gradient. Let's try radial. So let's go back to my snippets, and now we're going to copy the code that does the radial one. We'll copy and we'll paste. All right, let's take a look at the code.
So in this case, I'm now creating a radial gradient, and this is the first circle. The first circle has its center point at 525, 150 and has a radius of 20. The second circle is centered at the same location the first circle is but has a larger radius, in this case 100. So we've got two circles that are centered at the same point, but of just different radii. So now we're going to add some color stops to this gradient. We're going to addColorStop and we're going to do the same thing; we're going to red to blue to green. So red goes at 0, blue is at halfway, green is at the 100% position.
We set the fillStyle to be the gradient we just created. And then we're going to just create an arc that has the contents of the arc as the gradient. So after setting the fillStyle, when you call fill, it's going to fill it with gradient. Let's save. Okay, let's go back to the browser and we'll refresh, and you can see that we now have a circle with the radial gradient. So here is the inner circle right here filled with red, and the gradient is traveling from the edge of that circle out to the edge of the larger one. So let's go back to the radial gradient definition and offset the circle a little bit.
So we'll go back to the code, and instead of putting this circle at x of 525 and 150, let's offset a little bit. Let's put this one at y of 100 and we'll put the x, let's say, at 500. And this is going to cause the circle to move up and to the left a little bit. So let's save, back to the browser, refresh, and you can see that the circle was here. Now it's moved up here, and the gradient is a little compressed over this way and a little bit elongated this way.
So we've seen how to create linear and radial gradients, and we've seen how to add color stops and even change the directions and origins of the gradients.
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.