Skip to content

🎥 Make videos programmatically with React

add sine as a type

Jonny Burger412738e6b638
diff --git a/packages/convert/app/components/timing-editor/Sidebar.tsx b/packages/convert/app/components/timing-editor/Sidebar.tsx
index 141b92ec42..0052fb723a 100644
--- a/packages/convert/app/components/timing-editor/Sidebar.tsx
+++ b/packages/convert/app/components/timing-editor/Sidebar.tsx
@@ -11,7 +11,7 @@ import type {EasingType, TimingConfig} from './types';
export const Sidebar: React.FC<{
readonly config: TimingConfig;
readonly calculatedDurationInFrames: number;
- readonly onModeChange: (mode: 'spring' | 'interpolate') => void;
+ readonly onModeChange: (mode: 'spring' | 'interpolate' | 'sine') => void;
readonly onMassChange: (e: [number]) => void;
readonly onDampingChange: (e: [number]) => void;
readonly onStiffnessChange: (e: [number]) => void;
@@ -21,6 +21,9 @@ export const Sidebar: React.FC<{
readonly onOvershootClampingChange: (checked: boolean) => void;
readonly onReverseChange: (checked: boolean) => void;
readonly onEasingChange: (easing: EasingType) => void;
+ readonly onAmplitudeChange: (e: number) => void;
+ readonly onFrequencyChange: (e: number) => void;
+ readonly onFrameOffsetChange: (e: number) => void;
}> = ({
config,
calculatedDurationInFrames,
@@ -34,6 +37,9 @@ export const Sidebar: React.FC<{
onReverseChange,
onDelayChange,
onEasingChange,
+ onAmplitudeChange,
+ onFrequencyChange,
+ onFrameOffsetChange,
}) => {
return (
<div
@@ -49,7 +55,7 @@ export const Sidebar: React.FC<{
<Tabs
value={config.type}
onValueChange={(value) =>
- onModeChange(value as 'spring' | 'interpolate')
+ onModeChange(value as 'spring' | 'interpolate' | 'sine')
}
>
<TabsList style={{width: '100%'}}>
@@ -59,6 +65,9 @@ export const Sidebar: React.FC<{
<TabsTrigger value="interpolate" style={{flex: 1}}>
Interpolate
</TabsTrigger>
+ <TabsTrigger value="sine" style={{flex: 1}}>
+ Sine
+ </TabsTrigger>
</TabsList>
</Tabs>
<Spacing y={3} />
@@ -149,7 +158,7 @@ export const Sidebar: React.FC<{
onCheckedChange={onReverseChange}
/>
</>
- ) : (
+ ) : config.type === 'interpolate' ? (
<>
<label
htmlFor="easing-select"
@@ -208,6 +217,70 @@ export const Sidebar: React.FC<{
/>
<SliderLabel label="delay" toggleable={null} value={config.delay} />
</>
+ ) : (
+ <>
+ <Slider
+ min={1}
+ max={200}
+ value={[config.durationInFrames]}
+ onValueChange={(val) => {
+ onDurationInFramesChange(val[0]);
+ }}
+ onPointerUp={onRelease}
+ />
+ <SliderLabel
+ label="durationInFrames"
+ toggleable={null}
+ value={config.durationInFrames}
+ />
+ <br />
+ <Slider
+ min={0.1}
+ max={2}
+ step={0.1}
+ value={[config.amplitude]}
+ onValueChange={(val) => {
+ onAmplitudeChange(val[0]);
+ }}
+ onPointerUp={onRelease}
+ />
+ <SliderLabel
+ label="amplitude"
+ toggleable={null}
+ value={config.amplitude}
+ />
+ <br />
+ <Slider
+ min={0.1}
+ max={10}
+ step={0.1}
+ value={[config.frequency]}
+ onValueChange={(val) => {
+ onFrequencyChange(val[0]);
+ }}
+ onPointerUp={onRelease}
+ />
+ <SliderLabel
+ label="frequency"
+ toggleable={null}
+ value={config.frequency}
+ />
+ <br />
+ <Slider
+ min={0}
+ max={100}
+ value={[config.frameOffset]}
+ onValueChange={(val) => {
+ onFrameOffsetChange(val[0]);
+ }}
+ onPointerUp={onRelease}
+ />
+ <SliderLabel
+ label="frameOffset"
+ toggleable={null}
+ value={config.frameOffset}
+ />
+ </>
)}
<Spacing y={2} />
</div>
diff --git a/packages/convert/app/components/timing-editor/TimingEditor.tsx b/packages/convert/app/components/timing-editor/TimingEditor.tsx
index 2464c41c5b..9d0d483ca0 100644
--- a/packages/convert/app/components/timing-editor/TimingEditor.tsx
+++ b/packages/convert/app/components/timing-editor/TimingEditor.tsx
@@ -2,7 +2,11 @@ import {useCallback, useState} from 'react';
import {measureSpring} from 'remotion';
import {AnimationPreview} from './AnimationPreview';
import {CanvasWrapper} from './CanvasWrapper';
-import {DEFAULT_INTERPOLATE_CONFIG, DEFAULT_SPRING_CONFIG} from './defaults';
+import {
+ DEFAULT_INTERPOLATE_CONFIG,
+ DEFAULT_SINE_CONFIG,
+ DEFAULT_SPRING_CONFIG,
+} from './defaults';
import {Header} from './Header';
import {Sidebar} from './Sidebar';
import type {EasingType, TimingConfig} from './types';
@@ -13,15 +17,20 @@ export function TimingEditor() {
const [config, setConfig] = useState<TimingConfig>(DEFAULT_SPRING_CONFIG);
const [draggedConfig, setDraggedConfig] = useState<TimingConfig | null>(null);
- const onModeChange = useCallback((mode: 'spring' | 'interpolate') => {
- if (mode === 'spring') {
- setConfig(DEFAULT_SPRING_CONFIG);
- } else {
- setConfig(DEFAULT_INTERPOLATE_CONFIG);
- }
+ 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);
+ }
- setDraggedConfig(null);
- }, []);
+ setDraggedConfig(null);
+ },
+ [],
+ );
const onMassChange = useCallback(
(e: [number]) => {
@@ -71,6 +80,7 @@ export function TimingEditor() {
const onDelayChange = useCallback(
(e: number) => {
+ if (config.type === 'sine') return;
setDraggedConfig({...config, delay: e});
// Only immediately commit for spring mode
if (config.type === 'spring') {
@@ -113,6 +123,30 @@ export function TimingEditor() {
[config],
);
+ const onAmplitudeChange = useCallback(
+ (amplitude: number) => {
+ if (config.type !== 'sine') return;
+ setDraggedConfig({...config, amplitude});
+ },
+ [config],
+ );
+
+ const onFrequencyChange = useCallback(
+ (frequency: number) => {
+ if (config.type !== 'sine') return;
+ setDraggedConfig({...config, frequency});
+ },
+ [config],
+ );
+
+ const onFrameOffsetChange = useCallback(
+ (frameOffset: number) => {
+ if (config.type !== 'sine') return;
+ setDraggedConfig({...config, frameOffset});
+ },
+ [config],
+ );
+
const onRelease = useCallback(() => {
if (draggedConfig) {
setConfig(draggedConfig);
@@ -123,28 +157,29 @@ export function TimingEditor() {
const currentConfig = draggedConfig ?? config;
- const duration =
- currentConfig.delay +
- (currentConfig.type === 'spring'
- ? (currentConfig.durationInFrames ??
- measureSpring({
- fps,
- threshold: 0.001,
- config: currentConfig.springConfig,
- }))
- : currentConfig.durationInFrames);
-
- const draggedDuration = draggedConfig
- ? draggedConfig.delay +
- (draggedConfig.type === 'spring'
- ? (draggedConfig.durationInFrames ??
+ const getDuration = (cfg: TimingConfig) => {
+ if (cfg.type === 'spring') {
+ return (
+ cfg.delay +
+ (cfg.durationInFrames ??
measureSpring({
fps,
threshold: 0.001,
- config: draggedConfig.springConfig,
+ config: cfg.springConfig,
}))
- : draggedConfig.durationInFrames)
- : null;
+ );
+ }
+
+ if (cfg.type === 'interpolate') {
+ return cfg.delay + cfg.durationInFrames;
+ }
+
+ // sine
+ return cfg.durationInFrames;
+ };
+
+ const duration = getDuration(currentConfig);
+ const draggedDuration = draggedConfig ? getDuration(draggedConfig) : null;
return (
<div
@@ -182,6 +217,9 @@ export function TimingEditor() {
onDurationInFramesChange={onDurationInFramesChange}
onDelayChange={onDelayChange}
onEasingChange={onEasingChange}
+ onAmplitudeChange={onAmplitudeChange}
+ onFrequencyChange={onFrequencyChange}
+ onFrameOffsetChange={onFrameOffsetChange}
/>
<div id="spring-canvas">
<CanvasWrapper
diff --git a/packages/convert/app/components/timing-editor/defaults.ts b/packages/convert/app/components/timing-editor/defaults.ts
index 11553ff11a..2b133cb7bf 100644
--- a/packages/convert/app/components/timing-editor/defaults.ts
+++ b/packages/convert/app/components/timing-editor/defaults.ts
@@ -1,4 +1,9 @@
-import type {EasingType, InterpolateTimingConfig, SpringTimingConfig} from './types';
+import type {
+ EasingType,
+ InterpolateTimingConfig,
+ SineTimingConfig,
+ SpringTimingConfig,
+} from './types';
export const DEFAULT_DAMPING = 10;
export const DEFAULT_MASS = 1;
@@ -7,6 +12,11 @@ export const DEFAULT_STIFFNESS = 100;
export const DEFAULT_INTERPOLATE_DURATION = 30;
export const DEFAULT_EASING: EasingType = 'ease-out-cubic';
+export const DEFAULT_SINE_DURATION = 60;
+export const DEFAULT_SINE_AMPLITUDE = 1;
+export const DEFAULT_SINE_FREQUENCY = 1;
+export const DEFAULT_SINE_FRAME_OFFSET = 0;
+
export const DEFAULT_SPRING_CONFIG: SpringTimingConfig = {
type: 'spring',
springConfig: {
@@ -27,6 +37,14 @@ export const DEFAULT_INTERPOLATE_CONFIG: InterpolateTimingConfig = {
delay: 0,
};
+export const DEFAULT_SINE_CONFIG: SineTimingConfig = {
+ type: 'sine',
+ durationInFrames: DEFAULT_SINE_DURATION,
+ amplitude: DEFAULT_SINE_AMPLITUDE,
+ frequency: DEFAULT_SINE_FREQUENCY,
+ frameOffset: DEFAULT_SINE_FRAME_OFFSET,
+};
+
export const EASING_OPTIONS: {value: EasingType; label: string}[] = [
{value: 'linear', label: 'Linear'},
{value: 'ease', label: 'Ease'},
diff --git a/packages/convert/app/components/timing-editor/get-trajectory.ts b/packages/convert/app/components/timing-editor/get-trajectory.ts
index 85e84bbd41..ca67e0aab3 100644
--- a/packages/convert/app/components/timing-editor/get-trajectory.ts
+++ b/packages/convert/app/components/timing-editor/get-trajectory.ts
@@ -67,6 +67,18 @@ export const getTrajectory = (
});
}
+ if (config.type === 'sine') {
+ const {amplitude, frequency, frameOffset} = config;
+ const sineDuration = config.durationInFrames;
+
+ return new Array(durationInFrames).fill(true).map((_, i) => {
+ return (
+ amplitude *
+ Math.sin((2 * Math.PI * frequency * (i + frameOffset)) / sineDuration)
+ );
+ });
+ }
+
// Interpolate mode
const {easing, delay: interpolateDelay} = config;
const easingFn = getEasingFunction(easing);
diff --git a/packages/convert/app/components/timing-editor/types.ts b/packages/convert/app/components/timing-editor/types.ts
index d4097adc38..408f02ecba 100644
--- a/packages/convert/app/components/timing-editor/types.ts
+++ b/packages/convert/app/components/timing-editor/types.ts
@@ -37,4 +37,15 @@ export type InterpolateTimingConfig = {
delay: number;
};
-export type TimingConfig = SpringTimingConfig | InterpolateTimingConfig;
+export type SineTimingConfig = {
+ type: 'sine';
+ durationInFrames: number;
+ amplitude: number;
+ frequency: number;
+ frameOffset: number;
+};
+
+export type TimingConfig =
+ | SpringTimingConfig
+ | InterpolateTimingConfig
+ | SineTimingConfig;