Start learning with our library of video tutorials taught by experts. Get started

HTML5: Graphics and Animation with Canvas
Illustration by John Hersey
Watching:

Introducing the Canvas tag


From:

HTML5: Graphics and Animation with Canvas

with Joe Marini

Video: Introducing the Canvas tag

Let's begin by introducing the canvas element. The canvas element provides web pages with a place where they can draw free- form graphics of all kinds: shapes, images, text, lines, all kinds of objects. Just think if it as a piece of paper on the page where your script code can just draw anything it wants. The canvas tag is supported in all of the latest browsers, and I have listed them here. So as long as you have a modern version of that particular browser, it should support the canvas tag. Now, browsers that don't support canvas can display fallback content.
Expand all | Collapse all
  1. 4m 54s
    1. Welcome
      1m 3s
    2. Using the exercise files
      2m 3s
    3. Using the HTML5 Canvas element in the real world
      1m 48s
  2. 10m 31s
    1. Real-world example: CanvasMol
      2m 9s
    2. Real-world example: Raphaël-JavaScript Library
      1m 47s
    3. Real-world example: The Wilderness Downtown
      4m 1s
    4. Real-world example: Sketchpad
      1m 10s
    5. Real-world example: Pirates Love Daisies
      1m 24s
  3. 3m 28s
    1. Installing the tools
      1m 29s
    2. Exploring the Canvas examples used in this course
      1m 59s
  4. 8m 58s
    1. Introducing the Canvas tag
      6m 30s
    2. Understanding the differences between Canvas and SVG
      2m 28s
  5. 5m 36s
    1. Identifying the Canvas element's methods and properties
      1m 40s
    2. Using the Canvas drawing context
      3m 56s
  6. 43m 14s
    1. Setting and using colors and styles
      3m 19s
    2. Drawing basic shapes: Rectangles and lines
      10m 21s
    3. Understanding the Canvas state
      5m 15s
    4. Drawing complex shapes: Arcs and paths
      9m 15s
    5. Drawing complex shapes: Bézier and quadratic curves
      5m 46s
    6. Rendering text
      9m 18s
  7. 32m 35s
    1. Creating shadows
      6m 41s
    2. Drawing with patterns
      7m 20s
    3. Drawing with gradients
      6m 18s
    4. Using clipping paths
      4m 46s
    5. Drawing images and video
      7m 30s
  8. 35m 42s
    1. Transforming objects using the translate tag
      4m 18s
    2. Scaling objects with the scale transformation
      4m 7s
    3. Rotating objects with the rotate transformation
      4m 33s
    4. Applying a custom transformation
      6m 58s
    5. Compositing in Canvas using globalAlpha
      6m 36s
    6. Manipulating raw pixels
      9m 10s
  9. 41m 23s
    1. Building an image slideshow control
      4m 24s
    2. Using smooth transitions in a slideshow
      4m 28s
    3. Creating a basic animation
      5m 42s
    4. Creating animation with double buffering
      13m 13s
    5. Incorporating Canvas into a real page
      13m 36s
  10. 48s
    1. Goodbye
      48s

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
Please wait...
HTML5: Graphics and Animation with Canvas
3h 7m Intermediate Jun 03, 2011

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.

Topics include:
  • 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
Subjects:
Developer Web Animation Web Design Web Development
Software:
HTML
Author:
Joe Marini

Introducing the Canvas tag

Let's begin by introducing the canvas element. The canvas element provides web pages with a place where they can draw free- form graphics of all kinds: shapes, images, text, lines, all kinds of objects. Just think if it as a piece of paper on the page where your script code can just draw anything it wants. The canvas tag is supported in all of the latest browsers, and I have listed them here. So as long as you have a modern version of that particular browser, it should support the canvas tag. Now, browsers that don't support canvas can display fallback content.

So if you have a browser that doesn't understand what a canvas is, you can place content inside the canvas tag that will be displayed to the user when their browser tries to render it. Pages can actually have more than one canvas and in fact, you can even overlap them. One of reasons why you might do this is because canvases can be set to be transparent. So you might have one canvas that contains say the background for a game and another canvas that has the characters in the game and so on and so forth, and you can position them on top of each other. So you can have more than one canvas, and it might come in handy to have more than one canvas.

Now it's important to know that canvas contents are created on the fly using script. Now what this means is once you've drawn stuff onto the canvas, there is no going back to get it again; it doesn't become part of the page DOM. So the bits are on the screen, and if you want to remember them, you have to care of it yourself. Once they're drawn, they are just forgotten about by the script engine. They're good for dynamic visual media, but the canvas is not good for things that you might want to manipulate later using script, and that's what SVG is for, and we'll take a little bit of a peek at that later.

Let's take a look at the canvas tag. The canvas coordinate system starts at the upper left, and it has two axes with increasing values of X going from the left to the right and the Y axis going from top to the bottom. All canvas element start out invisible, and that means that they are a transparent black background. So, there is the black canvas with the opacity set all the way down to 0, so they can't be seen. This graphic shows you what the coordinate system looks like. So on the left-hand side there we have the Y axis, increasing in values from top to bottom.

We have the X axis going from left to right, and we have 0, 0 point up there in the corner, and the width and height down on the lower right. The canvas tag is declared using the word 'canvas', and you typically provide it with an ID, because you'll be accessing it from script, and a width and a height. And that Fallback content you see there inside the canvas tag is what will be displayed to the user if a browser does not understand what a canvas is. Canvases come along with some attributes and functions as well, and here they are. There is a width and a height, which defined the width and the height of the canvas, and there is a couple of methods.

There is toDataURL and the getContext. So let's take a look at each one of those. The width and the height properties are pretty much self-explanatory. They are the width and the height of the canvas, and if you don't define them, they default to 300 wide by 150 height. And it's important to note that, as well as getting the values of these properties, they can be set. So you can dynamically change the width and the height of the canvas from your code. The toDataURL method takes the contents of the canvas and makes a static image out of them as a data URL.

Now to do this, you pass in the type of image you want the toDataURL method to create, such as image/png, and that support is mandatory according to the W3C spec. But some browsers may choose to support other formats like image/jpeg, or image/gif that kind of thing. So this method basically takes the contents and gives you back a Base64 encoded data URL of the contents. The getContext method is probably the more important ones. It retrieves the drawing context for the canvas, and the drawing context contains information about the canvas and provides all of the methods you use to draw with.

Things for setting colors and stroke styles and drawing shapes and so on, all of that is done using the context. So, let's actually switch over to our code editor and build our first canvas. Okay, so here I am in Aptana Studio and I'm going to open up the example files. So if you go into the Exercise Files folder, I'm going to go into the folder for this lesson and open up the start. This is our Firstcanvas_start.html. I'm also going to open up my snippets file, and I'll be referring to this throughout this course.

Essentially, this file contains all the code snippets for each one of the examples in order to go from the start state to the finished state, and I'm going to use these so that you guys don't have to watch me type. And if you have access to this file, you can follow along with me in your own code editor. So what we're going to first do is put a canvas into this document. I am going to put it right here in the body. So I'll go to the Snippets, and I'll take this canvas and I'll paste it in. So let's save. So here is the canvas tag, here's it's ID, and I've got a width and a height on there.

And here is the fallback content right there. It just says, "Your browser does not support canvas." All right, let's take a look at this in the browser. So we'll go out here. I am going to double-click, and you can see that nothing shows up. Remember, I said that canvases all start out as invisible. So we need to do something to make this canvas visible. Let's go back into the code. I am going to go back to my snippets and I'm going to copy this style declaration right here. And just like other HTML elements, you can define styles that affect canvas tags.

So I am going to copy this and go back to my source and paste that here in the head section. So I've defined a style for the canvas with this ID-- that's this guy right here--and it has a border which is a dotted 3-pixel black outline and a light gray background-color. So let's save, and let's go back to the browser, and let's refresh. Okay, now you can see that the canvas is visible, so it has a dotted border here, and the background is light gray.

So let's see how this looks in another browser. This is IE. Let's open this up in Firefox, and you can see it looks the same. Now we know how to declare a canvas tag. We know how to style it and make it visible. Let's take a look at how canvas compares to SVG, and then we'll move in to some actual drawing.

There are currently no FAQs about HTML5: Graphics and Animation with Canvas.

Share a link to this course
Please wait... Please wait...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.
Upgrade now


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

join now

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed HTML5: Graphics and Animation with Canvas.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Welcome to the redesigned course page.

We’ve moved some things around, and now you can



Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked