New Feature: Playlist Center! Pick a topic and let our playlists guide the way—like a learning mixtape.

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

HTML5: Graphics and Animation with Canvas
Illustration by John Hersey

Creating shadows


From:

HTML5: Graphics and Animation with Canvas

with Joe Marini

Video: Creating shadows

In this chapter we're going to take a look at using some more complex canvas drawing techniques, and the first one we're going to look at is creating shadows. So there are four attributes that you can use to draw shadows on the drawing context on the canvas. And all drawing operations that take place on the canvas are affected by these shadow attributes. This includes paths, images, text, lines, whatever. When you draw shadows, they can be colored, they can be offset in both the X and Y direction, and they can have a blur value.
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

Creating shadows

In this chapter we're going to take a look at using some more complex canvas drawing techniques, and the first one we're going to look at is creating shadows. So there are four attributes that you can use to draw shadows on the drawing context on the canvas. And all drawing operations that take place on the canvas are affected by these shadow attributes. This includes paths, images, text, lines, whatever. When you draw shadows, they can be colored, they can be offset in both the X and Y direction, and they can have a blur value.

And in fact, these right here are the available properties to use when drawing a shadow. So the first one is the shadowColor, and that's the color to use when drawing the shadow. And again, like other places where you can set a color, you can use any kind of CSS color string to specify a color. Now if you don't specify one, it defaults to transparent black. The OffsetX and OffsetY properties are pretty self-explanatory. They control the horizontal offset of the shadow, which defaults to 0, and the vertical offset, which also defaults to 0.

And there is a Blur factor that you can set on the shadow as well. This defaults to 0, but it has to have a value that's greater than 0 in order to have any visible effect. So let's jump over to the code and take a look at drawing shadows. Here we are in the code, and I've got my shadows_start file opened up, and that is located in the folder for chapter 06. So let's just take a quick look at that in the browser, take a look at shadows_start.

So we start off with an empty canvas, which is only visible by its dotted outline, and we'll go back over to the code. In my Snippets file, under the section for Shadows, I am going to copy some code over. So this first set of lines here basically sets up some basic shadow settings, so I'll copy those and I'll bring those over here in the section where we've tested for the canvas and gotten the context. We're going to set some shadow settings, so here you can see I'm setting the shadowColor to black and the Offsets to 10 pixels in both X and Y, and I'm giving it bit of a Blur factor.

And then I'm going to copy the first example. So let's copy that and bring it over. So, in this case, we've created a rectangle using the fillRect function right here, along with a fillStyle. And the fillStyle is basically going to fill this rectangle with a blue color, at 100% opacity, and make a rectangle 200 pixels wide and 100 high. And because we've set up some shadow properties here, it will draw a shadow behind the rectangle.

So let's go back to the browser, and we'll refresh. And you can see that there's the blue rectangle, and there's the shadow behind it. And let's take a look at this in some other browsers. So we'll just right-click and we will say Open with. In this case, let's take a look at Firefox. Okay, there's the same effect in Firefox. There's the shadow. Let's take a look in Opera. And you can see there's the shadow around the object in the Opera browser. Let's just do one more. Let's go ahead and look at this in Chrome.

And there it is in Chrome. There's the shadow around the object right there. Let's go back to the code. Let's get another example. In this case, let's use some text. Let's see what drawing a shadow on a text looks like. So we'll come back and we'll paste this in. So in this case, we have a text string, and we are setting the fillStyle to be green. And here I'm changing the shadow colors.

So in this case, the shadow is a bit of a combination of the green and blue colors at half opacity. I've changed the Offset, so I have changed the Blur, and I've also set the font size to be a little bit larger. So when we go to fill the text, we should see a different color shadow behind the text. So let's save. Let's look at it in IE. And sure enough, you can see there's the text. There's the shadow behind it. And let's go back to Firefox. Do the same thing. Yup! That works. And let's look at it in Chrome.

Sure enough, it all seems to work just fine. Let's go back to the code and let's look at another example. In this case, we're going to do it on a line. So we'll copy, and we will paste. So in this case, now we have a line, and I've got a round line cap with a width of 25. And once again, changing some of the shadow properties, in this case, making more of a purple color with a higher Blur number. And the strokeStyle is going to be red.

We're going to have a red line with a round line cap that's 25 pixels wide. So we begin our path, we do the moveTo and lineTo, and when we call the stroke function. That is when the shadow gets created. So let's save. Let's refresh IE. You can see there it is, and there's that little purply kind of shadow behind it. And let's do the same thing in Firefox. Yeah, it works fine. Let's go to Chrome. Yeah, works fine there too. Before we go, let's just take a look at some things we can do with shadows.

What I'm going to do is I'm actually going to take the shadowOffset here and down here, I'm actually going to make these negative numbers. So I'm going to do -15 and -15, and we'll save. Okay, now let's go back to the browser and refresh. So you see how the shadow in that case moved up and to the left behind the object. So in addition to specifying positive numbers which moves the shadow to the right on the X axis and down on the Y axis, by specifying negative numbers, we can move the shadow up and to the left.

So negative X means go to the left, and negative Y means go up. So in this example, we've seen how to draw shadows with varying colors, varying blur effects, and varying offsets using the shadow properties.

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.