Now rectangles and circles are nice, but if you're opening any SVG, then you're going to find a path element inside of it. And the path element is basically a freeform element, which we can build up from multiple path segments. We can have straight lines and curves and arc and so on. So let's try to break it down and see what's inside the definition of a path.
Here we have a simple line. And this line is already containing two separate segments. First we have a moveTo segment, and then we have a lineTo segment. Each segment always defines the end position, so the lineTo segment defines where does the line end, but it doesn't define where does it start. So that's why we have a previous segment with the moveTo that will describe where do we start things. So this one is basically a capital M, each segment always starts with a letter. Then we just have a coordinate, which says that we just move here, and from here, we draw a straight line here.
Now of course we can continue this, and we can just keep drawing straight lines to different coordinates. But what we can also do is instead of continuing this path, we can also just have another moveTo command and have a hamburger menu icon. So a path doesn't have to be continuous.
Now for styling, we use the same styling properties we had before. Here we don't have a fill property because this path is not surrounding any region. So even if you don't set the fill property, or if you would set it to any color, it wouldn't actually fill anything. But we set a stroke and strokeWidth to a relatively thick line, and then we also set this strokeLineCap property to have the ends of these lines rounded.
Now after talking about these basic path segments, let's talk about transformations. As I said before, every shape is defined by coordinates, and we could already draw this because we already know the circle element and we know how to draw a basic line, but we would need some trigonometry calculations to figure out this position, for instance. And instead of that, we can use transformations.
So we just start with a circle and then we have a simple line, which is just going from here to here. That's it. It has a moveTo and a lineTo segment. Then what we can do is we can assign an ID for it, and then we can reuse this path with the use element, which refers back to the path by ID, and it applies a transformation. So for each of these sun rays, we just move it, we just rotate it by 45 degrees. This way we don't have to figure out where does, where is this position actually or this position, we just keep rotating them. It's also important to note that this works in this case because the center of the coordinate system is in the middle of the image, so that's the zero, zero coordinate, because the view box is defined in a way that it starts at minus 15, minus 15, and it ends at 30, 30.
Comments