保存动画实例以供以后使用
我正在尝试将Lottie对象保存,并使用以后使用的方法(另一种使用效率)到该州,但该州表示这是无效的。
我想使用文档中指定的anim.playsegments方法。
lottie 文档。
更新#1:尝试在依赖项数组和事件侦听器中包括动画,以检查动画是否已加载,但这无济于事,应用程序会导致周期。
动画现在无限期加载
更新#2 :在使用效果之外声明一个变量,并在其内部定义它。摆脱了国家的方法。动画仍然为空。
import React, { useEffect, useRef, useState } from 'react';
import { useColorMode } from '@chakra-ui/react';
import lottie from 'lottie-web';
import animationData from '../assets/json/sun-to-night'
const ColorMode = () => {
const container = useRef(null)
const { colorMode, toggleColorMode } = useColorMode()
// Segments of the animation
const midToEnd = [14, 28]
const startToMid = [0, 14]
let anim = null
useEffect(() => {
anim = lottie.loadAnimation({
container: container.current,
animationData: animationData,
autoplay: false,
loop: false,
name: 'anim'
})
// Here the animation instance exists and animation is played
console.log(`${anim}`)
anim.playSegments(
(colorMode === 'dark'
? startToMid
: midToEnd), true
)
}, []);
useEffect(() => {
//anim is null in here and the React app throws an error
anim.addEventListener('DOMLoaded', () => {
anim.playSegments(
(colorMode === 'dark'
? startToMid
: midToEnd), true
)
})
}, [colorMode])
return (
<>
<div>
ref={container}
onClick={toggleColorMode}
</div>
</>
)
}
export default ColorMode;
I'm trying to save a Lottie object with methods for later use (another useEffect) into the state, but the state says it's null.
I would like to use the anim.playSegments method specified in the documentation.
Lottie documentation.
Update #1: Tried including anim into the dependency array and event listener for checking if the animation has loaded, but that didn't help and the application resulted in a cycle.
Animation now loads indefinitely
Update #2: Declared a variable outside of the useEffect and defined it inside of it. Got rid of the state approach. Anim is still null.
import React, { useEffect, useRef, useState } from 'react';
import { useColorMode } from '@chakra-ui/react';
import lottie from 'lottie-web';
import animationData from '../assets/json/sun-to-night'
const ColorMode = () => {
const container = useRef(null)
const { colorMode, toggleColorMode } = useColorMode()
// Segments of the animation
const midToEnd = [14, 28]
const startToMid = [0, 14]
let anim = null
useEffect(() => {
anim = lottie.loadAnimation({
container: container.current,
animationData: animationData,
autoplay: false,
loop: false,
name: 'anim'
})
// Here the animation instance exists and animation is played
console.log(`${anim}`)
anim.playSegments(
(colorMode === 'dark'
? startToMid
: midToEnd), true
)
}, []);
useEffect(() => {
//anim is null in here and the React app throws an error
anim.addEventListener('DOMLoaded', () => {
anim.playSegments(
(colorMode === 'dark'
? startToMid
: midToEnd), true
)
})
}, [colorMode])
return (
<>
<div>
ref={container}
onClick={toggleColorMode}
</div>
</>
)
}
export default ColorMode;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要等到Lottie加载。您可以通过收听
domloaded
事件来做到这一点:更多此处:
https://github.com/airbnb/lottie-web#events
You need to wait until your lottie is loaded. You can do this by listening for the
DOMLoaded
event:More here:
https://github.com/airbnb/lottie-web#events
我通过在第二个使用效果挂钩中使用Lottie的内置方法“ GetRegisteredAnimations”解决了问题,并解析了我的动画数组的第一个元素。
然后,我将其存储到一个可以操纵动画的变量中。
这有点解决方法,但有效。如果有人有更好的方法,请随时分享。
这是最终代码:
I solved the problem by using lottie's built-in method "getRegisteredAnimations" in the second useEffect hook and parsing the first element of the array which was my animation.
Then I stored it into a variable which I could manipulate my animation with.
It's a bit of a workaround, but it works. If anyone has a better approach, don't hesitate to share.
Here is the final code: