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

HTML5: Graphics and Animation with Canvas
Illustration by John Hersey

Using clipping paths


From:

HTML5: Graphics and Animation with Canvas

with Joe Marini

Video: Using clipping paths

The HTML5 canvas element gives you the ability to create what are called clipping paths. You can think of a clipping path as basically a mask. It defines a region or a boundary inside of which drawing will take place and outside of which drawing will have no effect. Now initially, the entire canvas is, by default, the current clipping path, which means anywhere you draw on the canvas, as long as it's visible on the screen, those bits are going to show up to the user when you draw. You can create a clipping path using any path, and it's pretty easy. There's just one function to do, so you draw out a path normally, and then you call the clip function, and everything just works.
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

Using clipping paths

The HTML5 canvas element gives you the ability to create what are called clipping paths. You can think of a clipping path as basically a mask. It defines a region or a boundary inside of which drawing will take place and outside of which drawing will have no effect. Now initially, the entire canvas is, by default, the current clipping path, which means anywhere you draw on the canvas, as long as it's visible on the screen, those bits are going to show up to the user when you draw. You can create a clipping path using any path, and it's pretty easy. There's just one function to do, so you draw out a path normally, and then you call the clip function, and everything just works.

So imagine we had a photo that looks like this. We applied a clipping path that looks like this. So the white area is where the image will show through, and the black area is where the image will be blocked out. If we were to draw this image on the canvas using that clipping path, it would look something like this. So, only the part of the image that's shown through the open area would actually be drawn onto the canvas. Now to do this, you use the clip function. The clip function creates a new clipping region by calculating how the current clipping region intersects with the area that's defined by the current path.

And then the new clipping region simply replaces the current clipping region, because each context can only have one current clipping region. Now, to do this in code, it looks something like this. Here is a canvas, and imagine you have some code that looks like this. So we define a path, and then we make an arc, and then we call stroke. That usually gives us a circle that looks like this. If we now go ahead and call the clip function on the canvas context, now drawing will only take place inside of that arc. So let's take a look at this in code.

So here I am in the editor, and I've got my snippets opened to my clipping paths region. I'm going to go ahead and open up my clipping_start example. So here's what we're going to do. First, let's go to the snippets, and let's copy the first few lines over. We'll copy that one, and we'll paste it in. Then we'll scroll down a little bit, and we'll get the drawImage call. We'll copy that, and we'll paste that in. So let's save, and let's see how this looks in the browser.

So I'm going to bring up clipping_start in the browser. So you can see that the current clipping region right now is the entire canvas; nothing is being blocked out. So the entire image is filling the canvas. So let's go back to the code and that image, by the way, is down here in the document. So I'm just getting the image and drawing it onto the canvas. We've seen this already; nothing new here. Let's go back to the snippets and let's try creating a clipping path. Let's start by making a circular clipping path. This is what I just showed in the slides.

So I'm going to paste that in, and I'm going to uncomment these two lines. So before we run the code, let me point it out. So this code is going to draw an arc at the center of the canvas. So we divide the Width by 2, divide the Height by 2. That gives the middle of the canvas. It's got a radius of 150 pixels, and it's a full and complete circle. So that gives us a full circle in the middle of the canvas, and then this function call right here is the important one. That creates a clipping region out of the current path. So let's go ahead and do that.

We'll save, and we're going to refresh. And you can see that the results of that, we have a circle in the middle of the canvas, and only the part where the circle is is where the image is being drawn; the image is being masked out on either side. So that's a pretty simple, easy example, but let's make it a little bit more complex. So let me go back to the code, and let's go and comment these two lines out again. Let's go back to the snippets. Now, let's create an arbitrary clipping path.

So using what we already know and have learned about creating paths can be applied to clipping paths. So here, we have a call to beginpath, we have a series of lines, then we close the path, and then we call clip. So this is going to create a clipping path out of just some arbitrary path that I've made with a few line segments. There's going to be four line segments: one, two, three, and then closePath will make the final one. So let's save, and let's click the Refresh button. You can see that now the clipping region has been changed to the four points that I've created using a path.

So I took an ordinary path, didn't stroke it--although I could if I wanted to-- and I made a clipping region out of it. Let's see if it works in other browsers. So let's go ahead and open this in say Firefox. You can see, the result are the same. So in this example, we saw how to take ordinary paths and create clipping regions, which are masks, through which drawing can take place and outside of which drawing is prevented.

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

 
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

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 Upgrade 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
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.

Are you sure you want to delete this note?

No

Notes cannot be added for locked videos.

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
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.