如何使用 React Spring 对 SVG 线的 x1 坐标进行动画处理?

发布于 2025-01-13 20:28:47 字数 1026 浏览 2 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

謸气贵蔟 2025-01-20 20:28:47

我找到了一种方法来做到这一点 - 这对我来说并不明显,也许它也会对其他人有所帮助:

import React, { useState } from "react";
import { useSpring, animated } from "react-spring";

function App() {
    const [toggle, setToggle] = useState(false);

    const { x1 } = useSpring({
        from: { x1: 5 },
        to: { x1: 22 },
        immediate: toggle === false,
        reset: true
    });

    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={x1}
                    y1="10"
                    x2="35"
                    y2="10"
                    transform="rotate(0 23.5 10)"
                />
            </svg>
        </>
    );
}

export default App;

可以将 useSpring 创建的 SpringValue 直接分配给 SVG 属性,然后它'会工作的。对于那些不能像罗伯特·朗森在原始帖子中评论的那样设计的道具特别有用。感谢您指出这一点。

I found a way to do it - it was non-obvious to me, maybe it'll help someone else as well:

import React, { useState } from "react";
import { useSpring, animated } from "react-spring";

function App() {
    const [toggle, setToggle] = useState(false);

    const { x1 } = useSpring({
        from: { x1: 5 },
        to: { x1: 22 },
        immediate: toggle === false,
        reset: true
    });

    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={x1}
                    y1="10"
                    x2="35"
                    y2="10"
                    transform="rotate(0 23.5 10)"
                />
            </svg>
        </>
    );
}

export default App;

One can assign the SpringValues 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文