Skip to content

🎥 Make videos programmatically with React

aspect ratio

Jonny Burger3b2b85d27568
diff --git a/packages/cli/src/editor/components/NewComposition/NewCompAspectRatio.tsx b/packages/cli/src/editor/components/NewComposition/NewCompAspectRatio.tsx
index 5f539d73e1..3f1e9a5794 100644
--- a/packages/cli/src/editor/components/NewComposition/NewCompAspectRatio.tsx
+++ b/packages/cli/src/editor/components/NewComposition/NewCompAspectRatio.tsx
@@ -6,22 +6,22 @@ import {ToggleAspectRatio} from './ToggleAspectRatio';
const GUIDE_HEIGHT = 55;
const GUIDE_WIDTH = 10;
+const container: React.CSSProperties = {
+ display: 'flex',
+ flexDirection: 'row',
+ marginLeft: 10,
+ alignItems: 'center',
+};
+
export const NewCompAspectRatio: React.FC<{
width: number;
height: number;
aspectRatioLocked: boolean;
- setAspectRatioLocked: React.Dispatch<React.SetStateAction<boolean>>;
+ setAspectRatioLocked: (newOption: boolean) => void;
}> = ({width, height, aspectRatioLocked, setAspectRatioLocked}) => {
return (
<div>
- <div
- style={{
- display: 'flex',
- flexDirection: 'row',
- marginLeft: 10,
- alignItems: 'center',
- }}
- >
+ <div style={container}>
<svg width={GUIDE_WIDTH} height={GUIDE_HEIGHT}>
<path
d={`M 0 0 L ${GUIDE_WIDTH} 0 L ${GUIDE_WIDTH} ${GUIDE_HEIGHT} L 0 ${GUIDE_HEIGHT}`}
diff --git a/packages/cli/src/editor/components/NewComposition/NewComposition.tsx b/packages/cli/src/editor/components/NewComposition/NewComposition.tsx
index 2250288f30..2a523a7073 100644
--- a/packages/cli/src/editor/components/NewComposition/NewComposition.tsx
+++ b/packages/cli/src/editor/components/NewComposition/NewComposition.tsx
@@ -7,6 +7,10 @@ import React, {
} from 'react';
import {Internals} from 'remotion';
import {Checkmark} from '../../icons/Checkmark';
+import {
+ loadAspectRatioOption,
+ persistAspectRatioOption,
+} from '../../state/aspect-ratio-locked';
import {ModalsContext} from '../../state/modals';
import {CopyButton} from '../CopyButton';
import {Row, Spacing} from '../layout';
@@ -62,7 +66,15 @@ export const NewComposition: React.FC<{initialCompType: CompType}> = ({
const [width, setWidth] = useState('1280');
const [height, setHeight] = useState('720');
const [durationInFrames, setDurationInFrames] = useState('150');
- const [aspectRatioLocked, setAspectRatioLocked] = useState(false);
+ const [aspectRatioLocked, setAspectRatioLockedState] = useState(
+ loadAspectRatioOption()
+ );
+
+ const setAspectRatioLocked = useCallback((option: boolean) => {
+ setAspectRatioLockedState(option);
+ persistAspectRatioOption(option);
+ }, []);
+
const {setSelectedModal} = useContext(ModalsContext);
const onQuit = useCallback(() => {
diff --git a/packages/cli/src/editor/components/NewComposition/ToggleAspectRatio.tsx b/packages/cli/src/editor/components/NewComposition/ToggleAspectRatio.tsx
index 008fc5e278..34dff00887 100644
--- a/packages/cli/src/editor/components/NewComposition/ToggleAspectRatio.tsx
+++ b/packages/cli/src/editor/components/NewComposition/ToggleAspectRatio.tsx
@@ -6,20 +6,24 @@ const buttonStyle: React.CSSProperties = {
backgroundColor: BACKGROUND,
border: 'none',
position: 'absolute',
+ paddingLeft: 2,
+ paddingRight: 2,
+ marginLeft: 3,
};
const iconStyle: React.CSSProperties = {
- width: 14,
+ width: 10,
color: 'white',
};
export const ToggleAspectRatio: React.FC<{
aspectRatioLocked: boolean;
- setAspectRatioLocked: React.Dispatch<React.SetStateAction<boolean>>;
+ setAspectRatioLocked: (option: boolean) => void;
}> = ({aspectRatioLocked, setAspectRatioLocked}) => {
const onClick: MouseEventHandler = useCallback(() => {
- setAspectRatioLocked((a) => !a);
- }, [setAspectRatioLocked]);
+ setAspectRatioLocked(!aspectRatioLocked);
+ }, [aspectRatioLocked, setAspectRatioLocked]);
+
return (
<button type="button" onClick={onClick} style={buttonStyle}>
{aspectRatioLocked ? (
diff --git a/packages/cli/src/editor/state/aspect-ratio-locked.ts b/packages/cli/src/editor/state/aspect-ratio-locked.ts
new file mode 100644
index 0000000000..8c9eb43401
--- /dev/null
+++ b/packages/cli/src/editor/state/aspect-ratio-locked.ts
@@ -0,0 +1,14 @@
+const aspectRatioLocalStorageKey = 'aspectRatio';
+
+export const persistAspectRatioOption = (option: boolean) => {
+ localStorage.setItem(aspectRatioLocalStorageKey, String(option));
+};
+
+export const loadAspectRatioOption = () => {
+ const item = localStorage.getItem(aspectRatioLocalStorageKey);
+ if (item === null) {
+ return true;
+ }
+
+ return item !== 'false';
+};
diff --git a/packages/player/src/use-player.ts b/packages/player/src/use-player.ts
index d9898045f8..baf8d31e90 100644
--- a/packages/player/src/use-player.ts
+++ b/packages/player/src/use-player.ts
@@ -89,7 +89,7 @@ export const usePlayer = (): {
return Math.max(0, f - frames);
});
},
- [frame, setFrame, video]
+ [hasVideo, setFrame]
);
const frameForward = useCallback(