skia-motion-runtime 0.0.0-stage → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/NOTICE.md +62 -0
- package/package.json +65 -4
- package/runtime/asset-draw.ts +366 -0
- package/runtime/asset-image.ts +47 -0
- package/runtime/asset-layout.ts +49 -0
- package/runtime/asset-load.ts +70 -0
- package/runtime/asset-mesh.ts +37 -0
- package/runtime/asset.ts +647 -0
- package/runtime/base64.ts +19 -0
- package/runtime/expo-audio.ts +72 -0
- package/runtime/expo.ts +16 -0
- package/runtime/index.ts +7 -0
- package/runtime/machine-schema.d.ts +6 -0
- package/runtime/machine-schema.js +384 -0
- package/runtime/mobile.tsx +263 -0
- package/runtime/useMachine.ts +276 -0
- package/runtime/useMotionAsset.ts +107 -0
- package/runtime/version.ts +37 -0
- package/templates/motion/face.ts +118 -0
- package/templates/motion/machine.ts +649 -0
- package/templates/motion/motion.ts +215 -0
- package/README.md +0 -3
package/runtime/asset.ts
ADDED
|
@@ -0,0 +1,647 @@
|
|
|
1
|
+
import { validateMachine } from './machine-schema.js';
|
|
2
|
+
import { EASE_IN, EASE_IN_OUT, EASE_OUT } from '../templates/motion/motion';
|
|
3
|
+
import type { Clips, Machine, StateEntry } from '../templates/motion/machine';
|
|
4
|
+
import type { AssetAnchor } from './asset-layout';
|
|
5
|
+
import { imageSize } from './asset-image';
|
|
6
|
+
import type { MeshNode, MeshVertex } from './asset-mesh';
|
|
7
|
+
|
|
8
|
+
export const ASSET_SCHEMA = 'skac.asset/1';
|
|
9
|
+
export const ASSET_BUNDLE_SCHEMA = 'skac.bundle/1';
|
|
10
|
+
export const MAX_ASSET_BYTES = 1024 * 1024;
|
|
11
|
+
export const MAX_FEATHER = 64;
|
|
12
|
+
|
|
13
|
+
export type Scalar = number | string;
|
|
14
|
+
type Ease = 'linear' | 'in' | 'out' | 'inOut' | 'hold';
|
|
15
|
+
type Key = readonly [ms: number, value: number, ease?: Ease];
|
|
16
|
+
type Track = readonly Key[];
|
|
17
|
+
export type PathCommand =
|
|
18
|
+
| readonly [op: 'M' | 'L', x: Scalar, y: Scalar]
|
|
19
|
+
| readonly [op: 'Q', controlX: Scalar, controlY: Scalar, x: Scalar, y: Scalar]
|
|
20
|
+
| readonly [op: 'C', control1X: Scalar, control1Y: Scalar, control2X: Scalar, control2Y: Scalar, x: Scalar, y: Scalar]
|
|
21
|
+
| readonly [op: 'Z'];
|
|
22
|
+
export type AssetColor = string | { palette: string };
|
|
23
|
+
type Shade = AssetColor | { kind: 'radial'; x: Scalar; y: Scalar; radius: Scalar; colors: readonly [string, string] };
|
|
24
|
+
export type AssetStroke = {
|
|
25
|
+
color: AssetColor; width: Scalar;
|
|
26
|
+
cap?: 'butt' | 'round' | 'square'; join?: 'miter' | 'round' | 'bevel';
|
|
27
|
+
trim?: { start?: Scalar; end?: Scalar };
|
|
28
|
+
dash?: { intervals: readonly number[]; phase?: Scalar };
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
type Group = {
|
|
32
|
+
kind: 'group'; x?: Scalar; y?: Scalar; scaleX?: Scalar; scaleY?: Scalar; rotation?: Scalar;
|
|
33
|
+
anchor?: AssetAnchor;
|
|
34
|
+
clip?: readonly PathCommand[];
|
|
35
|
+
points?: Readonly<Record<string, { x: Scalar; y: Scalar }>>;
|
|
36
|
+
children: readonly AssetNode[];
|
|
37
|
+
};
|
|
38
|
+
type Shape = { opacity?: Scalar; feather?: Scalar } & (
|
|
39
|
+
| { kind: 'ellipse'; x: Scalar; y: Scalar; width: Scalar; height: Scalar; fill: Shade }
|
|
40
|
+
| { kind: 'roundRect'; x: Scalar; y: Scalar; width: Scalar; height: Scalar; radius: Scalar; fill: Shade }
|
|
41
|
+
| { kind: 'path'; commands: readonly PathCommand[]; fill?: Shade; stroke?: AssetStroke }
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
type ImageNode = {
|
|
45
|
+
kind: 'image'; image: string; opacity?: Scalar;
|
|
46
|
+
x: Scalar; y: Scalar; width: Scalar; height: Scalar;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export type BoundText = string | { value: string };
|
|
50
|
+
export type AssetTextNode = {
|
|
51
|
+
kind: 'text'; text: BoundText; x: Scalar; y: Scalar; width: number; fontSize: number; fill: string;
|
|
52
|
+
fonts?: readonly string[]; direction?: 'ltr' | 'rtl'; align?: 'start' | 'center' | 'end'; maxLines?: number;
|
|
53
|
+
};
|
|
54
|
+
export type AssetSemantic = {
|
|
55
|
+
name: string; label: BoundText; role: 'button' | 'image' | 'text' | 'header';
|
|
56
|
+
bounds: readonly [x: number, y: number, width: number, height: number];
|
|
57
|
+
trigger?: string;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export type AssetNode = Group | Shape | ImageNode | MeshNode | AssetTextNode;
|
|
61
|
+
export type AssetMarker = { name: string; layer: string; state: string; audio?: string };
|
|
62
|
+
export type AssetMarkerEvent = AssetMarker & { at: number };
|
|
63
|
+
export type AssetAttachment = { from: string; to: string; maxDistance: number };
|
|
64
|
+
export type MotionAsset<M extends Machine = Machine> = {
|
|
65
|
+
schema: typeof ASSET_SCHEMA;
|
|
66
|
+
name: string;
|
|
67
|
+
artboard: { width: number; height: number; nodes: readonly AssetNode[] };
|
|
68
|
+
channels: Readonly<Record<string, number>>;
|
|
69
|
+
images?: Readonly<Record<string, string>>;
|
|
70
|
+
imageSlots?: Readonly<Record<string, string>>;
|
|
71
|
+
palette?: Readonly<Record<string, string>>;
|
|
72
|
+
fonts?: Readonly<Record<string, string>>;
|
|
73
|
+
fontLicenses?: Readonly<Record<string, string>>;
|
|
74
|
+
audio?: Readonly<Record<string, string>>;
|
|
75
|
+
semantics?: readonly AssetSemantic[];
|
|
76
|
+
clips: Readonly<Record<string, { ms: number; reducedAt?: number; tracks: Readonly<Record<string, Track>> }>>;
|
|
77
|
+
machine: M;
|
|
78
|
+
markers?: readonly AssetMarker[];
|
|
79
|
+
attachments?: readonly AssetAttachment[];
|
|
80
|
+
};
|
|
81
|
+
export type MotionAssetFile = MotionAsset | {
|
|
82
|
+
schema: typeof ASSET_BUNDLE_SCHEMA;
|
|
83
|
+
name: string;
|
|
84
|
+
defaultArtboard: string;
|
|
85
|
+
artboards: Readonly<Record<string, MotionAsset>>;
|
|
86
|
+
fonts?: MotionAsset['fonts'];
|
|
87
|
+
fontLicenses?: MotionAsset['fontLicenses'];
|
|
88
|
+
audio?: MotionAsset['audio'];
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const IDENTIFIER = /^[A-Za-z_][A-Za-z0-9_-]{0,63}$/;
|
|
92
|
+
const HEX_COLOR = /^#[0-9a-fA-F]{6}([0-9a-fA-F]{2})?$/;
|
|
93
|
+
const FONT_URI = /^data:font\/(ttf|otf);base64,([A-Za-z0-9+/]+={0,2})$/;
|
|
94
|
+
const AUDIO_URI = /^data:audio\/(mpeg|wav|mp4|aac);base64,([A-Za-z0-9+/]+={0,2})$/;
|
|
95
|
+
const PATH_ARGUMENTS: Record<string, number> = { M: 2, L: 2, Q: 4, C: 6, Z: 0 };
|
|
96
|
+
const EASES = ['linear', 'in', 'out', 'inOut', 'hold'];
|
|
97
|
+
const RESERVED_KEYS = new Set([...Object.getOwnPropertyNames(Object.prototype), 'prototype']);
|
|
98
|
+
|
|
99
|
+
function invalid(at: string, message: string): never {
|
|
100
|
+
throw new Error(`${at}: ${message}`);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function record(value: unknown, at: string, keys?: readonly string[]): Record<string, unknown> {
|
|
104
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) invalid(at, 'expected an object');
|
|
105
|
+
const data = value as Record<string, unknown>;
|
|
106
|
+
if (keys) for (const key of Object.keys(data)) if (!keys.includes(key)) invalid(`${at}.${key}`, 'unknown field');
|
|
107
|
+
return data;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function list(value: unknown, at: string, limit: number): unknown[] {
|
|
111
|
+
if (!Array.isArray(value) || value.length > limit) invalid(at, `expected an array of up to ${limit} items`);
|
|
112
|
+
return value;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function number(value: unknown, at: string, min = -1e7, max = 1e7): number {
|
|
116
|
+
if (typeof value !== 'number' || !Number.isFinite(value) || value < min || value > max) {
|
|
117
|
+
invalid(at, `expected a number from ${min} to ${max}`);
|
|
118
|
+
}
|
|
119
|
+
return value;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function identifier(value: unknown, at: string): string {
|
|
123
|
+
if (typeof value !== 'string' || !IDENTIFIER.test(value) || RESERVED_KEYS.has(value)) invalid(at, 'expected an identifier');
|
|
124
|
+
return value;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function scalar(value: unknown, at: string, channels: Record<string, unknown>, min?: number, max?: number): void {
|
|
128
|
+
if (typeof value === 'string' && Object.hasOwn(channels, value)) return;
|
|
129
|
+
if (typeof value === 'string') invalid(at, `unknown channel "${value}"`);
|
|
130
|
+
number(value, at, min, max);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function color(value: unknown, at: string): void {
|
|
134
|
+
if (typeof value !== 'string' || !HEX_COLOR.test(value)) invalid(at, 'expected #RRGGBB or #RRGGBBAA');
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function checkPath(value: unknown, at: string, channels: Record<string, unknown>): void {
|
|
138
|
+
const commands = list(value, at, 1024);
|
|
139
|
+
if (!commands.length) invalid(at, 'a path must start with M');
|
|
140
|
+
commands.forEach((value, i) => {
|
|
141
|
+
const key = `${at}[${i}]`;
|
|
142
|
+
const [op, ...args] = list(value, key, 7);
|
|
143
|
+
if (typeof op !== 'string' || !Object.hasOwn(PATH_ARGUMENTS, op) || args.length !== PATH_ARGUMENTS[op] || (!i && op !== 'M')) {
|
|
144
|
+
invalid(key, 'expected M, L, Q, C or Z with the correct coordinates, starting with M');
|
|
145
|
+
}
|
|
146
|
+
args.forEach((arg, j) => scalar(arg, `${key}[${j + 1}]`, channels));
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
type NodeValidation = {
|
|
151
|
+
channels: Record<string, unknown>;
|
|
152
|
+
images: Record<string, unknown>;
|
|
153
|
+
imageSlots: Record<string, unknown>;
|
|
154
|
+
points: Set<string>;
|
|
155
|
+
fonts: Record<string, unknown>;
|
|
156
|
+
values: Record<string, unknown>;
|
|
157
|
+
palette: Record<string, unknown>;
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
function checkColor(value: unknown, at: string, palette: Record<string, unknown>): void {
|
|
161
|
+
if (typeof value === 'string') return color(value, at);
|
|
162
|
+
const name = identifier(record(value, at, ['palette']).palette, `${at}.palette`);
|
|
163
|
+
if (!Object.hasOwn(palette, name)) invalid(at, `unknown palette entry "${name}"`);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function checkStroke(value: unknown, at: string, { channels, palette }: NodeValidation): void {
|
|
167
|
+
const stroke = record(value, at, ['color', 'width', 'cap', 'join', 'trim', 'dash']);
|
|
168
|
+
checkColor(stroke.color, `${at}.color`, palette);
|
|
169
|
+
scalar(stroke.width, `${at}.width`, channels, 0, 4096);
|
|
170
|
+
if (stroke.cap !== undefined && !['butt', 'round', 'square'].includes(stroke.cap as string)) invalid(`${at}.cap`, 'expected butt, round or square');
|
|
171
|
+
if (stroke.join !== undefined && !['miter', 'round', 'bevel'].includes(stroke.join as string)) invalid(`${at}.join`, 'expected miter, round or bevel');
|
|
172
|
+
if (stroke.trim !== undefined) {
|
|
173
|
+
const trim = record(stroke.trim, `${at}.trim`, ['start', 'end']);
|
|
174
|
+
for (const key of ['start', 'end']) if (trim[key] !== undefined) scalar(trim[key], `${at}.trim.${key}`, channels, 0, 1);
|
|
175
|
+
}
|
|
176
|
+
if (stroke.dash !== undefined) {
|
|
177
|
+
const dash = record(stroke.dash, `${at}.dash`, ['intervals', 'phase']);
|
|
178
|
+
const intervals = list(dash.intervals, `${at}.dash.intervals`, 32);
|
|
179
|
+
if (intervals.length < 2 || intervals.length % 2) invalid(`${at}.dash.intervals`, 'expected an even number of intervals');
|
|
180
|
+
intervals.forEach((interval, i) => number(interval, `${at}.dash.intervals[${i}]`, 0.001, 4096));
|
|
181
|
+
if (dash.phase !== undefined) scalar(dash.phase, `${at}.dash.phase`, channels);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function checkText(value: unknown, at: string, values: Record<string, unknown>): void {
|
|
186
|
+
if (typeof value === 'string') {
|
|
187
|
+
if (value.length > 4096) invalid(at, 'expected at most 4096 characters');
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
const input = identifier(record(value, at, ['value']).value, `${at}.value`);
|
|
191
|
+
if ((values[input] as { type?: string } | undefined)?.type !== 'string') {
|
|
192
|
+
invalid(`${at}.value`, 'expected a string value from machine.values');
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function resourceMap(value: unknown, at: string, pattern: RegExp) {
|
|
197
|
+
if (value === undefined) return {};
|
|
198
|
+
const resources = record(value, at);
|
|
199
|
+
if (Object.keys(resources).length > 32) invalid(at, 'expected at most 32 resources');
|
|
200
|
+
for (const [name, uri] of Object.entries(resources)) {
|
|
201
|
+
const key = `${at}.${name}`;
|
|
202
|
+
identifier(name, key);
|
|
203
|
+
const match = typeof uri === 'string' ? pattern.exec(uri) : null;
|
|
204
|
+
if (!match || match[0] !== uri || match[2]!.length % 4 !== 0) invalid(key, 'expected an embedded base64 data URI');
|
|
205
|
+
}
|
|
206
|
+
return resources;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function checkFontLicenses(value: unknown, fonts: Record<string, unknown>, at: string): void {
|
|
210
|
+
if (value === undefined) return;
|
|
211
|
+
for (const [name, license] of Object.entries(record(value, at))) {
|
|
212
|
+
if (!Object.hasOwn(fonts, name) || typeof license !== 'string' || license.length > 16_384) {
|
|
213
|
+
invalid(`${at}.${name}`, 'expected a license of up to 16384 characters for a declared font');
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function registerPoint(name: string, at: string, points: Set<string>): void {
|
|
219
|
+
identifier(name, at);
|
|
220
|
+
if (points.has(name)) invalid(at, 'duplicate point name');
|
|
221
|
+
points.add(name);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function checkChildren(value: unknown, at: string, context: NodeValidation, depth: number): void {
|
|
225
|
+
if (depth >= 16) invalid(at, 'nodes are nested more than 16 levels');
|
|
226
|
+
list(value, at, 256).forEach((child, i) => checkNode(child, `${at}[${i}]`, context, depth + 1));
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function checkMesh(node: Record<string, unknown>, at: string, context: NodeValidation, depth: number): void {
|
|
230
|
+
record(node, at, ['kind', 'image', 'vertices', 'indices', 'opacity', 'pins']);
|
|
231
|
+
const vertices = list(node.vertices, `${at}.vertices`, 1024).map((value, i) => {
|
|
232
|
+
const key = `${at}.vertices[${i}]`;
|
|
233
|
+
const vertex = list(value, key, 4);
|
|
234
|
+
if (vertex.length !== 4) invalid(key, 'expected [x, y, u, v]');
|
|
235
|
+
scalar(vertex[0], `${key}[0]`, context.channels);
|
|
236
|
+
scalar(vertex[1], `${key}[1]`, context.channels);
|
|
237
|
+
number(vertex[2], `${key}[2]`, 0, 1);
|
|
238
|
+
number(vertex[3], `${key}[3]`, 0, 1);
|
|
239
|
+
return vertex as unknown as MeshVertex;
|
|
240
|
+
});
|
|
241
|
+
if (vertices.length < 3) invalid(`${at}.vertices`, 'expected at least three vertices');
|
|
242
|
+
const indices = list(node.indices, `${at}.indices`, 6144).map((value, i) => {
|
|
243
|
+
const key = `${at}.indices[${i}]`;
|
|
244
|
+
const index = number(value, key, 0, vertices.length - 1);
|
|
245
|
+
if (!Number.isInteger(index)) invalid(key, 'expected an integer vertex index');
|
|
246
|
+
return index;
|
|
247
|
+
});
|
|
248
|
+
if (!indices.length || indices.length % 3 !== 0) invalid(`${at}.indices`, 'expected index triples');
|
|
249
|
+
for (let i = 0; i < indices.length; i += 3) {
|
|
250
|
+
const [, , au, av] = vertices[indices[i]!]!;
|
|
251
|
+
const [, , bu, bv] = vertices[indices[i + 1]!]!;
|
|
252
|
+
const [, , cu, cv] = vertices[indices[i + 2]!]!;
|
|
253
|
+
const area = (bu - au) * (cv - av) - (bv - av) * (cu - au);
|
|
254
|
+
if (Math.abs(area) < 1e-9) invalid(`${at}.indices[${i}]`, 'triangle needs three non-collinear texture coordinates');
|
|
255
|
+
}
|
|
256
|
+
if (node.pins === undefined) return;
|
|
257
|
+
for (const [name, value] of Object.entries(record(node.pins, `${at}.pins`))) {
|
|
258
|
+
const key = `${at}.pins.${name}`;
|
|
259
|
+
registerPoint(name, key, context.points);
|
|
260
|
+
const pin = record(value, key, ['triangle', 'weights', 'children']);
|
|
261
|
+
const triangle = number(pin.triangle, `${key}.triangle`, 0, indices.length / 3 - 1);
|
|
262
|
+
if (!Number.isInteger(triangle)) invalid(`${key}.triangle`, 'expected an integer triangle index');
|
|
263
|
+
const weights = list(pin.weights, `${key}.weights`, 3);
|
|
264
|
+
if (weights.length !== 3) invalid(`${key}.weights`, 'expected one weight per vertex');
|
|
265
|
+
const sum = weights.reduce<number>((total, value, i) => total + number(value, `${key}.weights[${i}]`, 0, 1), 0);
|
|
266
|
+
if (Math.abs(sum - 1) > 1e-6) invalid(`${key}.weights`, 'weights must add up to 1');
|
|
267
|
+
if (pin.children !== undefined) checkChildren(pin.children, `${key}.children`, context, depth);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function checkNode(value: unknown, at: string, context: NodeValidation, depth: number): void {
|
|
272
|
+
const { channels, images, imageSlots, points } = context;
|
|
273
|
+
const node = record(value, at);
|
|
274
|
+
if (node.kind === 'text') {
|
|
275
|
+
record(node, at, ['kind', 'text', 'x', 'y', 'width', 'fontSize', 'fill', 'fonts', 'direction', 'align', 'maxLines']);
|
|
276
|
+
checkText(node.text, `${at}.text`, context.values);
|
|
277
|
+
scalar(node.x, `${at}.x`, channels);
|
|
278
|
+
scalar(node.y, `${at}.y`, channels);
|
|
279
|
+
number(node.width, `${at}.width`, 1, 4096);
|
|
280
|
+
number(node.fontSize, `${at}.fontSize`, 1, 512);
|
|
281
|
+
color(node.fill, `${at}.fill`);
|
|
282
|
+
if (node.fonts !== undefined) list(node.fonts, `${at}.fonts`, 8).forEach((name, i) => {
|
|
283
|
+
if (typeof name !== 'string' || !Object.hasOwn(context.fonts, name)) invalid(`${at}.fonts[${i}]`, 'unknown embedded font');
|
|
284
|
+
});
|
|
285
|
+
if (node.direction !== undefined && node.direction !== 'ltr' && node.direction !== 'rtl') invalid(`${at}.direction`, 'expected ltr or rtl');
|
|
286
|
+
if (node.align !== undefined && !['start', 'center', 'end'].includes(node.align as string)) invalid(`${at}.align`, 'expected start, center or end');
|
|
287
|
+
if (node.maxLines !== undefined && !Number.isInteger(number(node.maxLines, `${at}.maxLines`, 1, 16))) {
|
|
288
|
+
invalid(`${at}.maxLines`, 'expected an integer');
|
|
289
|
+
}
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
if (node.kind === 'group') {
|
|
293
|
+
record(node, at, ['kind', 'children', 'x', 'y', 'scaleX', 'scaleY', 'rotation', 'anchor', 'clip', 'points']);
|
|
294
|
+
for (const key of ['x', 'y', 'scaleX', 'scaleY', 'rotation']) {
|
|
295
|
+
if (node[key] !== undefined) scalar(node[key], `${at}.${key}`, channels);
|
|
296
|
+
}
|
|
297
|
+
if (node.points !== undefined) {
|
|
298
|
+
for (const [name, rawPoint] of Object.entries(record(node.points, `${at}.points`))) {
|
|
299
|
+
const key = `${at}.points.${name}`;
|
|
300
|
+
registerPoint(name, key, points);
|
|
301
|
+
const point = record(rawPoint, key, ['x', 'y']);
|
|
302
|
+
scalar(point.x, `${key}.x`, channels);
|
|
303
|
+
scalar(point.y, `${key}.y`, channels);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
if (node.clip !== undefined) checkPath(node.clip, `${at}.clip`, channels);
|
|
307
|
+
if (node.anchor !== undefined) {
|
|
308
|
+
if (depth > 0) invalid(`${at}.anchor`, 'anchors belong to top-level groups');
|
|
309
|
+
const anchor = record(node.anchor, `${at}.anchor`, ['x', 'y']);
|
|
310
|
+
if (anchor.x === undefined && anchor.y === undefined) invalid(`${at}.anchor`, 'give an x or y anchor');
|
|
311
|
+
if (anchor.x !== undefined && !['left', 'center', 'right'].some((value) => value === anchor.x)) {
|
|
312
|
+
invalid(`${at}.anchor.x`, 'expected left, center or right');
|
|
313
|
+
}
|
|
314
|
+
if (anchor.y !== undefined && !['top', 'center', 'bottom'].some((value) => value === anchor.y)) {
|
|
315
|
+
invalid(`${at}.anchor.y`, 'expected top, center or bottom');
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
checkChildren(node.children, `${at}.children`, context, depth);
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
if (node.kind === 'image' || node.kind === 'mesh') {
|
|
322
|
+
if (typeof node.image !== 'string' || (!Object.hasOwn(images, node.image) && !Object.hasOwn(imageSlots, node.image))) {
|
|
323
|
+
invalid(`${at}.image`, 'expected a name from images or imageSlots');
|
|
324
|
+
}
|
|
325
|
+
if (node.opacity !== undefined) scalar(node.opacity, `${at}.opacity`, channels, 0, 1);
|
|
326
|
+
if (node.kind === 'mesh') {
|
|
327
|
+
checkMesh(node, at, context, depth);
|
|
328
|
+
} else {
|
|
329
|
+
record(node, at, ['kind', 'image', 'x', 'y', 'width', 'height', 'opacity']);
|
|
330
|
+
for (const key of ['x', 'y']) scalar(node[key], `${at}.${key}`, channels);
|
|
331
|
+
for (const key of ['width', 'height']) scalar(node[key], `${at}.${key}`, channels, 0);
|
|
332
|
+
}
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
if (node.kind === 'path') {
|
|
336
|
+
record(node, at, ['kind', 'fill', 'opacity', 'feather', 'commands', 'stroke']);
|
|
337
|
+
checkPath(node.commands, `${at}.commands`, channels);
|
|
338
|
+
if (node.stroke !== undefined) checkStroke(node.stroke, `${at}.stroke`, context);
|
|
339
|
+
} else if (node.kind === 'ellipse' || node.kind === 'roundRect') {
|
|
340
|
+
const keys = ['kind', 'fill', 'opacity', 'feather', 'x', 'y', 'width', 'height'];
|
|
341
|
+
if (node.kind === 'roundRect') keys.push('radius');
|
|
342
|
+
record(node, at, keys);
|
|
343
|
+
for (const key of ['x', 'y', 'width', 'height']) scalar(node[key], `${at}.${key}`, channels, key === 'x' || key === 'y' ? undefined : 0);
|
|
344
|
+
if (node.kind === 'roundRect') scalar(node.radius, `${at}.radius`, channels, 0);
|
|
345
|
+
} else {
|
|
346
|
+
invalid(`${at}.kind`, 'expected ellipse, roundRect, path, image, mesh, text or group');
|
|
347
|
+
}
|
|
348
|
+
if (node.opacity !== undefined) scalar(node.opacity, `${at}.opacity`, channels, 0, 1);
|
|
349
|
+
if (node.feather !== undefined) scalar(node.feather, `${at}.feather`, channels, 0, MAX_FEATHER);
|
|
350
|
+
if (node.kind === 'path' && node.fill === undefined && node.stroke !== undefined) return;
|
|
351
|
+
if (typeof node.fill === 'string') {
|
|
352
|
+
color(node.fill, `${at}.fill`);
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
if (node.fill && typeof node.fill === 'object' && Object.hasOwn(node.fill, 'palette')) {
|
|
356
|
+
checkColor(node.fill, `${at}.fill`, context.palette);
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
const fill = record(node.fill, `${at}.fill`, ['kind', 'x', 'y', 'radius', 'colors']);
|
|
360
|
+
if (fill.kind !== 'radial') invalid(`${at}.fill.kind`, 'expected radial');
|
|
361
|
+
scalar(fill.x, `${at}.fill.x`, channels);
|
|
362
|
+
scalar(fill.y, `${at}.fill.y`, channels);
|
|
363
|
+
scalar(fill.radius, `${at}.fill.radius`, channels, 0.001);
|
|
364
|
+
const colors = list(fill.colors, `${at}.fill.colors`, 2);
|
|
365
|
+
if (colors.length !== 2) invalid(`${at}.fill.colors`, 'expected two colors');
|
|
366
|
+
colors.forEach((item, i) => color(item, `${at}.fill.colors[${i}]`));
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
function validateAsset(value: unknown): MotionAsset {
|
|
370
|
+
const data = record(value, 'asset', [
|
|
371
|
+
'schema', 'name', 'artboard', 'channels', 'clips', 'machine', 'markers', 'images', 'imageSlots', 'palette', 'attachments', 'fonts', 'fontLicenses', 'audio', 'semantics',
|
|
372
|
+
]);
|
|
373
|
+
if (data.schema !== ASSET_SCHEMA) invalid('asset.schema', `this player reads ${ASSET_SCHEMA}`);
|
|
374
|
+
identifier(data.name, 'asset.name');
|
|
375
|
+
const machine = record(data.machine, 'machine');
|
|
376
|
+
const values = machine.values === undefined ? {} : record(machine.values, 'machine.values');
|
|
377
|
+
const fonts = resourceMap(data.fonts, 'fonts', FONT_URI);
|
|
378
|
+
checkFontLicenses(data.fontLicenses, fonts, 'fontLicenses');
|
|
379
|
+
const audio = resourceMap(data.audio, 'audio', AUDIO_URI);
|
|
380
|
+
const palette = data.palette === undefined ? {} : record(data.palette, 'palette');
|
|
381
|
+
for (const [name, value] of Object.entries(palette)) {
|
|
382
|
+
identifier(name, `palette.${name}`);
|
|
383
|
+
color(value, `palette.${name}`);
|
|
384
|
+
}
|
|
385
|
+
const channels = record(data.channels, 'channels');
|
|
386
|
+
for (const [name, value] of Object.entries(channels)) {
|
|
387
|
+
identifier(name, `channels.${name}`);
|
|
388
|
+
number(value, `channels.${name}`);
|
|
389
|
+
}
|
|
390
|
+
const images = data.images === undefined ? {} : record(data.images, 'images');
|
|
391
|
+
const imageSlots = data.imageSlots === undefined ? {} : record(data.imageSlots, 'imageSlots');
|
|
392
|
+
for (const [name, fallback] of Object.entries(imageSlots)) {
|
|
393
|
+
const at = `imageSlots.${name}`;
|
|
394
|
+
identifier(name, at);
|
|
395
|
+
if (Object.hasOwn(images, name)) invalid(at, 'the slot name must be distinct from its embedded fallback');
|
|
396
|
+
if (typeof fallback !== 'string' || !Object.hasOwn(images, fallback)) invalid(at, 'expected an embedded fallback from images');
|
|
397
|
+
}
|
|
398
|
+
let pixels = 0;
|
|
399
|
+
for (const [name, uri] of Object.entries(images)) {
|
|
400
|
+
const at = `images.${name}`;
|
|
401
|
+
identifier(name, at);
|
|
402
|
+
const size = imageSize(uri);
|
|
403
|
+
if (!size || size.width < 1 || size.height < 1 || size.width > 4096 || size.height > 4096) {
|
|
404
|
+
invalid(at, 'expected an embedded PNG or WebP data URI with dimensions from 1 to 4096 pixels');
|
|
405
|
+
}
|
|
406
|
+
pixels += size.width * size.height;
|
|
407
|
+
if (pixels > 16 * 1024 * 1024) invalid('images', 'embedded images exceed 16 Mi pixels in total');
|
|
408
|
+
}
|
|
409
|
+
const artboard = record(data.artboard, 'artboard', ['width', 'height', 'nodes']);
|
|
410
|
+
number(artboard.width, 'artboard.width', 1, 4096);
|
|
411
|
+
number(artboard.height, 'artboard.height', 1, 4096);
|
|
412
|
+
const nodes = list(artboard.nodes, 'artboard.nodes', 256);
|
|
413
|
+
if (!nodes.length) invalid('artboard.nodes', 'add at least one shape');
|
|
414
|
+
const context = { channels, images, imageSlots, fonts, values, palette, points: new Set<string>() };
|
|
415
|
+
nodes.forEach((node, i) => checkNode(node, `artboard.nodes[${i}]`, context, 0));
|
|
416
|
+
if (data.semantics !== undefined) {
|
|
417
|
+
const names = new Set<string>();
|
|
418
|
+
list(data.semantics, 'semantics', 64).forEach((item, i) => {
|
|
419
|
+
const at = `semantics[${i}]`;
|
|
420
|
+
const entry = record(item, at, ['name', 'role', 'label', 'bounds', 'trigger']);
|
|
421
|
+
const name = identifier(entry.name, `${at}.name`);
|
|
422
|
+
if (names.has(name)) invalid(`${at}.name`, 'duplicate semantic name');
|
|
423
|
+
names.add(name);
|
|
424
|
+
if (!['button', 'image', 'text', 'header'].includes(entry.role as string)) invalid(`${at}.role`, 'unknown role');
|
|
425
|
+
checkText(entry.label, `${at}.label`, values);
|
|
426
|
+
const bounds = list(entry.bounds, `${at}.bounds`, 4);
|
|
427
|
+
if (bounds.length !== 4) invalid(`${at}.bounds`, 'expected x, y, width and height');
|
|
428
|
+
bounds.forEach((side, index) => number(side, `${at}.bounds[${index}]`, index < 2 ? -4096 : 1, 4096));
|
|
429
|
+
if (entry.trigger !== undefined) {
|
|
430
|
+
const trigger = identifier(entry.trigger, `${at}.trigger`);
|
|
431
|
+
const property = values[trigger] as { type?: string; bind?: string } | undefined;
|
|
432
|
+
if (entry.role !== 'button' || property?.type !== 'trigger' || property.bind === 'out') {
|
|
433
|
+
invalid(`${at}.trigger`, 'expected a writable trigger on a button');
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
});
|
|
437
|
+
}
|
|
438
|
+
if (data.attachments !== undefined) {
|
|
439
|
+
list(data.attachments, 'attachments', 256).forEach((item, i) => {
|
|
440
|
+
const at = `attachments[${i}]`;
|
|
441
|
+
const attachment = record(item, at, ['from', 'to', 'maxDistance']);
|
|
442
|
+
for (const key of ['from', 'to']) {
|
|
443
|
+
const point = identifier(attachment[key], `${at}.${key}`);
|
|
444
|
+
if (!context.points.has(point)) invalid(`${at}.${key}`, `unknown point "${point}"`);
|
|
445
|
+
}
|
|
446
|
+
number(attachment.maxDistance, `${at}.maxDistance`, 0);
|
|
447
|
+
});
|
|
448
|
+
}
|
|
449
|
+
const clips = record(data.clips, 'clips');
|
|
450
|
+
for (const [name, value] of Object.entries(clips)) {
|
|
451
|
+
const at = `clips.${name}`;
|
|
452
|
+
identifier(name, at);
|
|
453
|
+
const clip = record(value, at, ['ms', 'reducedAt', 'tracks']);
|
|
454
|
+
const ms = number(clip.ms, `${at}.ms`, 0, 60000);
|
|
455
|
+
if (clip.reducedAt !== undefined) number(clip.reducedAt, `${at}.reducedAt`, 0, ms);
|
|
456
|
+
for (const [channel, value] of Object.entries(record(clip.tracks, `${at}.tracks`))) {
|
|
457
|
+
const key = `${at}.tracks.${channel}`;
|
|
458
|
+
if (!Object.hasOwn(channels, channel)) invalid(key, 'unknown channel');
|
|
459
|
+
const track = list(value, key, 1024);
|
|
460
|
+
if (!track.length) invalid(key, 'a track needs at least one keyframe');
|
|
461
|
+
let previous = -1;
|
|
462
|
+
track.forEach((value, i) => {
|
|
463
|
+
const at = `${key}[${i}]`;
|
|
464
|
+
const frame = list(value, at, 3);
|
|
465
|
+
if (frame.length < 2) invalid(at, 'expected [ms, value, ease?]');
|
|
466
|
+
const time = number(frame[0], `${at}[0]`, 0, ms);
|
|
467
|
+
if (time <= previous) invalid(at, 'keyframe times must be strictly increasing');
|
|
468
|
+
previous = time;
|
|
469
|
+
number(frame[1], `${at}[1]`);
|
|
470
|
+
if (frame.length === 3 && (typeof frame[2] !== 'string' || !EASES.includes(frame[2]))) {
|
|
471
|
+
invalid(`${at}[2]`, 'expected linear, in, out, inOut or hold');
|
|
472
|
+
}
|
|
473
|
+
});
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
if (machine.hits !== undefined) invalid('machine.hits', 'packed scenes use host input and semantic regions');
|
|
477
|
+
list(machine.layers, 'machine.layers', 32).forEach((value, i) => {
|
|
478
|
+
const at = `machine.layers[${i}]`;
|
|
479
|
+
const layer = record(value, at);
|
|
480
|
+
if (Object.keys(record(layer.states, `${at}.states`)).length > 128) invalid(`${at}.states`, 'maximum of 128 states per layer');
|
|
481
|
+
if (layer.transitions !== undefined) list(layer.transitions, `${at}.transitions`, 512);
|
|
482
|
+
});
|
|
483
|
+
const checked = validateMachine(machine, { clips: clips as MotionAsset['clips'] });
|
|
484
|
+
if (checked.problems.length) {
|
|
485
|
+
const { where, error, hint } = checked.problems[0]!;
|
|
486
|
+
invalid(where ? `machine.${where}` : 'machine', `${error}. ${hint}`);
|
|
487
|
+
}
|
|
488
|
+
const typed = data as MotionAsset;
|
|
489
|
+
const names = new Set();
|
|
490
|
+
if (data.markers !== undefined) {
|
|
491
|
+
list(data.markers, 'markers', 256).forEach((item, i) => {
|
|
492
|
+
const at = `markers[${i}]`;
|
|
493
|
+
const marker = record(item, at, ['name', 'layer', 'state', 'audio']);
|
|
494
|
+
const name = identifier(marker.name, `${at}.name`);
|
|
495
|
+
if (names.has(name)) invalid(`${at}.name`, 'duplicate marker name');
|
|
496
|
+
names.add(name);
|
|
497
|
+
const layer = typed.machine.layers.find((layer) => layer.name === marker.layer);
|
|
498
|
+
const state = identifier(marker.state, `${at}.state`);
|
|
499
|
+
if (!layer || !Object.hasOwn(layer.states, state)) invalid(at, 'unknown layer or state');
|
|
500
|
+
if (marker.audio !== undefined && (typeof marker.audio !== 'string' || !Object.hasOwn(audio, marker.audio))) {
|
|
501
|
+
invalid(`${at}.audio`, 'unknown embedded audio cue');
|
|
502
|
+
}
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
return typed;
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
/** Decode the same data file on a React Native JS thread and in the CLI. */
|
|
509
|
+
export function parseAssetFile(text: string): MotionAssetFile {
|
|
510
|
+
if (typeof text !== 'string' || text.length > MAX_ASSET_BYTES) invalid('asset', 'expected a JSON asset of at most 1 MiB');
|
|
511
|
+
const bytes = encodeURI(text).replace(/%[0-9A-F]{2}/g, 'x').length;
|
|
512
|
+
if (bytes > MAX_ASSET_BYTES) invalid('asset', 'expected a JSON asset of at most 1 MiB');
|
|
513
|
+
const decoded: unknown = JSON.parse(text, (key, value: unknown) => {
|
|
514
|
+
if (RESERVED_KEYS.has(key)) invalid(key, 'reserved field name');
|
|
515
|
+
if (typeof value === 'number' && !Number.isFinite(value)) invalid(key, 'expected a finite number');
|
|
516
|
+
return value;
|
|
517
|
+
});
|
|
518
|
+
const file = record(decoded, 'asset');
|
|
519
|
+
if (file.schema !== ASSET_BUNDLE_SCHEMA) return validateAsset(file);
|
|
520
|
+
record(file, 'bundle', ['schema', 'name', 'defaultArtboard', 'artboards', 'fonts', 'fontLicenses', 'audio']);
|
|
521
|
+
const name = identifier(file.name, 'bundle.name');
|
|
522
|
+
const defaultArtboard = identifier(file.defaultArtboard, 'bundle.defaultArtboard');
|
|
523
|
+
const artboards = record(file.artboards, 'bundle.artboards');
|
|
524
|
+
const fonts = resourceMap(file.fonts, 'bundle.fonts', FONT_URI) as NonNullable<MotionAsset['fonts']>;
|
|
525
|
+
checkFontLicenses(file.fontLicenses, fonts, 'bundle.fontLicenses');
|
|
526
|
+
const fontLicenses = file.fontLicenses as MotionAsset['fontLicenses'];
|
|
527
|
+
const audio = resourceMap(file.audio, 'bundle.audio', AUDIO_URI) as NonNullable<MotionAsset['audio']>;
|
|
528
|
+
const entries = Object.entries(artboards);
|
|
529
|
+
if (entries.length < 1 || entries.length > 16) invalid('bundle.artboards', 'expected one to sixteen artboards');
|
|
530
|
+
if (!Object.hasOwn(artboards, defaultArtboard)) invalid('bundle.defaultArtboard', 'unknown artboard');
|
|
531
|
+
return {
|
|
532
|
+
schema: ASSET_BUNDLE_SCHEMA, name, defaultArtboard,
|
|
533
|
+
...(Object.keys(fonts).length && { fonts }),
|
|
534
|
+
...(fontLicenses && { fontLicenses }),
|
|
535
|
+
...(Object.keys(audio).length && { audio }),
|
|
536
|
+
artboards: Object.fromEntries(entries.map(([key, value]) => {
|
|
537
|
+
identifier(key, `bundle.artboards.${key}`);
|
|
538
|
+
try {
|
|
539
|
+
const board = record(value, `bundle.artboards.${key}`);
|
|
540
|
+
const localFonts = board.fonts === undefined ? {} : record(board.fonts, 'fonts');
|
|
541
|
+
const localAudio = board.audio === undefined ? {} : record(board.audio, 'audio');
|
|
542
|
+
const localLicenses = board.fontLicenses === undefined ? {} : record(board.fontLicenses, 'fontLicenses');
|
|
543
|
+
validateAsset({
|
|
544
|
+
...board, fonts: { ...fonts, ...localFonts },
|
|
545
|
+
fontLicenses: { ...fontLicenses, ...localLicenses }, audio: { ...audio, ...localAudio },
|
|
546
|
+
});
|
|
547
|
+
return [key, board as MotionAsset];
|
|
548
|
+
} catch (error) {
|
|
549
|
+
invalid(`bundle.artboards.${key}`, (error as Error).message);
|
|
550
|
+
}
|
|
551
|
+
})),
|
|
552
|
+
};
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
export function selectArtboard(file: MotionAssetFile, artboard?: string): MotionAsset {
|
|
556
|
+
if (file.schema === ASSET_SCHEMA) {
|
|
557
|
+
if (artboard === undefined || artboard === file.name) return file;
|
|
558
|
+
invalid('artboard', `unknown artboard "${artboard}". Available: ${file.name}`);
|
|
559
|
+
}
|
|
560
|
+
const name = artboard ?? file.defaultArtboard;
|
|
561
|
+
if (!Object.hasOwn(file.artboards, name)) {
|
|
562
|
+
invalid('artboard', `unknown artboard "${name}". Available: ${Object.keys(file.artboards).join(', ')}`);
|
|
563
|
+
}
|
|
564
|
+
const selected = file.artboards[name]!;
|
|
565
|
+
return {
|
|
566
|
+
...selected,
|
|
567
|
+
...(file.fonts && { fonts: { ...file.fonts, ...selected.fonts } }),
|
|
568
|
+
...(file.fontLicenses && { fontLicenses: { ...file.fontLicenses, ...selected.fontLicenses } }),
|
|
569
|
+
...(file.audio && { audio: { ...file.audio, ...selected.audio } }),
|
|
570
|
+
};
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
export function parseAsset(text: string, artboard?: string): MotionAsset {
|
|
574
|
+
return selectArtboard(parseAssetFile(text), artboard);
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
export function resolveAssetColor(value: AssetColor, palette: Readonly<Record<string, string>>): string {
|
|
578
|
+
'worklet';
|
|
579
|
+
return typeof value === 'string' ? value : palette[value.palette]!;
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
export function bindAssetPalette(asset: MotionAsset, overrides?: Readonly<Record<string, string>>): MotionAsset {
|
|
583
|
+
if (!overrides) return asset;
|
|
584
|
+
for (const [name, value] of Object.entries(overrides)) {
|
|
585
|
+
if (!asset.palette || !Object.hasOwn(asset.palette, name)) invalid(`palette.${name}`, 'unknown palette entry');
|
|
586
|
+
color(value, `palette.${name}`);
|
|
587
|
+
}
|
|
588
|
+
return { ...asset, palette: { ...asset.palette, ...overrides } };
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
/** Image names used by the drawing. Unreferenced library entries can be omitted from playback. */
|
|
592
|
+
export function usedAssetImages(nodes: readonly AssetNode[], names = new Set<string>()): Set<string> {
|
|
593
|
+
for (const node of nodes) {
|
|
594
|
+
if (node.kind === 'image' || node.kind === 'mesh') names.add(node.image);
|
|
595
|
+
if (node.kind === 'group') usedAssetImages(node.children, names);
|
|
596
|
+
if (node.kind === 'mesh') {
|
|
597
|
+
for (const pin of Object.values(node.pins ?? {})) {
|
|
598
|
+
if (pin.children) usedAssetImages(pin.children, names);
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
return names;
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
function trackValue(track: Track, ms: number): number {
|
|
606
|
+
'worklet';
|
|
607
|
+
const first = track[0]!;
|
|
608
|
+
if (ms <= first[0]) return first[1];
|
|
609
|
+
let low = 1;
|
|
610
|
+
let high = track.length - 1;
|
|
611
|
+
const last = track[high]!;
|
|
612
|
+
if (!(ms <= last[0])) return last[1];
|
|
613
|
+
// Find the first key at or after the sample. Its easing controls the segment ending there.
|
|
614
|
+
while (low < high) {
|
|
615
|
+
const middle = Math.floor((low + high) / 2);
|
|
616
|
+
if (ms <= track[middle]![0]) high = middle;
|
|
617
|
+
else low = middle + 1;
|
|
618
|
+
}
|
|
619
|
+
const [end, to, ease] = track[low]!;
|
|
620
|
+
const [start, from] = track[low - 1]!;
|
|
621
|
+
if (ease === 'hold') return ms < end ? from : to;
|
|
622
|
+
const p = (ms - start) / (end - start);
|
|
623
|
+
const eased = ease === 'in' ? EASE_IN(p) : ease === 'out' ? EASE_OUT(p) : ease === 'inOut' ? EASE_IN_OUT(p) : p;
|
|
624
|
+
return from + (to - from) * eased;
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
/** Adapt data tracks to the existing state-machine clip interface. */
|
|
628
|
+
export function assetClips(asset: MotionAsset): Clips {
|
|
629
|
+
return Object.fromEntries(Object.entries(asset.clips).map(([name, { ms, reducedAt, tracks }]) => {
|
|
630
|
+
const channels = Object.entries(tracks);
|
|
631
|
+
return [name, {
|
|
632
|
+
ms, reducedAt,
|
|
633
|
+
play(localMs: number) {
|
|
634
|
+
'worklet';
|
|
635
|
+
const pose: Record<string, number> = {};
|
|
636
|
+
for (const [channel, keys] of channels) pose[channel] = trackValue(keys, localMs);
|
|
637
|
+
return { channels: pose };
|
|
638
|
+
},
|
|
639
|
+
}];
|
|
640
|
+
}));
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
export function assetMarkerEvents(asset: Pick<MotionAsset, 'markers'>, entries: StateEntry[]): AssetMarkerEvent[] {
|
|
644
|
+
const markers = asset.markers ?? [];
|
|
645
|
+
return entries.flatMap(({ layer, state, at }) =>
|
|
646
|
+
markers.filter((marker) => marker.layer === layer && marker.state === state).map((marker) => ({ ...marker, at })));
|
|
647
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
const ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
|
2
|
+
|
|
3
|
+
/** Decode a validated base64 string on Node, Hermes, or the web. */
|
|
4
|
+
export function decodeBase64(encoded: string): Uint8Array {
|
|
5
|
+
const bytes = new Uint8Array(encoded.length * 3 / 4);
|
|
6
|
+
let length = 0;
|
|
7
|
+
let bits = 0;
|
|
8
|
+
let value = 0;
|
|
9
|
+
for (const char of encoded) {
|
|
10
|
+
if (char === '=') break;
|
|
11
|
+
value = (value << 6) | ALPHABET.indexOf(char);
|
|
12
|
+
bits += 6;
|
|
13
|
+
if (bits >= 8) {
|
|
14
|
+
bits -= 8;
|
|
15
|
+
bytes[length++] = (value >>> bits) & 255;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return bytes.subarray(0, length);
|
|
19
|
+
}
|