Drawing triangles with paths
Up until now all of our shapes where very basic. We created lines, and filled up pre configured shapes like the rectangle and circle. In this lecture we will take advantage of the new method we where introduced to in the last lecture the beginPath to define a path only this time we will define this path as a closed path. By the end of this lesson you will know how to work with the beginPath and closePath methods and create your own geometric shapes.
Keynotes:
- Working with the beginPath and closePath methods.
- Creating a utility function.
- Drawing a triangle in canvas.
Video Transcript
00:00In the last lesson we were introduced to the beginPath method and in this one
00:05we're going to be drawing out triangles with paths which means that we're going
00:08to need to have a distinct starting point and ending point. To do that we're
00:12going to use the beginPath and the closePath which is it's comparable or
00:17it's a the opposite of beginning of path we're declaring that this is the end of
00:21a path or an end of a shape. Through that we're going to help go along and
00:26create a utility function which will be the triangle function that will enable us
00:30to draw triangles onto our canvas whenever we'll need to and we're going
00:34to need to do that a lot throughout this title. So let's jump right into it and
00:38let's create our drawing of a triangle. In the last lecture we got our first
00:44introduction to shapes and in this one what I want to do I'm going to go ahead
00:48and create a new flag not really creating the flag yet because we're
00:52going to do that in the next lecture and the flag that we're going to end the end
00:56create is the flag of Guyana. Now before I do that I'm just going to go ahead and
01:00make sure that our grease won't take our center stage but instead of that we're
01:04going to send to it a canvas that is automatically created. Alright so now
01:09that we're creating Guyana let's go ahead and meet the next element. When
01:12we were creating the flag of grease we met the beginPaths and saw it in
01:17action as we drew lines and then stroke them out. In this lecture what
01:21we're going to do is we're going to work with paths again only this time
01:24we're going to create a complete shape and the complete shape that we're going
01:28to be creating together is going to be the triangle. There is no triangle built
01:33in there is a rectangle but no triangle. What would happen if we wanted
01:37to create a fill triangle similar to the fill rect and that's exactly what
01:41we're going to do in this lecture. I'm just going to go ahead and copy those
01:45initial variables that we used throughout all of our projects. So now
01:49that we got that sorted let's get right to it. So step one is just like always
01:54I'm going to go ahead and fill a style or in my case I could do a fill or a stroke
02:00I could do a stroke style or a fill style but what I want to do is I want
02:04to create a fill style because in our last lecture we've met already how to
02:08work with the stroke. I'm just going to set it to be a red color. Okay next
02:14step for us is I'm going to go to our context and I'm going to move our dial
02:17I'm going to move our mouse to some point in our stage. I'm going to go ahead
02:21and position it at our zero at our width of the center of the screen and our Y
02:29to be 20 pixels from above. Okay now that I moved to the position and if I go
02:34ahead here and click on refresh I literally moved and if I click here on
02:38refresh I probably should save first and I click here on refresh. Alright we're
02:42positioned somewhere around this area. Now what I want to do is I want to line
02:46through to here then back here and then back up to create a closed shape. Alright
02:51so let's go ahead. So once I move to this initial position I know that at some
02:56point in the end I'm going to want to return to that exact same position so I
02:59could go ahead there and just set that to be a line two as our last line two.
03:04Okay perfect now there's two line twos that we're going to want to do before
03:08we get to that line. The first of them is we're going to want to move down from
03:11the center to actually to be 20 pixels from our left border and we're going to
03:17want to position our bottom here to be roughly I would say maybe our h minus 20
03:23a little bit underneath our end of our height. Next step for us is also once
03:29we've created that line our next line is going to be a line through so I'm
03:33going to keep that same height the same but I'm going to go to my width
03:37and define it to be the width of just before then. So again so if what we're
03:42actually doing here we're starting from this point we're heading down to this
03:46area and then we're heading down to this area. Last but not least which we
03:51pre-configured originally is to go back to the original position where we
03:54started from and by that we created a closed shape a circle a triangle. All
04:00that's left for us to do to test it out and see that it actually works would
04:04be to probably go ahead to my context and fill it out. Now that's really all I had
04:09to do to create this triangle but again in a real world scenario we're probably
04:14going to want to create more than one triangle which is exactly what we're
04:18going to do in the next lecture. This is not going to work remember in their last
04:22lecture because if I create another path it's going to all override each other
04:25creating all these lines together. So if I go ahead and continue I'm going to
04:29have an issue. So I'm going to go ahead here and making sure that before I
04:33even start drawing I'm going to declare that I'm beginning a new path. Moreover
04:38than that because we don't want users to need to close the shape each time or
04:42start a new one we're going to also declare that we're closing the path just
04:46before we fill it out defining a beginning and an end to our content.
04:50Basically closing our path. So what I'm doing here is I'm
04:57defining this is the beginning of the path this is the end of the path and
05:00okay let's go ahead and fill it or if we wanted to we could have gone ahead and
05:04set a stroke. Now again if we set a stroke we would want to set the context
05:09and stroke style, stroke style and let me set this to be blue and if I go ahead
05:21here and just do a stroke instead of a fill then I'll go ahead here and click
05:24on refresh and we'll see that now I have the stroke of that outline. I could
05:29go ahead oh so if I wanted to and set oh to a stroke and oh to a fill and go
05:35ahead and save and click on refresh and now we'll have the rectangle with its
05:38bounding stroke and fill. So that's really the the in a nutshell how you
05:43create shapes and obviously you could create a lot more dots connecting them
05:47as long as you close them up and then you could fill them up if you're not
05:50planning to close and then you could just go ahead and just create the
05:53stroke. Alright so now that we've created this basic outline what I
05:57really want to do is I want to grab this logic and I want to extract it into its
06:01own utility function which I'm gonna call fill triangle and fill triangle is gonna
06:10accept a few properties and we're gonna walk through them in a second but let me
06:13just go ahead and first just paste right inside of here all this context for a
06:18second and let's go ahead and start with the logic. First of all we want to
06:22send in a context. Next we want to send the x coordinate the first x coordinate
06:27and then the first y and x coordinate and the second y and x coordinate and the
06:32third y and x coordinates x and y I don't know why I'm starting with y. Last
06:36but not least we probably want to set a color as well. Now that we have that
06:40configured all that's left for me to do and let me just go to copy this
06:43really quickly up above here because we're gonna want to use this again in
06:45a second. What I want to do is just replace the elements with their related
06:52items so because we're doing a fill I'm gonna get rid of that stroke and I'm
06:55just gonna set that to be the color itself for the fill and I'm also gonna
06:59get rid of the stroke and I'm gonna go ahead and set that initial item to be x1
07:04and y1 and I'm gonna make sure that I complete it with x1 and y1. Next for us
07:13is gonna be the x2 and y2 so let's go ahead and do that x2 y2. Last but not
07:21least we're gonna go ahead and update our third line to be the x3 y3. So that's
07:27how we created a utility function that will help us create rectangles well
07:32sorry triangles. So let's go ahead now and actually trigger this and call it
07:36and I'm gonna go ahead here and just define it right above here before that
07:39and then we'll delete all that extra information. First step is the context.
07:43Next step is gonna be the actual first coordinate which is our x and y. The next
07:50item will be our next coordinate which is gonna be the second x and y and last
07:55but not least is our third coordinate followed up and completed with the
07:59actual color that we want to define and let's say we're gonna keep with that
08:03red and if I go ahead here click on save and click on refresh we should have
08:08ourselves our triangle again but this time it's gonna go through through our
08:12utility function which we're gonna be using throughout the next lecture as we
08:17create the flag of Guyana. In this lecture we were introduced to complete
08:22shapes and created with it a triangle a utility triangle function. In the next
08:28lecture we're gonna continue down the path taking that triangle logic to
08:32create the flag of Guyana.
