Skip to content

🎥 Make videos programmatically with React

Fix <Lottie loop />

Jonny Burger4df581b6a052
diff --git a/packages/lottie/src/Lottie.tsx b/packages/lottie/src/Lottie.tsx
index ef9bb99b6e..6e68569343 100644
--- a/packages/lottie/src/Lottie.tsx
+++ b/packages/lottie/src/Lottie.tsx
@@ -3,7 +3,7 @@ import lottie from 'lottie-web';
import {useEffect, useRef, useState} from 'react';
import {continueRender, delayRender, useCurrentFrame} from 'remotion';
import type {LottieProps} from './types';
-import {getNextFrame} from './utils';
+import {getLottieFrame} from './utils';
import {validateLoop} from './validate-loop';
import {validatePlaybackRate} from './validate-playbackrate';
@@ -59,11 +59,14 @@ export const Lottie = ({
// See LottieInitializationBugfix composition in the example project for a repro.
// We can work around it by seeking twice, initially.
if (currentFrameRef.current) {
- animationRef.current?.goToAndStop(
- Math.max(0, currentFrameRef.current - 1),
- true,
- );
- animationRef.current?.goToAndStop(currentFrameRef.current, true);
+ const frameToSet = getLottieFrame({
+ currentFrame: currentFrameRef.current * (playbackRate ?? 1),
+ direction,
+ loop,
+ totalFrames: animation.totalFrames,
+ });
+ animationRef.current?.goToAndStop(Math.max(0, frameToSet - 1), true);
+ animationRef.current?.goToAndStop(frameToSet, true);
}
continueRender(handle);
@@ -77,7 +80,7 @@ export const Lottie = ({
animation.removeEventListener('DOMLoaded', onComplete);
animation.destroy();
};
- }, [animationData, handle]);
+ }, [animationData, direction, handle, loop, playbackRate]);
useEffect(() => {
if (animationRef.current && direction) {
@@ -97,14 +100,14 @@ export const Lottie = ({
}
const {totalFrames} = animationRef.current;
- const nextFrame = getNextFrame({
+ const frameToSet = getLottieFrame({
currentFrame: frame * (playbackRate ?? 1),
direction,
loop,
totalFrames,
});
- animationRef.current.goToAndStop(nextFrame, true);
+ animationRef.current.goToAndStop(frameToSet, true);
const images = containerRef.current?.querySelectorAll(
'image',
) as NodeListOf<SVGImageElement>;
diff --git a/packages/lottie/src/test/utils.test.ts b/packages/lottie/src/test/utils.test.ts
index 13d265d8b9..87b8681098 100644
--- a/packages/lottie/src/test/utils.test.ts
+++ b/packages/lottie/src/test/utils.test.ts
@@ -1,27 +1,27 @@
import {describe, expect, it} from 'vitest';
-import {getNextFrame} from '../utils';
+import {getLottieFrame} from '../utils';
describe('getNextFrame', () => {
describe('when loop is falsy', () => {
it('returns the current frame if smaller than total frames', () => {
- expect(getNextFrame({currentFrame: 23, totalFrames: 56})).toBe(23);
+ expect(getLottieFrame({currentFrame: 23, totalFrames: 56})).toBe(23);
});
it('returns the last frame if current frame is bigger than total frames', () => {
- expect(getNextFrame({currentFrame: 23, totalFrames: 20})).toBe(20);
+ expect(getLottieFrame({currentFrame: 23, totalFrames: 20})).toBe(20);
});
});
describe('when loop is truthy', () => {
it('returns the current frame if smaller than total frames', () => {
expect(
- getNextFrame({currentFrame: 23, totalFrames: 56, loop: true}),
+ getLottieFrame({currentFrame: 23, totalFrames: 56, loop: true}),
).toBe(23);
});
it('returns the modulo if current frame is bigger than total frames', () => {
expect(
- getNextFrame({currentFrame: 23, totalFrames: 20, loop: true}),
+ getLottieFrame({currentFrame: 23, totalFrames: 20, loop: true}),
).toBe(3);
});
});
@@ -29,7 +29,7 @@ describe('getNextFrame', () => {
describe('when direction is reverse and loop is falsy', () => {
it('returns the correct frame if current frame is smaller than total frames', () => {
expect(
- getNextFrame({
+ getLottieFrame({
currentFrame: 15,
totalFrames: 20,
direction: 'backward',
@@ -39,7 +39,7 @@ describe('getNextFrame', () => {
it('returns frame zero if current frame is bigger than total frames', () => {
expect(
- getNextFrame({
+ getLottieFrame({
currentFrame: 23,
totalFrames: 20,
direction: 'backward',
@@ -51,7 +51,7 @@ describe('getNextFrame', () => {
describe('when direction is reverse and loop is truthy', () => {
it('returns the correct frame if current frame is smaller than total frames', () => {
expect(
- getNextFrame({
+ getLottieFrame({
currentFrame: 15,
totalFrames: 20,
direction: 'backward',
@@ -62,7 +62,7 @@ describe('getNextFrame', () => {
it('returns (total-overflow) if current frame is bigger than total frames', () => {
expect(
- getNextFrame({
+ getLottieFrame({
currentFrame: 23,
totalFrames: 20,
direction: 'backward',
diff --git a/packages/lottie/src/utils.ts b/packages/lottie/src/utils.ts
index 4de4e36fd6..1202b52e12 100644
--- a/packages/lottie/src/utils.ts
+++ b/packages/lottie/src/utils.ts
@@ -5,7 +5,7 @@ type Params = Pick<LottieProps, 'direction' | 'loop'> & {
totalFrames: number;
};
-export const getNextFrame = ({
+export const getLottieFrame = ({
currentFrame,
direction,
loop,