I have two SVG rectangles; two of their corners are connected by a SVGLine, and I am trying to animate the whole.
Now the rectangles are moving to a new position using the Element.animate() function (the new positions have to be computed at runtime so I think it is only possible with the animate() function in JS?).
Until that point everything works fine, but when I try to animate the line so that it is still connecting the corners during the animation, it won´t move.
Is there any way to animate the lines movement to the new position? (I can´t just set the attribute to the new position).
If I have to use a <path> or <polyline> or something for that a quick explanation on how I should do that would be great, as path.animate([{points:...},{points:...}],{...}) also didn't move the path as i wanted.
This is a quick code example which I think should work, but the line won't move.
let svg = document.querySelector("#theSVG");
const SVGNS = "http://www.w3.org/2000/svg";
function drawing() {
let rect = document.createElementNS(SVGNS, "rect");
rect.setAttribute("x", 100);
rect.setAttribute("y", 100);
rect.setAttribute("width", 100);
rect.setAttribute("height", 100);
rect.setAttribute("stroke", "black");
svg.appendChild(rect);
let rect2 = document.createElementNS(SVGNS, "rect");
rect2.setAttribute("x", 10);
rect2.setAttribute("y", 10);
rect2.setAttribute("width", 50);
rect2.setAttribute("height", 25);
rect2.setAttribute("stroke", "black");
svg.appendChild(rect2);
let line = document.createElementNS(SVGNS, "line");
line.setAttribute("x1", rect.x.baseVal.value);
line.setAttribute("x2", rect2.x.baseVal.value);
line.setAttribute("y1", rect.y.baseVal.value);
line.setAttribute("y2", rect2.y.baseVal.value);
line.setAttribute("stroke", "darkgray");
svg.appendChild(line);
rect.animate([{
x: rect.x.baseVal.value
}, {
x: '200px'
}], {
duration: 5000,
iterations: 1
});
rect2.animate([{
y: rect2.y.baseVal.value
}, {
y: '300px'
}], {
duration: 5000,
iterations: 1
});
line.animate([{
x1: line.x1.baseVal.value,
y2: line.y2.baseVal.value
}, {
x1: '200px',
y2: '300px'
}], {
duration: 5000,
iterations: 1
});
}
drawing();
<svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 700 360" id="theSVG">
</svg>