Drawing in Canvas and the ColorPropsPlugin
This lesson is a mishmash of a lot of wonderful things. We start off with exploring the world of HTML5 Canvas and finish by creating a sophisticated Color animation within our canvas. If you want to learn more about HTML5 Canvas check out my book.
Video Transcript
00:00Alrighty, before we dive into our main topic, which was Bezier Curves, I wanted to show
00:05you the one last plugin before that last major plugin, which is the Color Props plugin that
00:11enables us to animate colors that are not CSS or directly in our HTML, basically when
00:18we're trying to animate objects.
00:20So I'm going to use this in Canvas itself, and we're going to create a really rad animation.
00:25So I'm all for just starting it.
00:27It's a little bit complicated.
00:29Not too bad.
00:30We're notching it up slowly, slowly, but this chapter is going to be one of the heavier ones.
00:33So let's get to it.
00:35Let's get to the last lesson.
00:36We said that we're going to see how can we actually animate things inside of our canvas.
00:41So I'm going to create something really, really cool.
00:43And again, I do strongly encourage you if this topic interests you of knowing how to
00:48draw things dynamically, check out my book.
00:51It is a massive book.
00:52It took me over six months to write it.
00:55Six months that I didn't actually work.
00:57I just sat and only wrote that book.
00:59It's a massive book, and I really think it's a great one.
01:01So if you are into that topic, you could find it in the link below, probably, or just Google
01:07HTML5 Canvas and Data Visualization Cookbook.
01:11And I'm sure it would be the only result there, because it's a fairly big, long name.
01:15All right.
01:16So let's get on to it.
01:17Let's start playing.
01:18And what I want to do, if already we're doing this, I also want to incorporate animating
01:21color.
01:22And so far, we were working with colors when they were already part of the CSS, and the
01:26plugin automatically knew how to animate those colors.
01:30But once we get into trying to animate things that are not directly in our HTML, not directly
01:34in our CSS, but of actual objects, that's where the plugin kind of loses context and
01:40doesn't really know how to do it.
01:42So for us to help it, we have to make sure that we're also loading up our colors prop,
01:48the color prop.
01:49And if I just grab that color prop, which is the plugin that enables us to animate properties,
01:55custom properties, and animate their color.
01:57Now, I'm not going to go into the ins and out of that plugin, but you're more than welcome
02:01to check it out in the documentation.
02:04But I'm going to show you it in action, how to work with it.
02:07And you'll find a lot more about it in the official Green Sock documentation.
02:12So I'm going to go ahead and just add that into our list of scripts that we're actually
02:16working with.
02:17And I'm going to go back into my JavaScript, and it's time for us to start playing.
02:21I'm going to go ahead right on the bottom here.
02:24And the first thing that I want to do is I'm not going to animate anything directly because
02:27there is nothing there.
02:29So I'm going to actually create a new variable.
02:31And what I want to happen is basically an animation that will take place that is going
02:35to draw itself out and basically draw a pipe more or less from here all the way to here
02:40as it's animating in.
02:42To make that happen, what I'm going to do is I'm going to create this object.
02:46And I'm not going to even give it a name.
02:48And I'm going to set its values.
02:50So for my x property, which was that initial point, which was at 100 pixels.
02:56And we're also going to set the y.
02:57And I'm going to set that at 150, although right now we're not really animating the y.
03:02But that's going to open the door for us very easily to animate our y position as well.
03:07And instead of starting position also for our color.
03:10So I'll set that to be whatever that was, color.
03:14And I'll set that initial color to be the color that we were in initially.
03:18Now the next step is I want to actually start animating it.
03:21So I'm going to go ahead and start creating our tween.
03:24And I'm going to go ahead and do a tween light.
03:26And we're going to learn a couple of new things about tween light as well right now.
03:30First of all, we're still going to animate our object.
03:33I'm going to have it animate for two seconds right now.
03:35And I'm going to start with setting my x position and y position.
03:39So I'm going to animate my x position to the position where I wanted to go, which
03:43was 700.
03:44And I'm going to animate my y position to go to that new position, which really
03:48didn't move anywhere.
03:49But that's going to enable us to change our properties if we choose to change them.
03:54Next thing is, because we're really not we're not animating anything,
03:57we want to actually call the on update params like we did in our first,
04:01first chapter of the params.
04:04Oh, sorry.
04:05On update, which, you know, an update params was actually the next topic
04:09that I wanted to talk about, which is we're going to call on update canvas.
04:13And I already gave away the secret because for us,
04:15and this is what we haven't seen yet, I literally when I create the on update
04:20canvas right now, it's not sending in any variables.
04:25And because it's not sending in any variables, I don't know what to do
04:27at this spot.
04:29Now, I could save this variable externally or other variables externally,
04:32but we learned how important it is to try to avoid putting things
04:35into the global scope.
04:37Instead of that, what we could do is add the on update params.
04:44And once we said that we could send in an array and literally just list out
04:47all the things we want to be in our properties.
04:51Now, I want to show you a couple of things.
04:52One thing that we're not really going to be using now, if you send inside
04:55of a string and just right inside of itself,
05:00tween, tween light and the tween library will automatically send your tween in.
05:05So literally, if I just do that and I just trace it out,
05:07we'll be able to get that tween in our console that was console.log.
05:13And if I do that and go back into my application and click on refresh,
05:15we should see now that while it's animating,
05:17we're going to see our tween coming through.
05:20Now, really what I want to do is that doesn't really interest me.
05:23So that's not what I really want to get through.
05:25What I want to get through it, I'm going to add here a couple of properties.
05:28One is that context that we have right here.
05:31The next thing that I want to pass through is also this original object
05:34because I want to fetch that information easily.
05:36So literally, all I have to do is just add the context as another property,
05:41add that object as another property,
05:43and now I have access to that information
05:45and I can go on and continue with my animation.
05:47Now, I could use the round params that we've seen already, the plugin,
05:51but I'm just going to do it quickly on my own.
05:54So I'm just going to go ahead and round up my X.
05:56So I'm going to set here my parse int and I'm going to go to my object
06:01because I'm already in the on update.
06:03I need to go into the on update anyways,
06:06and I'm not loading it up right now.
06:08So why load it?
06:09I don't really need to.
06:10I could do it right away in two lines.
06:12I don't need it to be two kilobytes in this scenario,
06:15one kilobyte in this scenario.
06:16Now that I've set that up,
06:18I'm literally ready to start animating.
06:20And what I want to do now,
06:22before I even do a real complete animation
06:25with the color and everything,
06:26I'm going to talk about that momentarily.
06:28I want to go ahead and just start drawing this arc out.
06:31And that's exactly the name of it.
06:32I want to draw basically circles,
06:34and circles in Canvas are called arcs.
06:37Well, anything that is shaped like a circle
06:40or in the, well, anything that is a part circle
06:43or a full circle is created using arcs.
06:45So I'm going to go ahead
06:46and I'm going to create your context
06:50and I'm going to go ahead
06:51and going to create a stroke style.
06:53Well, not yet because we were going to change the color soon.
06:56I'm going to do a begin path,
06:57something we haven't seen yet, another something new.
07:00And the reason is because I don't want all the paths
07:03from before to mix in with the current one.
07:05So I'm basically saying begin path,
07:07then I create whatever execution I want to create.
07:11Then I have to also close that path.
07:13In this case, what I'm going to do
07:14is I'm going to call the context arc function
07:16and the arc function expects a few parameters.
07:19First is the X and Y position
07:20where the circles center point is going to be.
07:23The next parameter is the radius of the circle.
07:26In my case, I'm going to put a radius of 40.
07:29The next element that is going to request
07:31is going to also request the starting point
07:33of the actual arc itself.
07:36And we're going to say we're going to start
07:37from position zero.
07:38Now, if you know and if you don't,
07:40an arc equals two pies.
07:43Basically, the whole diameter of a circle is two pies.
07:47If I wanted to, I could have created
07:48basically only a partial by sending here math.py.
07:53And if I do that, let me just do that.
07:55I'll send that really quickly.
07:57I'll say to your true, don't worry what that is
07:59because this course is not about the canvas,
08:02although we're looking at the canvas as we're doing it.
08:04If I do this and I just save this
08:05and I go ahead and test out my application
08:08and by the way, before even we test it,
08:10I first of all rounded the properties
08:12because I have to have your complete variables.
08:14I have to have your integers.
08:15So now that we got that out of the way,
08:17we set our on update.
08:18We're sending auto on update params
08:21and it's time for us to test it out.
08:22And if we run it now for a second,
08:23we're going to see that as our element is running in,
08:26it is actually animating,
08:27but it's not actually showing it
08:29because we're missing one last thing.
08:31Just like last time around when we created a stroke,
08:34we have to actually tell it,
08:35hey, could you draw this out as well?
08:38Now I don't have to write here close path,
08:41but it is a good, good, good practice.
08:44Say context.closePath because I'm doing that.
08:47I'm basically saying here,
08:48this is what I'm actually wanna,
08:49this is what I wanna draw.
08:50It starts here, it ends here.
08:52I kind of put it here a lot more stuff than I have.
08:55And then I draw it out.
08:56This is really important.
08:58So once, and don't worry about it too much
09:00because this is not our main content, our main course.
09:03We dive really deep into this, into the book,
09:06but I wanted to throw you into another splash of water
09:08as we're learning different things.
09:09So it's not gonna be boring.
09:12And it click on refresh here.
09:13We're gonna see that now we're getting that arc
09:15and our arc is going on for basically a half a circle.
09:18And if I wanted to,
09:19I could also set it to start from a different spot.
09:22So if I wanted to, I could have said,
09:23hey, I want you to start from maybe half a pie
09:26and then it would be a quarter.
09:27But really in my case,
09:28I wanted to basically take the full size of a circle.
09:32And if I save this, go back and click on refresh,
09:34our animation should now animate in
09:37with those circles going on at the same time.
09:39And you can see also the ease,
09:40how it gets more tight as we get close to the end,
09:43how there's more calls, basically it's slowing down.
09:47And this is a really great way for you to see eases
09:49in a very visual way where you can see that here,
09:51there's a lot more spacing.
09:52While as you go down, later down the line,
09:55the spacings become a lot less severe.
09:58Now, if I wanted to continue this
10:01and actually drive in the main topic,
10:03which was animating our actual colors,
10:05let's see how could we also animate colors.
10:08Now the process of animating the colors
10:09is I can't just go ahead here and just animate my color
10:13by just adding the colors property.
10:15What I have to do, and let me just break a line here
10:18so we could see this more easily.
10:19I have to rate colors, color props.
10:23So it knows to use that plugin.
10:26And the next step is I actually could go
10:27into that property and say, okay,
10:29this is the color I want you to go to.
10:31And in my case, I'm gonna have it basically animate
10:33to that blue that I have here.
10:35So I'm just gonna grab my color picker, pick that color.
10:39It's gonna automatically copy it.
10:40And then I could go ahead and just paste that
10:42into my color, letting it know that this is the color
10:45I wanna go to.
10:47Now that I set my colors prop,
10:48I could go ahead and update my color as well.
10:52But to update the color,
10:53because this is not actually a tangible element,
10:56I'm gonna have to call that stroke style again.
10:58And I'm gonna do that right before we begin drawing our arc.
11:03And I'm gonna go ahead and just say that,
11:05hey, just put inside of here that object.color.
11:10And once I do that,
11:11we should have a really nice animation going on here.
11:14If I didn't have it, you're in error.
11:16Let me see, it's line 21.
11:18Let me jump really quickly to line 21.
11:22And that is because I didn't put here a comma.
11:24So let me put here a comma.
11:25And go back in there, click on refresh.
11:28And now we should see that the color
11:29is gonna progress through until it gets
11:31to that deep rich blue color starting from gray.
11:35Now, obviously, if we wanted to,
11:36we could have slowed down the animation,
11:37we could have changed the curvature.
11:39So if we wanted to,
11:40how about we change that line
11:41to go instead of in a straight line?
11:43How about we have it go a little bit tilting downwards?
11:45If we're already talking about this topic,
11:47and that is all we're gonna talk
11:49about the colors property,
11:50that's when we use the color props plugin.
11:54So if I wanted to change that line,
11:56I'm just gonna change that line really quickly
11:58instead of it staying in that same static position,
12:00let's bump it up to go down a line
12:03to like, let's say to 200.
12:05And in that case,
12:05we would animate our element to 200 here.
12:09And we would also go to our object right here
12:12and change its direction
12:15where it's going to the Y to 200 as well.
12:17And because we were always animating
12:18all the positions together,
12:20we should theoretically get the actual same,
12:23we should get that curvature
12:24and everything should be fine.
12:25Now, if we wanna make it even more interesting,
12:26which I definitely do,
12:28how about we start with the circle really small
12:30and having it grow as it gets closer?
12:33So to do that,
12:33I'll add another property in my object right here.
12:36And all I really have to do is just add another property.
12:39I'm gonna call this R for radius.
12:41I'll start my radius at, let's say four.
12:44And I'm just gonna go ahead and just add my radius
12:47and say, hey,
12:47we're gonna go to the radius of 40
12:49that we had previously.
12:50And literally all I have to do is just make sure
12:53that I'm gonna round down my item
12:57so it's not gonna be a floating value.
12:59And then I could use that R
13:01and just send it in as our radius.
13:03And what we should have here is the radius
13:05starting from a really small radius
13:07and growing as the animation progresses.
13:09So if we click on refresh,
13:10we should get after we fix my typo, as always,
13:14I'm missing a comma here and right back in there,
13:18we'll click on refresh.
13:19And we should see that now the circles are growing
13:21as it's getting in,
13:23creating a really interesting effect.
13:25And obviously the longer the animation will be,
13:27the more detailed this animation would be.
13:29So if our animation would be a lot longer,
13:32if our animation would be instead of being two seconds,
13:36we would bump it up to maybe six seconds.
13:38Our animation would be longer,
13:39but also the amount of drawing
13:41that we would add to the stage would be longer.
13:43Now, if you're concerned about,
13:44oh, but you're adding a lot of things onto the stage,
13:46won't it make your computer chug
13:48or something of that nature?
13:49Well, the answer is no.
13:51And the reason is is because once it's drawn, it's dumped.
13:53Contrary to vector items,
13:55that vector items are there forever.
13:57And in a scenario where you wanna draw a lot of things,
14:00it is a really, really good and viable solution
14:03to use canvas and not use SVG
14:06and not use different scenarios.
14:07That is assuming that you don't need to animate
14:10each item separately once you've created them.
14:12In this case, we're creating it.
14:13And once we're done, we're done.
14:14So we don't have to worry about it too much.
14:16And that is a lot more efficient
14:18because if we wanted to create this effect,
14:20we could create this effect
14:21with a relatively low amount of processor time
14:25because we're only updating what we're updating
14:27as we're adding the content in
14:28because this is already baked in
14:30and we haven't deleted it or touched it
14:32in any means, ways, or form.
14:34So that's it.
14:35That's about how you animate canvas elements.
14:37There's a lot more to talk about canvas,
14:40which we're not gonna talk about at all
14:41because this whole video is made a little bit off topic,
14:45but we really wanted to cover that
14:46as we were talking about the color properties.
14:49In the next few lessons,
14:50we're gonna continue working on these animations,
14:52but this time around,
14:53we're gonna move our attention to the Bezier curve
14:56and find out how does the Bezier curve work,
14:59what does it do,
15:00and in what scenarios would we wanna actually use it?
15:02So we'll see you in the next lesson
15:04when we explore that.
