M
M
madc0de2020-03-17 02:53:47
JavaScript
madc0de, 2020-03-17 02:53:47

Is it possible to route an element using path in pure javascript?

There is a svg in which the path / route is built through path d
And for a day now I can’t understand how simple and without problems and convas to make it follow the element I need. I ask for help because there are no solutions on the network, either complex or incomprehensible

Example: There is a svg path curve, using svg.getTotalLength() I get its length 6000 and this is 100%
And for example I need to move the object by 20%, that is, the element must retreat by 1200 pixels along the way
The svg itself has a viewbox 0 0 1920 8900
and the resolution on which I'm testing is 1222

var svg = document.querySelector('.teams')
var x = svg.getPointAtLength(1200).x;
var y = svg.getPointAtLength(1200).y;

document.querySelector('.star').setAttribute('y', y)
document.querySelector('.star').setAttribute('x', x)

I get x - 1570, and y - 6100
values ​​are somehow hellishly large, provided that the parent element of this svg is 1222x4600

How can I make a simple rendering of an element on the route by setting the desired variable?
Maybe there are libraries with examples. I tried motion.js and everyone is trying to position the element through the usual transform relative to the entire page, when, as the route itself, I give huge values, so I suspect because of the viewbox. And what will it give if wrapped in convas and how it will look. Please help, hands down, but the task is interesting

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question