Animating canvas
Although there is much more to cover in canvas (don't forget to check out my title dedicated fully to canvas), We are ready to wrap things up by creating an animation in canvas.
Keynotes:
- Clearing the canvas.
- How canvas works.
- Animating it all.
Video Transcript
00:00Canvas is very different than SVG.
00:03While SVG keeps a state of every single element,
00:06Canvas is a bitmap rendering process.
00:09With that said, when we're animating in Canvas,
00:12we don't have a reference of what we animated,
00:15so we can't just move it.
00:16Instead of that, what we're going to have to do is delete it
00:19by clearing the Canvas and then redrawing it.
00:22We're going to learn on how Canvas works in
00:24this process throughout this lecture and by the end of it,
00:27we're going to have a Canvas that is animating.
00:29Let's jump right into it and let's see
00:31how to animate Canvas elements.
00:34We're almost done. This is going to be really short.
00:37Canvas in general, once it's done,
00:39once it's created, that's it, you're done.
00:42Now, you could see this that really it would be great if we
00:45could create one more circle inside of our Canvas.
00:48I'm going to go ahead and do that by just adding into our width.
00:51Instead of just getting it to be smaller,
00:53I could just go ahead and make sure that it
00:54always is just one more than the minimum needed.
00:58Beyond that also, once it's done,
01:00once it's drawn, that's it, it's there to stay.
01:03Only way for us to undo that is to literally draw on top of it again.
01:07To do that, what I could do is not only calling the update Canvas,
01:10I could go ahead there and make the auto create here an interval.
01:14I could call set interval and I could say,
01:16hey, I want to call the update Canvas and I want to send into it,
01:23I want it to happen every 200 milliseconds or every 100 milliseconds.
01:27I want to send in the Canvas and the Canvas width.
01:31Well, I basically want to send all these parameters
01:33as the parameters that are going to be sent in.
01:35Let me just paste them right in there.
01:37If I do that and I save it and I go back and I click here on refresh,
01:41we'll see that every 100 milliseconds is going to go ahead and refresh and redraw.
01:46Now, if in any stage you needed to clean the stage,
01:49in our case, we don't really need to because it's just redrawing
01:52and it doesn't really keep it in memory.
01:54But if we wanted to,
01:55if we needed to, we could go ahead into our code itself and let me
01:59just speed this down a bit to make you every 500 milliseconds.
02:03I could go ahead and every time before I even start drawing anything,
02:07I could literally go ahead and call a method which is called clear rect.
02:12Clear rect works the same way as fill rect,
02:15it only deletes everything.
02:16So clear rect will accept the same type of parameters.
02:21What I'm doing here is I'm basically getting rid of
02:23everything in this line.
02:25Now, by the way, I'm just going to go ahead here and just get rid of
02:28these two lines because I want our animation to maybe happen,
02:32not on a regular background,
02:35but for it to be a little bit see-through to whatever is happening behind it,
02:38which is quite interesting what we've just created here.
02:41That's it. But I'm going to actually like this color that was there.
02:45So that is it.
02:46That is how you create with Canvas and how you animate with it.
02:50Without further ado, we are done with this section.
02:54In this lecture, we learned about the differences between Canvas and SVG.
03:01When in Canvas, our animations,
03:03we have to basically redraw them while in SVG,
03:06we're just going to manipulate and change them.
03:08There's a big difference between those two techniques.
03:11One has instances and is much more vector.
03:14Well, it is a vector-based which is SVG.
03:17Well, Canvas is a bitmap-based.
03:19So once it's drawn, that's it.
03:21It's flat, which means it's going to run really, really smoothly,
03:24especially when there's a lot of things going on,
03:26like when we just created here this grid.
03:28If we would do this with SVG,
03:30it could become really taxing on the system.
03:34Contrary to that, Canvas could be much more smooth
03:39because it doesn't have a reference to every single item all the time.
03:44So with that said, we're done with this section.
03:46In the next section, we're going to move on to a completely new topic,
03:50which will be JavaScript and jQuery.
03:52Although this lecture in this section had JavaScript in them,
03:55our main focus is going to be native animations to things that
03:58are plainly out there inside of your HTML document.
04:02So you'll see in the next section,
04:03the last one which will be dedicated to JavaScript and jQuery.
