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

Drawing out a grid in canvas

Although we don't have the time in this title to go over all the features available in canvas we will go ahead and jump right in deep by drawing out together a grid that will cover the full visible area on our stage. In the process we will learn about shapes, strokes, fills and arcs.

Keynotes:

  • Defining shapes/paths.
  • Creating a shape (arc).
  • Drawing with strokes and fills.

Video Transcript

00:00Welcome back. In the last lecture, we drew out a rectangle. Not that exciting.
00:07This lecture is going to be about creating something a lot more complicated.
00:11We're going to go out and draw out a grid and get canvas.
00:14To do that, we're going to learn how to define shapes, paths for that matter, by opening and closing a path.
00:21We're going to also create a shape itself, which in this case is going to be an arc.
00:25With it, we're going to create a complete circle.
00:28We'll finish it up by drawing out our circles and defining them with strokes and fills.
00:34By the end of this, we'll know how to create shapes in canvas.
00:37We'll know how to work with fills, strokes, and even how to create a really relatively complicated grid.
00:43Let's jump right into it and let's start drawing out our circle grid.
00:49Let's start creating something a lot more complicated.
00:52Let's jump right into it. In our source files, what we're actually going to do,
00:57and I'm going to go into our script itself,
00:59what we're actually going to do is we're going to work on that update canvas,
01:02but create something a lot more spectacular than just creating a rectangle.
01:07What I'm going to do is I'm going to create a few variables that are going to help me define what we want to do.
01:13I want to create basically a spectacle of circles on our screen.
01:18To do that, I'm going to define a radius for our circles and the radius is going to be 10 pixels.
01:25We're also going to define the gap between each circle.
01:28I'm going to call it gaps and I'm going to use the radius times 2 to basically have a gap of literally nothing
01:35because it's going to be a gap of the size of the circle.
01:39Okay, so once we have a gap, I'm going to go ahead and calculate the width count
01:44and the width count is going to be basically the width of our element
01:48defined by the size of each circle, which is the gap itself.
01:52I'm going to go ahead and take that value and just make sure that I'm going to parse it into an integer
01:57getting rid of any leftovers.
01:59I'm going to go ahead and do the same thing for the height itself.
02:02So our height count and our height count is going to be basically the same thing
02:06only this time we're going to do it with the height itself,
02:09which will basically give us enough information about how many items could fit in a height
02:14and how many items could fit in a width.
02:17The next I'm going to use is A colors, which is going to be an array
02:21and this array is literally all it's going to do.
02:23I'm going to go ahead and paste here this array that has a few different colors.
02:27Really the idea behind here is to have a different color for every circle that we're creating.
02:32Beautiful. Next step for us to do is we want to define a variable
02:37that will hold the length of our colors so we don't have to fetch it each time.
02:41So I'm going to call this AC length.
02:46AC length, which is for our A colors length,
02:50which is literally all it's going to be is just that array
02:52and I'm going to ask for the length, length, what am I doing?
03:03I am so mixing this up.
03:05There we go. All right.
03:06So now that we have all our variables again really quickly.
03:08We have a radius.
03:09We have the gap size, which is also the size of the circle.
03:12We also have how many circles will fit in our width.
03:15How many circles will fit in our height and we also have here a list of colors
03:19that we're going to be using and shortcut your shortcut variable
03:23that will define the length of that array
03:26because we're not going to change that array any further.
03:29Let's go ahead now and create a for loop
03:31and I'm going to go ahead and just create here a new variable,
03:34which we'll call X and we're going to set it to be 0 at first
03:38and I'm going to have our X as long as our X is smaller than our width count.
03:42Then I'm going to go ahead and also make sure that I grow that X by one value.
03:47Beautiful. We want to do the exact same thing for the height itself.
03:51So I'm just going to go ahead here and create a new variable called Y,
03:55set it to be 0 as long as our Y is smaller than our height count.
04:00I'm going to go ahead and grow our Y by one.
04:03So I'm creating here a two-dimensional for loop
04:06or two for loops, two dimensionals
04:08and that will enable me to then create a grid of colors.
04:12So now that I've had that sorted,
04:15the first step that I want to do is define myself
04:17what color will be the circle.
04:20So to do that, I'm going to go again to that,
04:21remember that fill style that we defined earlier.
04:24So we're going to use the exact same logic,
04:26but instead of defining the color to be a hard set color,
04:30I want to go ahead and randomly select a color from a colors.
04:33So I'm going to go ahead and go to a colors
04:36and I'm going to put inside of here parse int
04:39because I want it to be an integer
04:41and I want to take a math dot random.
04:45So I'm basically creating a random number,
04:47duplicating that value by the size of our array.
04:51So what's happening here is basically
04:53if I just run through this really quickly for a second,
04:55let me just make sure that I'm word wrapped.
04:58Again, what I'm doing here is I'm taking a random number
05:00between zero and one duplicating it by the length of our array.
05:03Now I have here a value that will be a floating value.
05:06So I want to get rid of whatever's floating
05:08and only values I could have here is basically zero
05:11through a value less than the height itself
05:14and then I'm getting the a colors array from within it.
05:18So that's all I had to do to basically get myself a new color
05:21each time this for loop runs.
05:23The next step for me to do is going to,
05:25I'm going to go ahead and define a beginning point
05:28and ending point for our element that we're drawing.
05:31We're going to be drawing a circle.
05:33So to do that, I'm going to go to my context
05:34and declare that I'm beginning a new path.
05:37I'm beginning basically a new shape that I'm creating.
05:40Once I've set that in motion,
05:42I want to also make sure that when I'm done creating it
05:44that I'm going to close that path.
05:46Literally those are the steps that I need to take
05:48to start a drawing, to end a drawing.
05:51Now in the middle, I'm going to go ahead and draw an arc
05:54and this arc is basically going to be a circle.
05:56So I'm going to call the arc function
05:58and an arc could be a full circle or not a full circle,
06:02but the steps involved are very simple.
06:04The first two coordinates are the X and Y coordinates
06:07where I want the item to be.
06:09I'm going to start first by setting it to be
06:11at least a minimum of the radius
06:13because I don't want it to be the circle
06:14to be out of the stage.
06:16And I want to take the amount of gaps
06:18and duplicate it, excuse me,
06:21duplicated by our current X coordinate.
06:23I'm going to do the exact same thing for our Y,
06:26making sure that each round we're going to grow
06:28by a size of a circle.
06:30So those are my two positions.
06:32The next is the radius.
06:33I'm going to go ahead and send a radius.
06:35The next two parameters are from where and until where
06:38do I want my drawing to be.
06:40We want to start from angle zero
06:42and in computer language many times
06:44we don't use 360 degrees to define a circle,
06:48but instead of that we use two times pi.
06:50And two times pi is literally that.
06:52It's two times, it's 360 degrees in human talk.
06:57And last but not least is another parameter
06:59that you don't have to worry about.
07:00You could just set it to be true or false.
07:02As long as you set it,
07:03it doesn't really matter what the value is for our lecture,
07:05but in case that you're pondering,
07:07if we didn't create a complete shape,
07:08then this value would define the direction of our angle.
07:11Are we going from right to left or left to right,
07:14clockwise or counterclockwise?
07:16So it doesn't really matter in our case.
07:17So we created our arc,
07:19but we have one more thing that we have to do.
07:21Although we drew it theoretically,
07:23we didn't actually draw anything out.
07:25For us to draw it out,
07:26contrary to when we were working with a fill rect,
07:29which literally filled our rectangle immediately
07:32and drew it out, contrary to that,
07:34we only called an arc.
07:35We didn't say if we want to fill it.
07:36And the reason is because we could do two things.
07:39We could go to our context and say,
07:40hey, we want to give it a stroke.
07:42If we gave it a stroke,
07:43then it would draw out a stroke.
07:45In our case, we don't really want to do that,
07:47but if we would,
07:47we would create strokes for our outline stroke,
07:51which we could also define a stroke style as well,
07:53but we're not going to do that.
07:55Instead of that,
07:55what we want to do is we want to call the context fill,
07:58which will fill out each circle.
08:00So I'm going to leave that stroke and fill both in place.
08:03If I go ahead here and click here on refresh,
08:05we'll see that now each item is going to be filled out
08:07and it's also going to have a stroke
08:09that has a bit of an overlap
08:10because each circle is overlapping the other stroke.
08:14So that's it, we've created now together
08:16a really complicated animation,
08:19well, not animation yet,
08:20because that's going to be in the next lecture
08:21when we go ahead and animate this.
08:24In this lecture,
08:25we learned quite a lot about Canvas
08:27and with it we're more or less done
08:28talking about the basics of how to draw in Canvas.
08:32There's so much more to cover
08:34and I urge you to check out my book,
08:36my packed published book on Canvas,
08:39which you'll find in the references
08:41or my video lecture course on Canvas.
08:45In the next lecture,
08:46we're going to move on into a different topic
08:47which will be animating our Canvas.
08:50So we'll see in the next lecture
08:51when we animate our elements inside of Canvas.

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!