Skip to content

🎥 Make videos programmatically with React

tweaks

Jonny Burger822be60f6b49
diff --git a/packages/cli/src/editor/components/FpsCounter.tsx b/packages/cli/src/editor/components/FpsCounter.tsx
index f7848d2c46..a1f2d0dc69 100644
--- a/packages/cli/src/editor/components/FpsCounter.tsx
+++ b/packages/cli/src/editor/components/FpsCounter.tsx
@@ -1,4 +1,4 @@
-import React, {useEffect, useState} from 'react';
+import React, {useEffect, useMemo, useState} from 'react';
import {Internals} from 'remotion';
import styled from 'styled-components';
import {getLastFrames} from '../state/last-frames';
@@ -21,19 +21,24 @@ export const FpsCounter: React.FC = () => {
const lastFrames = getLastFrames();
+ const diff = Math.max(...lastFrames) - Math.min(...lastFrames);
+ const averageDistanceBetween = diff / (lastFrames.length - 1);
+ const fps = 1000 / averageDistanceBetween;
+
+ const style = useMemo((): React.CSSProperties => {
+ if (!videoConfig) {
+ return {};
+ }
+ return {color: fps < videoConfig.fps * 0.9 ? 'red' : 'white'};
+ }, [fps, videoConfig]);
+
if (lastFrames.length === 0) {
return null;
}
+
if (videoConfig === null) {
return null;
}
- const diff = Math.max(...lastFrames) - Math.min(...lastFrames);
- 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
- </Label>
- );
+ return <Label style={style}>{String(fps.toFixed(1))} FPS</Label>;
};
diff --git a/packages/cli/src/editor/components/PlayPause.tsx b/packages/cli/src/editor/components/PlayPause.tsx
index 0ad31e30d3..059f6ea423 100644
--- a/packages/cli/src/editor/components/PlayPause.tsx
+++ b/packages/cli/src/editor/components/PlayPause.tsx
@@ -88,33 +88,33 @@ export const PlayPause: React.FC = () => {
if (!config) {
return;
}
-
- if (playing) {
- let reqAnimFrameCall: number | null = null;
- const startedTime = performance.now();
- const startedFrame = frameRef.current;
-
- const step = () => {
- const time = performance.now() - startedTime;
- const calculatedFrame =
- (Math.round(time / (1000 / config.fps)) + startedFrame) %
- config.durationInFrames;
- if (calculatedFrame !== frameRef.current) {
- setLastFrames([...getLastFrames(), Date.now()]);
- setFrame(calculatedFrame);
- }
- reqAnimFrameCall = requestAnimationFrame(step);
- };
-
- reqAnimFrameCall = requestAnimationFrame(step);
-
- return () => {
- if (reqAnimFrameCall !== null) {
- console.log('can');
- cancelAnimationFrame(reqAnimFrameCall);
- }
- };
+ if (!playing) {
+ return;
}
+
+ let reqAnimFrameCall: number | null = null;
+ const startedTime = performance.now();
+ const startedFrame = frameRef.current;
+
+ const callback = () => {
+ const time = performance.now() - startedTime;
+ const calculatedFrame =
+ (Math.round(time / (1000 / config.fps)) + startedFrame) %
+ config.durationInFrames;
+ if (calculatedFrame !== frameRef.current) {
+ setLastFrames([...getLastFrames(), Date.now()]);
+ setFrame(calculatedFrame);
+ }
+ reqAnimFrameCall = requestAnimationFrame(callback);
+ };
+
+ reqAnimFrameCall = requestAnimationFrame(callback);
+
+ return () => {
+ if (reqAnimFrameCall !== null) {
+ cancelAnimationFrame(reqAnimFrameCall);
+ }
+ };
}, [config, setFrame, playing]);
return (
@@ -170,7 +170,6 @@ export const PlayPause: React.FC = () => {
}}
/>
</ControlButton>
- <div style={{width: 10}} />
</>
);
};