summaryrefslogtreecommitdiff
path: root/apps/web/src/animation/graphic-param-channel.ts
blob: ebf0f940db4e82aa358eb4d1532af8df4dfd0d43 (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
import type {
	ElementAnimations,
	GraphicParamPath,
} from "@/animation/types";
import type { ParamValues } from "@/params";
import {
	getGraphicDefinition,
	resolveGraphicParams,
} from "@/graphics";
import { resolveAnimationPathValueAtTime } from "./resolve";

export const GRAPHIC_PARAM_PATH_PREFIX = "params.";

export function buildGraphicParamPath({
	paramKey,
}: {
	paramKey: string;
}): GraphicParamPath {
	return `${GRAPHIC_PARAM_PATH_PREFIX}${paramKey}`;
}

export function isGraphicParamPath(
	propertyPath: string,
): propertyPath is GraphicParamPath {
	return propertyPath.startsWith(GRAPHIC_PARAM_PATH_PREFIX);
}

export function parseGraphicParamPath({
	propertyPath,
}: {
	propertyPath: string;
}): { paramKey: string } | null {
	if (!isGraphicParamPath(propertyPath)) {
		return null;
	}

	const paramKey = propertyPath.slice(GRAPHIC_PARAM_PATH_PREFIX.length);
	return paramKey.length > 0 ? { paramKey } : null;
}

export function resolveGraphicParamsAtTime({
	element,
	localTime,
}: {
	element: {
		definitionId: string;
		params: ParamValues;
		animations?: ElementAnimations;
	};
	localTime: number;
}): ParamValues {
	const definition = getGraphicDefinition({
		definitionId: element.definitionId,
	});
	const baseParams = resolveGraphicParams(definition, element.params);
	const resolved: ParamValues = { ...baseParams };

	for (const param of definition.params) {
		const path = buildGraphicParamPath({ paramKey: param.key });
		if (!element.animations?.bindings[path]) {
			continue;
		}

		resolved[param.key] = resolveAnimationPathValueAtTime({
			animations: element.animations,
			propertyPath: path,
			localTime: Math.max(0, localTime),
			fallbackValue: baseParams[param.key] ?? param.default,
		});
	}

	return resolved;
}