Meeting canvas paths
In the last lecture we started to create the flag of Greece. We are still short on a few lines of code to complete this flag but before we can complete creating the flag we need to meet another new concept paths. By the end of this lesson you will understand how to work with paths in HTML5 Canvas.
Keynotes:
- Moving from stand alone lines to paths.
- Explicitly defining the start point of a shape.
- Drawing out the flag of Greece.
Video Transcript
00:00the last in the last lecture we created a line and in this one we're gonna move
00:05into paths and through that we're gonna move from standalone lines into paths
00:10of multiple lines in the process we're gonna explicitly be will define the
00:16beginning point of the creation of a shape to tell the canvas hey from this
00:20point and onwards we're actually creating a joint path we're creating a
00:24shape we'll together draw out once we get this concept we'll go ahead and draw
00:30out the flag of Greece so let's jump right into it before we continue the
00:36creation of the flag of Greece I wanted to teach you about a new method that we're
00:41just gonna need to be able to create the flag to showcase the need of it I'm gonna
00:45go ahead after we created the last stroke of our element and I'm gonna go
00:49ahead and grab the styling or really any property I could go to the line with
00:55property as well and if I go to the line with property and I'm gonna go ahead and
00:59change it to be just one in size of one and I go ahead and call the stroke again
01:05you might think well this shouldn't affect our lines or maybe let's make
01:09it even more visual I'm good gonna go ahead and change the stroke style to
01:14just be a really really red red now you might think that okay so if I do this
01:20this will only apply to new lines that are being created but that's not true
01:24even though we called stroke every time we call stroke it's gonna run through all
01:29the logic all the line logic all the fill logic all the logic that is sitting
01:34in memory waiting to be applied now because it's still waiting to be applied
01:38these items still are waiting to be applied even if they were applied once
01:42that doesn't mean we're done with them so if I go ahead and click on here on
01:46refresh you'll see that even though these lines were created previously they'll be
01:50redrawn which is exactly what we don't want because now we want to put a
01:53rectangle on top and then draw a X on top of it or a cross on top of it so we
02:00want to do is we want to basically tell canvas the API hey by the way all the
02:04lines we had before we're kind of done with them and we're starting a new
02:08group of lines a new group of paths so to do that I'm gonna call the begin path
02:15and the begin path method all that literally does is basically clears all
02:20the things that were previously and gets us ready to start a new path now by
02:25doing that if I click on refresh we'll see that even though I change the color
02:28it's not gonna affect anything because those lines have then been flushed out
02:32of memory you could say so now that we flushed our memory and we're ready to
02:36start drawing let's go ahead and continue drawing the actual flag of
02:40Greece the the top area you know and I'm gonna start with drawing a rectangle and
02:47if I look at this you'll see that the width of it is five times the width that
02:51we were working with so I'm gonna go ahead here and just create here some
02:54sort of I'm gonna call this rectangle width and I'm gonna just create here a
02:58new variable and take the line height and just duplicate it by five so I have a
03:02local reference to the actual height that I'm working with so let me go ahead
03:07here and I'm gonna go to the context I'm gonna fill a rectangle and I'm gonna go
03:12ahead and start from position 0 0 and I'm gonna make sure that my rectangle
03:16width is gonna be used from both my width and the height and if I go ahead
03:21here and I save this and I run my application we should now see that our
03:25rectangle should be filled out with the blue color that was previously
03:28defined now recall when we're working with fill it's gonna be applying the
03:33fill style when we're working with stroke it's gonna apply the stroke style
03:36in this case because we're working with the fill rectangle the fill rect we're
03:40gonna fill it up with the blue that we were working with beautiful the next
03:45step that we want to do if we're gonna look at this we want to move our pointer
03:48to be in the beginning of that or in between the white line so if it was
03:52like 0 1 and 3 so it's more or less around line 2 and we want to line it
03:57through from the 0 0 until the full width of our rectangle so let's go
04:01ahead and do that now I'm gonna go ahead and I'm gonna leave the stroke here
04:05making sure that anything I add before that will be drawn in immediately as we
04:10draw it and I'm gonna go to my context and I'm gonna move my line to where do I
04:14want to move it again I want to move it to my rectangle with half of it so
04:19literally what I'm doing with our height it doesn't really matter at the
04:22stage but for our X coordinate again we're moving our X coordinate to the
04:260 okay and the Y coordinate we're going halfway down beautiful so now that we
04:31configured where do we want to move to it's time for us to also draw that line
04:35in so I'm gonna go ahead here and I'm gonna call the line 2 and this time
04:40around I want to go for the full width of our rectangle going sure making sure
04:45that I'm gonna create that full line if I go ahead and click here on refresh
04:49we'll see that I'm drawing the line exactly in the spot that it needs to
04:53last but not least we want to do the exact same thing for our other side of our
04:58cross and to do that I'm just gonna go ahead and copy this logic and really all
05:02I want to do is flip the logic so instead of having the X at 0 I'm gonna have the
05:07Y starting from 0 and I'm gonna also have it end in the full height or the
05:12full width because in our case it's the same and if I go ahead here and click
05:16on refresh I should have the full flag of Greece in all of its glory ready to
05:21be waved so there we go we completed creating the flag of Greece in this
05:28lecture we were introduced to paths and the next natural step beyond that is to
05:33go ahead and draw out something complicated so we're gonna go ahead and
05:37create triangles which don't exist natively in canvas using paths
