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

Creating Multi Pointed Shapes ( Kingdom of Bahrain )

In the last few lectures we worked with our first closed shapes the triangle (a 3 point shape). In this lecture we continue down the road while create a multi shaped object. By the end of this lesson you will have a deep understanding of how to work with paths to create complex shapes.

Keynotes:

  • Creating multi pointed shapes.
  • Using loops to create patterns.
  • Creating the flag of Bahrain.

Video Transcript

00:00Our next step, and with it wrapping up this section
00:03about shapes, we're gonna go ahead and create a shape
00:06that has a lot of points, which is the flag of Bahrain.
00:10Through it, we're gonna take a look at how to create
00:13multiple pointed shapes, and that means we're gonna take
00:16advantage of a loop to create a pattern of sort
00:19to be able to draw out all the lines that we need
00:21to create this relatively complicated shape,
00:24or the most complicated that we created to date.
00:27So let's go ahead and create the flag of Bahrain.
00:31Before we wrap up this lecture and section,
00:34let's go ahead and go to the kingdom of Bahrain
00:38to create a structure that is a little bit more complicated,
00:42or much more complicated than we've seen so far.
00:45So I went ahead and cleared our main canvas,
00:47let's jump right into it, and I'm gonna go ahead
00:50and create the Bahrain flag by creating
00:54the new Bahrain method, and let me go really quickly,
00:57and I'm just gonna put the startup logic
01:00right there in place before we start working.
01:02So around line 105, I'm gonna put our standard logic
01:06that we've put throughout all of our sections.
01:10Beautiful, so now that we have our basic construct
01:12in place, what is the first thing we wanna do?
01:15Let's take a deeper look into that flag.
01:18So when we take a look at this flag,
01:19you'll notice that it's constructed
01:21out of a background red color.
01:23Let's go ahead and set that background color,
01:25because that's easy enough to do.
01:27So let's go ahead here, and let me do set the context.
01:30I'm gonna set our fill style,
01:35and I'm gonna set that fill style
01:36to be the red color that we have right there.
01:38So let me just go ahead and paste
01:40that pre-configured color that I have.
01:42And last but not least, all I have to do
01:44is create a fill rect, as we've done many times before,
01:47and define it for the full width
01:49and height of our application.
01:51Beautiful, so we have a great starting point.
01:53Click here, refresh.
01:54We have the main core color of the flag.
01:58The next step is we wanna create here a square of sorts,
02:01you could say, or a rectangle of sorts,
02:04that is gonna take a quarter of,
02:06or maybe more accurately,
02:07yeah, it's about a quarter of the screen.
02:09So I would put here one, two, three, four.
02:12We have basically a quarter plus all these zigzags.
02:14But let's start with that quarter rectangle, easy enough,
02:18but we're not gonna create it as a draw a rect.
02:20Instead of that, we're gonna draw it out step by step.
02:24So we're gonna start off by defining
02:25in our context a new fill color.
02:28So I'm gonna fill style, let me just copy that.
02:31And we're gonna set that fill color to be white.
02:35Beautiful.
02:37The next step is I'm gonna go ahead and start a new path.
02:39So I'm gonna go ahead there and define a new begin path.
02:43And the next step is I'm gonna go ahead and draw a line.
02:46So I'm gonna line two,
02:47to where do I wanna draw my line?
02:49Well, I don't have a value here pre-configured.
02:51So let me go ahead here and create a helper variable.
02:54I'm gonna call it x,
02:55because that's gonna be my base x throughout the sample.
02:57And this base x, as we said,
02:59is gonna be in the position of a fourth of our width.
03:02So I'm gonna divide it by four.
03:04So where do I wanna go to?
03:05I wanna go to my position, my core x, my base point,
03:09which is basically that base point
03:11is basically somewhere right here.
03:14Okay, so next step is once I've created that base
03:18and I walked to, and by the way, my default,
03:20because I'm starting my application, is zero, zero.
03:23So I'm going from zero, zero to that position.
03:25Okay, beautiful.
03:27The next step after that,
03:28and let me just break a few lines here,
03:30is I'm gonna take a line all the way down.
03:31So I'm gonna go to my context
03:34and I'm gonna set a new line to,
03:36and then I'm gonna go to that full height.
03:41And next step is once I've done that,
03:43I'm gonna go ahead and also,
03:45oh, and by the way, was that a good move?
03:47Probably not, I believe not,
03:51because I wanna stay in the width point that I was in.
03:53So I'm gonna go ahead here
03:54and just set it to be the full width.
03:56So I'm going to that x position and the full height.
04:00Then I'm gonna take a line back to take a step back.
04:04And last but not least,
04:05I wanna go all the way to the beginning point,
04:07which is right here.
04:10Okay, so I created all those steps that I needed to do.
04:13All that's left for me to do is to test this out,
04:15is to go ahead and call the fill
04:18and literally see if I just drew a rectangle.
04:21If I've done, I'm already closer to creating our flag,
04:23another step closer.
04:25So you can see that I have here a small issue,
04:27so I'm gonna have to make sure that I sort it out.
04:29I'm missing probably a line here
04:30or I'm not going for one more line that is missing.
04:33So let's see, what did I miss?
04:36So what I miss is very simple.
04:37When in this last line,
04:39I actually traveled back to the second point
04:41instead of to the first point.
04:43So what I wanna do here,
04:44instead of going to that X coordinate,
04:46I wanna go back to the zero, zero point.
04:48Literally what I did is I came from this point
04:50and went all the way here
04:52instead of going to this point.
04:54So let's go ahead and click here on refresh.
04:56Beautiful, and now we have a beautiful rectangle,
04:59but we're not done
05:00because we actually wanna also add these triangular shapes
05:02into our element.
05:04So let's think, first of all,
05:05how many shapes are we gonna create?
05:07One, two, three, four, five.
05:09We're gonna need to create,
05:10basically, we're gonna need to take the point
05:13from here to here to here to here to here to here
05:16to here to here to here to here,
05:17zigzagging back and forth.
05:19Where do we wanna do that?
05:20We wanna do that exactly right here.
05:23And we wanna do it instead of moving that line to here.
05:26So basically, instead of this line,
05:27instead of taking our line from this point
05:31all the way down to this point,
05:32we wanna basically create this zigzag rule
05:35which will end us in that area.
05:37So I'm gonna go ahead here and delete that extra line
05:39there and break everything there.
05:41And I'm gonna create a for loop right in the middle,
05:43setting my var i to be smaller than five
05:47and growing until we've reached five positions
05:50from zero through four.
05:52All right, so now for us to be able to do this,
05:54I'm gonna have to create two lines.
05:56One line, in each loop,
05:58I'm gonna have to create two lines.
05:59One line would go forwards and another would go back.
06:02And each time, we'd wanna travel half through
06:05the first line, half through the height of this area.
06:08So I'm gonna need a height reference.
06:10So let's go ahead and create very quickly another variable.
06:13And I'm just gonna call it y height.
06:17And for our y height, what is that y height?
06:19How much do we want it to be?
06:20We actually want it to be the actual full height divide by five.
06:26Now there's one more variable that we're gonna need
06:29and that variable is also gonna be the distance
06:31that we're gonna go in our x zone.
06:33And that distance is gonna be half of the size
06:35of our rectangle.
06:37So let's go ahead and also add that helper variable.
06:39And I'm gonna call it x width.
06:41And I'm gonna take that value of x
06:43and divide it by two as well.
06:45Creating the two variables that I'm gonna need,
06:48one that's gonna control my width
06:50and the other which is gonna control my height.
06:52Beautiful.
06:53So let's go ahead and start building.
06:54It is actually quite simple
06:56once you get into the logic behind here.
06:58And literally what I'm gonna do is
07:00from that point in time,
07:02I'm gonna start from that specific point,
07:04I wanna travel basically half the height
07:06and the full width.
07:08So let's do that.
07:09If I wanna travel, I'm gonna call that line two.
07:12First property is gonna be our x base coordinate
07:15and we wanna add to it what?
07:17The actual new x width variable that we've created.
07:21The next step is we wanna travel half the height.
07:23So I wanna go to my I coordinate,
07:26which right now is at zero
07:27but each time is gonna be duplicated.
07:29I'm gonna go to that I coordinate
07:30and I wanna start from starting from zero.
07:32I want it to always be a half a point
07:34more than where I'm at actually in my I coordinates.
07:38And how much do I wanna duplicate it by?
07:40I wanna duplicate it by the actual height itself
07:43of our element.
07:44So again, what's going on here,
07:45if the I equals zero, which is our first case,
07:49what's gonna happen is we're gonna basically
07:50travel down to here
07:52because we're taking one step really of our height
07:55or a half a step of our height.
07:56If this is the full height,
07:58we're gonna go a half a step
07:59because we'll only have your half.
08:01But each travel is gonna be half
08:03and then one and a half
08:04and then two and a half
08:05and then three and a half
08:06and then four and a half,
08:08which is exactly what we want.
08:10But we also wanna travel back.
08:11So that was the first step.
08:13The next step, let's go ahead here
08:14and just copy this.
08:16The next step is where is our X coordinate?
08:18It's without that width.
08:19We wanna go back to our base X.
08:21We wanna go back to that base X position
08:23that we were in.
08:25For our Y coordinate though,
08:26we wanna go with the full height,
08:28the full height of that Y.
08:29So we don't wanna plus half it,
08:32we wanna plus one it.
08:33And there we go.
08:34We've just created the steps that we need.
08:36And if I go ahead and save this
08:37and go to my flag and click on refresh,
08:40we should have ourselves now the complete flag
08:43of the kingdom of Baharan.
08:47In this lecture, we created the flag of Bahrain,
08:49our most complicated shape to date.
08:51And with it, we completed this section.
08:54In the next section,
08:55we're gonna go ahead and move into drawing with angles.
08:58So see you in the next lecture
09:00and next section that will be dedicated to angles.

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!