如何使用 React Spring 对 SVG 线的 x1 坐标进行动画处理?
我正在尝试使用 React Spring 对 SVG 线的 x1 坐标进行动画处理,但没有任何反应。我也没有看到任何错误或失败记录。如何在 React-Spring 中对线条的位置进行动画处理?
import React, { useState } from "react";
import { useSpring, animated } from "react-spring";
function App() {
const [toggle, setToggle] = useState(false);
const spring = useSpring({
from: { x1: 5 },
to: { x1: 22 }
});
return (
<>
<div>
<button
type="button"
onClick={() => {
setToggle(!toggle);
}}
>
Toggle animation
</button>
</div>
<svg
xmlns="http://www.w3.org/2000/svg"
width="40"
height="40"
viewBox="0 0 40 40"
stroke="#000"
strokeWidth="2"
>
<animated.line
x1="5"
y1="10"
x2="35"
y2="10"
transform="rotate(0 23.5 10)"
styles={spring}
/>
</svg>
</>
);
}
export default App;
I'm trying to animate the x1 coordinate of an SVG line with react spring, but nothing is happening. I also don't see any errors or failures logged. How do I animate the position of a line in react-spring?
import React, { useState } from "react";
import { useSpring, animated } from "react-spring";
function App() {
const [toggle, setToggle] = useState(false);
const spring = useSpring({
from: { x1: 5 },
to: { x1: 22 }
});
return (
<>
<div>
<button
type="button"
onClick={() => {
setToggle(!toggle);
}}
>
Toggle animation
</button>
</div>
<svg
xmlns="http://www.w3.org/2000/svg"
width="40"
height="40"
viewBox="0 0 40 40"
stroke="#000"
strokeWidth="2"
>
<animated.line
x1="5"
y1="10"
x2="35"
y2="10"
transform="rotate(0 23.5 10)"
styles={spring}
/>
</svg>
</>
);
}
export default App;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了一种方法来做到这一点 - 这对我来说并不明显,也许它也会对其他人有所帮助:
可以将 useSpring 创建的 SpringValue 直接分配给 SVG 属性,然后它'会工作的。对于那些不能像罗伯特·朗森在原始帖子中评论的那样设计的道具特别有用。感谢您指出这一点。
I found a way to do it - it was non-obvious to me, maybe it'll help someone else as well:
One can assign the
SpringValue
s created by useSpring directly to the SVG properties and it'll work. Especially useful for those props, which can't be styled as Robert Longson commented on the original post. Thanks for pointing that out.