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

Creating a polygon

Up until now all the shapes we created in SVG where pre-made shapes. In this lecture we will learn how to create a polygon by defining as many points as we want into it.

Keynotes:

  • Creating a polygon.
  • Adding Text.
  • Transforming in SVG.

Video Transcript

00:00Now that our SVG is integrated into the HTML5 document,
00:05it's time for us to create another piece of art.
00:08We're going to start by creating a polygon and add text to it,
00:12and even transform it using SVG in
00:15a very similar way to how it was done when we were working with CSS3 transformations.
00:21Let's jump right into it and let's create that polygon.
00:24In the last lesson,
00:25we integrated an SVG into our document.
00:28It's invisible right now,
00:30so there's nothing we can really see.
00:31Let's go ahead and actually put some content inside of it as we learn a bit more about SVG.
00:37In our SVG area,
00:38I'm going to start with something relatively simple,
00:40which is the text item.
00:42The text item accepts parameters and these are,
00:45by the way, parameters that you could put in any item.
00:48But I'm going to go ahead and just create a text element.
00:51This is our first element that actually has
00:53content and I'm going to write here,
00:55my name is Ben.
00:57My name is Ben, that's a text element.
01:00If I just go ahead and save this and refresh and go back into my application,
01:04we'll start seeing something already, not that noticeable,
01:07but we're going to, let's go ahead and also make sure that we
01:09could position this text in a place where we could actually see it.
01:12I'm going to go ahead here and I'm going to set an X coordinate,
01:16this item and set it at the position 50,
01:19and I'm going to set a Y coordinate and position in the position 50 as well.
01:23Let's go ahead and click here on refresh,
01:25and I should now see our copy positioned somewhere
01:27because before it was just out of our view.
01:30Now that we see our copy, I'm fine with the color of it,
01:33but I'm going to make sure that its color is really defined.
01:36I'm going to set the fill to be black.
01:39The next step that I want to do is besides setting that my name is Ben,
01:42I'm going to go ahead and also create above it a polygon.
01:46Now, a polygon contrary to all the other shapes we've created previously
01:50is a shape that we could actually define
01:52as many points as we want to have on it.
01:56The way it works, and I'm going to just go ahead here
01:58and just create this polygon, is by defining points,
02:02and we're going to create those points and define them in a second,
02:04but also let's make sure that we add a style to it.
02:07I'm going to make sure that it has a fill,
02:09and I already chose a color for that fill,
02:11so I'm just going to go ahead and copy that color.
02:13It's going to have this yellowy tint, so it has a style.
02:18Let's go ahead and start working with points,
02:19and I'm going to start with by positioning our item.
02:22I'm going to say first point in our positioning
02:24is going to be 50-50.
02:26Then I'm going to put a space, that's for our X and Y coordinates.
02:29The next point that I want to have, I want it to be at 150.
02:33I'm going to put another space here,
02:35and the next point that I want to have is going to be at maybe,
02:38let's say 100 and 100,
02:41and those are all the points that I want.
02:43What I'm actually doing here is I'm creating here a triangle of sort,
02:46and if I go ahead and save this and click here on refresh,
02:49we'll see that I'm going to have an interesting shape,
02:51a triangle sitting right here
02:53that is going to go between the points that I've defined.
02:56You could create many shapes this way using polygons,
02:59but I'm going to go ahead and what I already pre-created,
03:02a polygon that I thought of already earlier
03:05and figured out the right points for it.
03:07If I go ahead here and I'm just going to paste all those points
03:09and save it, and you go back and click here
03:11on refresh inside of your application,
03:13you'll see that I've created here a lovely star.
03:16Now, I would love for the star that I've just created
03:19and play around with whatever values you want to really have,
03:22but I really want the star to be in the center
03:24of this squiggly, squiggly, squiggle that's going on there.
03:27Unfortunately, right now, it's not in the center
03:30and I really want it to be,
03:31but I don't want to need to go ahead and start manipulating
03:34all these values to try to figure out
03:36how to have that happen.
03:38Now, if you recall from CSS and styles,
03:41we could use a style to do this,
03:43or we could use a property in SVG,
03:47which is called transform.
03:49Exactly like the transforms that exist in CSS3.
03:53And what I'm going to do with my transform,
03:55I'm going to call the translate function.
03:59And in my translate function,
04:00I'm going to put here an offset and define
04:02where do I want the offset to go?
04:04Now, again, if I send zero, zero into that translate
04:07of the transform, what I'm actually doing is very simple.
04:10I am doing nothing because I'm not moving it anywhere.
04:14What I want to do is I want to define
04:15how much do I want to move my X?
04:17How much do I want to move my Y?
04:19When I'm looking at this right now,
04:21I think for my Y coordinate,
04:22I probably want to push myself down by roughly 30 pixels
04:26for my X a lot more.
04:28So I'm going to go ahead here
04:28and I'm going to set here, let's say 400,
04:31and I'm going to set here maybe 30
04:33and see if the value somehow works for me.
04:36And if I go ahead and do that,
04:37my copy is sitting exactly where I want it to be now,
04:40more or less.
04:41And let me go ahead and do the exact same thing,
04:43O2 for our polygon itself.
04:45So I'm going to go ahead here,
04:47after my polygon styling,
04:48I'm going to create a transform.
04:50We're going to click here on refresh.
04:51And now we're starting to be in the area
04:54and region that we want to be.
04:56I could also go ahead and in my SVG inside of my editor,
05:00inside of my element here,
05:01I could go ahead and play around with the translate values
05:04and literally just go to this translate and say,
05:07let's see, maybe this will work better
05:10or maybe this will work better.
05:12So let me go here to this item.
05:13I could go ahead here and I want it to be maybe,
05:16let's try 370.
05:18That looks a lot better, 370, but let's try 360.
05:22So 360 seems to be hitting the spot.
05:25And for our Y coordinate, let's push it down by 20,
05:27see if that works, so perfect.
05:29So 360 by 20 is what we need.
05:32So let's set it to 360 by 20.
05:37And if already added, I could also add more transformations
05:40just like in CSS.
05:41Let's go ahead and rotate everything
05:43by let's say five degrees.
05:45And I'm going to go ahead and save that,
05:47click here on refresh and we'll see that our item
05:48should be sitting in a much better spot
05:51and it's going to be rotated.
05:52And that's basically the art of working with SVG.
05:55There's a lot more things you could do.
05:56Check out our references where you could learn
05:58a lot more different elements
06:00and things you could add inside of SVG,
06:02but that really ends our lecture processes,
06:06last few lectures that are really related
06:08to how to create SVG and integrate it into HTML5.
06:14In this lecture, we created a polygon
06:16and even added a transformation to it using SVG.
06:20In the next lecture, we're going to go and take all of that
06:22and actually animate it.

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!