Cómo crear una animación en bucle with react-spring useChain.
Quería crear una animación de bucle para la carga utilizando react-spring, pero cuando uso “loop” no se ajusta correctamente el timing y no sé cómo hacer que la animación “useChain” se repita.
const animation = {
from: { scale: 0, opacity: 0, background: 'red' },
to: [
{ scale: 1, opacity: 1 },
{ scale: 0, opacity: 0 }
],
config: {
duration: 500
}
}
const Loading = () => {
const leftBottomRef = useSpringRef()
const leftBottom = useSpring({
ref: leftBottomRef,
...animation
})
const leftTwoRef = useSpringRef()
const leftTwo = useSpring({
ref: leftTwoRef,
...animation
})
const centerRef = useSpringRef()
const center = useSpring({
ref: centerRef,
...animation
})
const rightTwoRef = useSpringRef()
const rightTwo = useSpring({
ref: rightTwoRef,
...animation
})
const rightTopRef = useSpringRef()
const rightTop = useSpring({
ref: rightTopRef,
...animation
})
useChain(
[leftBottomRef, leftTwoRef, centerRef, rightTwoRef, rightTopRef],
[0, 0.3, 0.6, 0.9, 1.2],
)
return (
<div classname='loading'>
<div classname='loader'>
<animated.div style="{center}"></animated.div>
<animated.div style="{rightTwo}"></animated.div>
<animated.div style="{rightTop}"></animated.div>
<animated.div style="{leftTwo}"></animated.div>
<animated.div style="{center}"></animated.div>
<animated.div style="{rightTwo}"></animated.div>
<animated.div style="{leftBottom}"></animated.div>
<animated.div style="{leftTwo}"></animated.div>
<animated.div style="{center}"></animated.div>
</div>
Cargando...
</div>
)
}
¿Hay alguna forma de hacer que la animación “useChain” se repita o hay alguna otra manera de lograr esta animación?
Aquí hay un poco de contexto para lo que intento hacer:
davy.ai
Para crear una animación en bucle usando react-spring, necesitamos usar el hook
useLoop()
proporcionado por react-spring. Este hook nos permite crear un bucle sencillo que repite una serie de animaciones indefinidamente. Así es como podemos modificar el componenteLoading
para usar el hookuseLoop()
:En este ejemplo, hemos utilizado el hook
useLoop()
para crear un bucle para nuestra animación. Hemos configurado la propiedadreverse
comotrue
, lo que significa que la animación se invertirá en cada iteración, dándole un efecto de bucle continuo. También hemos añadido una demora de 500ms entre cada iteración para hacerlo más atractivo visualmente. Por último, hemos añadido un controladoronClick
al componenteloader
para iniciar el bucle.Con estos cambios, ahora deberíamos tener una animación en bucle para nuestro componente de carga.