summaryrefslogtreecommitdiff
path: root/apps/web/src/rendering/transition.ts
blob: 874975a112ed2f0a5e8094b7f4b6d6666d3f6923 (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
import type { ParamValues } from "@/params";
import { clamp } from "@/utils/math";
import { TICKS_PER_SECOND } from "@/wasm";
import type { Transform } from "./index";

export type TransitionKind =
	| "fade"
	| "push-left"
	| "push-right"
	| "zoom"
	| "zoom-out"
	| "spin";

export interface VisualTransition {
	kind: TransitionKind;
	inDuration: number;
	outDuration: number;
}

export interface TransitionState {
	opacityMultiplier: number;
	transform: Transform;
}

export const TRANSITION_KIND_OPTIONS: Array<{
	value: TransitionKind;
	label: string;
}> = [
	{ value: "fade", label: "Soft fade" },
	{ value: "push-left", label: "Slide left" },
	{ value: "push-right", label: "Slide right" },
	{ value: "zoom", label: "Zoom in" },
	{ value: "zoom-out", label: "Zoom out" },
	{ value: "spin", label: "Spin dissolve" },
];

export function isTransitionKind(value: string): value is TransitionKind {
	return TRANSITION_KIND_OPTIONS.some((option) => option.value === value);
}
export function withVisualTransitionParams({
	params,
	transitionKind,
	inDuration,
	outDuration,
}: {
	params: ParamValues;
	transitionKind: TransitionKind;
	inDuration: number;
	outDuration: number;
}): ParamValues {
	return {
		...params,
		"transition.kind": transitionKind,
		"transition.inDuration": inDuration,
		"transition.outDuration": outDuration,
	};
}

export function buildTransitionFromParams({
	params,
}: {
	params: ParamValues;
}): VisualTransition {
	const kindValue = params["transition.kind"];
	const inDurationValue = params["transition.inDuration"];
	const outDurationValue = params["transition.outDuration"];
	return {
		kind:
			typeof kindValue === "string" && isTransitionKind(kindValue)
				? kindValue
				: "fade",
		inDuration: readDuration({ value: inDurationValue }),
		outDuration: readDuration({ value: outDurationValue }),
	};
}

export function applyVisualTransition({
	baseTransform,
	transition,
	localTime,
	duration,
	canvasWidth,
}: {
	baseTransform: Transform;
	transition?: VisualTransition;
	localTime: number;
	duration: number;
	canvasWidth: number;
}): TransitionState {
	if (!transition || duration <= 0) {
		return { opacityMultiplier: 1, transform: baseTransform };
	}

	const inProgress = progressWithin({
		elapsed: localTime,
		duration: transition.inDuration,
	});
	const outProgress = progressWithin({
		elapsed: duration - localTime,
		duration: transition.outDuration,
	});
	const progress = Math.min(inProgress, outProgress);
	const isSlide =
		transition.kind === "push-left" || transition.kind === "push-right";
	const opacityMultiplier = isSlide ? 1 : ease({ progress });
	const transform = applyTransitionTransform({
		transform: baseTransform,
		transition,
		inProgress,
		outProgress,
		canvasWidth,
	});
	return { opacityMultiplier, transform };
}

function readDuration({ value }: { value: unknown }): number {
	const seconds =
		typeof value === "number" && Number.isFinite(value)
			? clamp({ value, min: 0, max: 5 })
			: 0;
	return seconds * TICKS_PER_SECOND;
}

function progressWithin({
	elapsed,
	duration,
}: {
	elapsed: number;
	duration: number;
}): number {
	if (duration <= 0) {
		return 1;
	}
	return clamp({ value: elapsed / duration, min: 0, max: 1 });
}

function ease({ progress }: { progress: number }): number {
	return progress * progress * (3 - 2 * progress);
}

function applyTransitionTransform({
	transform,
	transition,
	inProgress,
	outProgress,
	canvasWidth,
}: {
	transform: Transform;
	transition: VisualTransition;
	inProgress: number;
	outProgress: number;
	canvasWidth: number;
}): Transform {
	if (transition.kind === "fade") {
		return transform;
	}

	if (transition.kind === "zoom" || transition.kind === "zoom-out") {
		const progress = Math.min(inProgress, outProgress);
		const eased = ease({ progress });
		const scale =
			transition.kind === "zoom" ? 0.96 + eased * 0.04 : 1.04 - eased * 0.04;
		return {
			...transform,
			scaleX: transform.scaleX * scale,
			scaleY: transform.scaleY * scale,
		};
	}

	if (transition.kind === "spin") {
		const progress = Math.min(inProgress, outProgress);
		const eased = ease({ progress });
		const scale = 0.96 + eased * 0.04;
		return {
			...transform,
			scaleX: transform.scaleX * scale,
			scaleY: transform.scaleY * scale,
			rotate: transform.rotate - (1 - eased) * 6,
		};
	}

	const direction = transition.kind === "push-right" ? -1 : 1;
	const enteringOffset =
		(1 - ease({ progress: inProgress })) * canvasWidth * direction;
	const leavingOffset =
		(ease({ progress: outProgress }) - 1) * canvasWidth * direction;
	return {
		...transform,
		position: {
			x: transform.position.x + enteringOffset + leavingOffset,
			y: transform.position.y,
		},
	};
}