Preparing to draw using loops
null
Video Transcript
00:00This this episode is you know, we're also learning about different API's that are inside of JavaScript and
00:06One of the stuff that we want to do is just tinker a bit with this canvas API
00:11We're not digging too deep into it
00:12But with it also one of the secondary tasks that we wanted to do is kind of revisit a few different
00:19Things that relate to loops. So let's start off
00:22what we want to do is basically want to draw circles basically one after each other and
00:27Basically set them up in our application. So to do that really really simply we're gonna create a few variables
00:34So let's decide what our variables are. We're gonna create a radius for each circle of 10 pixels
00:39Which is something that we're going to need we want to also set how much gaps will be between each circle
00:44So I'm gonna call that gaps
00:46And I'm gonna use a gap of 10 pixels times 2
00:50So I'm gonna take that radius times 2 times not times 10 times 2 to make sure that our circles are almost overlapping each other
00:57But they're basically they have no space in between
01:00Now the next thing is we want to make sure that we we want to count how many circles could we have?
01:07For our width so width count would what would that be?
01:11It would be basically we would parse int which is a function that takes a number. That's a
01:17Floating number and chops out the floating part. So what do we want to do?
01:21We want to actually take the current width of our application
01:25And we want to divide it by what we want to divide it by our gap because our gap is actually the size of our circle
01:31So now we're gonna have in our width count the amount of circles that could be in our width
01:36And we want to do that also for our height count
01:40So same exact logic only this time we're going to use the height and we're going to divide it as well against the
01:46Gaps, so now we know what our width count is
01:49We know what our height count is and if we change or resize our element, it will update automatically, right?
01:56So any if we change lines four or five, it will completely change the logic
02:00It will change the implementation of the logic, but we we would be using the exact same logic
02:06Alright, let's move on to the next part
02:08So first of all, the next part is I want to make sure that each circle has a different color to do that
02:13I'm gonna create a
02:15Array, which you remember an array from our arrays lesson and I'm gonna call it a colors
02:21And I'm gonna set inside of this array a couple of colors
02:23So I already have predefined a couple of colors so to help me out so I don't have to type it myself
02:30I kind of like
02:31Pre-prepared here in advance a couple of colors for us to to start off with and we got here four different colors that we could
02:38Use if you want to type these colors yourself as well
02:41You could pay paste them in but you could put any color that you want inside of these eight colors and as many colors as you
02:47Want all right
02:48So we got our colors all set
02:50The next step is we want to make sure that we know the length of what well well, well, well, did I did I skip something?
02:57I definitely skip something. Okay, so let me explain
03:00Why am I creating so many variables the more the less calculations?
03:04We do inside of the loop that we're going to be creating in a second the better and that's exactly the reason why I want
03:10To know the length of a colors, although I could always go to the property a colors that length to get the length
03:17But each time I approach then there's a calculation going on
03:21So to avoid this calculation and to avoid the need to call a property
03:26it's much better off for me to create a variable and I could just call it a
03:31Colors length and I could just set a colors length
03:42length right into it
03:44So now I have all the prep variables that I need
