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

Drawing our first canvas lines

We learned as we created the flag for Palau that when we create a circle using the arc method, we have to trigger a request separately to fill the shape. This is true for all shapes that we create from scratch, and it is true for creating lines as well. In this lecture, we move our attention to lines.

Keynotes:

  • Moving our mouse pointer with the moveTo method.
  • Drawing a line with the lineTo method.
  • The difference between fill and stroke.

Video Transcript

00:00Welcome back. In section 2, our main focus is going to be creating shapes using paths.
00:07We're going to start by being introduced to the drawing of lines.
00:11Once we know how to draw lines, we'll move into paths, and from paths we'll draw complete shapes such as a triangle.
00:19We won't just do theoretical things because in the process we'll create the flag of Guyana once we have paths in place.
00:28And we're also going to talk about, beyond that, the creation of shapes that are really complicated, that have multiple points.
00:35By the end of this session, you'll know really everything that you need to know about paths and shapes in canvas.
00:42So let's jump into our first lecture, and in our first lecture we're going to go ahead and be introduced to drawing lines.
00:48We're going to start off with moving our mouse pointer to the position where we want to start from using the move to method.
00:55We're going to draw a line using the line to method, and we're going to see the differences between the fill and stroke methods that exist in the API.
01:05So let's jump right into it.
01:08Alright, it's time for us to create a brand new flag, and this flag that we're going to focus on is the flag of Greece.
01:16Now, instead of using fills, which you could use still the draw rect and literally create all of that with the draw rect,
01:23we're going to take a different approach because we want you to meet the lines as well, and not only complete shapes.
01:31So let's go ahead and start creating it.
01:33And to do that, go into your doCanvas method around line 16, and create a new method.
01:38We're going to call it createGreece.
01:44And createGreece is going to accept a canvas, and I'm just going to go ahead and send in our main stage canvas into it.
01:52And let's go ahead there and create that createGreece method now that we've activated it right underneath our createPaulo.
01:59And I'm going to go ahead here and createGreece.
02:02It's going to accept a canvas, and also it's going to have the same properties as the first few lines here inside of our previous method.
02:10Alright, so now that we have the basic construct configured, let's take a look at the flag itself and figure out what are the things that we need to do to it.
02:20So let's take a quick look at the flag again.
02:22Let's go ahead and start with defining a background color.
02:26And I'm going to go ahead and define my background color to be this white, or no, yeah, no, let's do blue.
02:35Let's do the blue as our background.
02:37Okay, let's go ahead and let's create it.
02:40And we already created a rectangle, so we're just going to go ahead and literally do the same thing.
02:44And I'm going to go to the context.
02:45I'm going to define a fill style, and I'm going to set that value to be a color I already pre-configured previously, which is of our blue.
02:53And here we go.
02:54That's our blue.
02:55And all that's left for me to do is to go to my context and fill the rectangle out.
03:00What is this dimensions from the 00 position to the full width and full height to create the full spectrum of the rectangle.
03:08That's all we really had to do to create the baseline color for our flag.
03:12In the next few steps, the next thing we're going to want to do is to create the vertical lines or the horizontal lines more accurately.
03:20If we take a look at the map at the flag, we'll see that we have your one, two, three, four, five, six, seven, eight, nine horizontal lines.
03:29Among those nine horizontal lines, we have only one, two, three, four white ones.
03:36But really, we want to take the total height and divide it by nine to get to the actual size that we want of each element.
03:43To do that, I'm going to literally just add that variable.
03:46And I'm going to go into my and just add one more variable to our list.
03:50And that variable, I'm going to call line height because we really want to define height.
03:55Now, lines don't have a width and height.
03:57They just have one or the two really just have a width.
03:59But so it doesn't really matter that we're calling it height.
04:02It makes sense for us from a perspective of our design right now.
04:06And what we actually have right here is basically the height of that line.
04:11Now, the height of the line would basically be the height divided by nine.
04:15That'll be the height of each line or the width of each line.
04:18Now, if we take another look at our flag as we're continuing doing this,
04:22we'll see that there is a bit of an offset of the flags.
04:26And we're going to have to take care of that offset.
04:28Our flag doesn't start immediately.
04:30It starts a push into it.
04:33Beyond that, before we even create that offset,
04:35let's start creating it and then we'll figure out what and how do we offset.
04:39So I'll explain the offset momentarily.
04:42Before we even create that offset, let's go ahead and move to the next new topic.
04:47And that next new topic is going to be contrary to fills,
04:50which we're basically filling up a shape.
04:52If we're going to draw lines, we are not going to fill them up.
04:55Instead of that, we're going to define their stroke.
04:59So I'm going to go ahead and define a stroke style.
05:03Now, stroke itself is basically defines lines
05:07while fills defines their content,
05:09which implies that in the future we're going to create shapes using lines.
05:13And then we could decide if we want to give them a stroke or a fill
05:16or if we want to do both.
05:18Now, in this case, I want it to be white,
05:20so I'm just going to go ahead there and fill it out with white.
05:23Six times F, we got their white.
05:25And the next step we're going to have to do
05:27is literally loop through our element.
05:30We need to loop through four times to create the color four.
05:33So I'm going to go ahead here and create our four loop of our eye
05:36starting from position one.
05:38And I'm going to make sure as long as my eye is smaller,
05:40I could do smaller than four and then duplicate it each time.
05:44But probably better off for me to do smaller than eight
05:47and then jump by two each time
05:49because what I want to do is I want to get to one,
05:51to three, to five, and to seven.
05:55All right, let me show this to you in a more visual way.
05:58I want to basically, we're going to ignore zero.
06:02I want to get to one, three, five, seven.
06:07So I want to get to seven from one to seven
06:09and hit all those targets.
06:11So all I'm going to do is I'm going to make sure
06:13that my eye grows by two each time.
06:15So I'm going through one through seven
06:17and once we cross eight, as long as we're smaller than eight,
06:20we could continue.
06:21Perfect.
06:22The next step, we're going to learn another few new methods
06:25and the first new method that we're going to learn
06:27is going to be the move to method.
06:30The move to method doesn't draw anything on the stage
06:33but the idea behind the move to is that you have a pointer
06:37and from that pointer, you're always drawing.
06:40Now, the drawing process, there's two main line creators.
06:44There's the move to and then there's the line to.
06:47The move to basically picks up your needle
06:49and moves it to a different spot without drawing anything.
06:51It's like taking your pencil off of the page
06:54and moving it to a new point.
06:56Where do I want the point to be?
06:58Well, I want it to start from all the way in the length,
07:01all the way in the dot there of that width.
07:04I want us to get literally to this point.
07:07So that's for the X position, we want to start from zero.
07:11Contrary to that, to the Y position,
07:13what we really want to do is we want to take that eye,
07:15our current index, and duplicate it by that line length.
07:19So we're always moving down the line height,
07:22excuse me, moving down that height each time.
07:25Now, if I just go ahead and do this
07:27and I'm going to go ahead and also draw,
07:28once we moved our pointer
07:31and I'm going to go ahead and create a line to,
07:33you'll notice that the line to process,
07:35again, this time around it's the same point for our Y,
07:38but this time around we want to go
07:40to the full width of our element.
07:42So basically making sure that we're starting
07:44from all the way to the left to all the way to the right.
07:47Moving and then creating the line.
07:49Now, if I go right into my browser
07:51and click on refresh after adding this,
07:53I'm not going to see anything,
07:54but also I'm getting here a small error in line 64.
07:57Let me make sure that I save,
07:59oh, I put an extra semicolon.
08:02So let me just go ahead there,
08:04not colon, comma,
08:06and you'll see that nothing is seen.
08:08Now, the reason nothing is seen is very simple
08:10because exactly the same way when we were creating the arc,
08:14it would not actually go to the stage
08:16until we filled it out.
08:17The same is true with lines,
08:19only because a line we don't fill,
08:21we add a stroke to it,
08:23we want to call the stroke command
08:24once we're ready to actually see it.
08:26So if I go ahead there and call the stroke
08:28right after we're done with that loop,
08:30right after we drew those four lines
08:32in line one, three, five, and seven.
08:35So literally what we would do
08:37is we're going from line one,
08:38if I just go ahead and refresh,
08:39we're going in line one, three, five, seven.
08:43Now, we still have a few things that we want to configure.
08:45One is this line is way too skinny,
08:48so I'm going to go ahead and also define,
08:50right after I define the stroke or round
08:53where I'm defining the stroke,
08:54also define a line width.
08:57Then line width will accept a width,
08:59and in this case,
09:00I'm going to give it also that line height value
09:02because it's the exact same value that I'm in need of.
09:06And if I go ahead there and click on refresh,
09:08we'll see now the line will be nice and thick.
09:10My only problem is,
09:11is because lines are configured from their center point,
09:14then I have here an offset of half of that line.
09:17To be able to solve that,
09:18I'm going to go ahead and add an offset variable
09:21so I could add that into our logic, so offset,
09:24and the offset will equal the line height divided by two.
09:29And once I have that offset,
09:31all that's left for me to do is to push that actual position,
09:34our actual Y position by that offset all the time.
09:38So I'm always offsetting my line just by a bit
09:41to be able to compensate for the fact
09:43that the line width itself is defined from its center.
09:47So if it's 40 pixels,
09:48then I'm going to have a padding really of 40 pixels
09:51around it of 20 pixels on each side.
09:53So I need to take that into consideration.
09:56Once I've done that,
09:57I completed the first step in the creation of the flag of Greece.
10:01In the next lecture,
10:02we'll continue down the line
10:04and complete the actual creation of the flag itself.
10:08In this lesson,
10:09we learned how to draw lines using the move to
10:12and line to methods.
10:14In the next lecture,
10:15we're going to meet paths.
10:16So we'll see you in the next lecture as we move to paths.

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!