Im trying to animate a svg
path
with animate
tag, following this tutorial from css tricks. I could animate path with css keyframes, and the result is this:
#mySvg path{
animation: scale-path 10s ease-in-out infinite;
}
@keyframes scale-path {
50% {
d: path('M1036,540L883,540L883,693Z');
}
}
<svg id="mySvg" xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
version="1.1"
x="0"
y="0"
width="100%"
height="100%"
viewBox="0 0 1920 1080"
preserveAspectRatio="none">
<path d="M1045,520L1173,558L1184,393Z"
fill="lightblue"
stroke="#eee9ea"
stroke-width="1.51" />
</svg>
But the problem is I cant achieve the same effect animation with animate
tag (its supposed to will be there a lot of path
tags with different animations). Im not sure if this is the correct syntax:
<svg id="mySvg" xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
version="1.1"
x="0"
y="0"
width="100%"
height="100%"
viewBox="0 0 1920 1080"
preserveAspectRatio="none">
<path d="M1045,520L1173,558L1184,393Z"
fill="lightblue"
stroke="#eee9ea"
stroke-width="1.51">
<animate
attributeName="d"
from="M1045, 520L1173, 558L1184, 393Z"
to="M1036; 540L883; 540L883; 693Z"
dur="10s"
repeatCount="indefinite"
values="M1036; 540L883; 540L883; 693Z"
keyTimes="0.5;" />
</path>
</svg>
You are writing the values wrongly, you should pay attention to ,
and ;
. The whole value of the path use ,
as delimiter (ex : M1045, 520L1173, 558L1184, 393Z
) and those values are delimited by ;
inside the values
attrbiute
<svg id="mySvg" xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
version="1.1"
x="0"
y="0"
width="100%"
height="100%"
viewBox="0 0 1920 1080"
preserveAspectRatio="none">
<path d="M1045,520L1173,558L1184,393Z"
fill="lightblue"
stroke="#eee9ea"
stroke-width="1.51">
<animate
attributeName="d"
from="M1045, 520L1173, 558L1184, 393Z"
to="M1036, 540L883, 540L883, 693Z"
dur="5s"
values="M1045, 520L1173, 558L1184, 393Z;M1036, 540L883, 540L883, 693Z;M1045, 520L1173, 558L1184, 393Z"
repeatCount="indefinite" />
</path>
</svg>
Semi-colons (;
) are used as separators in attributes like values
and keyTimes
, to mark the different keyframe values. The number of values in these two attributes should match.
You seem to have replaced commas with semicolons, which is not correct.
If you are animating between two values (A -> B), you only need from
and to
. If you need to animate between a series of three or more values you need to use values
and keyTimes
.
There is no automatic back and forth looping in SMIL animation. So if you were trying to go from A to B and then back to A, you would need to use values
and keyTimes
and list your values in the form "A; B; A"`.
Like this:
<svg id="mySvg" xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
version="1.1"
x="0"
y="0"
width="100%"
height="100%"
viewBox="0 0 1920 1080"
preserveAspectRatio="none">
<path d="M 1045,520 L 1173,558 L 1184,393 Z"
fill="lightblue"
stroke="#eee9ea"
stroke-width="1.51">
<animate
attributeName="d"
dur="10s"
repeatCount="indefinite"
values="M 1045,520 L 1173,558 L 1184,393 Z;
M 1036,540 L 883,540 L 883,693 Z;
M 1045,520 L 1173,558 L 1184,393 Z"
keyTimes="0; 0.5; 1" />
</path>
</svg>
If your animation is linearly paced, and the keyTimes
timings are evenly spaced, like they are here, you don't actually have to provide a keyTimes
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With