🎥 Make videos programmatically with React
multiple timings
Jonny Burger47cf9c6ce161
diff --git a/packages/convert/app/components/timing-editor/Canvas.tsx b/packages/convert/app/components/timing-editor/Canvas.tsxindex fc36727b93..59c7a33b07 100644--- a/packages/convert/app/components/timing-editor/Canvas.tsx+++ b/packages/convert/app/components/timing-editor/Canvas.tsx@@ -1,23 +1,22 @@import React, {useEffect, useMemo, useRef} from 'react';import {AnimationDuration} from './AnimationDuration';import {draw, stopDrawing} from './draw';-import type {TimingConfig} from './types';+import type {TimingComponent, TimingConfig} from './types';export const Canvas: React.FC<{readonly width: number;readonly height: number;- readonly draggedConfig: TimingConfig | null;+ readonly components: TimingComponent[];+ readonly draggedState: {componentId: string; config: TimingConfig} | null;readonly draggedDuration: number | null;readonly duration: number;- readonly config: TimingConfig;readonly fps: number;readonly replayKey: number;}> = ({height,width,- draggedConfig,- draggedDuration,- config,+ components,+ draggedState,duration,fps,replayKey,@@ -38,8 +37,8 @@ export const Canvas: React.FC<{const durationLabel =durationType === 'seconds'- ? `${((draggedDuration ?? duration) / fps).toFixed(2)}sec`- : `${draggedDuration ?? duration} frames`;+ ? `${(duration / fps).toFixed(2)}sec`+ : `${Math.round(duration)} frames`;useEffect(() => {if (!canvasRef.current) {@@ -49,20 +48,18 @@ export const Canvas: React.FC<{stopDrawing();draw({ref: canvasRef.current,- duration: draggedDuration ?? duration,- config,- draggedConfig,+ duration,+ components,+ draggedState,fps,- draggedDuration,height,width,labelText: durationLabel,});}, [- draggedDuration,duration,- config,- draggedConfig,+ components,+ draggedState,fps,width,height,diff --git a/packages/convert/app/components/timing-editor/CanvasWrapper.tsx b/packages/convert/app/components/timing-editor/CanvasWrapper.tsxindex 42140e3414..d10026d010 100644--- a/packages/convert/app/components/timing-editor/CanvasWrapper.tsx+++ b/packages/convert/app/components/timing-editor/CanvasWrapper.tsx@@ -1,16 +1,16 @@import {PlayerInternals} from '@remotion/player';import {useRef} from 'react';import {Canvas} from './Canvas';-import type {TimingConfig} from './types';+import type {TimingComponent, TimingConfig} from './types';export const CanvasWrapper: React.FC<{- readonly draggedConfig: TimingConfig | null;+ readonly components: TimingComponent[];+ readonly draggedState: {componentId: string; config: TimingConfig} | null;readonly draggedDuration: number | null;readonly duration: number;- readonly config: TimingConfig;readonly fps: number;readonly replayKey: number;-}> = ({config, draggedConfig, draggedDuration, duration, fps, replayKey}) => {+}> = ({components, draggedState, draggedDuration, duration, fps, replayKey}) => {const outer = useRef<HTMLDivElement>(null);const elementSize = PlayerInternals.useElementSize(outer, {@@ -31,8 +31,8 @@ export const CanvasWrapper: React.FC<{>{elementSize ? (<Canvas- config={config}- draggedConfig={draggedConfig}+ components={components}+ draggedState={draggedState}draggedDuration={draggedDuration}duration={duration}fps={fps}diff --git a/packages/convert/app/components/timing-editor/Sidebar.tsx b/packages/convert/app/components/timing-editor/Sidebar.tsxindex 5cd34630e1..1b210da6e3 100644--- a/packages/convert/app/components/timing-editor/Sidebar.tsx+++ b/packages/convert/app/components/timing-editor/Sidebar.tsx@@ -3,58 +3,65 @@ import React from 'react';import {Tabs, TabsList, TabsTrigger} from '~/components/ui/tabs';import {InterpolateEditor} from './InterpolateEditor';import {SineEditor} from './SineEditor';-import {Spacing} from './Spacing';import {SpringEditor} from './SpringEditor';-import type {TimingConfig} from './types';+import type {TimingComponent, TimingConfig} from './types';-export const Sidebar: React.FC<{- readonly config: TimingConfig;+const TimingComponentEditor: React.FC<{+ readonly component: TimingComponent;+ readonly draggedConfig: TimingConfig | null;readonly calculatedDurationInFrames: number;readonly onModeChange: (mode: 'spring' | 'interpolate' | 'sine') => void;readonly setDraggedConfig: (config: TimingConfig) => void;readonly onChange: (config: TimingConfig) => void;readonly onRelease: () => void;- readonly onReplay: () => void;+ readonly onRemove: () => void;+ readonly canRemove: boolean;}> = ({- config,+ component,+ draggedConfig,calculatedDurationInFrames,onModeChange,onChange,setDraggedConfig,onRelease,- onReplay,+ onRemove,+ canRemove,}) => {+ const config = draggedConfig ?? component.config;+return (- <Card className="p-4 flex flex-col flex-1 w-full overflow-y-auto md:w-[400px] md:h-full md:flex-none">- <div className="flex items-center justify-between mb-4">- <div className="font-brand font-bold text-xl">Timing Editor</div>- <button- type="button"- onClick={onReplay}- className="px-3 py-1 text-sm bg-[#0b84f3] text-white rounded font-brand font-medium hover:bg-[#0a75d9] transition-colors"+ <Card className="p-3 mb-3">+ <div className="flex items-center justify-between mb-2">+ <Tabs+ value={config.type}+ onValueChange={(value) =>+ onModeChange(value as 'spring' | 'interpolate' | 'sine')+ }+ className="flex-1">- Replay- </button>+ <TabsList style={{width: '100%'}}>+ <TabsTrigger value="spring" style={{flex: 1}}>+ Spring+ </TabsTrigger>+ <TabsTrigger value="interpolate" style={{flex: 1}}>+ Interpolate+ </TabsTrigger>+ <TabsTrigger value="sine" style={{flex: 1}}>+ Sine+ </TabsTrigger>+ </TabsList>+ </Tabs>+ {canRemove ? (+ <button+ type="button"+ onClick={onRemove}+ className="ml-2 px-2 py-1 text-sm text-red-400 hover:text-red-300 hover:bg-red-900/30 rounded transition-colors"+ title="Remove timing component"+ >+ Remove+ </button>+ ) : null}</div>- <Tabs- value={config.type}- onValueChange={(value) =>- onModeChange(value as 'spring' | 'interpolate' | 'sine')- }- >- <TabsList style={{width: '100%'}}>- <TabsTrigger value="spring" style={{flex: 1}}>- Spring- </TabsTrigger>- <TabsTrigger value="interpolate" style={{flex: 1}}>- Interpolate- </TabsTrigger>- <TabsTrigger value="sine" style={{flex: 1}}>- Sine- </TabsTrigger>- </TabsList>- </Tabs>- <Spacing y={2} />{config.type === 'spring' ? (<SpringEditor@@ -81,3 +88,74 @@ export const Sidebar: React.FC<{</Card>);};++export const Sidebar: React.FC<{+ readonly components: TimingComponent[];+ readonly draggedState: {componentId: string; config: TimingConfig} | null;+ readonly calculatedDurationInFrames: number;+ readonly onModeChange: (+ componentId: string,+ mode: 'spring' | 'interpolate' | 'sine',+ ) => void;+ readonly setDraggedConfig: (+ componentId: string,+ config: TimingConfig,+ ) => void;+ readonly onChange: (componentId: string, config: TimingConfig) => void;+ readonly onRelease: (componentId: string) => void;+ readonly onReplay: () => void;+ readonly addComponent: () => void;+ readonly removeComponent: (componentId: string) => void;+}> = ({+ components,+ draggedState,+ calculatedDurationInFrames,+ onModeChange,+ onChange,+ setDraggedConfig,+ onRelease,+ onReplay,+ addComponent,+ removeComponent,+}) => {+ return (+ <div className="p-4 flex flex-col flex-1 w-full overflow-y-auto md:w-[400px] md:h-full md:flex-none overflow-y-auto">+ <div className="flex items-center justify-between mb-4">+ <div className="font-brand font-bold text-xl">Timing Editor</div>+ <button+ type="button"+ onClick={onReplay}+ className="px-3 py-1 text-sm bg-[#0b84f3] text-white rounded font-brand font-medium hover:bg-[#0a75d9] transition-colors"+ >+ Replay+ </button>+ </div>++ {components.map((component) => (+ <TimingComponentEditor+ key={component.id}+ component={component}+ draggedConfig={+ draggedState?.componentId === component.id+ ? draggedState.config+ : null+ }+ calculatedDurationInFrames={calculatedDurationInFrames}+ onModeChange={(mode) => onModeChange(component.id, mode)}+ setDraggedConfig={(config) => setDraggedConfig(component.id, config)}+ onChange={(config) => onChange(component.id, config)}+ onRelease={() => onRelease(component.id)}+ onRemove={() => removeComponent(component.id)}+ canRemove={components.length > 1}+ />+ ))}+ <button+ type="button"+ onClick={addComponent}+ className="w-full px-3 py-2 text-sm border border-dashed border-[#444] text-[#888] hover:border-[#666] hover:text-[#aaa] rounded font-brand font-medium transition-colors"+ >+ + Add Timing Component+ </button>+ </div>+ );+};diff --git a/packages/convert/app/components/timing-editor/TimingEditor.tsx b/packages/convert/app/components/timing-editor/TimingEditor.tsxindex 3c6f3b5f93..7b045ea624 100644--- a/packages/convert/app/components/timing-editor/TimingEditor.tsx+++ b/packages/convert/app/components/timing-editor/TimingEditor.tsx@@ -8,13 +8,21 @@ import {DEFAULT_SPRING_CONFIG,} from './defaults';import {Sidebar} from './Sidebar';-import type {TimingConfig} from './types';+import type {TimingComponent, TimingConfig} from './types';const fps = 60;+let nextId = 1;+const generateId = () => `timing-${nextId++}`;+export function TimingEditor() {- const [config, setConfig] = useState<TimingConfig>(DEFAULT_SPRING_CONFIG);- const [draggedConfig, setDraggedConfig] = useState<TimingConfig | null>(null);+ const [components, setComponents] = useState<TimingComponent[]>([+ {id: generateId(), config: DEFAULT_SPRING_CONFIG},+ ]);+ const [draggedState, setDraggedState] = useState<{+ componentId: string;+ config: TimingConfig;+ } | null>(null);const [replayKey, setReplayKey] = useState(0);const onReplay = useCallback(() => {@@ -22,29 +30,64 @@ export function TimingEditor() {}, []);const onModeChange = useCallback(- (mode: 'spring' | 'interpolate' | 'sine') => {- if (mode === 'spring') {- setConfig(DEFAULT_SPRING_CONFIG);- } else if (mode === 'interpolate') {- setConfig(DEFAULT_INTERPOLATE_CONFIG);- } else {- setConfig(DEFAULT_SINE_CONFIG);+ (componentId: string, mode: 'spring' | 'interpolate' | 'sine') => {+ setComponents((prev) =>+ prev.map((c) => {+ if (c.id !== componentId) return c;+ if (mode === 'spring') {+ return {...c, config: DEFAULT_SPRING_CONFIG};+ }++ if (mode === 'interpolate') {+ return {...c, config: DEFAULT_INTERPOLATE_CONFIG};+ }++ return {...c, config: DEFAULT_SINE_CONFIG};+ }),+ );+ setDraggedState(null);+ },+ [],+ );++ const onRelease = useCallback(+ (componentId: string) => {+ if (draggedState && draggedState.componentId === componentId) {+ setComponents((prev) =>+ prev.map((c) =>+ c.id === componentId ? {...c, config: draggedState.config} : c,+ ),+ );}- setDraggedConfig(null);+ setDraggedState(null);+ },+ [draggedState],+ );++ const setDraggedConfig = useCallback(+ (componentId: string, config: TimingConfig) => {+ setDraggedState({componentId, config});},[],);- const onRelease = useCallback(() => {- if (draggedConfig) {- setConfig(draggedConfig);- }+ const onChange = useCallback((componentId: string, config: TimingConfig) => {+ setComponents((prev) =>+ prev.map((c) => (c.id === componentId ? {...c, config} : c)),+ );+ }, []);- setDraggedConfig(null);- }, [draggedConfig]);+ const addComponent = useCallback(() => {+ setComponents((prev) => [+ ...prev,+ {id: generateId(), config: DEFAULT_SPRING_CONFIG},+ ]);+ }, []);- const currentConfig = draggedConfig ?? config;+ const removeComponent = useCallback((componentId: string) => {+ setComponents((prev) => prev.filter((c) => c.id !== componentId));+ }, []);const getDuration = (cfg: TimingConfig) => {if (cfg.type === 'spring') {@@ -67,25 +110,44 @@ export function TimingEditor() {return cfg.durationInFrames;};- const duration = getDuration(currentConfig);- const draggedDuration = draggedConfig ? getDuration(draggedConfig) : null;+ // Get current configs (applying dragged state if any)+ const currentComponents = components.map((c) => {+ if (draggedState && draggedState.componentId === c.id) {+ return {...c, config: draggedState.config};+ }++ return c;+ });++ // Calculate max duration across all configs+ const duration = Math.max(+ ...currentComponents.map((c) => getDuration(c.config)),+ );++ // For dragged duration, calculate based on dragged config only if dragging+ const draggedDuration = draggedState+ ? Math.max(...currentComponents.map((c) => getDuration(c.config)))+ : null;return (<div className="flex justify-center items-center h-full w-full absolute flex-col"><div className="flex overflow-hidden w-full flex-1 flex-col md:flex-row p-2"><Sidebar- config={currentConfig}+ components={components}+ draggedState={draggedState}calculatedDurationInFrames={duration}onModeChange={onModeChange}setDraggedConfig={setDraggedConfig}onRelease={onRelease}- onChange={setConfig}+ onChange={onChange}onReplay={onReplay}+ addComponent={addComponent}+ removeComponent={removeComponent}/><div className="flex flex-col h-[300px] w-full border-b border-[#242424] md:h-auto md:flex-1 md:border-b-0"><CanvasWrapper- config={config}- draggedConfig={draggedConfig}+ components={components}+ draggedState={draggedState}draggedDuration={draggedDuration}duration={duration}fps={fps}diff --git a/packages/convert/app/components/timing-editor/draw.ts b/packages/convert/app/components/timing-editor/draw.tsindex 3a9ac4d6f9..055a59e36c 100644--- a/packages/convert/app/components/timing-editor/draw.ts+++ b/packages/convert/app/components/timing-editor/draw.ts@@ -9,17 +9,34 @@ import {PADDING_TOP,drawTrajectory,} from './draw-trajectory';-import type {TimingConfig} from './types';+import type {TimingComponent, TimingConfig} from './types';export let stopDrawing = () => {};+const sumTrajectories = (trajectories: number[][]): number[] => {+ if (trajectories.length === 0) return [];+ const maxLength = Math.max(...trajectories.map((t) => t.length));+ const result: number[] = [];++ for (let i = 0; i < maxLength; i++) {+ let sum = 0;+ for (const trajectory of trajectories) {+ // If trajectory is shorter, use its last value (or 0 if empty)+ const value = i < trajectory.length ? trajectory[i] : (trajectory[trajectory.length - 1] ?? 0);+ sum += value;+ }+ result.push(sum);+ }++ return result;+};+export const draw = ({ref,duration,fps,- config,- draggedConfig,- draggedDuration,+ components,+ draggedState,height,width,labelText,@@ -27,9 +44,8 @@ export const draw = ({ref: HTMLCanvasElement;duration: number;fps: number;- config: TimingConfig;- draggedConfig: TimingConfig | null;- draggedDuration: number | null;+ components: TimingComponent[];+ draggedState: {componentId: string; config: TimingConfig} | null;width: number;height: number;labelText: string;@@ -41,14 +57,35 @@ export const draw = ({}context.clearRect(0, 0, width, height);- const trajectory = getTrajectory(duration, fps, config);- const draggedTrajectory = draggedConfig- ? getTrajectory(draggedDuration ?? duration, fps, draggedConfig)- : [];-- const activeTrajectory = draggedConfig ? draggedTrajectory : trajectory;- const max = Math.max(...activeTrajectory);- const min = Math.min(...activeTrajectory);++ // Get configs, applying dragged state if any+ const currentConfigs = components.map((c) => {+ if (draggedState && draggedState.componentId === c.id) {+ return draggedState.config;+ }+ return c.config;+ });++ // Get committed configs (without dragged state)+ const committedConfigs = components.map((c) => c.config);++ // Calculate trajectories for current state (with dragged)+ const currentTrajectories = currentConfigs.map((config) =>+ getTrajectory(duration, fps, config),+ );++ // Calculate trajectories for committed state (without dragged)+ const committedTrajectories = committedConfigs.map((config) =>+ getTrajectory(duration, fps, config),+ );++ // Sum the trajectories+ const combinedTrajectory = sumTrajectories(currentTrajectories);+ const committedCombinedTrajectory = sumTrajectories(committedTrajectories);++ // Use combined trajectory for min/max calculation+ const max = Math.max(...combinedTrajectory);+ const min = Math.min(...combinedTrajectory);const range = max - min;context.strokeStyle = 'rgba(0, 0, 0, 0.1)';@@ -122,35 +159,38 @@ export const draw = ({const toStop: (() => void)[] = [];- const stopPrimary = drawTrajectory({- springTrajectory: trajectory,- canvasHeight: height,- canvasWidth: width,- context,- min,- max,- primary: !draggedConfig,- animate: !draggedConfig,- fps,- });- toStop.push(stopPrimary);-- if (draggedConfig) {+ // If dragging, show committed trajectory as faded background+ if (draggedState) {toStop.push(drawTrajectory({- springTrajectory: draggedTrajectory,+ springTrajectory: committedCombinedTrajectory,canvasHeight: height,canvasWidth: width,context,min,max,- primary: true,+ primary: false,animate: false,fps,}),);}+ // Draw the combined trajectory (animated if not dragging)+ toStop.push(+ drawTrajectory({+ springTrajectory: combinedTrajectory,+ canvasHeight: height,+ canvasWidth: width,+ context,+ min,+ max,+ primary: true,+ animate: !draggedState,+ fps,+ }),+ );+stopDrawing = () => {toStop.forEach((stop) => stop());};diff --git a/packages/convert/app/components/timing-editor/types.ts b/packages/convert/app/components/timing-editor/types.tsindex 408f02ecba..5e02673f69 100644--- a/packages/convert/app/components/timing-editor/types.ts+++ b/packages/convert/app/components/timing-editor/types.ts@@ -49,3 +49,8 @@ export type TimingConfig =| SpringTimingConfig| InterpolateTimingConfig| SineTimingConfig;++export type TimingComponent = {+ id: string;+ config: TimingConfig;+};