Skip to content

🎥 Make videos programmatically with React

`@remotion/lottie`: Apply timing rules to intrinsic loops

Jonny Burgerc8e57779b74c
diff --git a/packages/docs/docs/lottie/lottie-comp.mdx b/packages/docs/docs/lottie/lottie-comp.mdx
index 8104c06056..243213710f 100644
--- a/packages/docs/docs/lottie/lottie-comp.mdx
+++ b/packages/docs/docs/lottie/lottie-comp.mdx
@@ -51,7 +51,7 @@ The direction of the animation. Default: `forward`.
_boolean_
-Whether the animation should loop or not. Default: `false`.
+Whether the animation should loop. If `durationInFrames` is set, the selected range repeats until the parent ends. Otherwise, the animation's remaining frames after `trimBefore` repeat. Default: `false`.
### `playbackRate`
diff --git a/packages/docs/docs/timing.mdx b/packages/docs/docs/timing.mdx
index 364b841186..fb59abbfc1 100644
--- a/packages/docs/docs/timing.mdx
+++ b/packages/docs/docs/timing.mdx
@@ -127,6 +127,7 @@ export const MyComposition = () => (
To set a finite total duration for a loop, wrap it in an outer item with `durationInFrames`.
[`<Video>`](/docs/media/video) and [`<Audio>`](/docs/media/audio) may omit `durationInFrames` because they have an intrinsic duration.
+[`<Lottie>`](/docs/lottie/lottie) may also omit it when looping: The animation's remaining frames after `trimBefore` become the loop range.
## Order of operations
diff --git a/packages/lottie/src/Lottie.tsx b/packages/lottie/src/Lottie.tsx
index fa8ab925e8..285629d086 100644
--- a/packages/lottie/src/Lottie.tsx
+++ b/packages/lottie/src/Lottie.tsx
@@ -21,23 +21,12 @@ const LottieContent = ({
animationData,
className,
direction,
- loop,
- playbackRate,
style,
onAnimationLoaded,
renderer,
preserveAspectRatio,
assetsPath,
}: LottieProps) => {
- if (typeof animationData !== 'object') {
- throw new Error(
- 'animationData should be provided as an object. If you only have the path to the JSON file, load it and pass it as animationData. See https://remotion.dev/docs/lottie/lottie#example for more information.',
- );
- }
-
- validatePlaybackRate(playbackRate);
- validateLoop(loop);
-
const animationRef = useRef<AnimationItem | null>(null);
const currentFrameRef = useRef<number | null>(null);
const containerRef = useRef<HTMLDivElement>(null);
@@ -84,9 +73,8 @@ const LottieContent = ({
// We can work around it by seeking twice, initially.
if (currentFrameRef.current) {
const frameToSet = getLottieFrame({
- currentFrame: currentFrameRef.current * (playbackRate ?? 1),
+ currentFrame: currentFrameRef.current,
direction,
- loop,
totalFrames: animation.totalFrames,
});
animationRef.current?.goToAndStop(Math.max(0, frameToSet - 1), true);
@@ -109,8 +97,6 @@ const LottieContent = ({
assetsPath,
direction,
handle,
- loop,
- playbackRate,
preserveAspectRatio,
renderer,
continueRender,
@@ -122,12 +108,6 @@ const LottieContent = ({
}
}, [direction]);
- useEffect(() => {
- if (animationRef.current && playbackRate) {
- animationRef.current.setSpeed(playbackRate);
- }
- }, [playbackRate]);
-
useEffect(() => {
if (!animationRef.current) {
return;
@@ -135,9 +115,8 @@ const LottieContent = ({
const {totalFrames} = animationRef.current;
const frameToSet = getLottieFrame({
- currentFrame: frame * (playbackRate ?? 1),
+ currentFrame: frame,
direction,
- loop,
totalFrames,
});
@@ -173,15 +152,17 @@ const LottieContent = ({
img.href.baseVal as string,
);
});
- }, [direction, frame, loop, playbackRate, delayRender, continueRender]);
+ }, [direction, frame, delayRender, continueRender]);
return <div ref={containerRef} className={className} style={style} />;
};
export const Lottie = ({
+ animationData,
from,
durationInFrames,
trimBefore,
+ playbackRate,
loop,
freeze,
hidden,
@@ -194,6 +175,24 @@ export const Lottie = ({
style,
...props
}: LottieProps) => {
+ if (typeof animationData !== 'object' || animationData === null) {
+ throw new Error(
+ 'animationData should be provided as an object. If you only have the path to the JSON file, load it and pass it as animationData. See https://remotion.dev/docs/lottie/lottie#example for more information.',
+ );
+ }
+
+ validatePlaybackRate(playbackRate);
+ validateLoop(loop);
+
+ // Match lottie-web's totalFrames so an implicit loop ends at the asset boundary.
+ const sequenceDurationInFrames =
+ loop && durationInFrames === undefined
+ ? Math.floor(
+ animationData.op -
+ (typeof animationData.ip === 'number' ? animationData.ip : 0),
+ ) - (trimBefore ?? 0)
+ : durationInFrames;
+
const {
effectivePremountFor,
effectivePostmountFor,
@@ -204,7 +203,11 @@ export const Lottie = ({
premountingStyle,
} = Internals.usePremounting({
from: from ?? 0,
- durationInFrames: durationInFrames ?? Infinity,
+ durationInFrames: Internals.resolveSequenceDuration({
+ durationInFrames: sequenceDurationInFrames,
+ playbackRate,
+ loop,
+ }),
premountFor: premountFor ?? null,
postmountFor: postmountFor ?? null,
style: style ?? null,
@@ -217,8 +220,10 @@ export const Lottie = ({
<Sequence
layout="none"
from={from}
- durationInFrames={durationInFrames}
+ durationInFrames={sequenceDurationInFrames}
trimBefore={trimBefore}
+ playbackRate={playbackRate}
+ loop={loop}
freeze={freeze}
hidden={hidden}
name={name ?? '<Lottie>'}
@@ -230,7 +235,7 @@ export const Lottie = ({
>
<LottieContent
{...props}
- loop={loop}
+ animationData={animationData}
style={premountingStyle ?? undefined}
/>
</Sequence>