@zanim/web 0.1.0-beta.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/CHANGELOG.md +14 -0
- package/LICENSE +21 -0
- package/README.md +95 -0
- package/dist/zanim_web_core.wasm +0 -0
- package/package.json +76 -0
- package/src/compositing.js +136 -0
- package/src/core.js +652 -0
- package/src/evaluator.js +73 -0
- package/src/ir.d.ts +26 -0
- package/src/ir.js +250 -0
- package/src/media.js +289 -0
- package/src/player.js +99 -0
- package/src/scene.js +633 -0
- package/src/svg.js +96 -0
- package/src/three.js +129 -0
- package/src/typst.js +39 -0
- package/src/zanim.d.ts +223 -0
- package/src/zanim.js +97 -0
- package/vite.d.ts +4 -0
- package/vite.js +82 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.0-beta.1 - 2026-09-20
|
|
4
|
+
|
|
5
|
+
First public beta of `@zanim/web`.
|
|
6
|
+
|
|
7
|
+
- Absolute-time, random-access Scene authoring and playback.
|
|
8
|
+
- Canvas2D renderer with Zig/WASM kernels and shared WASM loading.
|
|
9
|
+
- 2D/3D objects, vector documents, media, compositing, and Scene IR.
|
|
10
|
+
- Build-time Typst/Math compilation through the Vite plugin.
|
|
11
|
+
- Dynamic vector morphing and retained batch caches.
|
|
12
|
+
- Correct multi-scene destruction and playback lifecycle for embedded presentations.
|
|
13
|
+
|
|
14
|
+
The package is pre-1.0; API changes may occur in later 0.x releases.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Zanim contributors
|
|
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/README.md
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# @zanim/web
|
|
2
|
+
|
|
3
|
+
Browser runtime and TypeScript authoring API for Zanim.
|
|
4
|
+
|
|
5
|
+
> **Release status:** `0.1.0-beta.1` is a pre-1.0 public beta. Random-access scene semantics are stable enough for real projects, but API names may still evolve in 0.x releases.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @zanim/web@0.1.0-beta.1
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
For build-time `Math` / `Typst` authoring, also install Vite 5+ and enable `@zanim/web/vite`. Vite is an optional peer dependency; runtime-only consumers do not need it.
|
|
14
|
+
|
|
15
|
+
## Runtime
|
|
16
|
+
|
|
17
|
+
- `src/core.js`: retained 2D objects, Canvas2D rendering and WASM bridge.
|
|
18
|
+
- `src/scene.js`: authoring, timeline validation and scheduling.
|
|
19
|
+
- `src/evaluator.js`: absolute-time evaluation.
|
|
20
|
+
- `src/player.js`: playback.
|
|
21
|
+
- `src/ir.js`: Scene IR import/export.
|
|
22
|
+
- `src/media.js`: image/GIF/video/audio objects.
|
|
23
|
+
- `src/three.js`: mesh/camera API and Web 3D layer.
|
|
24
|
+
- `src/typst.js`: loads precompiled SVG/VectorDocument resources; it never embeds a Typst compiler.
|
|
25
|
+
- `src/svg.js`: internal SVG-to-cubic-vector importer used by Typst.
|
|
26
|
+
|
|
27
|
+
The repository no longer contains a Gallery or browser demo suite. User-facing examples are maintained in the separate tutorial repository.
|
|
28
|
+
|
|
29
|
+
## State ownership
|
|
30
|
+
|
|
31
|
+
Objects are ordinary declaration/layout values until `Scene.add()`. After registration, animation authoring updates `Scene.authored` and timeline clips; it does not rewrite the object's declaration state. `stateAt(object, t)` reconstructs historical state, while `authoredState(object)` exposes the current authoring head. Rendering temporarily applies sampled state and restores the raw object immediately afterward.
|
|
32
|
+
|
|
33
|
+
## Math / Typst
|
|
34
|
+
|
|
35
|
+
Use the Vite plugin once:
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
// vite.config.ts
|
|
39
|
+
import { zanim } from '@zanim/web/vite';
|
|
40
|
+
export default { plugins: [zanim()] };
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Then author normally:
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
import { Math } from '@zanim/web';
|
|
47
|
+
const formula = new Math('integral_0^1 x^2 dif x = 1/3', { fontSize: 34 });
|
|
48
|
+
await formula.ready;
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Build flow:
|
|
52
|
+
|
|
53
|
+
```text
|
|
54
|
+
new Math(static source)
|
|
55
|
+
→ @zanim/web/vite
|
|
56
|
+
→ local Typst CLI
|
|
57
|
+
→ content-addressed SVG
|
|
58
|
+
→ Vite production asset
|
|
59
|
+
→ browser fetch
|
|
60
|
+
→ VectorDocument
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
The npm runtime contains no Typst compiler/WASM and makes no font/CDN requests. Typst is required only on the developer/build machine. Resolution order is plugin `typst`, `ZANIM_TYPST`, project `.tools/typst/typst[.exe]`, then `PATH`.
|
|
64
|
+
|
|
65
|
+
Formula source, `fontSize` and `color` are intentionally build-time static. Use `DynamicNumber` / `FormulaTemplate` for runtime-changing values. A dynamic formula source is a build error rather than a reason to ship a browser compiler.
|
|
66
|
+
|
|
67
|
+
`configureTypstCompiler(...)` remains available for explicit development integrations. Python Preview uses it to point Web Preview at Python's `/api/typst`; production apps normally use the Vite-precompiled SVG path.
|
|
68
|
+
|
|
69
|
+
## Dynamic vector morphs
|
|
70
|
+
|
|
71
|
+
`prepareVectorMorph(source, target)` builds stable glyph/path correspondence between two `VectorDocument`s. `DynamicVectorObject2D` can then sample that plan from absolute time, so Typst text and other vector artwork can morph while remaining random-access rather than accumulating updater state. Matched groups interpolate cubic geometry directly; inserted/removed groups grow or shrink locally with opacity.
|
|
72
|
+
|
|
73
|
+
## Multi-scene embedding
|
|
74
|
+
|
|
75
|
+
`ZanimWasm.load(url)` is promise-cached per resolved URL. Pages that mount many independent `Scene` instances (for example a slide deck with several embedded Zanim views) therefore share one WebAssembly engine instead of refetching and reinstantiating the runtime for every canvas. Failed loads are evicted so a later mount can retry.
|
|
76
|
+
|
|
77
|
+
## Build
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
cd web
|
|
81
|
+
npm install
|
|
82
|
+
./build.sh
|
|
83
|
+
npm test
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
`build.sh` creates only `dist/zanim_web_core.wasm`. Typst SVGs belong to each consuming application and are emitted by `@zanim/web/vite` during that application's build.
|
|
87
|
+
|
|
88
|
+
## Scene IR
|
|
89
|
+
|
|
90
|
+
```ts
|
|
91
|
+
import { Scene } from '@zanim/web';
|
|
92
|
+
import { createSceneFromIR, sceneToIR } from '@zanim/web/ir';
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Scene IR is semantic and random-access; it is not a frame dump. Runtime callbacks require explicit sampling when exported.
|
|
Binary file
|
package/package.json
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@zanim/web",
|
|
3
|
+
"version": "0.1.0-beta.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"sideEffects": false,
|
|
6
|
+
"main": "./src/zanim.js",
|
|
7
|
+
"types": "./src/zanim.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./src/zanim.d.ts",
|
|
11
|
+
"import": "./src/zanim.js"
|
|
12
|
+
},
|
|
13
|
+
"./zanim_web_core.wasm": "./dist/zanim_web_core.wasm",
|
|
14
|
+
"./ir": {
|
|
15
|
+
"types": "./src/ir.d.ts",
|
|
16
|
+
"import": "./src/ir.js"
|
|
17
|
+
},
|
|
18
|
+
"./vite": {
|
|
19
|
+
"types": "./vite.d.ts",
|
|
20
|
+
"import": "./vite.js"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"src",
|
|
25
|
+
"dist",
|
|
26
|
+
"vite.js",
|
|
27
|
+
"vite.d.ts",
|
|
28
|
+
"README.md",
|
|
29
|
+
"LICENSE",
|
|
30
|
+
"CHANGELOG.md"
|
|
31
|
+
],
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "./build.sh",
|
|
34
|
+
"test": "node ./test.mjs && node ./rotation.test.mjs && node ./primitive-draw.test.mjs && node ./vite.test.mjs"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@babel/parser": "^7.29.9",
|
|
38
|
+
"magic-string": "^0.30.17"
|
|
39
|
+
},
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"vite": ">=5"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"vite": "^6.4.3"
|
|
45
|
+
},
|
|
46
|
+
"description": "Random-access browser animation runtime and TypeScript authoring API for Zanim",
|
|
47
|
+
"license": "MIT",
|
|
48
|
+
"repository": {
|
|
49
|
+
"type": "git",
|
|
50
|
+
"url": "git+https://github.com/zjwqsd/zanim.git",
|
|
51
|
+
"directory": "web"
|
|
52
|
+
},
|
|
53
|
+
"homepage": "https://github.com/zjwqsd/zanim#readme",
|
|
54
|
+
"bugs": {
|
|
55
|
+
"url": "https://github.com/zjwqsd/zanim/issues"
|
|
56
|
+
},
|
|
57
|
+
"keywords": [
|
|
58
|
+
"animation",
|
|
59
|
+
"graphics",
|
|
60
|
+
"canvas",
|
|
61
|
+
"wasm",
|
|
62
|
+
"typst",
|
|
63
|
+
"visualization"
|
|
64
|
+
],
|
|
65
|
+
"engines": {
|
|
66
|
+
"node": ">=18"
|
|
67
|
+
},
|
|
68
|
+
"publishConfig": {
|
|
69
|
+
"access": "public"
|
|
70
|
+
},
|
|
71
|
+
"peerDependenciesMeta": {
|
|
72
|
+
"vite": {
|
|
73
|
+
"optional": true
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Bounds2D,
|
|
3
|
+
Transform2D,
|
|
4
|
+
ZObject,
|
|
5
|
+
sampleValue,
|
|
6
|
+
setWorldCanvasTransform,
|
|
7
|
+
} from './core.js';
|
|
8
|
+
|
|
9
|
+
function finitePositive(value, name) {
|
|
10
|
+
const number = Number(value);
|
|
11
|
+
if (!(number > 0) || !Number.isFinite(number)) {
|
|
12
|
+
throw new RangeError(`${name} must be a positive finite number`);
|
|
13
|
+
}
|
|
14
|
+
return number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function sceneCanvas(scene) {
|
|
18
|
+
const canvas = scene?.renderer?.canvas;
|
|
19
|
+
if (!canvas || typeof canvas.width !== 'number' || typeof canvas.height !== 'number') {
|
|
20
|
+
throw new TypeError('SceneRasterObject2D requires a renderable child Scene');
|
|
21
|
+
}
|
|
22
|
+
return canvas;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function transformedBounds(width, height, transform) {
|
|
26
|
+
const hx = width / 2;
|
|
27
|
+
const hy = height / 2;
|
|
28
|
+
const points = [
|
|
29
|
+
transform.apply(-hx, -hy),
|
|
30
|
+
transform.apply(hx, -hy),
|
|
31
|
+
transform.apply(hx, hy),
|
|
32
|
+
transform.apply(-hx, hy),
|
|
33
|
+
];
|
|
34
|
+
return new Bounds2D(
|
|
35
|
+
Math.min(...points.map(point => point[0])),
|
|
36
|
+
Math.min(...points.map(point => point[1])),
|
|
37
|
+
Math.max(...points.map(point => point[0])),
|
|
38
|
+
Math.max(...points.map(point => point[1])),
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Draw another Zanim Scene as one transformable object.
|
|
44
|
+
*
|
|
45
|
+
* The child Scene keeps its own renderer/canvas and timeline. The parent only
|
|
46
|
+
* samples that surface, so embedding never flattens the child into a low-res
|
|
47
|
+
* screenshot and the child can later be promoted/reused without recompiling.
|
|
48
|
+
*/
|
|
49
|
+
export class SceneRasterObject2D extends ZObject {
|
|
50
|
+
constructor(
|
|
51
|
+
scene,
|
|
52
|
+
{
|
|
53
|
+
width = null,
|
|
54
|
+
height = null,
|
|
55
|
+
sourceTime = 0,
|
|
56
|
+
ownsScene = false,
|
|
57
|
+
...rest
|
|
58
|
+
} = {},
|
|
59
|
+
) {
|
|
60
|
+
super(rest);
|
|
61
|
+
const canvas = sceneCanvas(scene);
|
|
62
|
+
const sourceWidth = Math.max(1, Number(canvas.width) || 1);
|
|
63
|
+
const sourceHeight = Math.max(1, Number(canvas.height) || 1);
|
|
64
|
+
const aspect = sourceWidth / sourceHeight;
|
|
65
|
+
|
|
66
|
+
if (width == null && height == null) {
|
|
67
|
+
width = sourceWidth / 100;
|
|
68
|
+
height = sourceHeight / 100;
|
|
69
|
+
} else if (width == null) {
|
|
70
|
+
height = finitePositive(height, 'height');
|
|
71
|
+
width = height * aspect;
|
|
72
|
+
} else if (height == null) {
|
|
73
|
+
width = finitePositive(width, 'width');
|
|
74
|
+
height = width / aspect;
|
|
75
|
+
} else {
|
|
76
|
+
width = finitePositive(width, 'width');
|
|
77
|
+
height = finitePositive(height, 'height');
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
this.scene = scene;
|
|
81
|
+
this.width = width;
|
|
82
|
+
this.height = height;
|
|
83
|
+
this.sourceTime = sourceTime;
|
|
84
|
+
this.ownsScene = Boolean(ownsScene);
|
|
85
|
+
this._lastSample = null;
|
|
86
|
+
this._webRuntimeOnly = 'scene-raster';
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
_boundsWithTransform(transform) {
|
|
90
|
+
return transformedBounds(this.width, this.height, transform);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
bounds() {
|
|
94
|
+
return this._boundsWithTransform(this.transform);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
sample(time) {
|
|
98
|
+
const target = Math.max(0, sampleValue(this.sourceTime, time));
|
|
99
|
+
const duration = Number(this.scene.duration) || 0;
|
|
100
|
+
const resolved = duration > 0 ? Math.min(target, duration) : target;
|
|
101
|
+
if (this._lastSample == null || Math.abs(this._lastSample - resolved) > 1e-12) {
|
|
102
|
+
this.scene.seek(resolved);
|
|
103
|
+
this._lastSample = resolved;
|
|
104
|
+
} else if (!this.scene.stats?.frames) {
|
|
105
|
+
this.scene.render();
|
|
106
|
+
}
|
|
107
|
+
return this;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
draw(renderer, parent = Transform2D.identity()) {
|
|
111
|
+
const childCanvas = sceneCanvas(this.scene);
|
|
112
|
+
if (childCanvas === renderer.canvas) {
|
|
113
|
+
throw new Error('SceneRasterObject2D cannot sample from its parent render target');
|
|
114
|
+
}
|
|
115
|
+
this.sample(renderer.time ?? 0);
|
|
116
|
+
|
|
117
|
+
const ctx = renderer.ctx;
|
|
118
|
+
const transform = this.world(parent);
|
|
119
|
+
ctx.save();
|
|
120
|
+
ctx.globalAlpha *= Math.max(0, Math.min(1, this.opacity));
|
|
121
|
+
setWorldCanvasTransform(renderer, ctx, transform);
|
|
122
|
+
ctx.scale(1, -1);
|
|
123
|
+
ctx.drawImage(
|
|
124
|
+
childCanvas,
|
|
125
|
+
-this.width / 2,
|
|
126
|
+
-this.height / 2,
|
|
127
|
+
this.width,
|
|
128
|
+
this.height,
|
|
129
|
+
);
|
|
130
|
+
ctx.restore();
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
destroy() {
|
|
134
|
+
if (this.ownsScene) this.scene.destroy?.();
|
|
135
|
+
}
|
|
136
|
+
}
|