Skip to content

🎥 Make videos programmatically with React

use raf

Jonny Burger32aff146c58d
diff --git a/packages/cli/src/editor/components/FpsCounter.tsx b/packages/cli/src/editor/components/FpsCounter.tsx
index d8cd236776..f7848d2c46 100644
--- a/packages/cli/src/editor/components/FpsCounter.tsx
+++ b/packages/cli/src/editor/components/FpsCounter.tsx
@@ -29,8 +29,8 @@ export const FpsCounter: React.FC = () => {
}
const diff = Math.max(...lastFrames) - Math.min(...lastFrames);
- const avg = diff / lastFrames.length;
- const fps = 1000 / avg;
+ const averageDistanceBetween = diff / (lastFrames.length - 1);
+ const fps = 1000 / averageDistanceBetween;
return (
<Label style={{color: fps < videoConfig.fps * 0.9 ? 'red' : 'white'}}>
{String(fps.toFixed(1))} FPS
diff --git a/packages/cli/src/editor/components/PlayPause.tsx b/packages/cli/src/editor/components/PlayPause.tsx
index 70a5f8d1fb..0ad31e30d3 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, useRef, useState} from 'react';
+import React, {useCallback, useEffect, useRef} from 'react';
import {Internals} from 'remotion';
import {Pause} from '../icons/pause';
import {Play} from '../icons/play';
@@ -82,85 +82,40 @@ 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 last15Frames = getLastFrames();
- const timesBetweenFrames: number[] = last15Frames
- .map((f, i) => {
- if (i === 0) {
- return null;
- }
- return f - last15Frames[i - 1];
- })
- .filter((_t) => _t !== null) as number[];
- const averageTimeBetweenFrames =
- timesBetweenFrames.reduce((a, b) => {
- return a + b;
- }, 0) / timesBetweenFrames.length;
- const expectedTime = 1000 / config.fps;
- const slowerThanExpected = averageTimeBetweenFrames - expectedTime;
- const timeout =
- last15Frames.length === 0
- ? expectedTime
- : expectedTime - slowerThanExpected;
- const duration = config.durationInFrames;
- const t = setTimeout(() => {
- setFrame((currFrame) => {
- const nextFrame = currFrame + 1;
- if (nextFrame >= duration) {
- return 0;
- }
- return nextFrame;
- });
- }, timeout * 5);
- return () => {
- clearTimeout(t);
- };
- }
- }, [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;
+ let reqAnimFrameCall: number | null = null;
const startedTime = performance.now();
const startedFrame = frameRef.current;
const step = () => {
- setLastFrames([...getLastFrames(), Date.now()]);
const time = performance.now() - startedTime;
- const calcuratedFrame =
+ const calculatedFrame =
(Math.round(time / (1000 / config.fps)) + startedFrame) %
config.durationInFrames;
- if (calcuratedFrame !== frameRef.current) setFrame(calcuratedFrame);
- req = requestAnimationFrame(step);
+ if (calculatedFrame !== frameRef.current) {
+ setLastFrames([...getLastFrames(), Date.now()]);
+ setFrame(calculatedFrame);
+ }
+ reqAnimFrameCall = requestAnimationFrame(step);
};
- req = requestAnimationFrame(step);
+ reqAnimFrameCall = requestAnimationFrame(step);
return () => {
- if (req !== null) {
- cancelAnimationFrame(req);
+ if (reqAnimFrameCall !== null) {
+ console.log('can');
+ cancelAnimationFrame(reqAnimFrameCall);
}
};
}
- }, [isUsingRAF, config, setFrame, playing]);
+ }, [config, setFrame, playing]);
return (
<>
@@ -216,20 +171,6 @@ 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>
</>
);
};