Adding an interval using setInterval
Time for us to revisit everything we did and figure out what parts of it need to move around. We will be moving our code around to optimize it and enable us to only call the code within our nested loop repeatability over time using setInterval.
Video Transcript
00:00So, how do we deal with the refreshes?
00:02Now, because Canvas doesn't care about,
00:06it doesn't have a history,
00:07so whatever you draw, it doesn't even remember
00:09it drew it anymore, there's no need to delete
00:12and redraw things each time.
00:14The only thing we really wanna do every
00:17certain amount of time is basically
00:19all we wanna do is what's happening
00:21inside of this loop each time.
00:23All of the rest of this logic really
00:26is logic that we wanna keep.
00:29We don't wanna undo.
00:32So, we've seen how we could draw our element,
00:35but we really wanted to refresh it,
00:36and our problem right now is that
00:39we don't have an easy way to actually say
00:41we wanna reanimate everything here
00:44because the majority of the elements here,
00:46we actually wanna recreate.
00:47Now, there are some stuff that we don't,
00:49such as the different variables
00:51that could exist in advance.
00:53So, what I wanna do is I wanna extract
00:55all those stuff as just stuff that happened
00:57even before we call our init function,
01:00and among them are our width and height
01:02that we could define them externally.
01:04I would say even we could call them
01:06the first thing we want.
01:07We're gonna just set our variables.
01:09Now, for myCanvas, we don't need it afterwards.
01:12We're only using it once.
01:13We won't create an external one,
01:15but for the context, we do.
01:17So, we're gonna create a new context out here
01:19and delete the reference of the variable
01:22because now the variable is defined externally.
01:25Same for our radius, our gaps,
01:27our width count, our height count,
01:29our a colors, and our a colors length.
01:32We could have that as a global variable.
01:35Now, if you already are familiar with working with objects,
01:38it would be a really great idea
01:39to maybe put this all into an object,
01:42or if you're familiar with building classes in JavaScript,
01:45it would be a good place to put them into classes,
01:47but we're not gonna add new material
01:49into this getting started with JavaScript course,
01:51so we're not gonna introduce those topics.
01:54But I will say that it's a good idea
01:56not to create a lot of global variables.
01:58The more of them you create, the more of a chance you have
02:01that you'll use a library
02:03that's using the same exact variable name
02:05and create some sort of a conflict,
02:07but we're not gonna worry about that for now.
02:09Now, the next step for us is now
02:11that we extracted all of our variables,
02:13it's really time for us to make sure
02:15that everything is still working before we continue.
02:18So, I'm gonna save the file and just refresh
02:20to make sure that I'm still getting everything,
02:22and that I am.
02:23And now, if I go back to the next step,
02:25my next step now would be to separate between the steps
02:28that have to happen only once
02:29and the steps that need to happen every single time
02:32we will call an interval.
02:33And really, the steps, the differences
02:35are really just this part.
02:36This part really should be part of that init function,
02:40while this part would probably be saying
02:42that it would be called repetitively,
02:44so let's give this a new function name,
02:46and we'll call this animateCanvas,
02:48or animateCanvas.
02:52So, animateCanvas, or probably a better name
02:55would be drawCanvas.
02:57And drawCanvas already has its function set up,
03:00so all that's left for us to get our canvas
03:02to, again, draw that drawCanvas element
03:05is just to call it.
03:06Now that we've called it and we set it up,
03:08we're really just a second away
03:10from now starting our animations,
03:12because now we've created it for the first time.
03:14The next step for us is really just to set an interval.
03:17So, we could set an interval,
03:19and we could call drawCanvas now
03:23as many times as we want to.
03:24So, we'll call drawCanvas 10 times a second,
03:27or every 100 milliseconds.
03:29Now, if I refresh my application one more time,
03:32you'll see that now it's gonna be flickering,
03:34and I'm gonna slow it down a bit
03:36so it'll be easier for you guys to see,
03:37because the frame rate that we're using
03:39is a little bit lower,
03:40so we're gonna do five frames a second.
03:42And you're gonna see, again,
03:44our animation is happening over and over and over,
03:46and you've just completed a complete task
03:49of animating elements inside of JavaScript,
03:53and you're more than capable now
03:54of starting to really interact
03:56and really work with JavaScript.
03:58Now, a couple of steps that you might consider looking into
04:02before you really jump deep, deep, deep
04:04into development of JavaScript
04:06is really the last component
04:07that you need to really work with, which are events.
04:10We've looked at events,
04:12but we didn't look too deeply into events.
04:14And what I wanna do next
04:17is I wanna just use one event
04:18to show you how this works.
