Drawing a circle in SVG
In the last lesson we created an SVG image and filled it up with a red rectangle. In this lesson we will learn how to add more elements into our stage starting with a circle. Through this sample we will learn about how stacking works in SVG, positioning of elements and how to work with SVG styles.
Keynotes:
- Creating a circle.
- Positions: relative and absolute.
- Using SVG styles.
Video Transcript
00:00In the last lecture, we created a rectangle inside of SVG.
00:04In this one, we're gonna go ahead and create a circle.
00:07In the process of creating that circle,
00:10we're gonna learn and talk about positions
00:12that are relative and positions that are absolute.
00:15We're gonna also talk about
00:16how to define and use SVG styles.
00:19It's a very interesting topic
00:21that will take you a little bit deeper
00:22into the world of SVG.
00:24So let's jump right into it
00:25and let's start drawing our circle.
00:28All right, so let's continue exploring SVG.
00:31Let's jump right into it.
00:33And what I wanna do next is I wanna draw out a circle.
00:36Now, notice how I'm positioning my circle
00:38underneath the rectangle.
00:40And by doing that, what I'm basically saying,
00:42I wanna, just like in HTML,
00:43I'm gonna put it basically on top of it.
00:46As in SVG, we don't have positionings.
00:49They're not automatically stacked like it is in HTML.
00:52I needed to find a position for an element,
00:55especially if that element does not take the full screen.
00:58But let's go ahead and first create our circle
01:00and the most critical, most mandatory parameter
01:03is the radius.
01:04I'm gonna go ahead and define a 50 radius,
01:06creating a circle that has a length of 100 pixels.
01:10And let me go ahead and click here on refresh
01:12and notice what's gonna happen.
01:13By default, my circle is gonna be positioned
01:16at position zero, zero.
01:18But what I really wanna do
01:19is I wanna put it in the center of our screen.
01:21To do that, I'm gonna go ahead and add two new parameters.
01:24One is called CX.
01:27And for CX, which stands for circle X coordinate,
01:30so or center X coordinate, where do I want it to be?
01:34In this case, for my width, I want it to be 250.
01:37And for my Y, my center Y point, I want it to be,
01:40oh, let me just wrap it inside of a quotation.
01:44For 450, half of that would be 225.
01:47And if I go ahead here, click here on refresh,
01:49we'll see that our circle is gonna be positioned
01:50nicely in the center of the screen.
01:52Now, in reality, what I really want to happen is
01:55I want my circle to take roughly, let's say,
01:58I want it to be about 25% of the size of our screen.
02:03I don't have to use absolute values like I've done here.
02:06I could also put your percentages.
02:08By setting a percentage, I'm saying I want it to be
02:1025% of the bounding area that it is within,
02:13which is the full SVG, in this case, 500 pixels.
02:17So it's gonna go ahead and define it to be
02:19with a radius of 25%,
02:21making our circle a lot bigger,
02:23which is exactly what I want to happen.
02:25Now, beyond that, I want to make sure that
02:27if I go ahead and I change my width of my canvas,
02:31I would love for everything inside of here
02:33to refresh and position accordingly, but it's not.
02:36So what I'm gonna do next and what I want to do next,
02:38I want to go ahead to all these absolute values here,
02:41such as our rectangle, and I'm just gonna say,
02:43hey, I want you to be 100%.
02:46For the stroke width, I'm gonna leave it
02:47in that 10th value, but for our X and Y coordinates,
02:50I'm gonna say for my circle, I want it to be in the center,
02:53center 50% and 50%.
02:55Now, by doing this and using this relative measurements,
02:58no matter what size our canvas is gonna be,
03:01as I'm gonna change the size of it,
03:03maybe I'm gonna make it 200 here,
03:04and maybe I'm gonna make it 800 here,
03:06it's gonna automatically resize and position and align
03:09based on the values that we've provided.
03:12By the way, in this case, our circle is way bigger
03:14because it's reliant on our width and not on our height.
03:18So 25% of our width gives us a really big circle.
03:22But in this case, I'm gonna just go ahead here
03:23and just set it back to be 450,
03:25and I'm gonna set my normal width to be 500
03:28as I initially intended it to be.
03:30Now, what we've just done is we created a circle
03:33that is sitting above our rectangle by default
03:36because it was last added in.
03:38If our rectangle happened before our circle,
03:40what would actually happen is our circle
03:42would be invisible to us because it would be sitting
03:45behind our rectangle.
03:47Obviously not something that we would want
03:49to actually happen to our circle.
03:51Before we wrap up this topic of basic shapes
03:54and how to work with the basic shapes in SVG,
03:56I just wanna say also that there's one more way
03:58for us to approach and talk to our elements.
04:01In this case, I created a circle
04:03and didn't define a fill or stroke for it,
04:05and because of that, it got a default
04:07of a fill that was black.
04:10What I wanna do is I wanna go inside of here
04:11and I'm gonna use a style this time around.
04:14Although I could have used the fill stroke
04:15and stroke with, I could also use styles
04:17and define a style and say, hey, by the way,
04:19your fill, you're gonna be actually red
04:22and your stroke, let's say you're gonna be black.
04:27And notice how I'm just positioning all these items
04:29in the syntax that is more, oh, not equal,
04:32we wanna put here, colon, which is familiar
04:35for anyone that is familiar with CSS.
04:37So this is basically CSS.
04:39Our stroke and fill are basic CSS.
04:42Now I'm gonna go ahead and actually put here a value
04:44that makes a bit more sense.
04:45How about I'm gonna put here 550000,
04:48so it's red, but not very red,
04:50and I'm gonna also define a stroke, stroke with,
04:56and I'm gonna set that to be 10 as well.
04:58And if I go ahead and save this and click on refresh,
05:01you're gonna see that now we're gonna have a stroke with
05:04that is larger now.
05:05If you're asking yourself what is the logic
05:07behind using a style versus not using a style,
05:09one is you have that convenience of deciding.
05:13But moreover than that, when you're working
05:15with styles, you're not talking directly to an object
05:18anymore, you're really talking in general,
05:20you're talking about a style that you're adding
05:22in in general.
05:23In many ways, you could create CSS rules
05:26that would then be applied to a lot more
05:28than just one thing when you're working with styles,
05:30as you recall from working in HTML.
05:32And the advantages of that are quite big.
05:35Right now we're using styles and approaches
05:37that are directly associated to the items,
05:39but having styles means that we could have
05:40also external styles that control our elements,
05:44which we're gonna talk about that in a future lesson,
05:46not that far from now.
05:49In this lecture, we learned quite a few things
05:51in the process of creating a new circle.
05:54In the next lecture, we're gonna take everything
05:55we've created so far and integrate it into HTML5.
