You've already started watching Quadratic and Bezier Curves
In this video we create our own quadratic and bezier curves using the canvas. We look at how the curves are defined, and how to choose your control points.

0:00
[Think Vitamin Membership, membership.thinkvitamin.com]

0:07
[HTML5 Canvas Quadratic and Bezier Curves with Jim Hoskins

0:13
In the previous video, we used arcs to add fixedradius

0:16
curves to our shape.

0:18
Now we're going to create some more freeform curves by utilizing Bezier

0:21
curves and quadratic curves.

0:23
Now the two methods that allow us to draw more freeform curves

0:26
are called quadratic curves and Bezier curves.

0:30
In this demo I have both of them drawn out.

0:33
At the top we have what we call a quadratic curve.

0:36
And a quadratic curve is defined by a beginning point,

0:39
and ending point, and a single

0:42
control point.

0:44
Now the curves will normally be only this black line, but in this demo I've

0:47
highlighted both the control points, as well as the lines

0:50
to and from the start and control points.

0:53
So we define it by beginning a start and an end point

0:56
and a control point.

0:58
Now by manipulating this demo, you can see how the control point

1:01
affects the curve.

1:03
So if we pull the curve more down and towards the right, we

1:06
get this long beginning curve and then a short

1:09
tight curve towards the end.

1:12
You can see that our curve to begin with starts with the curve being very

1:15
parallel with the line being drawn from the beginning point to

1:18
the control point and then begins to diverge.

1:21
The same thing happens towards the end, and it becomes

1:24
more and more parallel to the point from the control point

1:27
to the ending point.

1:29
So a common use of a quadratic curve is maybe a corner in a box,

1:32
where you would normally have a right angle.

1:35
Now instead of drawing your lines all the way to the corner,

1:38
what you might want to do is draw your line almost to the edge

1:41
and start a curve and place your

1:44
control point right where the lines would intersect, and then place your ending

1:47
point some distance away from that point again.

1:50
This will give you a nice curved point, and

1:53
you can see how easy it is to define your points relative

1:56
to a normal corner.

1:59
Now let's look at the code that we use to create this curve.

2:03
Our quadratic curve starts like any other path with a

2:06
begin path call.

2:08
The I create a moveTo call that moves to a

2:11
start X and Y, which would be the first point in our curve.

2:15
Next, we call the quadtraticCurveTo method.

2:18
[.quadraticCurveTo(cpX, cpY, Y)]

2:20
This takes a control point, X and Y,

2:23
and an ending point, X and Y.

2:26
So the control point is this blue point right here, which will be

2:29
invisible in your actual curve, and your ending point will be

2:32
where your curve actually ends.

2:34
So the further that your control point is out

2:37
from the line drawn from your start to your end,

2:40
the bigger of a curve there will be.

2:43
Finally, we can just stroke it and we see our curve.

2:47
Now similar but different to our quadratic curve,

2:50
is what we call a Bezier curve.

2:52
It differs in that it has two control points

2:55
a control point for the beginning and a control point for the end.

3:00
So as we move the different control points around,

3:03
you can see that we have a different sort of curve evolving.

3:07
Again, you can see how the curve becomes more and more parallel

3:10
with the line drawn from the end point to the control point

3:13
or the start point to the control point.

3:16
That's really useful when you want to connect the two line segments

3:19
or two different shapes and have the curve

3:22
be parallel or perpendicular

3:25
to a certain point.

3:27
Let's say we had another corner.

3:29
We could do a similar thing where, if we had a line going

3:32
here and across, we could place our start

3:35
point on that line and our first control point

3:38
as an extension of that line.

3:40
So if our line was going to the right, we'd draw our control

3:43
point as an extension of that.

3:46
Then we can place our end point at the corner,

3:49
and if our ending line is going up and down, we can

3:52
place our second control point directly above

3:55
and that will create a nice angle.

3:58
But compared to the quadratic curve, we have a lot more control.

4:02
For instance, if we moved our control points much closer,

4:05
you can see we get a longer

4:08
section in the middle with a less tight corner.

4:11
You'll notice that we stillif we have

4:13
a line coming out from herewe still have our curve

4:16
be a natural extension of that line because it starts going off in the same direction

4:19
as the line instead of having some sharp corner

4:22
like it would if we were going from here down to here.

4:26
Now since we have two control points, we can create

4:29
much more complex shapes by manipulating

4:32
the control points into different directions.

4:35
With the quadratic curve we can only have really one curve.

4:38
We can adjust its sharpness in the different areas,

4:41
but you can't have it curve one way and then the other.

4:44
With a quadratic curve, we have all sorts of different options.

4:49
So for instance, we can curve down and then curve the other way.

4:52
This is great for maybe connecting two different shapes.

4:56
We can see the usefulness of a quadratic curve in this

4:59
example here, where we want to connect two different

5:01
divs together with a flowing line.

5:04
So in this case, I want our line to come out of the right side,

5:07
so I want it to appear that the wire is coming directly

5:10
out of the box.

5:12
So in this case, I put my beginning point right at the edge of the box here,

5:15
add a control point some distance out, but

5:18
directly out from the first point.

5:21
Similarly, the endpoint is right here, but the control point

5:24
for this exists somewhere out here.

5:26
So that allows us to create a line that appears to be going

5:29
directly into and out of our boxes.

5:32
Now with a little bit of change in the code, I can change how the two

5:35
boxes are connected.

5:37
Instead of the point coming out with a rightward direction, I can now

5:40
create a curve that goes this way.

5:43
So in this case, I still have my beginning point at the edge of the box,

5:46
but now my control point is downwards, creating a

5:49
downwardlooking curve.

5:51
These curves can be integrated into any other shape.

5:53
If for instance, I began with a

5:56
moveTo to the origin of 00

5:59
and I instead draw a lineTO

6:02
the beginning point, and

6:05
at the end of our curve I draw a lineTo

6:08
600, 400,

6:13
we can see that we integrate a line with our curve.

6:17
So a line going from here to here/

6:19
Then we can easily draw a curve and then continue drawing more

6:22
and more lines.

6:29
And the same thing can be done with our quadratic curve

6:32
and maybe even integrate it into a shape.

6:36
So here I've created a shape from 50, 100,

6:39
and then to our beginning point, a curve to our end point

6:42
and a line to another point,

6:45
and I close out the path.

6:47
If we flip over, we can see we now have a shape we can manipulate.

6:54
Now we've seen how to draw all sorts of different shapes using the canvas.

6:57
In the next video, we'll look at how to use transformations to make it eaiser

7:00
to draw shapes anywhere on our canvas.

7:03
[Think Vitamin Membership, membership.thinkvitamin.com]
You need to sign up for Treehouse in order to download course files.
Sign up