简体   繁体   English

ReactJS + SVG:点击 FireFox 的动画路径“d”

[英]ReactJS + SVG: Animate path "d" on click for FireFox

CodePen here: https://codepen.io/sadpandas/pen/xxbpKvz CodePen 在这里: https ://codepen.io/sadpandas/pen/xxbpKvz

const [currentScreen, setCurrentScreen] = React.useState(0);

return (
    <React.Fragment>
    <div>
       <svg
        className="theSvg"
        width="156" height="600" viewBox="0 0 156 600" xmlns="http://www.w3.org/2000/svg">
        <path
            d={pathArray[currentScreen].path}
            stroke={pathArray[currentScreen].strokeColor}
            fill="none"
        >
        </path>
    </svg>
    </div>
    <div>
        <div>
        <button onClick={()=> setCurrentScreen(0)}>0</button>
        <button onClick={()=> setCurrentScreen(1)}>1</button>
        <button onClick={()=> setCurrentScreen(2)}>2</button>
    </div>
    </div>
</React.Fragment>

I am trying to morph SVG paths on button clicks.我正在尝试在单击按钮时变形 SVG 路径。 This works perfectly in Chrome, however the transition is ignored in Firefox.这在 Chrome 中完美运行,但在 Firefox 中忽略了转换。

My current understanding is that Firefox does not support this kind of animation yet.我目前的理解是 Firefox 还不支持这种动画。 What are my options here?我在这里有哪些选择? Should I fall back to using the tag?我应该回到使用标签吗?

I have seen some examples that uses the tag, but all of them are just transforms, not triggered on click.我看过一些使用标签的例子,但它们都只是转换,而不是点击触发。 I have to use ReactJs to resolve this, so JQuery is out of the option as well.我必须使用 ReactJs 来解决这个问题,所以 JQuery 也不行。 If possible, I'd also like to avoid using a package, as this is the only place in my entire app that would use SVG morphing.如果可能,我还想避免使用包,因为这是我整个应用程序中唯一会使用 SVG 变形的地方。

Any tips would be greatly appreciated.任何提示将非常感谢。 Thanks!谢谢!

Ended up using the animate tag.最终使用了 animate 标签。 Works fine with Firefox and Chrome right now.现在可以在 Firefox 和 Chrome 上正常工作。

For anyone who's also learning SVG animations: CodePen: https://codepen.io/sadpandas/pen/GRgyrOJ?editors=1010对于也在学习 SVG 动画的任何人:CodePen: https ://codepen.io/sadpandas/pen/GRgyrOJ ? editors = 1010


const ButtonAndStrings = () => {
    const [currentScreen, setCurrentScreen] = React.useState(0);
    const [oldScreen, setOldScreen] = React.useState(0);


    const handleClick = (index, currentScreen) => {
        setOldScreen(currentScreen)
        setCurrentScreen(index);
    }

    return (
        <>
            <SVGPathAnimation
                path={pathArray[oldScreen]}
                newPath={pathArray[currentScreen]}
            />
            <button onClick={()=> handleClick(0, currentScreen)}>0</button>
            <button onClick={()=> handleClick(1, currentScreen)}>1</button>
            <button onClick={()=> handleClick(2, currentScreen)}>2</button>
        </>
    )
}

The SVG has something call 'animation timeline'. SVG 有一种叫做“动画时间线”的东西。 For a new animation to happen, the browser's time needs to be reset.要发生新动画,需要重置浏览器的时间。 In react's case, adding a ref to the SVG would solve the problem.在 react 的情况下,向 SVG 添加 ref 将解决问题。

reference: This medium article by Jacob Fletcher.参考:Jacob Fletcher 的这篇中等文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM