Skip to content

🎥 Make videos programmatically with React

explain why

Jonny Burger563538409833
diff --git a/packages/example/src/Lottie/FontInLottie/index.tsx b/packages/example/src/Lottie/LottieInitializationBugfix/index.tsx
similarity index 99%
rename from packages/example/src/Lottie/FontInLottie/index.tsx
rename to packages/example/src/Lottie/LottieInitializationBugfix/index.tsx
index 2ff61e4a5d..bc871b66d5 100644
--- a/packages/example/src/Lottie/FontInLottie/index.tsx
+++ b/packages/example/src/Lottie/LottieInitializationBugfix/index.tsx
@@ -1010,7 +1010,7 @@ const lottie = {
markers: [],
};
-const ImageInLottie = () => {
+const LottieInitializationBugfix = () => {
const {height, width} = useVideoConfig();
return (
@@ -1025,4 +1025,4 @@ const ImageInLottie = () => {
);
};
-export default ImageInLottie;
+export default LottieInitializationBugfix;
diff --git a/packages/example/src/Video.tsx b/packages/example/src/Video.tsx
index 402b2b8e17..5f2ca5858a 100644
--- a/packages/example/src/Video.tsx
+++ b/packages/example/src/Video.tsx
@@ -520,8 +520,8 @@ export const Index: React.FC = () => {
width={850}
/>
<Composition
- id="font-in-lottie"
- lazyComponent={() => import('./Lottie/FontInLottie')}
+ id="LottieInitializationBugfix"
+ lazyComponent={() => import('./Lottie/LottieInitializationBugfix')}
durationInFrames={300}
fps={30}
height={850}
diff --git a/packages/lottie/src/Lottie.tsx b/packages/lottie/src/Lottie.tsx
index e7119e91ed..e35413a529 100644
--- a/packages/lottie/src/Lottie.tsx
+++ b/packages/lottie/src/Lottie.tsx
@@ -30,7 +30,7 @@ export const Lottie = ({
validateLoop(loop);
const animationRef = useRef<AnimationItem>();
- const lastFrameRef = useRef<number | null>(null);
+ const currentFrameRef = useRef<number | null>(null);
const containerRef = useRef<HTMLDivElement>(null);
const onAnimationLoadedRef = useRef<LottieProps['onAnimationLoaded']>();
@@ -40,7 +40,7 @@ export const Lottie = ({
delayRender('Waiting for Lottie animation to load')
);
const frame = useCurrentFrame();
- lastFrameRef.current = frame;
+ currentFrameRef.current = frame;
useEffect(() => {
if (!containerRef.current) {
@@ -55,12 +55,14 @@ export const Lottie = ({
const {current: animation} = animationRef;
const onComplete = () => {
- if (lastFrameRef.current) {
+ // Seek frame twice to avoid Lottie initialization bug:
+ // See LottieInitializationBugfix composition in the example project for a repro.
+ if (currentFrameRef.current) {
animationRef.current?.goToAndStop(
- Math.max(0, lastFrameRef.current - 1),
+ Math.max(0, currentFrameRef.current - 1),
true
);
- animationRef.current?.goToAndStop(lastFrameRef.current, true);
+ animationRef.current?.goToAndStop(currentFrameRef.current, true);
}
continueRender(handle);