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

HTML5: Graphics and Animation with Canvas
Illustration by John Hersey

Drawing images and video


From:

HTML5: Graphics and Animation with Canvas

with Joe Marini

Video: Drawing images and video

So using the HTML5 canvas element, you can even draw images and video onto canvas elements in the web page. You can do this programmatically via JavaScript. So using the image-drawing functions, you can draw images in a variety of ways. You can draw them from either an image or a video element that's either in the page or dynamically loaded, or from another canvas element that's elsewhere in the page. You can even resize or crop the source image that's being drawn. So let's take a look at the image-drawing routines.
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

Drawing images and video

So using the HTML5 canvas element, you can even draw images and video onto canvas elements in the web page. You can do this programmatically via JavaScript. So using the image-drawing functions, you can draw images in a variety of ways. You can draw them from either an image or a video element that's either in the page or dynamically loaded, or from another canvas element that's elsewhere in the page. You can even resize or crop the source image that's being drawn. So let's take a look at the image-drawing routines.

There are three ways to draw images. The first one is the simplest. drawImage takes a source image, and the source image is either an image, a video, or a canvas, and it draws that image onto the destination canvas at the point specified by dx and dy. So that's the destination point where the image will be drawn. The second version drawImage again takes the source image that you want to draw and it draws it onto the destination canvas at the location point dx and dy, but it scales the image to fit into the width specified by dw and the height by dh.

The last version of drawImage takes a source image but also takes two sets of arguments that specify how the source and how the destination should be drawn. It takes a source x and source y as well as a source width and source height. So you can actually draw only a portion of the source image onto the destination canvas at dx, dy, and the given width and the given height. So that sounds pretty complex. It's probably a lot easier to see an illustration of how each of these work. So let's take a look.

The simple version of this takes the image and dx and dy. So you have the source image, and then you have the destination canvas, and this version just simply takes the source image and copies it directly onto the destination canvas at the dx, dy point. The next version up from there takes the image and draws at the location, but optionally scales it. So once again, you have the source image. You have the destination canvas. But in this case I've drawn the image onto the canvas at the destination point. But in this case it's been scaled to be a little bit larger.

You can also scale it to be smaller. Now, the last example is a little bit more complex. So it takes the source image and optionally a portion thereof and draws it onto the destination canvas. So here you have the source image and the destination canvas right there. But now, you've got only a portion of the image that you are copying. So you copy that portion onto the destination canvas specified by the arguments that you pass. Let's take a look at these in action and see how they work in the real world.

So here I am in my example. This is the snippets, and I've got my images_start file open. So the first example I'm going to show is just drawing an image directly onto the canvas. So I'm going to copy, and I'm going to come in here and I'm going to paste. So what I'm going to do is get the image element by using getElementById and then just simply draw that image right onto the canvas. So let's scroll down here in the document, and you can see that I've got this image down here as well as a video, and this is the source for that image.

And you can see that I've got both of these elements are set to not display, so they don't show up in the web page when we have them in the browser. So let's take a look at each one of these. So, here are the images. So this is the image I'm going to be using-- that's a nice little shot of Lake Tahoe--and this video right here. So I'm going to draw this video onto the canvas, and it's just a basic video. So, that's that. So let's take a look at the first example. Scroll back up. Again, pretty straight forward. Just get the image and draw it onto the canvas.

So we'll save and you can see it works. So the image is being copied and then pasted onto the canvas. Let's take a look at another example. So for the next example, I'm going to draw the image scaled down onto the canvas using the second version of the drawing routines. So I'm going to get rid of this one to avoid any competition. There we go! Let's get rid of that comment. So now this is the second version of the drawImage function. So, same idea; draw out in a location, but scale it down.

In this case, it looks like I am going to scale it down by about half. So I'll save and refresh. We can see that that worked, and in fact let's try it in another browser. Let's open that in Firefox. Yup! So that worked as well. Let's go back to the code. All right! So for the next example, now we're going to draw just a portion of the source image onto the destination canvas. So I'm going to copy that, go back to the sample, and paste.

So what we're going to do is specify a rectangle that starts in the source image--so we have the source image right here. We're going to copy from this portion of the source onto this part of the destination canvas. So, let's save. What we're going to do is just get a copy right here. This little island is going to be what's taken out of the source image and drawn. So we refresh. We can see that that's what happened. Let's go back to Firefox, do the same thing, Refresh.

You can see that in both cases a portion of the source image was essentially cropped and drawn into the destination canvas. And then for the last example, let's actually draw some video. Now, drawing video is a little bit challenging because you can't just simply say, hey, draw this video. The drawImage function is going to draw whatever the current playback frame happens to be. So here's how we need to do this. First, let's scroll down.

So, here's the video. The video tag is right here. And you can see that it's set to loop, and it's currently hidden. It's also not playing. There's no auto-play in here. So what we are going to do is get the video element, tell the video element to go ahead and play, and then we're going to set a interval function, which is going to be called every 16 milliseconds. So every 16 milliseconds, we're going to get the canvas, get the drawing context, and get the video element, and then we're just going to draw the image from the source video directly onto the canvas.

So this will grab each frame as it's being played, every 16 milliseconds, and draw the video. So let's save. Let's refresh. So what's happening is the video element is actually down here, and this is the canvas. But the interval function is being called every 16 milliseconds which gives us the frame rate that's needed to play the video back. So let's try it out in Safari because we haven't picked on Safari yet, and we can see it's working in Safari as well. So, what have we seen? We've seen ways to draw images onto the canvas, and we've seen ways of drawing source portions of the image.

We've seen how to resize images, and we even saw how to take video content from a video element and play it back on a canvas.

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