Skip to content

🎥 Make videos programmatically with React

use requestAnimationFrame for render frames

yuta0801d82f57d2a035
diff --git a/packages/cli/package.json b/packages/cli/package.json
index d303333992..6a40bd4f1b 100644
--- a/packages/cli/package.json
+++ b/packages/cli/package.json
@@ -10,7 +10,7 @@
"scripts": {
"test": "eslint src --ext ts,tsx",
"build": "tsc -d && node add-bin.js",
- "dev": "ts-node src/preview.ts x ../example/index.tsx"
+ "dev": "ts-node src/preview.ts x ../example/src/index.tsx"
},
"files": [
"dist",
diff --git a/packages/cli/src/editor/components/PlayPause.tsx b/packages/cli/src/editor/components/PlayPause.tsx
index 1a77fb8476..70a5f8d1fb 100644
--- a/packages/cli/src/editor/components/PlayPause.tsx
+++ b/packages/cli/src/editor/components/PlayPause.tsx
@@ -1,4 +1,4 @@
-import React, {useCallback, useEffect} from 'react';
+import React, {useCallback, useEffect, useRef, useState} from 'react';
import {Internals} from 'remotion';
import {Pause} from '../icons/pause';
import {Play} from '../icons/play';
@@ -82,20 +82,24 @@ export const PlayPause: React.FC = () => {
};
}, [onKeyPress]);
+ const [isUsingRAF, toggleRAF] = useState(false);
+
useEffect(() => {
+ if (isUsingRAF) return;
+
if (!config) {
return;
}
if (playing) {
setLastFrames([...getLastFrames(), Date.now()]);
- const last10Frames = getLastFrames();
- const timesBetweenFrames: number[] = last10Frames
+ const last15Frames = getLastFrames();
+ const timesBetweenFrames: number[] = last15Frames
.map((f, i) => {
if (i === 0) {
return null;
}
- return f - last10Frames[i - 1];
+ return f - last15Frames[i - 1];
})
.filter((_t) => _t !== null) as number[];
const averageTimeBetweenFrames =
@@ -105,7 +109,7 @@ export const PlayPause: React.FC = () => {
const expectedTime = 1000 / config.fps;
const slowerThanExpected = averageTimeBetweenFrames - expectedTime;
const timeout =
- last10Frames.length === 0
+ last15Frames.length === 0
? expectedTime
: expectedTime - slowerThanExpected;
const duration = config.durationInFrames;
@@ -115,14 +119,48 @@ export const PlayPause: React.FC = () => {
if (nextFrame >= duration) {
return 0;
}
- return currFrame + 1;
+ return nextFrame;
});
- }, timeout);
+ }, timeout * 5);
return () => {
clearTimeout(t);
};
}
- }, [config, frame, playing, setFrame, video]);
+ }, [isUsingRAF, config, frame, playing, setFrame, video]);
+
+ const frameRef = useRef(frame);
+ frameRef.current = frame;
+ useEffect(() => {
+ if (!isUsingRAF) return;
+
+ if (!config) {
+ return;
+ }
+
+ if (playing) {
+ let req: number | null = null;
+ const startedTime = performance.now();
+ const startedFrame = frameRef.current;
+
+ const step = () => {
+ setLastFrames([...getLastFrames(), Date.now()]);
+ const time = performance.now() - startedTime;
+ const calcuratedFrame =
+ (Math.round(time / (1000 / config.fps)) + startedFrame) %
+ config.durationInFrames;
+ if (calcuratedFrame !== frameRef.current) setFrame(calcuratedFrame);
+ req = requestAnimationFrame(step);
+ };
+
+ req = requestAnimationFrame(step);
+
+ return () => {
+ if (req !== null) {
+ cancelAnimationFrame(req);
+ }
+ };
+ }
+ }, [isUsingRAF, config, setFrame, playing]);
return (
<>
@@ -177,6 +215,21 @@ export const PlayPause: React.FC = () => {
}}
/>
</ControlButton>
+ <div style={{width: 10}} />
+ <ControlButton
+ aria-label="Step forward one frame"
+ disabled={isLastFrame}
+ onClick={() => toggleRAF((s) => !s)}
+ >
+ <span
+ style={{
+ height: 16,
+ color: 'white',
+ }}
+ >
+ {isUsingRAF ? 'requestAnimationFrame' : 'setTimeout'}
+ </span>
+ </ControlButton>
</>
);
};