【问题标题】:React-three-fiber with react-spring animationsReact-three-fiber 与 react-spring 动画
【发布时间】:2020-08-13 20:41:46
【问题描述】:

很抱歉,这似乎是一个显而易见的答案,但我正在尝试创建类似于 this 的动画。我正在尝试使用 react-three-fiber 创建一个类似的,但我不能使用文档中显示的那些,因为它们操纵了使用three.js 无法实现的样式转换属性。我试图通过位置属性和道具来操纵它,但它带有一个错误,如here 所示,我真的不知道从哪里开始解决这个问题。这是我的代码:

const props = useSpring({
    to: async next => {
      await next({ position: [100, 100, 100] });
      await next({ position: [50, 50, 50] });
    },
    from: { position: [100, 100, 100] },
    config: { duration: 3500 },
    reset: true
  });

 return (
    <Canvas>

      <a.group {...props}>
        <Box position={[-1.2, 0, 0]} />
        <Box position={[1.2, 0, 0]} />
      </a.group>
    </Canvas>

)

我已成功使用 react-spring 进行悬停和缩放,但在使用位置时它不起作用。

【问题讨论】:

    标签: reactjs three.js react-spring react-three-fiber


    【解决方案1】:

    我认为最好用三角函数来完成类似的事情

    const ref = useRef()
    useFrame(() => {
      ref.current.position.y = Math.sin(state.clock.getElapsedTime()) * 10
    })
    return <group ref={ref}> ...
    

    Math.sin 将产生一个介于 -1 到 1 之间的值,并在两者之间平滑交替。乘以你的因子(距离),你就有了。这是一个像这样移动相机的例子:https://codesandbox.io/s/r3f-lod-e9vpx

    否则它是 react-spring/three,而不是 react-spring:https://codesandbox.io/s/clever-bartik-tkql8

    【讨论】:

    • okkaaay 这更有意义哈哈。谢谢!就一个问题。我试图使动画循环无限大,所以我尝试添加reset:true,然后添加reverse:true,但它似乎不起作用。而且我认为我不能使用插值,因为动画是通过道具传递的。我错过了什么吗?对不起,如果这看起来很明显
    猜你喜欢
    • 2020-01-20
    • 2022-11-30
    • 2020-07-16
    • 1970-01-01
    • 2020-12-04
    • 2020-10-31
    • 2021-08-20
    • 2021-01-04
    • 1970-01-01
    相关资源
    最近更新 更多