Skip to content

🎥 Make videos programmatically with React

some stuff

Jonny Burger71b34a3cfae4
diff --git a/packages/cli/src/editor/components/NewComposition/CancelButton.tsx b/packages/cli/src/editor/components/NewComposition/CancelButton.tsx
new file mode 100644
index 0000000000..1d23957f2d
--- /dev/null
+++ b/packages/cli/src/editor/components/NewComposition/CancelButton.tsx
@@ -0,0 +1,10 @@
+import React, {SVGProps} from 'react';
+
+export const CancelButton: React.FC<SVGProps<SVGSVGElement>> = (props) => (
+ <svg viewBox="0 0 320 512" {...props}>
+ <path
+ fill="currentColor"
+ d="M207.6 256l107.72-107.72c6.23-6.23 6.23-16.34 0-22.58l-25.03-25.03c-6.23-6.23-16.34-6.23-22.58 0L160 208.4 52.28 100.68c-6.23-6.23-16.34-6.23-22.58 0L4.68 125.7c-6.23 6.23-6.23 16.34 0 22.58L112.4 256 4.68 363.72c-6.23 6.23-6.23 16.34 0 22.58l25.03 25.03c6.23 6.23 16.34 6.23 22.58 0L160 303.6l107.72 107.72c6.23 6.23 16.34 6.23 22.58 0l25.03-25.03c6.23-6.23 6.23-16.34 0-22.58L207.6 256z"
+ />
+ </svg>
+);
diff --git a/packages/cli/src/editor/components/NewComposition/NewCompAspectRatio.tsx b/packages/cli/src/editor/components/NewComposition/NewCompAspectRatio.tsx
new file mode 100644
index 0000000000..10761e3daa
--- /dev/null
+++ b/packages/cli/src/editor/components/NewComposition/NewCompAspectRatio.tsx
@@ -0,0 +1,41 @@
+import React from 'react';
+import {aspectRatio} from './render-aspect-ratio';
+
+const GUIDE_HEIGHT = 55;
+const GUIDE_WIDTH = 10;
+
+const label: React.CSSProperties = {
+ fontSize: 13,
+ color: 'rgba(255, 255, 255, 0.5)',
+ marginLeft: 10,
+};
+
+export const NewCompAspectRatio: React.FC<{
+ width: number;
+ height: number;
+}> = ({width, height}) => {
+ return (
+ <div>
+ <div
+ style={{
+ display: 'flex',
+ flexDirection: 'row',
+ marginLeft: 10,
+ alignItems: 'center',
+ }}
+ >
+ <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}`}
+ fill="transparent"
+ strokeWidth="2"
+ stroke="rgba(255, 255, 255, 0.2)"
+ />
+ </svg>
+ <div style={label}>
+ Aspect ratio {aspectRatio(Number(width), Number(height))}
+ </div>
+ </div>
+ </div>
+ );
+};
diff --git a/packages/cli/src/editor/components/NewComposition/NewCompHeader.tsx b/packages/cli/src/editor/components/NewComposition/NewCompHeader.tsx
index c8df63809c..e7ef428f0a 100644
--- a/packages/cli/src/editor/components/NewComposition/NewCompHeader.tsx
+++ b/packages/cli/src/editor/components/NewComposition/NewCompHeader.tsx
@@ -1,4 +1,5 @@
import React from 'react';
+import {CancelButton} from './CancelButton';
const container: React.CSSProperties = {
display: 'flex',
@@ -10,5 +11,11 @@ const container: React.CSSProperties = {
};
export const NewCompHeader: React.FC = () => {
- return <div style={container}>New composition</div>;
+ return (
+ <div style={container}>
+ <div>New composition</div>
+ <div style={{flex: 1}} />
+ <CancelButton style={{height: 20, width: 20}} />
+ </div>
+ );
};
diff --git a/packages/cli/src/editor/components/NewComposition/NewComposition.tsx b/packages/cli/src/editor/components/NewComposition/NewComposition.tsx
index d5b384cfa4..78f5d0ff34 100644
--- a/packages/cli/src/editor/components/NewComposition/NewComposition.tsx
+++ b/packages/cli/src/editor/components/NewComposition/NewComposition.tsx
@@ -1,10 +1,10 @@
import React, {ChangeEventHandler, useCallback, useState} from 'react';
import {BACKGROUND} from '../../helpers/colors';
import {FONT_FAMILY} from '../../helpers/font';
+import {NewCompAspectRatio} from './NewCompAspectRatio';
import {getNewCompositionCode} from './NewCompCode';
import {NewCompHeader} from './NewCompHeader';
import {RemotionInput} from './RemInput';
-import {aspectRatio} from './render-aspect-ratio';
const backgroundOverlay: React.CSSProperties = {
backgroundColor: 'rgba(255, 255, 255, 0.2)',
@@ -157,34 +157,45 @@ export const NewSequence: React.FC = () => {
</label>
</div>
<br />
- <label>
- <div style={leftLabel}>Width</div>
- <RemotionInput
- type="number"
- value={width}
- placeholder="Width (px)"
- onChange={onWidthChanged}
- name="width"
- />
- </label>
- <br />
- <br />
- <label>
- <div style={leftLabel}>Height</div>
- <RemotionInput
- type="number"
- value={height}
- onChange={onHeightChanged}
- placeholder="Height (px)"
- name="height"
- />
- </label>
- <br />
- <br />
- <div>
- <div style={leftLabel}>Aspect ratio</div>
- {aspectRatio(Number(width) / Number(height))}
+ <div
+ style={{
+ display: 'flex',
+ flexDirection: 'row',
+ alignItems: 'center',
+ }}
+ >
+ <div>
+ <label>
+ <div style={leftLabel}>Width</div>
+ <RemotionInput
+ type="number"
+ value={width}
+ placeholder="Width (px)"
+ onChange={onWidthChanged}
+ name="width"
+ />
+ </label>
+ <br />
+ <br />
+ <label>
+ <div style={leftLabel}>Height</div>
+ <RemotionInput
+ type="number"
+ value={height}
+ onChange={onHeightChanged}
+ placeholder="Height (px)"
+ name="height"
+ />
+ </label>
+ </div>
+ <div>
+ <NewCompAspectRatio
+ width={Number(width)}
+ height={Number(height)}
+ />
+ </div>
</div>
+
<br />
<label>
<div style={leftLabel}> Duration in frames</div>
diff --git a/packages/cli/src/editor/components/NewComposition/RemInput.tsx b/packages/cli/src/editor/components/NewComposition/RemInput.tsx
index 93c73f7685..52c3ac51c6 100644
--- a/packages/cli/src/editor/components/NewComposition/RemInput.tsx
+++ b/packages/cli/src/editor/components/NewComposition/RemInput.tsx
@@ -1,4 +1,11 @@
-import React, {forwardRef, useMemo} from 'react';
+import React, {
+ forwardRef,
+ useEffect,
+ useImperativeHandle,
+ useMemo,
+ useRef,
+ useState,
+} from 'react';
import {BORDER_COLOR} from '../../helpers/colors';
import {FONT_FAMILY} from '../../helpers/font';
@@ -11,18 +18,49 @@ const RemInputForwardRef: React.ForwardRefRenderFunction<
HTMLInputElement,
Props
> = (props, ref) => {
+ const [isFocused, setIsFocused] = useState(false);
+ const inputRef = useRef<HTMLInputElement>(null);
+
const style = useMemo(() => {
return {
- backgroundColor: 'rgba(255, 255, 255, 0.2)',
+ backgroundColor: 'rgba(255, 255, 255, 0.05)',
border: BORDER_COLOR,
fontFamily: FONT_FAMILY,
- padding: '14px 14px',
+ padding: '10px 10px',
color: 'white',
+ outline: 'none',
+ borderStyle: 'solid',
+ borderWidth: 1,
+ borderRadius: 5,
+ borderColor: isFocused ? 'rgba(255, 255, 255, 0.5)' : 'transparent',
...(props.style ?? {}),
};
- }, [props.style]);
+ }, [isFocused, props.style]);
+
+ useImperativeHandle(ref, () => {
+ return inputRef.current as HTMLInputElement;
+ });
+
+ useEffect(() => {
+ if (!inputRef.current) {
+ return;
+ }
+
+ const {current} = inputRef;
+
+ const onFocus = () => setIsFocused(true);
+ const onBlur = () => setIsFocused(false);
+
+ current.addEventListener('focus', onFocus);
+ current.addEventListener('blur', onBlur);
+
+ return () => {
+ current.removeEventListener('focus', onFocus);
+ current.removeEventListener('blur', onBlur);
+ };
+ }, [inputRef]);
- return <input ref={ref} {...props} style={style} />;
+ return <input ref={inputRef} {...props} style={style} />;
};
export const RemotionInput = forwardRef(RemInputForwardRef);
diff --git a/packages/cli/src/editor/components/NewComposition/render-aspect-ratio.tsx b/packages/cli/src/editor/components/NewComposition/render-aspect-ratio.tsx
index f8493d0de3..ccf0b81749 100644
--- a/packages/cli/src/editor/components/NewComposition/render-aspect-ratio.tsx
+++ b/packages/cli/src/editor/components/NewComposition/render-aspect-ratio.tsx
@@ -1,11 +1,16 @@
-export const aspectRatio = (ratio: number) => {
- if (ratio === 16 / 9) {
- return '16:9';
+function gcd_two_numbers(x: number, y: number) {
+ x = Math.abs(x);
+ y = Math.abs(y);
+ while (y) {
+ const t = y;
+ y = x % y;
+ x = t;
}
- if (ratio === 5 / 4) {
- return '5:4';
- }
+ return x;
+}
- return ratio.toFixed(2);
+export const aspectRatio = (width: number, height: number) => {
+ const commonDivisor = gcd_two_numbers(width, height);
+ return width / commonDivisor + ':' + height / commonDivisor;
};