I'm trying to animate the line extension.
I already have this solution in css , but I need to do it in javaScript , because this is the only way to get the length of the path that I need to implement the animation.
I think I came very close to the solution, but it does not work! Any ideas?
Below is my code. As you can see, I get the length of the path and assign a strokeDashArray value to this length.
This means that the line will be dashed, but the dotted line will fill the entire line.
The trick is to decrease the strokeDashoffset value, because it determines where the dash begins.
Therefore, if it also starts with pathLength , the line will be completely invisible, and decreasing the value will show the line drawing.
var element = document.getElementById("animpath"); var pathLength = element.getTotalLength(); element.style.strokeDasharray = pathLength; element.style.strokeDashoffset = pathLength; function animateRoute (e) { e.style.strokeDashoffset = e.style.strokeDashoffset - 100; } for (i = 0; i < 100; i++) { animateRoute(element); } Note translator
I chose this post for translation for two reasons: since I often encountered type questions, how to calculate the length of a line using JS and draw it. Secondly, the method of hatching animation using JS seemed interesting to me. This technique, in my opinion, can be used in interactive web design. For example, hatching checkboxes, instead of standard check marks, etc.