Skip to content

🎥 Make videos programmatically with React

rename speed to playbackRate and validate

Jonny Burger4f35c290715f
diff --git a/packages/docs/docs/lottie/lottie-comp.md b/packages/docs/docs/lottie/lottie-comp.md
index 17cb4eb942..4cb4b1a62b 100644
--- a/packages/docs/docs/lottie/lottie-comp.md
+++ b/packages/docs/docs/lottie/lottie-comp.md
@@ -43,7 +43,7 @@ _boolean_
Whether the animation should loop or not. Default false.
-### `speed`
+### `playbackRate`
_number_
diff --git a/packages/lottie/src/Lottie.tsx b/packages/lottie/src/Lottie.tsx
index 5eb4a37451..607d88e70a 100644
--- a/packages/lottie/src/Lottie.tsx
+++ b/packages/lottie/src/Lottie.tsx
@@ -5,6 +5,7 @@ import {useEffect, useRef, useState} from 'react';
import {continueRender, delayRender, useCurrentFrame} from 'remotion';
import type {LottieAnimationData} from './types';
import {getNextFrame} from './utils';
+import {validatePlaybackRate} from './validate-playbackrate';
export interface LottieProps {
/**
@@ -18,7 +19,7 @@ export interface LottieProps {
/**
* The speed of the animation. Defaults to 1.
*/
- speed?: number;
+ playbackRate?: number;
/**
* CSS classes to apply on the container of the animation.
*/
@@ -33,8 +34,7 @@ export const Lottie = ({
animationData,
className,
loop,
- // TODO: Rename to playback rate to have same name as other components.
- speed = 1,
+ playbackRate = 1,
style,
}: LottieProps) => {
if (typeof animationData !== 'object') {
@@ -43,6 +43,8 @@ export const Lottie = ({
);
}
+ validatePlaybackRate(playbackRate);
+
const animationRef = useRef<AnimationItem>();
const lastFrameRef = useRef<number | null>(null);
const containerRef = useRef<HTMLDivElement>(null);
@@ -85,8 +87,8 @@ export const Lottie = ({
return;
}
- animationRef.current.setSpeed(speed);
- }, [speed]);
+ animationRef.current.setSpeed(playbackRate);
+ }, [playbackRate]);
useEffect(() => {
if (!animationRef.current) {
@@ -94,11 +96,11 @@ export const Lottie = ({
}
const {totalFrames} = animationRef.current;
- const expectedFrame = frame * speed;
+ const expectedFrame = frame * playbackRate;
const nextFrame = getNextFrame(expectedFrame, totalFrames, loop);
animationRef.current.goToAndStop(nextFrame, true);
- }, [frame, loop, speed]);
+ }, [frame, loop, playbackRate]);
// TODO: Implement lottie.destroy
diff --git a/packages/lottie/src/validate-playbackrate.ts b/packages/lottie/src/validate-playbackrate.ts
new file mode 100644
index 0000000000..c4839fc5db
--- /dev/null
+++ b/packages/lottie/src/validate-playbackrate.ts
@@ -0,0 +1,25 @@
+export const validatePlaybackRate = (playbackRate: unknown) => {
+ if (typeof playbackRate === 'undefined') {
+ return;
+ }
+
+ if (typeof playbackRate !== 'number') {
+ throw new TypeError(
+ `The "playbackRate" prop must be a number or undefined, but is ${JSON.stringify(
+ playbackRate
+ )}`
+ );
+ }
+
+ if (Number.isNaN(playbackRate) || !Number.isFinite(playbackRate)) {
+ throw new TypeError(
+ `The "playbackRate" props must be a real number, but is ${playbackRate}`
+ );
+ }
+
+ if (playbackRate <= 0) {
+ throw new TypeError(
+ `The "playbackRate" props must be positive, but is ${playbackRate}`
+ );
+ }
+};