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

Exploring quadratic bezier curves

Although I don't control the elements, I do control quadratic bezier curves and animations. So will you by the end of this lesson. A quadratic bezier curve is a curve that is controlled with a point. In other words, between A and B there is a control Point that helps define the angle of the curve.

Video Transcript

00:00Alrighty, we're ready for the wheel deal. Wheel deal. Wheel dealy. Don't woey. Don't woey,
00:05because although it's complicated, I'm explaining it. And that means it's not going to be that
00:11complicated, because I'm going to draw things and it's going to be illustrated and visual.
00:16And you're going to be creating really cool animations in the end.
00:20So exciting. Here in the background, the rain, but I do not control the elements,
00:25although I do control the Bezier curves, and so will you by the end of this lesson.
00:30Alright, so let's continue. What I want to do is I'm going to, before we even animate
00:34Bezier curves, which could be complicated, how about we draw them out and really see them in
00:40action before we even approach animating them like that will have a really good understanding of them.
00:45And again, I'm going to refer back to my book that I did with Packet on HTML5.
00:52And if you have the book, just open up page number 40 to see this illustration. And you'll see that
00:58basically a Bezier curve, or more known as quadric curve, a quadric curve basically is you have a
01:05point A, you have a point B, and you have another controller point that is basically tugging on the
01:11on the, I would say on the curvature of your element. The more it's high up, the more it's
01:16further away, the more bigger the curve is. So you have a lot more controls. If I take this
01:21point, and I have it here, then my curve would be leaning towards that direction. And notice how
01:26the control line is not that it goes all the way through, like we saw in the last lesson.
01:32But the quadric curve basically, in many ways, it's like a, I would say, three, like a third way
01:39through or something of that nature. It's hard to really define it, but it's not point in the
01:45point itself. And a lot of this control is really down to how comfortable you get at drawing.
01:50Now, what I want to do is I want to create this shape more or less, or more accurately,
01:54I really want to create first this rectangle. And then I'll try to point out between those two
01:59lines using a quadric curve, so we could see how that is affects, how it affects things.
02:04So how about we go back into our code and start doing it. And I'm going to go ahead into my
02:08element, instead of moving it to 100, 100, what I really want to do is I want to create here a
02:13rectangle of sorts. So I'm going to go to position, let's say, I know that my width here,
02:19if I go and see that it's 800 by 300, I'm going to ignore most of them and just treat that 300 by
02:24300. So if assuming that logic, I'll just go ahead and move my element to, let's say, 150,
02:31and position more or less 10. And that was that would be where I'm moving to where I'm going to
02:36start from. The next is I'm going to, let's see, I want it to go, I'm going to create some sort of
02:42not accurate, I just want to create some sort of a shape here. So I'm going to go ahead and
02:46just continue on and drawing it. So let's say I'll go here to 300, 300, that sounds good to me.
02:53And let's see what I'm drawing here. And let me just comment down all these animations. So I'm
02:58just going to go ahead here and just comment out this animation just so I could look at the curve
03:04that I'm creating before I do anything else. Oopsie. Will I find it? No, I won't. Where the hell?
03:13Where are you? Well, that's where you are. All right. So let me save that go back,
03:18it's just starting to get dark and it's raining, which is always a good combination.
03:22Okay, so I created here this interesting kind of like, triangly shape. Now the next thing that I
03:28want to do is I want to draw a line that goes from this point to this point, using this as
03:32an anchor point. So if I go ahead and do that, let me just do that really quickly, I'll show you
03:37how that is done. Now I'm going to create here, basically, I'm going to do the same
03:41logic where we set here, a begin path. And you want, let me also set a color. And I'm not
03:46creating an arc, but I'm let me just copy this whole thing. I'm just going to put it right here
03:49and we'll delete what we're not actually using. Let me just push it in a bit. And let me just
03:54set a different color. So I'm going to set the color red. So I'm going to go here, RGB,
03:59red, green, blue. So it's going to be red, it's going to be completely red. I'm going to
04:03begin a path, we're going to close a path, we're going to stroke again. And all that's
04:07left for us to do is to actually draw our line. So I'm going to go ahead to my context,
04:12and I'm going to move my line again, to that initial position where it was before.
04:18And once that we're in that initial position, what did I do, delete, delete, delete,
04:24move to, I actually want to paste it right here. Now that I moved it to that position,
04:29what I want to do, I want to do a quadric curve. And to do that in, in, in canvas is the,
04:35I use the function called quadric quadratic quadratic curve to then I said the curve, the point,
04:50the anchor point that I want to use in my case, I'm going to use the anchor of 300 300. So I'm
04:56going to go ahead there and just set the anchor 300 300 as my first point. And my second point,
05:01that's going to be my XY first point. And the second point will be that 710 that we have up there,
05:06710. Now, if we go ahead and look at this, we'll see that our curve should be at, you know,
05:11it should be red while our regular line was gray. So we should, we should be able to see very,
05:15very clearly what it actually does. And by the way, don't worry about it closing,
05:19because it's just something that canvas does in general, it closes things up when it's,
05:26when it actually creates its stroke. But what I want to really do, I don't worry about the
05:31top area, what I want to look at is what happens when I change that actual point and move it in
05:35space. If let's say, for example, I'm just going to move it space in the, in the x space,
05:41basically keeping it in the same distance as it is, and just pushing it a little bit more,
05:45let's say, to the right, if I go ahead and do that, notice how our curvature is going to change.
05:49So if I find that control point, which this is our control point, and I want to push,
05:55I want to push the x position to be further out. So let me push it to be 500 out,
05:59I'm just going to push that, that controller curve much out. And when I do that, if I click on refresh
06:06here, we're going to see that our curvature is aligning and adjusting to that dimensions.
06:11And obviously, or not obviously, wherever we put it, it's going to control that curvature.
06:16Now, over time, you're going to get used to this idea. But literally what we're doing is
06:19just creating the smoothest curve we can, well, from going from point A to point B,
06:24in the parameter in the space that we could, it's not creating a weird shape or whatever,
06:29it's literally just one logic, it's a curve that goes through it not through really,
06:33it's a quadratic curve that basically says, this is my bounds, this is what is the biggest curve
06:39I could create inside of the space. Basically, that is the rule. Now, if I go ahead and I
06:44move the point, let's say to a point that's a little bit deeper in the content, let's say I
06:47push it even further down. And this time, we're able to move up my theoretical x position as well
06:53x and y positions. So if I go ahead there, and let me push it up, and let's say we'll set that
06:57line to maybe even 550. And I'm going to set my instead of 300, that took our whole space,
07:03I'll set it to 200. And again, I'm going to have to make sure that I also update our
07:08element that we're actually driving there so we can see the curve itself. And if I do that,
07:12and I save that, go back into my application, click on refresh, again, we're going to see
07:16our curve changing and morphing and redefining itself based on the curve that we have that
07:22we're creating. Now that we've seen that in action, what I want to do is I want to
07:26take our animation our element and have it animate in those that curved space. So to do that is
07:31quite easy. Now that we know how it works, the way we do it in jQuery, not jQuery, excuse me in
07:37tween light and tween max in the tween library is quite simple, I'm going to go ahead and I'm
07:42going to copy that bezier logic again, but I'm not going to do it the same way that I've done
07:47it before. I'm going to just paste it right here. So we have our bezier curve. Now,
07:51the next thing that I want to do, obviously, besides, I need more points, it's not going
07:55to be two points anymore. But even before I do that, I need to set and say to bezier
08:01engine that it's actually a quadric animation. Because I need to add here custom properties,
08:08I can't just go ahead and put my array the way it was before anyways. So I'm going to go ahead and
08:13wrap this inside of an object. And I'm going to set its value to be basically the values
08:19themselves, I'm going to type your val use and values is where I'm going to send that array that
08:25I was using before. Now I need to add another property and that property is going to be type.
08:30And the type basically accepts a string. And by default, we said it was that through,
08:36which we don't even have to look at because it is the default. Now, if we want to have the quad,
08:41quad rat, oh my god, I can't say that word quad, say it with me, quad rat tick. So if
08:47I want to have a quad rat tick, I have to set the tone and set it to be quad rat tick. Now,
08:53contrary to the points that we had earlier, we have two points, we have to have three points.
08:58And the first point is always going to be an anchor point, or the point where it's actually
09:03starting point ending point. And the middle is going to be that quadric curve that controller.
09:09So we said the first the first place we want to be is we want to start off at 150 10. So let me
09:15set that as my starting point. So that's going to be that 150. And our top is going to be 10.
09:22The next point we have there was 550 200. And last but not least was 710. Oh, and we have 710
09:33already set. And literally, the middle element is literally that controller and the as we play
09:39around with it, we could adjust and change the where our animation is going as our animation
09:45is going. So we have a small issue here in line 35, I forgot to close something,
09:51or maybe I close too many things. So let's see, first of all, this item closes that okay,
09:56but we don't have an item that closes that because our editor didn't save it. So I'm
10:01going to save that close that run it one more time. And here we go. And you can see how our
10:07animation is going through that quad rat tick logic. And very easily, we could adjust that
10:12position. So if I say, okay, let me move that center point to further out here, so I could
10:17go ahead here and just change my position. So okay, I want it to go, let's say to,
10:21if that was the left, let's say let's have it go to like, let something was a little,
10:25let's go to 250 instead. And I'm just going to change everywhere we had that value,
10:30which was our control point to 250. And 250 so we can see it all over as we're doing it.
10:37And if I go ahead and click on refresh, we can see that now our curve has changed,
10:41and we have a lot smoother control over how our animation is going and where is it going.
10:46Now, I want to say one more thing about this, we're not done, by the way, with Bezier curves,
10:50it's a big, big topic on its own. But before we complete this conversation about Bezier curves,
10:56about quadra curves, I want to say that you could add as many curves as you want.
11:00So you can go ahead and add here 100 if you want to, as long as you have always a starting
11:05point and ending point, and a curvature. So if I wanted to, and I wanted to, let's say,
11:10go back to that initial position that I was at, and have it go through the same curvature,
11:14in theory, what I could do is just set this as the as the fourth parameter. So if I go ahead here,
11:20I could set that as my fourth parameter, and I could set this as my last parameter, as long as
11:25I make sure that I always have a curvature. So if that was the beginning, this is our curve,
11:29this is to where it's going. And here it's our curve, and this is where it's going to the next
11:33one. If I go ahead and save this and run my application just one more time, theoretically,
11:37we should see it going back and forth. And that's what it does. So there you go. And obviously,
11:43you could take it into a lot of different directions. It doesn't have to be only five,
11:46it could be a lot of points. So if you have a scenario where you have some sort of a path,
11:50or something that you want your element to go through some sort of a path, you could literally
11:53create that quadratic curve logic, and use this mechanism that I drew here for you to make
11:59your life easier. And once you're done with it, you can just get rid of that canvas,
12:03and you don't have to see it. So it gives you a lot more control over your animation,
12:06and more visible control over your animation as you're creating it. So that's it. See you
12:11in the next topic. We're still in Bezier curves. This topic is not over.

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!