How to Reuse Image Elements in SVG
Instead of repeating the same code over and over again we can also reuse image elements.
How to draw a Sun icon with SVG
After drawing a Moon Icon it’s time to draw a Sun icon. You can also check out this YouTube tutorial that covers these icons in more details.
Let’s draw simple sun icon. We start with a circle element to draw the core of the sun, and we draw a line to draw a sunray.
Now, once we have one ray, we can reuse the same line element to draw the others. We can give this ray an id
and reuse it with the use
element.
Then, we can move the lines to their correct position with the transform
command in the same way we did with the star example, except that this time, we increment the rotation by 45 degrees for each ray.
You can use the Sun icon above and the Moon icon we are about to cover later to create a toggle button that can switch between bright and dark modes. Check out this YouTube tutorial to learn how.
How to Draw a Snowflake with SVG
We can use the same technique to define a branch of a snowflake and then reuse it six times to draw a snowflake.
How to Draw Basic Paths with SVGThe branch is defined as a path
. Earlier we already covered how to draw basic paths. Here we draw the branch in a similar way. We can draw a simple line – the main branch – by using the move to (M
) and line to (L
) commands:
Then we can continue drawing, and add a side branch by adding another move to and line to command. Hover over the coordinates in the code or on the image to see how they are positioned.
We continue adding side branches with the move-to and line-to command. The finished branch looks like this:
Note that we also assigned an id
for this path. Once we define the branch, we can reuse it multiple times with the use
command.