Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Drawing complex shapes: Arcs and paths

From: HTML5: Graphics and Animation with Canvas

Video: Drawing complex shapes: Arcs and paths

Up until now, we've seen how to draw simple shapes like rectangles and lines. Now let's take a look at drawing more complex shapes, specifically arcs and paths. So, what is a path? A path is basically a set of connected points, and the points are connected by lines or by curves, and a path is either open or closed. The current drawing context always has one, and only one, current drawing path. Now, paths can be open or closed. A closed path has an end point that is the same as its starting point. Or the canvas can automatically draw a line from the ending point to the starting point and automatically close the path for you.

Drawing complex shapes: Arcs and paths

Up until now, we've seen how to draw simple shapes like rectangles and lines. Now let's take a look at drawing more complex shapes, specifically arcs and paths. So, what is a path? A path is basically a set of connected points, and the points are connected by lines or by curves, and a path is either open or closed. The current drawing context always has one, and only one, current drawing path. Now, paths can be open or closed. A closed path has an end point that is the same as its starting point. Or the canvas can automatically draw a line from the ending point to the starting point and automatically close the path for you.

Now, to begin creating a path, you use the beginPath method, and it doesn't take any arguments. You just simply say beginPath and start drawing your paths. To add paths, you basically use one or more path-drawing routines, and if you've watched me in the previous movies, you've seen how to do this with lines. In this movie, we're going to see how to do it with arcs. To stroke the current path, you just call the stroke function, again with no arguments. And to fill it, you just call fill. Both of those are no argument functions that will stroke and fill paths. And to close the current path, should you decide to close it, you just call closePath.

In case the end point is not the same as the start point, the canvas will close it for you. Let's talk about arcs for a moment. Arcs are curves that are portions of a circle, and in fact a circle is an arc. It's just a special case. It's a full 360-degree arc. So to draw an arc, you can either use the arc function or the arcTo function. So the arc function has several arguments. The first is the X and Y point, that if the arc was a full circle, would serve as the center of the circle.

The r argument is the radius, which is how far the arc is out from this imaginary circle center right here. Now, to actually stroke the path along the edge of the circle that the arc represents, you have a starting angle and an ending angle. And then the last argument is true if you want the arc to go anticlockwise rather than clockwise, which is the default direction. Now, the other arc function is arcTo, and that takes two control points and a radius. And then finally the closePath function, which I mentioned earlier, which closes the current drawing path.

Now we should make a special note that when you use angles, such as the angles here, these angles are radians; these are not degrees. So if you want to use degrees for your arguments instead of radians, you can use a very simple mathematical function right here, which takes Math.PI, you divide by 180, and you multiply by degrees and that will give you radians. Let's take a look at some arc examples. Remember 360 degrees is the same as 2 pi radians. So in this arc, we have a starting point and an ending point, and the way we do that was by stroking a 90-degree arc.

So we call beginPath to begin the path, and we create an arc with its center point at 50 and 150. That will be right about this center point right here. This 100 value is the radius, which is about 100 pixels from the center out to the edge of the arc. Then we start at the 1.5-radian mark, and we go to the 2-radian mark, so we'll start there and it will end at this angle right here. Let's look at another example. In this case, we have an arc that's going the other way. So to stroke 270 degrees arc, again, we call beginPath.

We have the X and Y center--that will be this center point right here. Radius is still 100. And we're going from the zero-angle part all the way around here to the 1.5-radian mark, anticlockwise in this case. So let's actually take a look at some real examples in code to see how this works. Here we are on the code editor, and I've got my snippets open to the paths section, and I've got my paths_starts example file for this chapter opened up. So let's go ahead and copy and paste some of these examples. So I will take these first lines right here, along with the first example, I'll copy and paste, okay, and save. And let's go to the browser, and we'll choose paths.start.

You can see that we've got three line segments for this particular path. Let's take a look. We've got the strokeStyle blue, a fillStyle of red, which we are now currently using. The lineWidth is 5, and here's where we are to draw the open path. So we call beginPath. We move the pen to its starting location, we add three line segments, and then we stroke, and that will stroke the current path so those three lines segments gets drawn. Okay, let's copy the next one. This time we're going to draw a closed path, so we'll copy and we'll paste.

So in this case, we call beginPath like we always do. Same kind of operation. We move the pen, draw three line segments, only in this case we call closePath before we call stroke. So let's save and let's go back to the browser and refresh. And you can see that there's the same three line segments, but in this case because of the closePath call, the canvas added this final line for us to make the ending point the same as the starting point. One more example. Let's go back to the code, back to the snippets.

This time we're going to draw a closed path. So I'm going to copy these lines here. Now we're going to paste. So one more time, three line segments, only in this case, in addition to stroke, we're going to call fill--and notice there's no closePath call here. So we save, and then we go back to the browser and we refresh. And in this case you can see that the path was stroked-- it's not closed, but it was filled in as if it were closed, there is an imaginary boundary right here--using the current fillStyle.

So now that we've seen paths, let's take a look at arcs. I have got my arcs_start example open right here, go back to my snippets. Let's go back up to the arcs, which are right here. I am going to copy the first example here and paste it. We'll save, and let's show it in the browser.

So that's the 90-degree arc right there. And just to show you the effect of changing the radius, let's make the radius 150 this time. Save, and we'll refresh and watch. So you can see that the center point stayed the same, but the radius got bigger. Let's copy some more. In this case, I want to copy this one, and then we'll paste.

Now in this case, we're going to stroke a three-quarter arc. So we've moved the center point over and we have got 100 radius. And in this case we've got these two angles, right? We've got a starting angle. We've got an ending angle. So we're going to save that and refresh it. You can see that the arc has now been drawn in the counterclockwise direction, starting from here, going all the way around three quarters to this point, around that center point right there. Okay, last example. Let's do a circle, and we'll copy and we'll paste.

All right, so in this example, we've started at zero. We go the full 2 pi radians. Save, we refresh, and there's our circle using the fill and stroke style, because you can see we call both fill and stroke. And just to show you how radians work, let's just do a simple example with degrees. Let's just say 'var degrees equals 360', and we'll say 'var radians equals Math.PI/180 * degrees', and we'll just substitute radians in there for this one.

Now since we're starting off with 360, it's going to have no effect. But just to show you that it is the same, let's refresh. So it's the same. But let's just change that to I don't know, 173 degrees, and we'll save, and we'll refresh. And you can see that it's not quite half a circle, because it's not 180 degrees, but you get the idea. You can use radians, or you can have degrees and convert them to radians. What we've seen in this example is how to draw paths and arcs using the canvas path- and arc-drawing routines. And we've seen how to draw open and closed paths, as well have stroke and fill paths. And then finally, we saw how to draw arcs in both directions using radians and degrees.

Show transcript

This video is part of

Image for HTML5: Graphics and Animation with Canvas
HTML5: Graphics and Animation with Canvas

37 video lessons · 18467 viewers

Joe Marini
Author

 
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

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
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.


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.

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

Your file was successfully uploaded.

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.