Skip to content

🎥 Make videos programmatically with React

feat(lottie): add support for animation direction

Arthur Dennere4ebbf394ecf
diff --git a/packages/example/public/reverse-loader.json b/packages/example/public/reverse-loader.json
new file mode 100644
index 0000000000..f5157a25a4
--- /dev/null
+++ b/packages/example/public/reverse-loader.json
@@ -0,0 +1 @@
+{"v":"5.6.10","fr":60,"ip":0,"op":120,"w":576,"h":576,"nm":"loop","ddd":0,"assets":[],"layers":[{"ddd":0,"ind":1,"ty":4,"nm":"O","sr":1,"ks":{"o":{"a":0,"k":100,"ix":11},"r":{"a":0,"k":0,"ix":10},"p":{"a":0,"k":[288,288,0],"ix":2},"a":{"a":0,"k":[0,0,0],"ix":1},"s":{"a":0,"k":[-100,100,100],"ix":6}},"ao":0,"shapes":[{"ty":"gr","it":[{"ind":0,"ty":"sh","ix":1,"ks":{"a":0,"k":{"i":[[-26.51,0],[0,0],[0,-26.51],[26.51,0],[0,0],[0,26.51]],"o":[[0,0],[26.51,0],[0,26.51],[0,0],[-26.51,0],[0,-26.51]],"v":[[-24,-48],[24,-48],[72,0],[24,48],[-24,48],[-72,0]],"c":true},"ix":2},"nm":"Path 1","mn":"ADBE Vector Shape - Group","hd":false},{"ty":"rd","nm":"Round Corners 1","r":{"a":0,"k":48,"ix":1},"ix":2,"mn":"ADBE Vector Filter - RC","hd":false},{"ty":"st","c":{"a":0,"k":[1,0.14901960784313725,0.14901960784313725,1],"ix":3},"o":{"a":0,"k":100,"ix":4},"w":{"a":0,"k":16,"ix":5},"lc":2,"lj":2,"bm":0,"d":[{"n":"d","nm":"dash","v":{"a":0,"k":135,"ix":1}},{"n":"g","nm":"gap","v":{"a":0,"k":62,"ix":2}},{"n":"o","nm":"offset","v":{"a":1,"k":[{"i":{"x":[0.67],"y":[1]},"o":{"x":[0.33],"y":[0]},"t":60,"s":[0]},{"t":102,"s":[197]}],"ix":7}}],"nm":"Stroke 1","mn":"ADBE Vector Graphic - Stroke","hd":false},{"ty":"tr","p":{"a":0,"k":[0,0],"ix":2},"a":{"a":0,"k":[0,0],"ix":1},"s":{"a":0,"k":[300,300],"ix":3},"r":{"a":0,"k":0,"ix":6},"o":{"a":0,"k":100,"ix":7},"sk":{"a":0,"k":0,"ix":4},"sa":{"a":0,"k":0,"ix":5},"nm":"Transform"}],"nm":"O","np":3,"cix":2,"bm":0,"ix":1,"mn":"ADBE Vector Group","hd":false}],"ip":0,"op":300,"st":0,"bm":0},{"ddd":0,"ind":2,"ty":4,"nm":"Path 12","sr":1,"ks":{"o":{"a":0,"k":100,"ix":11},"r":{"a":1,"k":[{"i":{"x":[0.833],"y":[0.833]},"o":{"x":[0.33],"y":[0]},"t":60,"s":[0]},{"i":{"x":[0.67],"y":[1]},"o":{"x":[0.167],"y":[0.167]},"t":80,"s":[113]},{"t":95,"s":[180]}],"ix":10},"p":{"a":1,"k":[{"i":{"x":0.833,"y":0.833},"o":{"x":0.33,"y":0},"t":60,"s":[420,144,0],"to":[133,95,0],"ti":[0,0,0]},{"i":{"x":0.67,"y":1},"o":{"x":0.167,"y":0.167},"t":80,"s":[479,370,0],"to":[-53,72,0],"ti":[221,0,0]},{"t":102,"s":[156,430.99999999999994,0]}],"ix":2},"a":{"a":0,"k":[0,0,0],"ix":1},"s":{"a":0,"k":[100,100,100],"ix":6}},"ao":0,"shapes":[{"ty":"gr","it":[{"ind":0,"ty":"sh","ix":1,"ks":{"a":0,"k":{"i":[[0,0],[0,0],[0,0]],"o":[[0,0],[0,0],[0,0]],"v":[[-20,32],[-20,-32],[20,0]],"c":true},"ix":2},"nm":"Path 1","mn":"ADBE Vector Shape - Group","hd":false},{"ty":"rd","nm":"Round Corners 1","r":{"a":0,"k":4,"ix":1},"ix":2,"mn":"ADBE Vector Filter - RC","hd":false},{"ty":"fl","c":{"a":0,"k":[1,0.14901960784313725,0.14901960784313725,1],"ix":4},"o":{"a":0,"k":100,"ix":5},"r":1,"bm":0,"nm":"Fill 1","mn":"ADBE Vector Graphic - Fill","hd":false},{"ty":"tr","p":{"a":0,"k":[0,0],"ix":2},"a":{"a":0,"k":[0,0],"ix":1},"s":{"a":0,"k":[300,300],"ix":3},"r":{"a":0,"k":0,"ix":6},"o":{"a":0,"k":100,"ix":7},"sk":{"a":0,"k":0,"ix":4},"sa":{"a":0,"k":0,"ix":5},"nm":"Transform"}],"nm":"Path 12","np":3,"cix":2,"bm":0,"ix":1,"mn":"ADBE Vector Group","hd":false}],"ip":0,"op":300,"st":0,"bm":0},{"ddd":0,"ind":3,"ty":4,"nm":"Path 12","sr":1,"ks":{"o":{"a":0,"k":100,"ix":11},"r":{"a":1,"k":[{"i":{"x":[0.833],"y":[0.833]},"o":{"x":[0.33],"y":[0]},"t":60,"s":[0]},{"i":{"x":[0.67],"y":[1]},"o":{"x":[0.167],"y":[0.167]},"t":80,"s":[113]},{"t":95,"s":[180]}],"ix":10},"p":{"a":1,"k":[{"i":{"x":0.833,"y":0.833},"o":{"x":0.33,"y":0},"t":60,"s":[156,432,0],"to":[-86.99,-49.979,0],"ti":[-55.026,59.948,0]},{"i":{"x":0.67,"y":1},"o":{"x":0.167,"y":0.167},"t":80,"s":[98.063,203.125,0],"to":[55.026,-59.948,0],"ti":[-219.01,-2.021,0]},{"t":102,"s":[420.125,144.25,0]}],"ix":2},"a":{"a":0,"k":[0,0,0],"ix":1},"s":{"a":0,"k":[-100,-100,100],"ix":6}},"ao":0,"shapes":[{"ty":"gr","it":[{"ind":0,"ty":"sh","ix":1,"ks":{"a":0,"k":{"i":[[0,0],[0,0],[0,0]],"o":[[0,0],[0,0],[0,0]],"v":[[-20,32],[-20,-32],[20,0]],"c":true},"ix":2},"nm":"Path 1","mn":"ADBE Vector Shape - Group","hd":false},{"ty":"rd","nm":"Round Corners 1","r":{"a":0,"k":4,"ix":1},"ix":2,"mn":"ADBE Vector Filter - RC","hd":false},{"ty":"fl","c":{"a":0,"k":[1,0.14901960784313725,0.14901960784313725,1],"ix":4},"o":{"a":0,"k":100,"ix":5},"r":1,"bm":0,"nm":"Fill 1","mn":"ADBE Vector Graphic - Fill","hd":false},{"ty":"tr","p":{"a":0,"k":[0,0],"ix":2},"a":{"a":0,"k":[0,0],"ix":1},"s":{"a":0,"k":[300,300],"ix":3},"r":{"a":0,"k":0,"ix":6},"o":{"a":0,"k":100,"ix":7},"sk":{"a":0,"k":0,"ix":4},"sa":{"a":0,"k":0,"ix":5},"nm":"Transform"}],"nm":"Path 12","np":3,"cix":2,"bm":0,"ix":1,"mn":"ADBE Vector Group","hd":false}],"ip":0,"op":300,"st":0,"bm":0}],"markers":[]}
\ No newline at end of file
diff --git a/packages/example/src/Lottie/Halloween/Balloons.tsx b/packages/example/src/Lottie/Halloween/Balloons.tsx
index d0f30df8be..7814672d8f 100644
--- a/packages/example/src/Lottie/Halloween/Balloons.tsx
+++ b/packages/example/src/Lottie/Halloween/Balloons.tsx
@@ -27,6 +27,7 @@ const Balloons = () => {
useState<LottieAnimationData | null>(null);
useEffect(() => {
+ // Credits: https://lottiefiles.com/81293-horror-ballons
fetch(staticFile('balloons.json'))
.then((data) => data.json())
.then((json) => {
@@ -45,7 +46,6 @@ const Balloons = () => {
return (
<div style={{opacity, display: 'grid', alignContent: 'center', flex: 1}}>
<Lottie
- // https://lottiefiles.com/81293-horror-ballons
animationData={animationData}
playbackRate={2}
style={{height: 700}}
diff --git a/packages/example/src/Lottie/Loader/index.tsx b/packages/example/src/Lottie/Loader/index.tsx
new file mode 100644
index 0000000000..15c7da1c9c
--- /dev/null
+++ b/packages/example/src/Lottie/Loader/index.tsx
@@ -0,0 +1,41 @@
+import {getLottieMetadata, Lottie, LottieAnimationData} from '@remotion/lottie';
+import {useEffect, useState} from 'react';
+import {
+ continueRender,
+ delayRender,
+ staticFile,
+ useCurrentFrame,
+} from 'remotion';
+
+const LottieLoader = () => {
+ const frame = useCurrentFrame();
+ const [handle] = useState(() => delayRender('Loading Lottie animation'));
+
+ const [animationData, setAnimationData] =
+ useState<LottieAnimationData | null>(null);
+
+ useEffect(() => {
+ // Credits: https://lottiefiles.com/93004-reverse-loader
+ fetch(staticFile('reverse-loader.json'))
+ .then((data) => data.json())
+ .then((json) => {
+ setAnimationData(json);
+ continueRender(handle);
+ })
+ .catch((err) => {
+ console.log('Animation failed to load', err);
+ });
+ }, [handle]);
+
+ if (!animationData) {
+ return null;
+ }
+
+ const {durationInFrames: df} = getLottieMetadata(animationData) || {};
+ const nLoop = df && frame > 0 ? Math.ceil(frame / df) : 1;
+ const direction = nLoop % 2 === 0 ? -1 : 1;
+
+ return <Lottie loop animationData={animationData} direction={direction} />;
+};
+
+export default LottieLoader;
diff --git a/packages/example/src/Video.tsx b/packages/example/src/Video.tsx
index 78179d59be..ce52411bb9 100644
--- a/packages/example/src/Video.tsx
+++ b/packages/example/src/Video.tsx
@@ -458,7 +458,6 @@ export const Index: React.FC = () => {
height={1200}
width={1600}
/>
-
<Composition
id="exploding-bird"
lazyComponent={() => import('./Lottie/ExplodingBird')}
@@ -467,6 +466,14 @@ export const Index: React.FC = () => {
height={850}
width={850}
/>
+ <Composition
+ id="loader"
+ lazyComponent={() => import('./Lottie/Loader')}
+ durationInFrames={240}
+ fps={60}
+ height={576}
+ width={576}
+ />
</Folder>
</>
);
diff --git a/packages/lottie/src/Lottie.tsx b/packages/lottie/src/Lottie.tsx
index d1dbdf02a6..cf32de0ffb 100644
--- a/packages/lottie/src/Lottie.tsx
+++ b/packages/lottie/src/Lottie.tsx
@@ -1,39 +1,16 @@
import type {AnimationItem} from 'lottie-web';
import lottie from 'lottie-web';
-import type {CSSProperties} from 'react';
import {useEffect, useRef, useState} from 'react';
import {continueRender, delayRender, useCurrentFrame} from 'remotion';
-import type {LottieAnimationData} from './types';
+import type {LottieProps} from './types';
import {getNextFrame} from './utils';
import {validateLoop} from './validate-loop';
import {validatePlaybackRate} from './validate-playbackrate';
-export interface LottieProps {
- /**
- * JSON object with the animation data.
- * */
- animationData: LottieAnimationData;
- /**
- * If the animation should loop after its end.
- */
- loop?: boolean;
- /**
- * The speed of the animation. Defaults to 1.
- */
- playbackRate?: number;
- /**
- * CSS classes to apply on the container of the animation.
- */
- className?: string;
- /**
- * CSS properties to apply on the container of the animation.
- */
- style?: CSSProperties;
-}
-
export const Lottie = ({
animationData,
className,
+ direction,
loop,
playbackRate,
style,
@@ -85,11 +62,15 @@ export const Lottie = ({
}, [animationData, handle]);
useEffect(() => {
- if (!animationRef.current) {
- return;
+ if (animationRef.current && direction) {
+ animationRef.current.setDirection(direction);
}
+ }, [direction]);
- animationRef.current.setSpeed(playbackRate ?? 1);
+ useEffect(() => {
+ if (animationRef.current && playbackRate) {
+ animationRef.current.setSpeed(playbackRate);
+ }
}, [playbackRate]);
useEffect(() => {
@@ -98,11 +79,15 @@ export const Lottie = ({
}
const {totalFrames} = animationRef.current;
- const expectedFrame = frame * (playbackRate ?? 1);
- const nextFrame = getNextFrame(expectedFrame, totalFrames, loop);
+ const nextFrame = getNextFrame({
+ currentFrame: frame * (playbackRate ?? 1),
+ direction,
+ loop,
+ totalFrames,
+ });
animationRef.current.goToAndStop(nextFrame, true);
- }, [frame, loop, playbackRate]);
+ }, [direction, frame, loop, playbackRate]);
return <div ref={containerRef} className={className} style={style} />;
};
diff --git a/packages/lottie/src/index.ts b/packages/lottie/src/index.ts
index 4fb1924dda..1188e7a7bf 100644
--- a/packages/lottie/src/index.ts
+++ b/packages/lottie/src/index.ts
@@ -1,4 +1,3 @@
export {getLottieMetadata} from './get-lottie-metadata';
export {Lottie} from './Lottie';
-export type {LottieProps} from './Lottie';
-export type {LottieAnimationData} from './types';
+export type {LottieAnimationData, LottieProps} from './types';
diff --git a/packages/lottie/src/test/utils.test.ts b/packages/lottie/src/test/utils.test.ts
index 2e81bc9747..d369d16a28 100644
--- a/packages/lottie/src/test/utils.test.ts
+++ b/packages/lottie/src/test/utils.test.ts
@@ -4,21 +4,63 @@ import {getNextFrame} from '../utils';
describe('getNextFrame', () => {
describe('when loop is falsy', () => {
it('returns the current frame if smaller than total frames', () => {
- expect(getNextFrame(23, 56)).toBe(23);
+ expect(getNextFrame({currentFrame: 23, totalFrames: 56})).toBe(23);
});
it('returns the last frame if current frame is bigger than total frames', () => {
- expect(getNextFrame(23, 20)).toBe(20);
+ expect(getNextFrame({currentFrame: 23, totalFrames: 20})).toBe(20);
});
});
describe('when loop is truthy', () => {
it('returns the current frame if smaller than total frames', () => {
- expect(getNextFrame(23, 56, true)).toBe(23);
+ expect(
+ getNextFrame({currentFrame: 23, totalFrames: 56, loop: true})
+ ).toBe(23);
});
- it('returns the last frame if current frame is bigger than total frames', () => {
- expect(getNextFrame(23, 20, true)).toBe(3);
+ it('returns the modulo if current frame is bigger than total frames', () => {
+ expect(
+ getNextFrame({currentFrame: 23, totalFrames: 20, loop: true})
+ ).toBe(3);
+ });
+ });
+
+ describe('when direction is reverse and loop is falsy', () => {
+ it('returns the correct frame if current frame is smaller than total frames', () => {
+ expect(
+ getNextFrame({currentFrame: 15, totalFrames: 20, direction: -1})
+ ).toBe(5);
+ });
+
+ it('returns frame zero if current frame is bigger than total frames', () => {
+ expect(
+ getNextFrame({currentFrame: 23, totalFrames: 20, direction: -1})
+ ).toBe(0);
+ });
+ });
+
+ describe('when direction is reverse and loop is truthy', () => {
+ it('returns the correct frame if current frame is smaller than total frames', () => {
+ expect(
+ getNextFrame({
+ currentFrame: 15,
+ totalFrames: 20,
+ direction: -1,
+ loop: true,
+ })
+ ).toBe(5);
+ });
+
+ it('returns (total-overflow) if current frame is bigger than total frames', () => {
+ expect(
+ getNextFrame({
+ currentFrame: 23,
+ totalFrames: 20,
+ direction: -1,
+ loop: true,
+ })
+ ).toBe(17);
});
});
});
diff --git a/packages/lottie/src/types.ts b/packages/lottie/src/types.ts
index 0e8df6f49e..a080edb8ee 100644
--- a/packages/lottie/src/types.ts
+++ b/packages/lottie/src/types.ts
@@ -1,6 +1,35 @@
+import type {CSSProperties} from 'react';
+
export type LottieAnimationData = {
fr: number;
w: number;
h: number;
op: number;
} & Record<string | number | symbol, unknown>;
+
+export type LottieProps = {
+ /**
+ * JSON object with the animation data.
+ * */
+ animationData: LottieAnimationData;
+ /**
+ * The direction of the animation. 1 is forward, -1 is reverse.
+ */
+ direction?: 1 | -1;
+ /**
+ * If the animation should loop after its end.
+ */
+ loop?: boolean;
+ /**
+ * The speed of the animation. Defaults to 1.
+ */
+ playbackRate?: number;
+ /**
+ * CSS classes to apply on the container of the animation.
+ */
+ className?: string;
+ /**
+ * CSS properties to apply on the container of the animation.
+ */
+ style?: CSSProperties;
+};
diff --git a/packages/lottie/src/utils.ts b/packages/lottie/src/utils.ts
index 0ec74e5310..f622caef74 100644
--- a/packages/lottie/src/utils.ts
+++ b/packages/lottie/src/utils.ts
@@ -1,9 +1,23 @@
-export const getNextFrame = (
- currentFrame: number,
- totalFrames: number,
- loop?: boolean
-) => {
- return loop
+interface Params {
+ currentFrame: number;
+ direction?: number;
+ loop?: boolean;
+ totalFrames: number;
+}
+
+export const getNextFrame = ({
+ currentFrame,
+ direction,
+ loop,
+ totalFrames,
+}: Params) => {
+ const nextFrame = loop
? currentFrame % totalFrames
: Math.min(currentFrame, totalFrames);
+
+ if (direction === -1) {
+ return totalFrames - nextFrame;
+ }
+
+ return nextFrame;
};