02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Drawing arcs

There are three types of curves we can create in canvas%u2014using the arc, quadratic curves, and Bezier curves. In this lecture we will move our attention to the arc again and this time really master it's full capabilities. In the end of this lesson we will create a pacman and a pie chart example.

Keynotes:

  • Creating a pie chart.
  • Creating a pacman.
  • Working with arcs.

Video Transcript

00:00Continuing the line of thought of arcs,
00:03session number four is dedicated to curves and gradients.
00:10We're gonna also talk about images briefly,
00:13but really the thematic topic here is control points.
00:16What does that mean?
00:18Well, drawing arcs is you're controlling
00:20the drawing inside of a path.
00:22While as soon as we finish drawing with arcs
00:25and we're gonna look at that in this lecture,
00:27the following lecture will follow curves,
00:30which basically are lines that you control
00:32and change their angles using a control point.
00:36These curves are called a quadratic curve.
00:39The following lecture will be a curve
00:41that has two control points, which is called a Bezier curve.
00:45We're gonna see both and really understand
00:46the differences between them and how we could use them
00:49to create really complicated shapes.
00:52We're gonna then take a break from controls
00:54because we're gonna learn how to draw images
00:56onto our screen.
00:58Once we do that, we're gonna revisit back to curves
01:00and cut through to a new topic, which will be gradients.
01:04And how do you create a color manipulation
01:07that is really a controlled point
01:09where you're controlling colors
01:10from one color to another?
01:12We'll start with the linear gradient
01:14and then we'll complete the section with the radial gradient.
01:19So without further ado, let's jump right into arcs.
01:23Drawing arcs, we've met a few times
01:25throughout this title.
01:27But in this lecture, we'll go ahead and look at
01:29how to create a pie chart, which is completely different
01:32than what we've created so far.
01:34And also we're gonna see how to create a Pac-Man.
01:37Literally what we're gonna do
01:38is get really familiar with arcs.
01:40So let's jump right into it.
01:43Let's kick off this section
01:44with something that should be really easy.
01:46I already created a basic flag
01:47and together we're gonna work with the arc
01:50to create Pac-Man or a Pac-Man type of person.
01:54Now I created already the function, the create Pac-Man
01:57and integrated it just the way we did in previous lectures.
02:01I got our width and height, I set our context,
02:04I set our X and Y coordinates to the center of our screen
02:06and defined a radius for our Pac-Man.
02:10And I also defined a radian property.
02:12So far we've been calling it degree,
02:13but really now that we're deep into the world
02:16of drawing in a computer, it is called a radian.
02:19One radian, which is reflects the idea of one degree.
02:24But in computer language, which is basically
02:26that math pi divided by 180.
02:29We then filled our set of fill style
02:31and drew a black canvas on our screen.
02:35Let's go ahead and continue it.
02:36I'm gonna go to our context and begin a path
02:38because now we wanna create a relatively complex shape.
02:41And I'm also gonna go ahead and reconfigure our fill style.
02:45And again, I have that pre-configured already.
02:47So I'm gonna go ahead here and just grab the value
02:50that should go inside of here.
02:52And once we have the color,
02:54I'm gonna go ahead and the first thing I'm gonna do
02:56before I start drawing is I'm gonna move my coordinates
02:59to the center of my screen,
03:00which is our X and Y coordinates.
03:02Now that we've got that
03:04and we're inside of the center point of the circle,
03:07the next step is we wanna draw out that circle area.
03:10Now to do that, I'm gonna go ahead and use the arc
03:12and we've met the arc previously.
03:14The first two properties are properties we've seen before,
03:17which are the center point of our circle.
03:20The next is the radius.
03:22The next point are two points that say,
03:24how far do we wanna go?
03:25Until now we went from zero to twice our,
03:29basically to 360 degrees from zero to two times pi.
03:33This time around, I'm gonna go ahead and say,
03:35I wanna go to 40 times our radian,
03:38giving us a 40 degree.
03:40And then I wanna do for our next property,
03:42I wanna end it at 320 degrees.
03:45I'd already calculated it to be true.
03:47Next, I'm gonna set this to be false
03:49and I'm gonna show you the difference between false
03:51and true in this case once we run this.
03:53And now that we created the arc,
03:54I really wanna close things up.
03:56And the way I wanna close things up
03:57is I wanna go to our context
04:00and I wanna go back to our center point of our circle.
04:03So to do that, I'm gonna go ahead and call the line two
04:06and go into our X and Y center point again.
04:09So again, I'm creating an open circle
04:11starting from our center point,
04:12creating a almost complete circle missing about 80 degrees.
04:17Well, not about 80 degrees.
04:18And then I'm traveling back to the initial point.
04:21All that's left for me to do is to close this shape
04:24and go ahead to close the path and then call the fill.
04:29And literally that's all I had to do.
04:31If I go ahead here and click on refresh,
04:33we'll see that now we will have the Pacman shape
04:35if I don't have an error.
04:36And I'm seeing that rod ion is not the right variable.
04:39It's called it's radian.
04:40So let me go ahead here and just make sure
04:41that I'm using the right variable.
04:44And let's go ahead and give this one more shot.
04:46And we still have your content lined with
04:49is not a function and line three, six, six.
04:53So let's see, line three, six, six.
04:55Oh, because I wanted to do line two
04:57and I wrote with for some reason.
04:59So line two, let's save that, click on refresh.
05:02And here we go, we have our Pacman
05:04and it's beautifully positioned exactly as we wanted it to.
05:07So let me just go through this really quickly.
05:08Again, we began a path, closed the path.
05:10Everything here is familiar.
05:11Even the arc is familiar,
05:13only this time it's the first time
05:15that we send in not a complete circle.
05:17We didn't complete our circle.
05:19And what we did was our first step
05:20was to start from the center point
05:22to draw out the partial circle
05:24and then close it by going back to the initial point
05:27where we started from
05:28and then we were ready to fill our shape up.
05:30Now, if this time around,
05:32if I set it to be true, our last parameter,
05:34counterclockwise actually has a meaning.
05:37So instead of having our define coming from here
05:40through here, what would happen is the shape itself
05:43would be drawn in the other direction.
05:46So we would actually get only the actual element
05:48that is missing.
05:49Now, to that matter, if you wanted, for example,
05:52instead of creating a Pacman shape,
05:54but instead of that, let me go ahead here.
05:56Instead of that, if you wanted to,
05:57I could have went ahead here
05:58and duplicated this whole section
06:01and then went ahead and just set this to be true.
06:04I could create a pie chart.
06:06All I really would have to do
06:07is push our X and Y coordinates
06:09a bit, really just our X coordinate, maybe.
06:11I could just push that X coordinate,
06:12maybe by let's say five points.
06:15And then it would be sit our slice
06:16would basically sit outside of our pie partially.
06:19Or if we wanted to make it really sharp,
06:20then go ahead there and make that pie go out.
06:23Or notice what's happening here
06:25that also we're having here a stretch
06:27instead of going out.
06:28So what I want to do is I want to make sure
06:31that when I'm changing my X position,
06:33everything here is valid.
06:35But the only problem is that
06:36I didn't change my move two points also.
06:39So I would have to also change my starting move point
06:42and also my line to move point.
06:44So my X coordinate is always in that right position
06:47and I'm not having a few different positions
06:49stretching out the item.
06:51Then if I would do that,
06:51then I would have that pie element removed.
06:54So there you go.
06:55So now you know how to create a Pac-Man
06:56and you would also know how to create
06:58a pie type of chart.
06:59So that's it.
07:01In this lesson, we really got to know arcs.
07:04And in the following lesson,
07:05we're gonna start drawing with quadratic curves,
07:08a really exciting topic.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!