Create Svg Line Animation
Learn to create svg line animation
Requirements: To create a svg line animation, you need to know basic html & css.
Now we need a svg path. so let's just take a straight path for now.
Here, we are creating a svg with a width of 200px and a height of 100px, and what is M & H ?
- we are going 50px down from the top by changing y-axis.
- M is used to move the pen to the point, in this case it is
0 50
(x y). - H is used to draw a horizontal line from the current position (Which is M 0 50 in our case) to the given coordinates
100 50
(x y), which is straight line to x-axis.
Now Let's see how this line looks like.
So, we have a line now.
Let's try to animate this line - with a light moving from left to right, To animate this we need to use few things.
- A circle tag
- A path mask
Let's start with a circle tag.
Here, we are creating a circle with a radius of 20px, and a fill of white.
Now, let's see how this circle looks like.
Amazing - we got a cute circle right there in middle beacuse we are moving circle -50% from x-axis and -50% from y-axis - from original 0 0
position.
Now, we need to create a path mask. here is the code for that.
Did you notice something similar to our line code? Yes, we are using the same path which we used to create a line - but this time it's a mask and it's invisible.
- Note: make sure to define the mask in
<defs>
tag.
Let's combine all code and see how it looks like.
Here, we are using the path id circle-path-mask
to mask the circle. and we are using the url()
function to reference the mask.
Woah - we got a circle with mask applied to it.
Now, Our last step is to animate the circle, from left to right. we will be using CSS
to animate the circle.
Now lets apply this class to our circle tag.
Damn - its moving? but it's not moving from left to right. its moving from center to right.
To Fix this lets change -50% to 0% from cx
attribute from our circle tag.
Hehe - it's moving from left to right. That's it.