skia-motion-runtime 3.1.0 → 3.1.1
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/package.json +1 -1
- package/runtime/asset.ts +30 -2
- package/runtime/mobile.tsx +23 -9
- package/runtime/useMachine.ts +7 -5
- package/templates/motion/machine.ts +15 -10
package/package.json
CHANGED
package/runtime/asset.ts
CHANGED
|
@@ -3,11 +3,13 @@ import { EASE_IN, EASE_IN_OUT, EASE_OUT } from '../templates/motion/motion';
|
|
|
3
3
|
import type { Clips, Machine, StateEntry } from '../templates/motion/machine';
|
|
4
4
|
import type { AssetAnchor } from './asset-layout';
|
|
5
5
|
import { imageSize } from './asset-image';
|
|
6
|
+
import { decodeBase64 } from './base64';
|
|
6
7
|
import type { MeshNode, MeshVertex } from './asset-mesh';
|
|
7
8
|
|
|
8
9
|
export const ASSET_SCHEMA = 'skac.asset/1';
|
|
9
10
|
export const ASSET_BUNDLE_SCHEMA = 'skac.bundle/1';
|
|
10
11
|
export const MAX_ASSET_BYTES = 1024 * 1024;
|
|
12
|
+
const MAX_ASSET_SOURCE_BYTES = 8 * MAX_ASSET_BYTES;
|
|
11
13
|
export const MAX_FEATHER = 64;
|
|
12
14
|
|
|
13
15
|
export type Scalar = number | string;
|
|
@@ -202,10 +204,26 @@ function resourceMap(value: unknown, at: string, pattern: RegExp) {
|
|
|
202
204
|
identifier(name, key);
|
|
203
205
|
const match = typeof uri === 'string' ? pattern.exec(uri) : null;
|
|
204
206
|
if (!match || match[0] !== uri || match[2]!.length % 4 !== 0) invalid(key, 'expected an embedded base64 data URI');
|
|
207
|
+
if (pattern === FONT_URI) {
|
|
208
|
+
const encoded = match[2]!;
|
|
209
|
+
const padding = encoded.endsWith('==') ? 2 : encoded.endsWith('=') ? 1 : 0;
|
|
210
|
+
if (!hasFontHeader(decodeBase64(encoded.slice(0, 16)), encoded.length * 3 / 4 - padding)) {
|
|
211
|
+
invalid(key, 'expected a TTF or OTF font header');
|
|
212
|
+
}
|
|
213
|
+
}
|
|
205
214
|
}
|
|
206
215
|
return resources;
|
|
207
216
|
}
|
|
208
217
|
|
|
218
|
+
export function hasFontHeader(bytes: Uint8Array, size = bytes.byteLength): boolean {
|
|
219
|
+
if (bytes.byteLength < 12) return false;
|
|
220
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
221
|
+
const format = view.getUint32(0);
|
|
222
|
+
const tables = view.getUint16(4);
|
|
223
|
+
return (format === 0x00010000 || format === 0x4f54544f || format === 0x74727565 || format === 0x74797031)
|
|
224
|
+
&& tables > 0 && 12 + 16 * tables <= size;
|
|
225
|
+
}
|
|
226
|
+
|
|
209
227
|
function checkFontLicenses(value: unknown, fonts: Record<string, unknown>, at: string): void {
|
|
210
228
|
if (value === undefined) return;
|
|
211
229
|
for (const [name, license] of Object.entries(record(value, at))) {
|
|
@@ -507,9 +525,19 @@ function validateAsset(value: unknown): MotionAsset {
|
|
|
507
525
|
|
|
508
526
|
/** Decode the same data file on a React Native JS thread and in the CLI. */
|
|
509
527
|
export function parseAssetFile(text: string): MotionAssetFile {
|
|
510
|
-
|
|
528
|
+
return parseAssetJSON(text, MAX_ASSET_BYTES);
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
/** The CLI may prune unused resources before enforcing the shipped limit. */
|
|
532
|
+
export function parseAssetSource(text: string): MotionAssetFile {
|
|
533
|
+
return parseAssetJSON(text, MAX_ASSET_SOURCE_BYTES);
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
function parseAssetJSON(text: string, limit: number): MotionAssetFile {
|
|
537
|
+
const message = `expected a JSON asset of at most ${limit / MAX_ASSET_BYTES} MiB`;
|
|
538
|
+
if (typeof text !== 'string' || text.length > limit) invalid('asset', message);
|
|
511
539
|
const bytes = encodeURI(text).replace(/%[0-9A-F]{2}/g, 'x').length;
|
|
512
|
-
if (bytes >
|
|
540
|
+
if (bytes > limit) invalid('asset', message);
|
|
513
541
|
const decoded: unknown = JSON.parse(text, (key, value: unknown) => {
|
|
514
542
|
if (RESERVED_KEYS.has(key)) invalid(key, 'reserved field name');
|
|
515
543
|
if (typeof value === 'number' && !Number.isFinite(value)) invalid(key, 'expected a finite number');
|
package/runtime/mobile.tsx
CHANGED
|
@@ -2,7 +2,7 @@ import { forwardRef, useCallback, useEffect, useImperativeHandle, useMemo, useRe
|
|
|
2
2
|
import type { ForwardedRef, ReactElement, RefAttributes } from 'react';
|
|
3
3
|
import { AppState, Pressable, StyleSheet, View, type LayoutChangeEvent, type ViewProps } from 'react-native';
|
|
4
4
|
import { Canvas, Picture, Skia, createPicture, type SkCanvas } from 'react-native-skia';
|
|
5
|
-
import { useDerivedValue, type SharedValue } from 'react-native-reanimated';
|
|
5
|
+
import { useAnimatedReaction, useDerivedValue, useSharedValue, type SharedValue } from 'react-native-reanimated';
|
|
6
6
|
import { assetMarkerEvents, type AssetMarkerEvent, type AssetSemantic } from './asset';
|
|
7
7
|
import { drawAsset, type AssetImages, type DrawableAsset } from './asset-draw';
|
|
8
8
|
import { loadMotionAsset, type LoadedMotionAsset } from './asset-load';
|
|
@@ -53,10 +53,10 @@ export type MotionPlayerProps<B extends MotionBindings = MotionBindings> = Pick<
|
|
|
53
53
|
|
|
54
54
|
type LoadedProps = Omit<MotionPlayerProps, 'source'> & { loaded: LoadedMotionAsset; playerRef: ForwardedRef<MotionPlayerHandle> };
|
|
55
55
|
|
|
56
|
-
function readChannels(
|
|
56
|
+
function readChannels(base: Pose['channels'], channels?: MotionSharedNumbers) {
|
|
57
57
|
'worklet';
|
|
58
|
-
if (!channels) return
|
|
59
|
-
const values = { ...
|
|
58
|
+
if (!channels) return base;
|
|
59
|
+
const values = { ...base };
|
|
60
60
|
for (const [name, shared] of Object.entries(channels)) {
|
|
61
61
|
const value = shared.value;
|
|
62
62
|
if (Number.isFinite(value)) values[name] = value;
|
|
@@ -64,7 +64,7 @@ function readChannels(pose: Pose, channels?: MotionSharedNumbers) {
|
|
|
64
64
|
return values;
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
-
function AssetPicture({ drawing, images, pose, snapshot, readOnUI, width, height, layout, channels }: {
|
|
67
|
+
function AssetPicture({ drawing, images, pose, snapshot, readOnUI, width, height, layout, channels, active }: {
|
|
68
68
|
drawing: DrawableAsset;
|
|
69
69
|
images: AssetImages;
|
|
70
70
|
pose: SharedValue<Pose>;
|
|
@@ -74,7 +74,19 @@ function AssetPicture({ drawing, images, pose, snapshot, readOnUI, width, height
|
|
|
74
74
|
height: number;
|
|
75
75
|
layout: AssetLayoutOptions;
|
|
76
76
|
channels?: MotionSharedNumbers;
|
|
77
|
+
active: boolean;
|
|
77
78
|
}) {
|
|
79
|
+
const overrides = useSharedValue<Record<string, number> | undefined>(undefined);
|
|
80
|
+
useAnimatedReaction(
|
|
81
|
+
() => active ? readChannels({}, channels) : null,
|
|
82
|
+
(next, previous) => {
|
|
83
|
+
if (next === null) return;
|
|
84
|
+
const keys = Object.keys(next);
|
|
85
|
+
if (!previous || keys.length !== Object.keys(previous).length || keys.some((name) => next[name] !== previous[name])) {
|
|
86
|
+
overrides.value = next;
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
);
|
|
78
90
|
const picture = useDerivedValue(() => {
|
|
79
91
|
// The mapper subscribes to captured shared values, and readOnUI hides its snapshot.
|
|
80
92
|
void snapshot?.value;
|
|
@@ -84,7 +96,7 @@ function AssetPicture({ drawing, images, pose, snapshot, readOnUI, width, height
|
|
|
84
96
|
if (typeof value === 'string') values[name] = value;
|
|
85
97
|
}
|
|
86
98
|
return createPicture(
|
|
87
|
-
(canvas) => drawAsset(canvas, drawing,
|
|
99
|
+
(canvas) => drawAsset(canvas, drawing, { ...pose.value.channels, ...overrides.value }, width, height, images, values, layout),
|
|
88
100
|
Skia.XYWHRect(0, 0, width, height),
|
|
89
101
|
);
|
|
90
102
|
});
|
|
@@ -151,11 +163,13 @@ function LoadedPlayer({
|
|
|
151
163
|
if (Object.keys(labelChanges).length) setLabels((previous) => ({ ...previous, ...labelChanges }));
|
|
152
164
|
if (Object.keys(appChanges).length) valuesCallback.current?.(appChanges);
|
|
153
165
|
}, [observeValues, semanticNames]);
|
|
166
|
+
const active = measured && foreground && (live ?? true);
|
|
154
167
|
const machine = useMachine(loaded.machine, clips, {
|
|
155
|
-
live:
|
|
168
|
+
live: active,
|
|
156
169
|
reduced,
|
|
157
170
|
frameRate,
|
|
158
171
|
clocks,
|
|
172
|
+
channelDefaults: drawing.channels,
|
|
159
173
|
onStateEntries: markers?.length && (onMarker || onAudio) ? onStateEntries : undefined,
|
|
160
174
|
initialValues,
|
|
161
175
|
observeValues: watched,
|
|
@@ -188,7 +202,7 @@ function LoadedPlayer({
|
|
|
188
202
|
<Canvas style={StyleSheet.absoluteFill}>
|
|
189
203
|
{measured && <AssetPicture drawing={drawing} images={images} pose={machine.pose} readOnUI={machine.readOnUI}
|
|
190
204
|
snapshot={drawing.textBindings?.length ? machine.snapshot : undefined}
|
|
191
|
-
width={viewport.width} height={viewport.height} layout={layout} channels={channels} />}
|
|
205
|
+
width={viewport.width} height={viewport.height} layout={layout} channels={channels} active={active} />}
|
|
192
206
|
</Canvas>
|
|
193
207
|
{regions}
|
|
194
208
|
</View>
|
|
@@ -244,7 +258,7 @@ export function drawMotionClip(
|
|
|
244
258
|
const value = options.text?.[key] ?? asset.machine.values?.[key]?.default;
|
|
245
259
|
if (typeof value === 'string') text[key] = value;
|
|
246
260
|
}
|
|
247
|
-
drawAsset(canvas, asset.drawing, readChannels(clip.play(time), options.channels),
|
|
261
|
+
drawAsset(canvas, asset.drawing, readChannels(clip.play(time).channels, options.channels),
|
|
248
262
|
options.width, options.height, asset.images, text, options);
|
|
249
263
|
}
|
|
250
264
|
|
package/runtime/useMachine.ts
CHANGED
|
@@ -29,6 +29,8 @@ export type MachineOptions = {
|
|
|
29
29
|
frameRate?: number;
|
|
30
30
|
/** Host clocks in milliseconds, keyed by the machine's declared clock names. */
|
|
31
31
|
clocks?: Record<string, SharedValue<number>>;
|
|
32
|
+
/** Resting values for channels that an animation does not key. */
|
|
33
|
+
channelDefaults?: Readonly<Record<string, number>>;
|
|
32
34
|
probe?: MachineProbe;
|
|
33
35
|
/** Entries after initialization, in machine order. Delivered on the React Native thread. */
|
|
34
36
|
onStateEntries?: (entries: StateEntry[]) => void;
|
|
@@ -117,7 +119,7 @@ function samePose(left: Pose, right: Pose): boolean {
|
|
|
117
119
|
/** Play `machine` with its `clips`. The machine and clips must be stable (module constants), as an exported scene's are. */
|
|
118
120
|
export function useMachine<const M extends Machine>(machine: M, clips: Clips, options: MachineOptions = {}): MachineHandle<M> {
|
|
119
121
|
if (__DEV__ && options.exportedBy !== undefined) assertRuntimeVersion(options.exportedBy);
|
|
120
|
-
const { live = true, clocks = noClocks, probe, onStateEntries, onValuesChange, observeValues } = options;
|
|
122
|
+
const { live = true, clocks = noClocks, channelDefaults, probe, onStateEntries, onValuesChange, observeValues } = options;
|
|
121
123
|
const systemReduced = useReducedMotion();
|
|
122
124
|
const reduced = options.reduced ?? systemReduced;
|
|
123
125
|
const rate = options.frameRate;
|
|
@@ -145,7 +147,7 @@ export function useMachine<const M extends Machine>(machine: M, clips: Clips, op
|
|
|
145
147
|
const lastChannels = useSharedValue('');
|
|
146
148
|
const awaitingChange = useSharedValue(false);
|
|
147
149
|
const liveNow = useSharedValue(live);
|
|
148
|
-
const firstPose = useMemo(() => poseAt(compiled, clips, first, 0, read, reduced), [first]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
150
|
+
const firstPose = useMemo(() => poseAt(compiled, clips, first, 0, read, reduced, channelDefaults), [first]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
149
151
|
const pose = useSharedValue<Pose>(firstPose);
|
|
150
152
|
|
|
151
153
|
const observed = useMemo(() => Array.from(new Set(observeValues), (name) => {
|
|
@@ -173,7 +175,7 @@ export function useMachine<const M extends Machine>(machine: M, clips: Clips, op
|
|
|
173
175
|
if (!liveNow.value) return undefined;
|
|
174
176
|
// Subscribe to the named clocks, including when the frame callback sleeps.
|
|
175
177
|
for (const n of clockNames) void clocks[n]!.value;
|
|
176
|
-
return poseAt(compiled, clips, snapshot.value, ms.value, read, reduced);
|
|
178
|
+
return poseAt(compiled, clips, snapshot.value, ms.value, read, reduced, channelDefaults);
|
|
177
179
|
},
|
|
178
180
|
(next) => {
|
|
179
181
|
// The reaction subscribes only to the prepare inputs. Updating its output cannot wake it again.
|
|
@@ -203,14 +205,14 @@ export function useMachine<const M extends Machine>(machine: M, clips: Clips, op
|
|
|
203
205
|
snapshot.value = next;
|
|
204
206
|
if (entries?.length && onStateEntries) scheduleOnRN(onStateEntries, entries);
|
|
205
207
|
if (probe?.onChanged) {
|
|
206
|
-
const key = JSON.stringify(poseAt(compiled, clips, next, t, read, reduced).channels);
|
|
208
|
+
const key = JSON.stringify(poseAt(compiled, clips, next, t, read, reduced, channelDefaults).channels);
|
|
207
209
|
if (key !== lastChannels.value) {
|
|
208
210
|
lastChannels.value = key;
|
|
209
211
|
if (awaitingChange.value) { awaitingChange.value = false; scheduleOnRN(probe.onChanged, frame.timestamp); }
|
|
210
212
|
}
|
|
211
213
|
}
|
|
212
214
|
if (pending.value.length === 0 && settled(compiled, next, read, reduced).frame) scheduleOnRN(sleep);
|
|
213
|
-
}, [compiled, clips, reduced, probe, onStateEntries, frameInterval, read]);
|
|
215
|
+
}, [compiled, clips, reduced, probe, onStateEntries, frameInterval, read, channelDefaults]);
|
|
214
216
|
|
|
215
217
|
const frames = useFrameCallback(tick, false);
|
|
216
218
|
// The callback is switched from JavaScript only: switching it from a worklet makes a synchronous remote call that
|
|
@@ -388,14 +388,19 @@ function clipTime(loop: number, ms: number, local: number, reduced: boolean, red
|
|
|
388
388
|
return p <= ms ? p : 2 * ms - p;
|
|
389
389
|
}
|
|
390
390
|
|
|
391
|
-
function lerpPose(a: Pose, b: Pose, w: number): Pose {
|
|
391
|
+
function lerpPose(a: Pose, b: Pose, w: number, underneath: Readonly<Record<string, number>>): Pose {
|
|
392
392
|
'worklet';
|
|
393
393
|
const channels: Record<string, number> = { ...a.channels };
|
|
394
394
|
for (const k of Object.keys(b.channels)) {
|
|
395
395
|
const bv = b.channels[k]!;
|
|
396
|
-
const av = a.channels[k];
|
|
396
|
+
const av = a.channels[k] ?? underneath[k];
|
|
397
397
|
channels[k] = av === undefined ? bv : av + (bv - av) * w;
|
|
398
398
|
}
|
|
399
|
+
for (const k of Object.keys(a.channels)) {
|
|
400
|
+
if (Object.hasOwn(b.channels, k) || !Object.hasOwn(underneath, k)) continue;
|
|
401
|
+
const av = a.channels[k]!;
|
|
402
|
+
channels[k] = av + (underneath[k]! - av) * w;
|
|
403
|
+
}
|
|
399
404
|
let parts: Record<string, PartBox> | undefined;
|
|
400
405
|
if (a.parts || b.parts) {
|
|
401
406
|
parts = { ...(a.parts ?? {}) };
|
|
@@ -413,7 +418,7 @@ function lerpPose(a: Pose, b: Pose, w: number): Pose {
|
|
|
413
418
|
}
|
|
414
419
|
|
|
415
420
|
/** State `si`'s pose `local` ms in: its clip, or its 1D blend at the current value. */
|
|
416
|
-
function statePose(L: CLayer, si: number, local: number, vals: Literal[], clips: Clips, reduced: boolean): Pose {
|
|
421
|
+
function statePose(L: CLayer, si: number, local: number, vals: Literal[], clips: Clips, reduced: boolean, underneath: Readonly<Record<string, number>> = {}): Pose {
|
|
417
422
|
'worklet';
|
|
418
423
|
const st = L.states[si]!;
|
|
419
424
|
if (!st.keys) {
|
|
@@ -432,7 +437,7 @@ function statePose(L: CLayer, si: number, local: number, vals: Literal[], clips:
|
|
|
432
437
|
if (v <= keys[i]![0]) {
|
|
433
438
|
const a = keys[i - 1]!;
|
|
434
439
|
const b = keys[i]!;
|
|
435
|
-
return lerpPose(at(a), at(b), (v - a[0]) / (b[0] - a[0]));
|
|
440
|
+
return lerpPose(at(a), at(b), (v - a[0]) / (b[0] - a[0]), underneath);
|
|
436
441
|
}
|
|
437
442
|
}
|
|
438
443
|
return at(keys[keys.length - 1]!);
|
|
@@ -451,9 +456,9 @@ function ease(kind: number, p: number): number {
|
|
|
451
456
|
* What the machine draws at `t` (at or after `s.t`, before the next instant): every layer's pose, mixes applied, a later
|
|
452
457
|
* layer's channel or part over an earlier one's.
|
|
453
458
|
*/
|
|
454
|
-
export function pose(c: Compiled, clips: Clips, s: Snapshot, t: number, clock: ClockRead, reduced = false): Pose {
|
|
459
|
+
export function pose(c: Compiled, clips: Clips, s: Snapshot, t: number, clock: ClockRead, reduced = false, defaults?: Readonly<Record<string, number>>): Pose {
|
|
455
460
|
'worklet';
|
|
456
|
-
const channels: Record<string, number> = {};
|
|
461
|
+
const channels: Record<string, number> = { ...defaults };
|
|
457
462
|
let parts: Record<string, PartBox> | undefined;
|
|
458
463
|
for (let li = 0; li < c.layers.length; li++) {
|
|
459
464
|
const L = c.layers[li]!;
|
|
@@ -465,16 +470,16 @@ export function pose(c: Compiled, clips: Clips, s: Snapshot, t: number, clock: C
|
|
|
465
470
|
const by = L.states[l.heldS]!.by;
|
|
466
471
|
let vals = s.vals;
|
|
467
472
|
if (by >= 0) { vals = s.vals.slice(); vals[by] = l.heldBy; }
|
|
468
|
-
p = statePose(L, l.heldS, l.held, vals, clips, reduced);
|
|
473
|
+
p = statePose(L, l.heldS, l.held, vals, clips, reduced, channels);
|
|
469
474
|
} else if (l.s < 0) {
|
|
470
475
|
continue;
|
|
471
476
|
} else {
|
|
472
|
-
p = statePose(L, l.s, localMs(L, l, t, clock), s.vals, clips, reduced);
|
|
477
|
+
p = statePose(L, l.s, localMs(L, l, t, clock), s.vals, clips, reduced, channels);
|
|
473
478
|
if (l.mixTr >= 0 && t < l.mixEnd && !reduced) {
|
|
474
479
|
const from = L.states[l.mixFrom]!;
|
|
475
|
-
const fromPose = statePose(L, l.mixFrom, sinceEntry(L, l.mixAt, l.mixCAt, t, clock) * from.speed, s.vals, clips, reduced);
|
|
480
|
+
const fromPose = statePose(L, l.mixFrom, sinceEntry(L, l.mixAt, l.mixCAt, t, clock) * from.speed, s.vals, clips, reduced, channels);
|
|
476
481
|
const tr = L.trans[l.mixTr]!;
|
|
477
|
-
p = lerpPose(fromPose, p, ease(tr.ease, (t - l.mixStart) / (l.mixEnd - l.mixStart)));
|
|
482
|
+
p = lerpPose(fromPose, p, ease(tr.ease, (t - l.mixStart) / (l.mixEnd - l.mixStart)), channels);
|
|
478
483
|
}
|
|
479
484
|
}
|
|
480
485
|
for (const k of Object.keys(p.channels)) channels[k] = p.channels[k]!;
|