有一个线性渐变的边界。我希望渐变旋转,即更改属性gradientTransform="rotate(40)"
文档说 gradientTransform 属性是“Animatable”
https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/gradientTransform
你能告诉我如何动画它吗?
我希望渐变从 0 度旋转到 360 度。
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
viewBox="0 0 240 120"
>
<defs>
<linearGradient id="linearGradient" gradientTransform="rotate(40)" >
<stop style="stop-color:violet; stop-opacity: .7;" offset="0%"/>
<stop style="stop-color:blue; stop-opacity: .4;" offset="40%"/>
<stop style="stop-color:orange" offset="90%"/>
</linearGradient>
</defs>
<g >
<rect x="5" y="5" width="230" height="110" ry="10"
style="fill:none;stroke-width:5;stroke:url(#linearGradient);" />
<animateTransform
attributeName="gradientTransform"
type="rotate"
from="0"
to="45"
repeatCount="indefinite"
dur="25s"
/>
</g>
</svg>
您正在将渐变旋转动画应用于矩形,并且您需要将渐变动画命令放置在渐变定义中。
为了清楚起见,稍微改变了旋转的时间和角度。您可以随时为自己自定义这些设置。
所以我做到了——没有 animateTransform
第一个答案使用渐变旋转动画命令得到渐变旋转效果
stop-color这里将使用线性渐变动画命令下面是完整的代码: