I'm working on an SVG animation. But I've encountered a problem... I want to animate a changing gradient offset in my SVG. I use this SVG for this. I have already animated changing colors animation but I can't change the offset of the gradient.
<svg className="SvgContain" width="923" height="382" viewBox="0 0 923 382" fill="none" xmlns="http://www.w3.org/2000/svg">
<defs>
<linearGradient id="gradient1">
<stop className="stop1" offset="0%" stop-color="red">
</stop>
<stop className="stop2" offset="100%" stop-color="blue">
</stop>
</linearGradient>
</defs>
<path className="SvgPath" id="PolygonePath" d="M0.5 381.5L382 0H923V381.5H0.5Z" fill="url(#gradient1)">
</svg>
I tried to use the <animate></animate> tag for creating the effect want to do but it doesn't work. I used this code for this. I asked ChatGPT if the animate tag can handle the animation of the offset. It says yes, I followed the instructions but the animation didn't work.
<svg className="SvgContain" width="923" height="382" viewBox="0 0 923 382" fill="none" xmlns="http://www.w3.org/2000/svg">
<defs>
<linearGradient id="gradient1">
<stop className="stop1" offset="0%" stop-color="red">
<animate
attributeName="offset"
values="0%; 100%"
dur="5s"
repeatCount="indefinite">
</animate>
</stop>
<stop className="stop2" offset="100%" stop-color="blue">
<animate
attributeName="offset"
values="100%; 0%"
dur="5s"
repeatCount="indefinite">
</animate>
</stop>
</linearGradient>
</defs>
<path className="SvgPath" id="PolygonePath" d="M0.5 381.5L382 0H923V381.5H0.5Z" fill="url(#gradient1)">
</svg>
I'm not sure if this is the animation you meant... Take a look at these two methods. I assume using
JSis not the problem. In my opinion, this is a better solution because you can control the animation more. Below is a variant withJSandCSS. TheCSSis a bit wild but it's a matter of adjusting the frames.JS option:
As you can see, there is a line at the junction of the colors, it doesn't look good, you can manipulate the number of stops to spread the color spectrum.
CSS option