skia-motion-runtime 0.0.0-stage → 3.0.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 +44 -4
- package/runtime/asset-draw.ts +262 -0
- package/runtime/asset-image.ts +64 -0
- package/runtime/asset-layout.ts +22 -0
- package/runtime/asset-mesh.ts +37 -0
- package/runtime/asset.ts +410 -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 +158 -0
- package/runtime/useMachine.ts +206 -0
- package/runtime/version.ts +37 -0
- package/templates/motion/face.ts +118 -0
- package/templates/motion/machine.ts +647 -0
- package/templates/motion/motion.ts +215 -0
- package/README.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 the skia-motion authors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/NOTICE.md
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# Notices
|
|
2
|
+
|
|
3
|
+
The motion docs in `docs/motion` and the files in `templates/motion` adapt code and rules from two MIT-licensed skills. Their licences follow, copied whole.
|
|
4
|
+
|
|
5
|
+
| This file | Adapts |
|
|
6
|
+
|---|---|
|
|
7
|
+
| templates/motion/motion.ts | rossark-motion-skill `templates/react-native/motion.ts` (track, flow, seg, wave, loop, bell, seeded, the curves); huashu-art-motion `scripts/engine/lib/motion.js` (`MO.bezier` solver, `MO.spring`) |
|
|
8
|
+
| templates/motion/clock.app.ts | rossark-motion-skill `templates/react-native/motion.ts` (useLoopClock, useOneShotClock) |
|
|
9
|
+
| templates/motion/liveFace.tsx | rossark-motion-skill `templates/react-native/liveFace.tsx` (lid, cheek, closed line, happy arc, lid clip); huashu-art-motion `scripts/engine/lib/toon.js` (the expression set: shock, happy, worry, mouth shapes) |
|
|
10
|
+
| docs/motion/principles.md, body.md | rossark-motion-skill `SKILL.md` §1–§8, §11 and `MOTION.md` |
|
|
11
|
+
| docs/motion/faces.md, idle.md | rossark-motion-skill `SKILL.md` §4–§5; huashu-art-motion `scripts/engine/lib/toon.js`, `rig_huashu.js`, `references/04-纯代码绘制.md`, `07-正面经验.md`, `镜头语言.md` |
|
|
12
|
+
| docs/motion/anticipation.md, body.md (timing tables) | ideas from LottieFiles motion-design (`director/disney-principles.md`, `reference/timing-easing-tables.md`, `patterns/entrance-exit.md`), written in our own words; that skill ships no licence file, so none of its text is copied |
|
|
13
|
+
| templates/motion/points.ts, templates/motion/moves.ts (morph and point motions) | morph and point-motion styles (paths and staggers of a point set morphing into another; the ways a point cloud arrives and leaves; the odometer's rolling digits) ported from Word Quran's own reels kit by its owner, MIT in this package. skac's package ships no onetake code; a project may load onetake's lib/motion.js through its config, and Word Quran holds the author's permission |
|
|
14
|
+
| templates/motion/clock.remotion.ts, docs/motion/remotion-and-app.md | the sRGB fix for Skia's WebGL drawing buffer, written for this package |
|
|
15
|
+
|
|
16
|
+
## rossark-motion-skill
|
|
17
|
+
|
|
18
|
+
MIT License
|
|
19
|
+
|
|
20
|
+
Copyright (c) 2026 rossark0
|
|
21
|
+
|
|
22
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
23
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
24
|
+
in the Software without restriction, including without limitation the rights
|
|
25
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
26
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
27
|
+
furnished to do so, subject to the following conditions:
|
|
28
|
+
|
|
29
|
+
The above copyright notice and this permission notice shall be included in all
|
|
30
|
+
copies or substantial portions of the Software.
|
|
31
|
+
|
|
32
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
33
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
34
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
35
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
36
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
37
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
38
|
+
SOFTWARE.
|
|
39
|
+
|
|
40
|
+
## huashu-art-motion
|
|
41
|
+
|
|
42
|
+
MIT License
|
|
43
|
+
|
|
44
|
+
Copyright (c) 2026 alchaincyf (花叔 · 花生)
|
|
45
|
+
|
|
46
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
47
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
48
|
+
in the Software without restriction, including without limitation the rights
|
|
49
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
50
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
51
|
+
furnished to do so, subject to the following conditions:
|
|
52
|
+
|
|
53
|
+
The above copyright notice and this permission notice shall be included in all
|
|
54
|
+
copies or substantial portions of the Software.
|
|
55
|
+
|
|
56
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
57
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
58
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
59
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
60
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
61
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
62
|
+
SOFTWARE.
|
package/package.json
CHANGED
|
@@ -1,6 +1,46 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "skia-motion-runtime",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "3.0.0",
|
|
4
|
+
"description": "React Native and Expo player for interactive .skmotion assets",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./runtime/mobile.tsx",
|
|
7
|
+
"types": "./runtime/mobile.tsx",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./runtime/mobile.tsx",
|
|
10
|
+
"./machine": "./runtime/index.ts"
|
|
11
|
+
},
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/asifnawaz/skia-motion-agentic-cli.git"
|
|
16
|
+
},
|
|
17
|
+
"homepage": "https://github.com/asifnawaz/skia-motion-agentic-cli",
|
|
18
|
+
"bugs": {
|
|
19
|
+
"url": "https://github.com/asifnawaz/skia-motion-agentic-cli/issues"
|
|
20
|
+
},
|
|
21
|
+
"peerDependencies": {
|
|
22
|
+
"react": ">=19",
|
|
23
|
+
"react-native": ">=0.79",
|
|
24
|
+
"react-native-reanimated": ">=4",
|
|
25
|
+
"react-native-skia": ">=3.0.0",
|
|
26
|
+
"react-native-worklets": ">=0.7"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"LICENSE",
|
|
30
|
+
"NOTICE.md",
|
|
31
|
+
"runtime/asset-draw.ts",
|
|
32
|
+
"runtime/asset-image.ts",
|
|
33
|
+
"runtime/asset-layout.ts",
|
|
34
|
+
"runtime/asset-mesh.ts",
|
|
35
|
+
"runtime/asset.ts",
|
|
36
|
+
"runtime/index.ts",
|
|
37
|
+
"runtime/machine-schema.d.ts",
|
|
38
|
+
"runtime/machine-schema.js",
|
|
39
|
+
"runtime/mobile.tsx",
|
|
40
|
+
"runtime/useMachine.ts",
|
|
41
|
+
"runtime/version.ts",
|
|
42
|
+
"templates/motion/face.ts",
|
|
43
|
+
"templates/motion/machine.ts",
|
|
44
|
+
"templates/motion/motion.ts"
|
|
45
|
+
]
|
|
46
|
+
}
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import { BlendMode, BlurStyle, ClipOp, FilterMode, MipmapMode, Skia, TileMode, VertexMode, type SkCanvas, type SkData, type SkImage, type SkPaint, type SkPath, type SkShader } from 'react-native-skia';
|
|
2
|
+
import { MAX_FEATHER, usedAssetImages, type AssetNode, type MotionAsset, type PathCommand } from './asset';
|
|
3
|
+
import { anchorOffset, layoutAsset } from './asset-layout';
|
|
4
|
+
import { meshPinPosition, meshPosition, type MeshNode, type MeshPin } from './asset-mesh';
|
|
5
|
+
|
|
6
|
+
type Channels = Readonly<Record<string, number>>;
|
|
7
|
+
type Point = ReturnType<typeof meshPosition>;
|
|
8
|
+
type DrawablePin = Omit<MeshPin, 'children'> & { children: readonly DrawableNode[] };
|
|
9
|
+
type DrawableMesh = Omit<MeshNode, 'pins'> & {
|
|
10
|
+
pins?: readonly DrawablePin[];
|
|
11
|
+
fixedPositions?: Point[];
|
|
12
|
+
textureCoords?: Point[];
|
|
13
|
+
};
|
|
14
|
+
type DrawableNode =
|
|
15
|
+
| (Omit<Extract<AssetNode, { kind: 'group' }>, 'children'> & {
|
|
16
|
+
children: readonly DrawableNode[];
|
|
17
|
+
preparedClip?: SkPath;
|
|
18
|
+
})
|
|
19
|
+
| DrawableMesh
|
|
20
|
+
| (DrawableShape & { preparedPath?: SkPath; preparedPaint?: SkPaint });
|
|
21
|
+
|
|
22
|
+
type DrawableShape = Exclude<AssetNode, { kind: 'group' | 'mesh' }>;
|
|
23
|
+
|
|
24
|
+
export type DrawableAsset = Pick<MotionAsset, 'channels'> & {
|
|
25
|
+
artboard: Omit<MotionAsset['artboard'], 'nodes'> & { nodes: readonly DrawableNode[] };
|
|
26
|
+
meshShaders?: AssetMeshShaders;
|
|
27
|
+
};
|
|
28
|
+
export type AssetImages = Readonly<Record<string, SkImage>>;
|
|
29
|
+
export type AssetMeshShaders = Readonly<Record<string, SkShader>>;
|
|
30
|
+
|
|
31
|
+
/** Decode on the JS thread once per loaded file. Only the native images enter the UI worklet. */
|
|
32
|
+
export function decodeAssetImages(asset: MotionAsset): AssetImages {
|
|
33
|
+
if (!asset.images) return {};
|
|
34
|
+
// Skia v3 renamed this factory after 3.0. Keep both names usable at the peer dependency floor.
|
|
35
|
+
const factory = Skia.Image as {
|
|
36
|
+
MakeFromEncoded?: (data: SkData) => SkImage | null;
|
|
37
|
+
MakeImageFromEncoded?: (data: SkData) => SkImage | null;
|
|
38
|
+
};
|
|
39
|
+
const byUri = new Map<string, SkImage>();
|
|
40
|
+
const embedded = asset.images;
|
|
41
|
+
const used = usedAssetImages(asset.artboard.nodes);
|
|
42
|
+
return Object.fromEntries(Array.from(used, (name) => {
|
|
43
|
+
const uri = embedded[name]!;
|
|
44
|
+
const previous = byUri.get(uri);
|
|
45
|
+
if (previous) return [name, previous];
|
|
46
|
+
const data = Skia.Data.fromBase64(uri.slice(uri.indexOf(',') + 1));
|
|
47
|
+
const image = factory.MakeFromEncoded ? factory.MakeFromEncoded(data) : factory.MakeImageFromEncoded?.(data);
|
|
48
|
+
if (!image) throw new Error(`images.${name}: could not decode the embedded image`);
|
|
49
|
+
byUri.set(uri, image);
|
|
50
|
+
return [name, image];
|
|
51
|
+
}));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function read(value: string | number | undefined, channels: Channels, fallback = 0): number {
|
|
55
|
+
'worklet';
|
|
56
|
+
if (value === undefined) return fallback;
|
|
57
|
+
return typeof value === 'string' ? channels[value]! : value;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function makePath(commands: readonly PathCommand[], channels: Channels) {
|
|
61
|
+
'worklet';
|
|
62
|
+
const path = Skia.PathBuilder.Make();
|
|
63
|
+
for (const [op, ...coords] of commands) {
|
|
64
|
+
const args = coords.map((coord) => read(coord, channels));
|
|
65
|
+
if (op === 'M') path.moveTo(args[0]!, args[1]!);
|
|
66
|
+
else if (op === 'L') path.lineTo(args[0]!, args[1]!);
|
|
67
|
+
else if (op === 'Q') path.quadTo(args[0]!, args[1]!, args[2]!, args[3]!);
|
|
68
|
+
else if (op === 'C') path.cubicTo(args[0]!, args[1]!, args[2]!, args[3]!, args[4]!, args[5]!);
|
|
69
|
+
else path.close();
|
|
70
|
+
}
|
|
71
|
+
return path.detach();
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function makePaint(node: DrawableShape, alpha: number, channels: Channels): SkPaint {
|
|
75
|
+
'worklet';
|
|
76
|
+
const paint = Skia.Paint();
|
|
77
|
+
paint.setAntiAlias(true);
|
|
78
|
+
if (node.kind === 'image') {
|
|
79
|
+
paint.setAlphaf(alpha);
|
|
80
|
+
return paint;
|
|
81
|
+
}
|
|
82
|
+
const feather = Math.min(MAX_FEATHER, read(node.feather, channels));
|
|
83
|
+
if (feather > 0) paint.setMaskFilter(Skia.MaskFilter.MakeBlur(BlurStyle.Normal, feather, true));
|
|
84
|
+
if (typeof node.fill === 'string') {
|
|
85
|
+
const color = Skia.Color(node.fill);
|
|
86
|
+
paint.setColor(color);
|
|
87
|
+
paint.setAlphaf(alpha * color[3]!);
|
|
88
|
+
} else {
|
|
89
|
+
const light = node.fill;
|
|
90
|
+
const center = { x: read(light.x, channels), y: read(light.y, channels) };
|
|
91
|
+
const radius = Math.max(0.001, read(light.radius, channels));
|
|
92
|
+
const colors = light.colors.map((color) => Skia.Color(color));
|
|
93
|
+
paint.setShader(Skia.Shader.MakeRadialGradient(center, radius, colors, null, TileMode.Clamp));
|
|
94
|
+
paint.setAlphaf(alpha);
|
|
95
|
+
}
|
|
96
|
+
return paint;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function preparePaint(node: DrawableShape): SkPaint | undefined {
|
|
100
|
+
if (typeof node.opacity === 'string' || node.opacity === 0) return undefined;
|
|
101
|
+
if (node.kind !== 'image') {
|
|
102
|
+
if (typeof node.feather === 'string') return undefined;
|
|
103
|
+
const light = node.fill;
|
|
104
|
+
if (typeof light !== 'string' && [light.x, light.y, light.radius].some((value) => typeof value === 'string')) {
|
|
105
|
+
return undefined;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return makePaint(node, node.opacity ?? 1, {});
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function meshTextureCoords(node: MeshNode | DrawableMesh, image: SkImage): Point[] {
|
|
112
|
+
'worklet';
|
|
113
|
+
const width = image.width();
|
|
114
|
+
const height = image.height();
|
|
115
|
+
return node.vertices.map(([, , u, v]) => ({ x: u * width, y: v * height }));
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function meshShader(image: SkImage): SkShader {
|
|
119
|
+
'worklet';
|
|
120
|
+
return image.makeShaderOptions(TileMode.Decal, TileMode.Decal, FilterMode.Linear, MipmapMode.None);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function prepareNode(node: AssetNode, images: AssetImages, shaders: Record<string, SkShader>, shadersByImage: Map<SkImage, SkShader>): DrawableNode {
|
|
124
|
+
const commands = node.kind === 'group' ? node.clip : node.kind === 'path' ? node.commands : undefined;
|
|
125
|
+
const prepared = commands?.every(([, ...coordinates]) => coordinates.every((value) => typeof value === 'number'))
|
|
126
|
+
? makePath(commands, {})
|
|
127
|
+
: undefined;
|
|
128
|
+
if (node.kind === 'group') {
|
|
129
|
+
const { points: _points, ...drawing } = node;
|
|
130
|
+
return { ...drawing, preparedClip: prepared, children: node.children.map((child) => prepareNode(child, images, shaders, shadersByImage)) };
|
|
131
|
+
}
|
|
132
|
+
if (node.kind === 'mesh') {
|
|
133
|
+
const image = images[node.image];
|
|
134
|
+
if (image && !Object.hasOwn(shaders, node.image)) {
|
|
135
|
+
const shader = shadersByImage.get(image) ?? meshShader(image);
|
|
136
|
+
shadersByImage.set(image, shader);
|
|
137
|
+
shaders[node.image] = shader;
|
|
138
|
+
}
|
|
139
|
+
const pins = node.pins && Object.values(node.pins).filter((pin) => pin.children?.length).map((pin) => ({
|
|
140
|
+
...pin,
|
|
141
|
+
children: pin.children!.map((child) => prepareNode(child, images, shaders, shadersByImage)),
|
|
142
|
+
}));
|
|
143
|
+
const fixed = node.vertices.every(([x, y]) => typeof x === 'number' && typeof y === 'number');
|
|
144
|
+
return {
|
|
145
|
+
...node, pins,
|
|
146
|
+
fixedPositions: fixed ? node.vertices.map((vertex) => meshPosition(vertex, {})) : undefined,
|
|
147
|
+
textureCoords: image ? meshTextureCoords(node, image) : undefined,
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
return { ...node, preparedPath: prepared, preparedPaint: preparePaint(node) };
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Build fixed geometry and paints once. Channel-driven paths and clips are still evaluated for each pose. */
|
|
154
|
+
export function prepareAssetDrawing(asset: MotionAsset, images: AssetImages = {}): DrawableAsset {
|
|
155
|
+
const shaders: Record<string, SkShader> = {};
|
|
156
|
+
const shadersByImage = new Map<SkImage, SkShader>();
|
|
157
|
+
const nodes = asset.artboard.nodes.map((node) => prepareNode(node, images, shaders, shadersByImage));
|
|
158
|
+
return { channels: asset.channels, artboard: { ...asset.artboard, nodes }, meshShaders: shaders };
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Warmed images may be different native objects. Prepare their shaders with the same geometry. */
|
|
162
|
+
export function rebindAssetMeshShaders(drawing: DrawableAsset, images: AssetImages): DrawableAsset {
|
|
163
|
+
const names = Object.keys(drawing.meshShaders ?? {});
|
|
164
|
+
if (!names.length) return drawing;
|
|
165
|
+
const byImage = new Map<SkImage, SkShader>();
|
|
166
|
+
const meshShaders = Object.fromEntries(names.map((name) => {
|
|
167
|
+
const image = images[name]!;
|
|
168
|
+
const shader = byImage.get(image) ?? meshShader(image);
|
|
169
|
+
byImage.set(image, shader);
|
|
170
|
+
return [name, shader];
|
|
171
|
+
}));
|
|
172
|
+
return { ...drawing, meshShaders };
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function drawMesh(canvas: SkCanvas, node: DrawableMesh, channels: Channels, image: SkImage, alpha: number, shader: SkShader | undefined) {
|
|
176
|
+
'worklet';
|
|
177
|
+
const positions = node.fixedPositions ?? node.vertices.map((vertex) => meshPosition(vertex, channels));
|
|
178
|
+
const textures = node.textureCoords ?? meshTextureCoords(node, image);
|
|
179
|
+
const paint = Skia.Paint();
|
|
180
|
+
paint.setAlphaf(alpha);
|
|
181
|
+
paint.setShader(shader ?? meshShader(image));
|
|
182
|
+
const mesh = Skia.MakeVertices(VertexMode.Triangles, positions, textures, undefined, node.indices as number[]);
|
|
183
|
+
canvas.drawVertices(mesh, BlendMode.SrcOver, paint);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function drawNodes(canvas: SkCanvas, nodes: readonly DrawableNode[], channels: Channels, images: AssetImages, shaders: AssetMeshShaders, insetX = 0, insetY = 0): void {
|
|
187
|
+
'worklet';
|
|
188
|
+
for (const node of nodes) {
|
|
189
|
+
if (node.kind === 'group') {
|
|
190
|
+
const offset = anchorOffset(node.anchor, insetX, insetY);
|
|
191
|
+
canvas.save();
|
|
192
|
+
canvas.translate(read(node.x, channels) + offset.x, read(node.y, channels) + offset.y);
|
|
193
|
+
canvas.rotate(read(node.rotation, channels), 0, 0);
|
|
194
|
+
canvas.scale(read(node.scaleX, channels, 1), read(node.scaleY, channels, 1));
|
|
195
|
+
if (node.clip) canvas.clipPath(node.preparedClip ?? makePath(node.clip, channels), ClipOp.Intersect, true);
|
|
196
|
+
drawNodes(canvas, node.children, channels, images, shaders);
|
|
197
|
+
canvas.restore();
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
const alpha = Math.min(1, Math.max(0, read(node.opacity, channels, 1)));
|
|
201
|
+
if (node.kind === 'mesh') {
|
|
202
|
+
const image = images[node.image];
|
|
203
|
+
if (image && alpha) drawMesh(canvas, node, channels, image, alpha, shaders[node.image]);
|
|
204
|
+
if (node.pins) {
|
|
205
|
+
for (const pin of node.pins) {
|
|
206
|
+
const { x, y } = meshPinPosition(node, pin, channels);
|
|
207
|
+
canvas.save();
|
|
208
|
+
canvas.translate(x, y);
|
|
209
|
+
drawNodes(canvas, pin.children, channels, images, shaders);
|
|
210
|
+
canvas.restore();
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
if (!alpha) continue;
|
|
216
|
+
const paint = node.preparedPaint ?? makePaint(node, alpha, channels);
|
|
217
|
+
if (node.kind === 'image') {
|
|
218
|
+
const image = images[node.image];
|
|
219
|
+
const width = read(node.width, channels);
|
|
220
|
+
const height = read(node.height, channels);
|
|
221
|
+
if (!image || width <= 0 || height <= 0) continue;
|
|
222
|
+
canvas.drawImageRectOptions(
|
|
223
|
+
image,
|
|
224
|
+
Skia.XYWHRect(0, 0, image.width(), image.height()),
|
|
225
|
+
Skia.XYWHRect(read(node.x, channels), read(node.y, channels), width, height),
|
|
226
|
+
FilterMode.Linear, MipmapMode.None, paint,
|
|
227
|
+
);
|
|
228
|
+
continue;
|
|
229
|
+
}
|
|
230
|
+
if (node.kind === 'path') {
|
|
231
|
+
canvas.drawPath(node.preparedPath ?? makePath(node.commands, channels), paint);
|
|
232
|
+
continue;
|
|
233
|
+
}
|
|
234
|
+
const w = read(node.width, channels);
|
|
235
|
+
const h = read(node.height, channels);
|
|
236
|
+
if (w <= 0 || h <= 0) continue;
|
|
237
|
+
const rect = Skia.XYWHRect(read(node.x, channels), read(node.y, channels), w, h);
|
|
238
|
+
if (node.kind === 'ellipse') canvas.drawOval(rect, paint);
|
|
239
|
+
else {
|
|
240
|
+
const radius = Math.min(w / 2, h / 2, Math.max(0, read(node.radius, channels)));
|
|
241
|
+
canvas.drawRRect(Skia.RRectXY(rect, radius, radius), paint);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** Draw the resolved pose inside a viewport, preserving artboard coordinates and aspect ratio. */
|
|
247
|
+
export function drawAsset(canvas: SkCanvas, asset: DrawableAsset, pose: Channels, width: number, height: number, images: AssetImages = {}): void {
|
|
248
|
+
'worklet';
|
|
249
|
+
if (width <= 0 || height <= 0) return;
|
|
250
|
+
const board = asset.artboard;
|
|
251
|
+
const { scale, insetX, insetY } = layoutAsset(board, width, height);
|
|
252
|
+
const anchored = board.nodes.some((node) => node.kind === 'group' && node.anchor);
|
|
253
|
+
canvas.save();
|
|
254
|
+
canvas.translate(insetX * scale, insetY * scale);
|
|
255
|
+
canvas.scale(scale, scale);
|
|
256
|
+
const bounds = anchored
|
|
257
|
+
? Skia.XYWHRect(-insetX, -insetY, board.width + 2 * insetX, board.height + 2 * insetY)
|
|
258
|
+
: Skia.XYWHRect(0, 0, board.width, board.height);
|
|
259
|
+
canvas.clipRect(bounds, ClipOp.Intersect, true);
|
|
260
|
+
drawNodes(canvas, board.nodes, { ...asset.channels, ...pose }, images, asset.meshShaders ?? {}, insetX, insetY);
|
|
261
|
+
canvas.restore();
|
|
262
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
const ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
|
2
|
+
|
|
3
|
+
function decodeBase64Slice(encoded: string): Uint8Array {
|
|
4
|
+
const bytes = new Uint8Array(encoded.length * 3 / 4);
|
|
5
|
+
let length = 0;
|
|
6
|
+
let bits = 0;
|
|
7
|
+
let value = 0;
|
|
8
|
+
for (const char of encoded) {
|
|
9
|
+
if (char === '=') break;
|
|
10
|
+
value = (value << 6) | ALPHABET.indexOf(char);
|
|
11
|
+
bits += 6;
|
|
12
|
+
if (bits >= 8) {
|
|
13
|
+
bits -= 8;
|
|
14
|
+
bytes[length++] = (value >>> bits) & 255;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return bytes.subarray(0, length);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Check the embedded image header before passing its bytes to a renderer. */
|
|
21
|
+
export function imageSize(uri: unknown): { width: number; height: number } | null {
|
|
22
|
+
if (typeof uri !== 'string') return null;
|
|
23
|
+
const match = /^data:image\/(png|webp);base64,([A-Za-z0-9+/]+={0,2})$/.exec(uri);
|
|
24
|
+
if (!match || match[0] !== uri || match[2]!.length % 4 !== 0) return null;
|
|
25
|
+
const encoded = match[2]!;
|
|
26
|
+
const padding = encoded.endsWith('==') ? 2 : encoded.endsWith('=') ? 1 : 0;
|
|
27
|
+
const size = encoded.length * 3 / 4 - padding;
|
|
28
|
+
const prefix = decodeBase64Slice(encoded.slice(0, 40));
|
|
29
|
+
const suffix = decodeBase64Slice(encoded.slice(-20)).subarray(-12);
|
|
30
|
+
return sizeFromParts(match[1]!, prefix, suffix, size);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Validate source artwork in the authoring CLI without encoding it to base64 first. */
|
|
34
|
+
export function imageFileSize(format: string, data: Uint8Array): { width: number; height: number } | null {
|
|
35
|
+
return sizeFromParts(format, data.subarray(0, 30), data.subarray(-12), data.length);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function sizeFromParts(format: string, bytes: Uint8Array, tail: Uint8Array, size: number) {
|
|
39
|
+
const tag = (at: number) => String.fromCharCode(...bytes.subarray(at, at + 4));
|
|
40
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
41
|
+
if (format === 'png') {
|
|
42
|
+
if (size < 57 || view.getUint32(0) !== 0x89504e47 || view.getUint32(4) !== 0x0d0a1a0a || view.getUint32(8) !== 13 || tag(12) !== 'IHDR') return null;
|
|
43
|
+
const end = new DataView(tail.buffer, tail.byteOffset, tail.byteLength);
|
|
44
|
+
if (end.getUint32(0) !== 0 || end.getUint32(4) !== 0x49454e44 || end.getUint32(8) !== 0xae426082) return null;
|
|
45
|
+
return { width: view.getUint32(16), height: view.getUint32(20) };
|
|
46
|
+
}
|
|
47
|
+
if (format !== 'webp' || bytes.length < 25 || tag(0) !== 'RIFF' || tag(8) !== 'WEBP') return null;
|
|
48
|
+
const chunkSize = view.getUint32(16, true);
|
|
49
|
+
if (view.getUint32(4, true) !== size - 8 || 20 + chunkSize + (chunkSize % 2) > size) return null;
|
|
50
|
+
const chunkType = tag(12);
|
|
51
|
+
if (chunkType === 'VP8X' && chunkSize === 10) {
|
|
52
|
+
if (size === 30 || (bytes[20]! & 2) !== 0) return null;
|
|
53
|
+
const word = (at: number) => bytes[at]! + 256 * bytes[at + 1]! + 65536 * bytes[at + 2]!;
|
|
54
|
+
return { width: 1 + word(24), height: 1 + word(27) };
|
|
55
|
+
}
|
|
56
|
+
if (chunkType === 'VP8L' && chunkSize >= 5 && bytes[20] === 0x2f) {
|
|
57
|
+
const size = view.getUint32(21, true);
|
|
58
|
+
return { width: 1 + (size & 0x3fff), height: 1 + ((size >>> 14) & 0x3fff) };
|
|
59
|
+
}
|
|
60
|
+
if (chunkType === 'VP8 ' && chunkSize >= 10 && bytes[23] === 0x9d && bytes[24] === 1 && bytes[25] === 0x2a) {
|
|
61
|
+
return { width: view.getUint16(26, true) & 0x3fff, height: view.getUint16(28, true) & 0x3fff };
|
|
62
|
+
}
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export type AssetAnchor = { x?: 'left' | 'center' | 'right'; y?: 'top' | 'center' | 'bottom' };
|
|
2
|
+
|
|
3
|
+
type Size = { width: number; height: number };
|
|
4
|
+
|
|
5
|
+
/** Express the viewport's unused space on either side of the fitted artboard in artboard points. */
|
|
6
|
+
export function layoutAsset(artboard: Size, width: number, height: number) {
|
|
7
|
+
'worklet';
|
|
8
|
+
const scale = Math.min(width / artboard.width, height / artboard.height);
|
|
9
|
+
return {
|
|
10
|
+
scale,
|
|
11
|
+
insetX: Math.max(0, (width / scale - artboard.width) / 2),
|
|
12
|
+
insetY: Math.max(0, (height / scale - artboard.height) / 2),
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function anchorOffset(anchor: AssetAnchor | undefined, insetX: number, insetY: number) {
|
|
17
|
+
'worklet';
|
|
18
|
+
return {
|
|
19
|
+
x: anchor?.x === 'left' ? -insetX : anchor?.x === 'right' ? insetX : 0,
|
|
20
|
+
y: anchor?.y === 'top' ? -insetY : anchor?.y === 'bottom' ? insetY : 0,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { AssetNode, Scalar } from './asset';
|
|
2
|
+
|
|
3
|
+
export type MeshVertex = readonly [x: Scalar, y: Scalar, u: number, v: number];
|
|
4
|
+
export type MeshPin = {
|
|
5
|
+
triangle: number;
|
|
6
|
+
weights: readonly [number, number, number];
|
|
7
|
+
children?: readonly AssetNode[];
|
|
8
|
+
};
|
|
9
|
+
export type MeshNode = {
|
|
10
|
+
kind: 'mesh';
|
|
11
|
+
image: string;
|
|
12
|
+
vertices: readonly MeshVertex[];
|
|
13
|
+
indices: readonly number[];
|
|
14
|
+
opacity?: Scalar;
|
|
15
|
+
pins?: Readonly<Record<string, MeshPin>>;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
type Channels = Readonly<Record<string, number>>;
|
|
19
|
+
|
|
20
|
+
export function meshPosition([x, y]: MeshVertex, channels: Channels) {
|
|
21
|
+
'worklet';
|
|
22
|
+
return { x: typeof x === 'string' ? channels[x]! : x, y: typeof y === 'string' ? channels[y]! : y };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function meshPinPosition(mesh: Pick<MeshNode, 'vertices' | 'indices'>, pin: Pick<MeshPin, 'triangle' | 'weights'>, channels: Channels) {
|
|
26
|
+
'worklet';
|
|
27
|
+
let x = 0;
|
|
28
|
+
let y = 0;
|
|
29
|
+
const start = pin.triangle * 3;
|
|
30
|
+
for (let i = 0; i < 3; i++) {
|
|
31
|
+
const vertex = meshPosition(mesh.vertices[mesh.indices[start + i]!]!, channels);
|
|
32
|
+
const weight = pin.weights[i]!;
|
|
33
|
+
x += vertex.x * weight;
|
|
34
|
+
y += vertex.y * weight;
|
|
35
|
+
}
|
|
36
|
+
return { x, y };
|
|
37
|
+
}
|