summaryrefslogtreecommitdiff
path: root/apps/web/src/effects/definitions/motion.ts
blob: 2e3292a2108816f7489d0131e011de43531791ef (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import type { ParamDefinition, ParamValues } from "@/params";
import type { EffectDefinition, MotionOffset } from "@/effects/types";
import { MOTION_EASING_OPTIONS, getMotionIntensity } from "@/effects/motion";

const ZOOM_RANGE = 0.35;
const PAN_ZOOM_HEADROOM = 0.15;
const PAN_TRAVEL = 0.06;
const SHAKE_SCALE_HEADROOM = 0.08;
const SHAKE_TRAVEL_X = 0.025;
const SHAKE_TRAVEL_Y = 0.02;
const SHAKE_FREQUENCY = 6;
const SHAKE_Y_DETUNE = 1.31;
const PULSE_RANGE = 0.12;
const PULSE_FREQUENCY = 3;
const SPIN_FULL_TURN_DEGREES = 360;
const CUSTOM_ZOOM_MIN_SCALE = 25;
const CUSTOM_ZOOM_MAX_SCALE = 400;

function readScaleParam({
	effectParams,
	key,
}: {
	effectParams: ParamValues;
	key: string;
}): number {
	const raw = effectParams[key];
	const value = typeof raw === "number" ? raw : Number.parseFloat(String(raw));
	return Number.isFinite(value) ? value : 100;
}

export const customZoomEffectDefinition: EffectDefinition = {
	type: "custom-zoom",
	name: "Custom Zoom",
	keywords: ["zoom", "ken burns", "custom", "scale", "start", "end"],
	category: "lens",
	params: [
		{
			key: "startScale",
			label: "Start scale",
			type: "number",
			default: 100,
			min: CUSTOM_ZOOM_MIN_SCALE,
			max: CUSTOM_ZOOM_MAX_SCALE,
			step: 1,
			keyframable: true,
		},
		{
			key: "endScale",
			label: "End scale",
			type: "number",
			default: 135,
			min: CUSTOM_ZOOM_MIN_SCALE,
			max: CUSTOM_ZOOM_MAX_SCALE,
			step: 1,
			keyframable: true,
		},
		{
			key: "easing",
			label: "Easing",
			type: "select",
			default: "smooth",
			options: MOTION_EASING_OPTIONS,
		},
	],
	renderer: { passes: [] },
	motion: {
		evaluate: ({ effectParams, progress }) => {
			const startScale = readScaleParam({ effectParams, key: "startScale" });
			const endScale = readScaleParam({ effectParams, key: "endScale" });
			return {
				scale: (startScale + (endScale - startScale) * progress) / 100,
				translateX: 0,
				translateY: 0,
			};
		},
	},
};

function motionParams(): ParamDefinition[] {
	return [
		{
			key: "intensity",
			label: "Intensity",
			type: "number",
			default: 100,
			min: 0,
			max: 100,
			step: 1,
			keyframable: true,
		},
		{
			key: "easing",
			label: "Easing",
			type: "select",
			default: "smooth",
			options: MOTION_EASING_OPTIONS,
		},
	];
}

function zoomOffset({
	effectParams,
	progress,
	direction,
}: {
	effectParams: ParamValues;
	progress: number;
	direction: 1 | -1;
}): MotionOffset {
	const intensity = getMotionIntensity({ effectParams });
	const amount =
		direction === 1 ? progress * intensity : (1 - progress) * intensity;
	return {
		scale: 1 + ZOOM_RANGE * amount,
		translateX: 0,
		translateY: 0,
	};
}

function panOffset({
	effectParams,
	progress,
	axis,
	direction,
}: {
	effectParams: ParamValues;
	progress: number;
	axis: "x" | "y";
	direction: 1 | -1;
}): MotionOffset {
	const intensity = getMotionIntensity({ effectParams });
	const travel = (progress - 0.5) * 2 * PAN_TRAVEL * intensity * direction;
	return {
		scale: 1 + PAN_ZOOM_HEADROOM * intensity,
		translateX: axis === "x" ? travel : 0,
		translateY: axis === "y" ? travel : 0,
	};
}

function buildMotionDefinition({
	type,
	name,
	keywords,
	evaluate,
}: Pick<EffectDefinition, "type" | "name" | "keywords"> & {
	evaluate: NonNullable<EffectDefinition["motion"]>["evaluate"];
}): EffectDefinition {
	return {
		type,
		name,
		keywords,
		category: "lens",
		params: motionParams(),
		renderer: { passes: [] },
		motion: { evaluate },
	};
}

export const motionEffectDefinitions: EffectDefinition[] = [
	buildMotionDefinition({
		type: "zoom-in",
		name: "Zoom In",
		keywords: ["zoom", "ken burns", "scale", "push in"],
		evaluate: ({ effectParams, progress }) =>
			zoomOffset({ effectParams, progress, direction: 1 }),
	}),
	buildMotionDefinition({
		type: "zoom-out",
		name: "Zoom Out",
		keywords: ["zoom", "ken burns", "scale", "pull out"],
		evaluate: ({ effectParams, progress }) =>
			zoomOffset({ effectParams, progress, direction: -1 }),
	}),
	buildMotionDefinition({
		type: "pan-left",
		name: "Pan Left",
		keywords: ["pan", "ken burns", "slide", "left"],
		evaluate: ({ effectParams, progress }) =>
			panOffset({ effectParams, progress, axis: "x", direction: -1 }),
	}),
	buildMotionDefinition({
		type: "pan-right",
		name: "Pan Right",
		keywords: ["pan", "ken burns", "slide", "right"],
		evaluate: ({ effectParams, progress }) =>
			panOffset({ effectParams, progress, axis: "x", direction: 1 }),
	}),
	buildMotionDefinition({
		type: "pan-up",
		name: "Pan Up",
		keywords: ["pan", "ken burns", "slide", "up", "tilt"],
		evaluate: ({ effectParams, progress }) =>
			panOffset({ effectParams, progress, axis: "y", direction: -1 }),
	}),
	buildMotionDefinition({
		type: "pan-down",
		name: "Pan Down",
		keywords: ["pan", "ken burns", "slide", "down", "tilt"],
		evaluate: ({ effectParams, progress }) =>
			panOffset({ effectParams, progress, axis: "y", direction: 1 }),
	}),
	buildMotionDefinition({
		type: "shake",
		name: "Shake",
		keywords: ["shake", "camera", "handheld", "wobble", "jitter"],
		evaluate: ({ effectParams, progress }) => {
			const intensity = getMotionIntensity({ effectParams });
			const angle = progress * Math.PI * 2 * SHAKE_FREQUENCY;
			return {
				scale: 1 + SHAKE_SCALE_HEADROOM * intensity,
				translateX: Math.sin(angle) * SHAKE_TRAVEL_X * intensity,
				translateY:
					Math.cos(angle * SHAKE_Y_DETUNE) * SHAKE_TRAVEL_Y * intensity,
			};
		},
	}),
	buildMotionDefinition({
		type: "pulse",
		name: "Pulse",
		keywords: ["pulse", "beat", "pump", "scale", "bounce"],
		evaluate: ({ effectParams, progress }) => ({
			scale:
				1 +
				Math.sin(progress * Math.PI * 2 * PULSE_FREQUENCY) *
					PULSE_RANGE *
					getMotionIntensity({ effectParams }),
			translateX: 0,
			translateY: 0,
		}),
	}),
	buildMotionDefinition({
		type: "spin",
		name: "Spin",
		keywords: ["spin", "rotate", "turn", "twirl"],
		evaluate: ({ effectParams, progress }) => ({
			scale: 1,
			translateX: 0,
			translateY: 0,
			rotate:
				progress *
				SPIN_FULL_TURN_DEGREES *
				getMotionIntensity({ effectParams }),
		}),
	}),
];