@liqweed/motion-kit 0.0.0-stage → 0.3.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/.claude-plugin/marketplace.json +13 -0
- package/.claude-plugin/plugin.json +9 -0
- package/LICENSE +21 -0
- package/README.md +141 -2
- package/adapters/astro/AnimatedDiagram.astro +55 -0
- package/adapters/element/README.md +49 -0
- package/adapters/element/motion-diagram.mjs +123 -0
- package/adapters/page/README.md +22 -0
- package/adapters/slidev/AnimatedDiagram.vue +137 -0
- package/brand/logo.scene.mjs +115 -0
- package/cli/motion-kit.mjs +239 -0
- package/docs/spec/technique.md +465 -0
- package/examples/agent-loop.json +18 -0
- package/examples/custom-pulse.scene.mjs +141 -0
- package/examples/integrations.json +20 -0
- package/examples/order-flow.json +17 -0
- package/examples/release-pipeline.json +17 -0
- package/examples/smart-home.json +17 -0
- package/lib/build.mjs +62 -0
- package/lib/bundle.mjs +68 -0
- package/lib/check.mjs +86 -0
- package/lib/export.mjs +113 -0
- package/lib/icon.mjs +41 -0
- package/lib/load.mjs +53 -0
- package/lib/raster.mjs +69 -0
- package/lib/video.mjs +77 -0
- package/package.json +69 -5
- package/runtime/bake.mjs +61 -0
- package/runtime/motion.mjs +154 -0
- package/runtime/parts.mjs +123 -0
- package/runtime/player.mjs +255 -0
- package/runtime/scene.mjs +101 -0
- package/runtime/style.mjs +142 -0
- package/runtime/svg.mjs +165 -0
- package/runtime/types/hub-spoke.mjs +215 -0
- package/runtime/types/index.mjs +25 -0
- package/runtime/types/sequence.mjs +201 -0
- package/runtime/wrap.mjs +44 -0
- package/skills/animated-diagram/SKILL.md +90 -0
- package/skills/animated-diagram/references/custom-scenes.md +56 -0
- package/skills/animated-diagram/references/ecosystem.md +28 -0
- package/skills/animated-diagram/references/interactivity.md +51 -0
- package/skills/animated-diagram/references/motion-vocabulary.md +70 -0
- package/skills/animated-diagram/references/qc.md +41 -0
- package/skills/animated-diagram/references/scene-types.md +59 -0
- package/skills/animated-diagram/references/styles.md +55 -0
- package/skills/animated-diagram/references/targets.md +27 -0
- package/skills/animated-diagram/references/technique.md +148 -0
- package/skills/motion-render/SKILL.md +79 -0
- package/skills/motion-render/references/formats.md +18 -0
- package/skills/motion-render/references/slides.md +18 -0
- package/skills/motion-render/references/video.md +61 -0
- package/skills/project-logo/SKILL.md +72 -0
- package/skills/project-logo/references/design.md +22 -0
- package/skills/project-logo/references/discovery.md +15 -0
- package/skills/project-logo/references/external-prompt.md +23 -0
- package/styles/neutral.json +26 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "motion-kit",
|
|
3
|
+
"owner": { "name": "Ophir Radnitz" },
|
|
4
|
+
"metadata": { "description": "motion-kit: animated diagram skills" },
|
|
5
|
+
"plugins": [
|
|
6
|
+
{
|
|
7
|
+
"name": "motion-kit",
|
|
8
|
+
"source": "./",
|
|
9
|
+
"description": "animated-diagram, motion-render and project-logo skills and the motion-kit runtime/CLI",
|
|
10
|
+
"version": "0.3.0"
|
|
11
|
+
}
|
|
12
|
+
]
|
|
13
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "motion-kit",
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Deterministic animated diagrams: scenes as pure functions of time for live pages, Slidev, slide frames, baked SVG and video; plus project logos/icons.",
|
|
5
|
+
"author": { "name": "Ophir Radnitz" },
|
|
6
|
+
"repository": "https://github.com/liqweed/motion-kit",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"keywords": ["animation", "diagrams", "svg", "slides", "video", "explainer"]
|
|
9
|
+
}
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ophir Radnitz
|
|
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
CHANGED
|
@@ -1,3 +1,142 @@
|
|
|
1
|
-
|
|
1
|
+
<p align="center"><img src="assets/icon-animated.svg" width="112" alt="motion-kit logo"></p>
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
# motion-kit
|
|
4
|
+
|
|
5
|
+
Deterministic animated diagrams. A scene is a **pure function of time**,
|
|
6
|
+
`frame(t) → SVG`. The same scene serves:
|
|
7
|
+
|
|
8
|
+
- a live, interactive player (web pages, Craft Pages, Astro islands),
|
|
9
|
+
- Slidev decks (one build step per click),
|
|
10
|
+
- slide-ready step frames (SVG/PNG, light/dark, any format),
|
|
11
|
+
- a zero-JS baked animated SVG (READMEs, notes, chat previews),
|
|
12
|
+
- MP4 / WebM / GIF video in 16:9, 4:3, 1:1 and 9:16.
|
|
13
|
+
|
|
14
|
+
Open source under the [MIT licence](LICENSE). Consumers vendor the runtime (see below).
|
|
15
|
+
|
|
16
|
+
## Examples
|
|
17
|
+
|
|
18
|
+
Baked with `motion-kit bake`: zero-JS animated SVGs that play once in this
|
|
19
|
+
README and follow your GitHub theme. Regenerate with `npm run media`.
|
|
20
|
+
|
|
21
|
+
<picture>
|
|
22
|
+
<source media="(prefers-color-scheme: dark)" srcset="docs/media/order-flow-dark.svg">
|
|
23
|
+
<img src="docs/media/order-flow-light.svg" alt="order-flow: a sequence scene" width="720">
|
|
24
|
+
</picture>
|
|
25
|
+
|
|
26
|
+
<picture>
|
|
27
|
+
<source media="(prefers-color-scheme: dark)" srcset="docs/media/smart-home-dark.svg">
|
|
28
|
+
<img src="docs/media/smart-home-light.svg" alt="smart-home: a hub-and-spoke scene" width="720">
|
|
29
|
+
</picture>
|
|
30
|
+
|
|
31
|
+
The interactive player (scrubber, steps, captions) needs scripts, which
|
|
32
|
+
GitHub does not run: `node cli/motion-kit.mjs build examples/order-flow.json -o out/order.html`
|
|
33
|
+
and open the file.
|
|
34
|
+
|
|
35
|
+
## Quick start
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
npx @liqweed/motion-kit --help # or: npm i -g @liqweed/motion-kit (installs `motion-kit`)
|
|
39
|
+
npm install # optional: @resvg/resvg-js for PNG/video
|
|
40
|
+
node cli/motion-kit.mjs check examples/order-flow.json
|
|
41
|
+
node cli/motion-kit.mjs build examples/order-flow.json -o out/order.html -f wide -l nl
|
|
42
|
+
node cli/motion-kit.mjs export examples/smart-home.json -o out/slides --png --qc
|
|
43
|
+
node cli/motion-kit.mjs bake examples/order-flow.json -o out/order.svg -f wide
|
|
44
|
+
node cli/motion-kit.mjs video examples/smart-home.json -f 9:16 -o out/reel.mp4 --sheet out/sheet.png
|
|
45
|
+
node cli/motion-kit.mjs icon brand/logo.scene.mjs -o . # project icon set: assets/ + .idea/
|
|
46
|
+
npm test
|
|
47
|
+
npm run gate # typecheck, versions, tests, scene gate, media freshness
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Requirements: Node ≥ 22. PNG and video: `@resvg/resvg-js` (optional
|
|
51
|
+
dependency) or Chrome; video: `ffmpeg`.
|
|
52
|
+
|
|
53
|
+
## Layout
|
|
54
|
+
|
|
55
|
+
```
|
|
56
|
+
runtime/ dependency-free ES modules (browser + Node)
|
|
57
|
+
motion.mjs easing (exact cubic-bezier), progress, critically damped spring, retarget, split edges
|
|
58
|
+
svg.mjs escaping, rounding, el/text/fade, polyline + Bézier arc-length, drawOn
|
|
59
|
+
wrap.mjs label wrapping with hard budgets
|
|
60
|
+
style.mjs tokens, neutral style, contrast checks, token resolution
|
|
61
|
+
scene.mjs scene contract, formats, renderSvg
|
|
62
|
+
parts.mjs shared node/label drawing
|
|
63
|
+
types/ spec-driven scene types: sequence, hub-spoke
|
|
64
|
+
bake.mjs zero-JS animated SVG
|
|
65
|
+
player.mjs live player (controls, a11y, reduced motion, capture hook)
|
|
66
|
+
lib/ Node-side: check (gate), export, build (HTML inliner), raster, video, icon
|
|
67
|
+
adapters/ slidev (Vue), astro, element (custom element), page (README)
|
|
68
|
+
skills/ animated-diagram, motion-render, project-logo (Claude Code / Craft skills)
|
|
69
|
+
examples/ order-flow (sequence), smart-home (hub-spoke), custom-pulse (custom module)
|
|
70
|
+
brand/ logo scene (built with motion-kit); renders go to assets/ + .idea/ (npm run media)
|
|
71
|
+
assets/ generated project icon: icon.svg, icon.png, icon-animated.svg
|
|
72
|
+
docs/media/ baked README animations (generated: npm run media)
|
|
73
|
+
docs/ prd.md, roadmap.md, spec/technique.md
|
|
74
|
+
styles/ neutral.json (the only style shipped; brands live in their repos)
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Install the skills
|
|
78
|
+
|
|
79
|
+
- **Claude Code** (all projects):
|
|
80
|
+
`claude plugin marketplace add liqweed/motion-kit` →
|
|
81
|
+
`claude plugin install motion-kit@motion-kit`
|
|
82
|
+
- **Craft Agent:** symlink `~/.agents/skills/animated-diagram`,
|
|
83
|
+
`~/.agents/skills/motion-render` and `~/.agents/skills/project-logo` to
|
|
84
|
+
`skills/*` in this clone.
|
|
85
|
+
|
|
86
|
+
The skills look for this clone at `~/workspace/motion-kit` first (it has
|
|
87
|
+
`node_modules`), then fall back to the plugin copy.
|
|
88
|
+
|
|
89
|
+
## Use in another repo (vendor)
|
|
90
|
+
|
|
91
|
+
```bash
|
|
92
|
+
node cli/motion-kit.mjs vendor <repo>/components/motion-kit --adapter slidev # or astro / page / element
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Copies `runtime/`, the adapter, `lib/check.mjs` and `LICENSE`; writes a gate
|
|
96
|
+
script and `MOTION_KIT_VERSION`. Don't edit vendored files; change motion-kit
|
|
97
|
+
and re-vendor. Vendoring keeps consumer CI
|
|
98
|
+
self-contained (no install step, no network).
|
|
99
|
+
|
|
100
|
+
### Plain HTML and framework-independent custom element
|
|
101
|
+
|
|
102
|
+
```bash
|
|
103
|
+
node cli/motion-kit.mjs vendor public/motion-kit --adapter element
|
|
104
|
+
cp diagram.json public/diagram.json
|
|
105
|
+
cp diagram-final.svg public/diagram-final.svg # the final-frame fallback image
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
```html
|
|
109
|
+
<motion-diagram src="./diagram.json" format="wide" locale="en">
|
|
110
|
+
<img src="./diagram-final.svg" alt="The complete diagram and its explanation">
|
|
111
|
+
</motion-diagram>
|
|
112
|
+
<script type="module" src="./motion-kit/adapter/motion-diagram.mjs"></script>
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Serve the files over HTTP; no build step is needed. The supplied final-frame
|
|
116
|
+
fallback stays visible without JavaScript, while loading and on errors. Specs
|
|
117
|
+
can also be inline JSON. `theme` and JSON `style-spec` attributes configure
|
|
118
|
+
the appearance, and `element.controller` exposes the live player after the
|
|
119
|
+
`motion-ready` event. See [the element adapter](adapters/element/README.md)
|
|
120
|
+
for inline specs, styles, lifecycle and error handling.
|
|
121
|
+
|
|
122
|
+
## Rules (the gate enforces them)
|
|
123
|
+
|
|
124
|
+
Deterministic bytes · settled step frames · rounded numbers · token-only colours ·
|
|
125
|
+
escaped labels · labels fit their budget in every locale · holds the final frame ·
|
|
126
|
+
≤ ~8–10 s · no clock or randomness in scenes. See
|
|
127
|
+
`skills/animated-diagram/references/technique.md`.
|
|
128
|
+
|
|
129
|
+
## Licence and credits
|
|
130
|
+
|
|
131
|
+
MIT, see [LICENSE](LICENSE). Keep the copied `LICENSE` file and the SPDX
|
|
132
|
+
headers when redistributing vendored files.
|
|
133
|
+
|
|
134
|
+
Ideas borrowed with thanks: the contact-sheet self-check and hover-linking
|
|
135
|
+
from [visual-explainer](https://github.com/nicobailon/visual-explainer), and
|
|
136
|
+
in-SVG pause and reduced-motion handling from Dashmotion. See
|
|
137
|
+
[`skills/animated-diagram/references/ecosystem.md`](skills/animated-diagram/references/ecosystem.md)
|
|
138
|
+
for related tools and when to use them instead.
|
|
139
|
+
|
|
140
|
+
Pull requests here are reviewed with the help of
|
|
141
|
+
[CodeRabbit](https://coderabbit.ai), which supports open source projects for
|
|
142
|
+
free. Thank you, CodeRabbit, for the support.
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
---
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) 2026 Ophir Radnitz
|
|
4
|
+
/**
|
|
5
|
+
* motion-kit · Astro adapter (reference).
|
|
6
|
+
*
|
|
7
|
+
* Server-renders the FINAL frame (no-JS visitors, crawlers and reduced-motion
|
|
8
|
+
* users get the complete diagram), then the client script mounts the live
|
|
9
|
+
* player, which replays once when the diagram scrolls into view.
|
|
10
|
+
*
|
|
11
|
+
* import AnimatedDiagram from "../motion-kit/adapter/AnimatedDiagram.astro";
|
|
12
|
+
* import spec from "../diagrams/order-flow.json";
|
|
13
|
+
* <AnimatedDiagram spec={spec} format="wide" locale="nl" class="hidden lg:block" />
|
|
14
|
+
* <AnimatedDiagram spec={spec} format="tall" locale="nl" class="lg:hidden" />
|
|
15
|
+
*
|
|
16
|
+
* Mount a wide and a tall instance and switch with CSS: display:none also
|
|
17
|
+
* removes the hidden one from the accessibility tree and its autoplay observer.
|
|
18
|
+
* Pass only serializable props (a spec, not a scene object).
|
|
19
|
+
*/
|
|
20
|
+
import { renderSvg } from "../runtime/scene.mjs";
|
|
21
|
+
import { createScene } from "../runtime/types/index.mjs";
|
|
22
|
+
|
|
23
|
+
interface Props {
|
|
24
|
+
spec: Record<string, unknown>;
|
|
25
|
+
format?: string;
|
|
26
|
+
locale?: string;
|
|
27
|
+
class?: string;
|
|
28
|
+
scrubber?: boolean;
|
|
29
|
+
labels?: Record<string, string>;
|
|
30
|
+
}
|
|
31
|
+
const { spec, format = "16:9", locale = "en", class: className, scrubber = false, labels } = Astro.props;
|
|
32
|
+
const scene = createScene(spec, { format, locale });
|
|
33
|
+
const fallback = renderSvg(scene, scene.duration, { live: true, idPrefix: `${scene.id}-ssr` });
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
<div
|
|
37
|
+
class:list={["mk-astro", className]}
|
|
38
|
+
data-mk-spec={JSON.stringify(spec)}
|
|
39
|
+
data-mk-opts={JSON.stringify({ format, locale, scrubber, labels })}
|
|
40
|
+
set:html={fallback}
|
|
41
|
+
/>
|
|
42
|
+
|
|
43
|
+
<script>
|
|
44
|
+
import { mountPlayer } from "../runtime/player.mjs";
|
|
45
|
+
import { createScene } from "../runtime/types/index.mjs";
|
|
46
|
+
|
|
47
|
+
for (const el of document.querySelectorAll<HTMLElement>("[data-mk-spec]")) {
|
|
48
|
+
if (el.dataset.mkMounted) continue;
|
|
49
|
+
el.dataset.mkMounted = "1";
|
|
50
|
+
const spec = JSON.parse(el.dataset.mkSpec ?? "{}");
|
|
51
|
+
const opts = JSON.parse(el.dataset.mkOpts ?? "{}");
|
|
52
|
+
const scene = createScene(spec, { format: opts.format, locale: opts.locale });
|
|
53
|
+
mountPlayer(el, scene, { scrubber: opts.scrubber, labels: opts.labels, id: `${scene.id}-${Math.random().toString(36).slice(2, 7)}` });
|
|
54
|
+
}
|
|
55
|
+
</script>
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# Custom element (unbuilt ES module)
|
|
2
|
+
|
|
3
|
+
```bash
|
|
4
|
+
node cli/motion-kit.mjs vendor public/motion-kit --adapter element
|
|
5
|
+
node cli/motion-kit.mjs export diagram.json -o public/frames -f wide
|
|
6
|
+
cp diagram.json public/diagram.json
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
Serve `public/` over HTTP. Copy the final exported SVG path into the fallback:
|
|
10
|
+
|
|
11
|
+
```html
|
|
12
|
+
<style>motion-diagram { display: block; }</style>
|
|
13
|
+
<motion-diagram src="./diagram.json" format="wide" locale="en" theme="light">
|
|
14
|
+
<img src="./frames/06-final.svg" alt="The complete diagram and its explanation">
|
|
15
|
+
</motion-diagram>
|
|
16
|
+
<script type="module" src="./motion-kit/adapter/motion-diagram.mjs"></script>
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Use the actual final SVG filename from the export manifest. The child markup
|
|
20
|
+
is the no-JavaScript fallback; it can also be a server-rendered final-frame SVG.
|
|
21
|
+
The adapter retains it while loading, restores it on errors and disconnection,
|
|
22
|
+
and hides it only after the live player mounts. Supply meaningful alternative
|
|
23
|
+
text and size the fallback to match the live diagram to avoid layout shifts.
|
|
24
|
+
|
|
25
|
+
Instead of `src`, supply JSON in a `spec` attribute or a child
|
|
26
|
+
`<script type="application/json">…</script>`. Precedence: `spec`, child JSON,
|
|
27
|
+
then `src`. Escape JSON for its HTML context (in a script, escape `<` as
|
|
28
|
+
`\u003c`). `src` resolves relative to the page, including its `<base>` URL.
|
|
29
|
+
|
|
30
|
+
Observed attributes are `spec`, `src`, `format` (default `16:9`), `locale`
|
|
31
|
+
(default `en`), `theme` (`light`/`dark`, absent follows the OS), and
|
|
32
|
+
`style-spec` (JSON visual-style tokens, default neutral). The standard `style`
|
|
33
|
+
attribute remains CSS, for example `style="max-width: 960px"`. Attribute
|
|
34
|
+
changes rebuild the player. Inline script text changes require a remount
|
|
35
|
+
(remove and reinsert the element).
|
|
36
|
+
|
|
37
|
+
```js
|
|
38
|
+
const diagram = document.querySelector("motion-diagram");
|
|
39
|
+
diagram.addEventListener("motion-ready", ({ detail }) => detail.controller.play());
|
|
40
|
+
diagram.addEventListener("motion-error", ({ detail }) => console.error(detail.error));
|
|
41
|
+
// null until mounted; exposes seek, seekStep, playToStep, play, pause, destroy, time, scene.
|
|
42
|
+
diagram.controller?.seek(2);
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
The player follows its normal visible autoplay and reduced-motion rules.
|
|
46
|
+
Removing the element destroys its player, cancels pending loading and restores
|
|
47
|
+
the fallback; reconnecting mounts a fresh controller. Events are dispatched
|
|
48
|
+
on the element. Loading failures leave `controller` null. Scripts and JSON
|
|
49
|
+
must be served from origins allowed by the page's CSP and CORS policy.
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
// Copyright (c) 2026 Ophir Radnitz
|
|
3
|
+
/**
|
|
4
|
+
* Unbuilt custom element adapter. Vendor this directory beside runtime/:
|
|
5
|
+
* <script type="module" src="./motion-kit/adapter/motion-diagram.mjs"></script>
|
|
6
|
+
* <motion-diagram src="./diagram.json"><img src="./final.svg" alt="…"></motion-diagram>
|
|
7
|
+
*
|
|
8
|
+
* The light-DOM fallback stays visible until a player mounts successfully.
|
|
9
|
+
* `style-spec` is JSON token data; the standard HTML `style` attribute stays CSS.
|
|
10
|
+
*/
|
|
11
|
+
import { mountPlayer } from "../runtime/player.mjs";
|
|
12
|
+
import { createScene } from "../runtime/types/index.mjs";
|
|
13
|
+
|
|
14
|
+
let nextId = 0;
|
|
15
|
+
|
|
16
|
+
export class MotionDiagram extends HTMLElement {
|
|
17
|
+
static get observedAttributes() {
|
|
18
|
+
return ["spec", "src", "format", "locale", "theme", "style-spec"];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
constructor() {
|
|
22
|
+
super();
|
|
23
|
+
/** @type {ReturnType<typeof mountPlayer> | null} */
|
|
24
|
+
this.controller = null;
|
|
25
|
+
this._id = `mk-element-${++nextId}`;
|
|
26
|
+
this._revision = 0;
|
|
27
|
+
this._queued = false;
|
|
28
|
+
/** @type {AbortController | null} */
|
|
29
|
+
this._request = null;
|
|
30
|
+
/** @type {HTMLDivElement | null} */
|
|
31
|
+
this._fallback = null;
|
|
32
|
+
/** @type {HTMLDivElement | null} */
|
|
33
|
+
this._player = null;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
connectedCallback() {
|
|
37
|
+
this._schedule();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
disconnectedCallback() {
|
|
41
|
+
this._revision++;
|
|
42
|
+
this._reset();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
attributeChangedCallback() {
|
|
46
|
+
// Invalidate before the queued remount: a resolved fetch may already
|
|
47
|
+
// have its continuation ahead of that microtask.
|
|
48
|
+
this._revision++;
|
|
49
|
+
this._request?.abort();
|
|
50
|
+
this._schedule();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
_schedule() {
|
|
54
|
+
if (this._queued) return;
|
|
55
|
+
this._queued = true;
|
|
56
|
+
queueMicrotask(() => {
|
|
57
|
+
this._queued = false;
|
|
58
|
+
if (this.isConnected) void this._mount();
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
_reset() {
|
|
63
|
+
this._request?.abort();
|
|
64
|
+
this._request = null;
|
|
65
|
+
this.controller?.destroy();
|
|
66
|
+
this.controller = null;
|
|
67
|
+
this._player?.remove();
|
|
68
|
+
this._player = null;
|
|
69
|
+
if (this._fallback) this._fallback.hidden = false;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async _mount() {
|
|
73
|
+
const revision = ++this._revision;
|
|
74
|
+
this._reset();
|
|
75
|
+
if (!this._fallback) {
|
|
76
|
+
const fallback = this.ownerDocument.createElement("div");
|
|
77
|
+
fallback.setAttribute("data-mk-fallback", "");
|
|
78
|
+
for (const child of [...this.childNodes]) {
|
|
79
|
+
// Keep inline JSON available for attribute-driven remounts.
|
|
80
|
+
if (child instanceof Element && child.matches('script[type="application/json"]')) continue;
|
|
81
|
+
fallback.appendChild(child);
|
|
82
|
+
}
|
|
83
|
+
this.appendChild(fallback);
|
|
84
|
+
this._fallback = fallback;
|
|
85
|
+
}
|
|
86
|
+
const request = new AbortController();
|
|
87
|
+
this._request = request;
|
|
88
|
+
/** @type {HTMLDivElement | null} */
|
|
89
|
+
let player = null;
|
|
90
|
+
try {
|
|
91
|
+
const inline = this.getAttribute("spec") ?? this.querySelector('script[type="application/json"]')?.textContent;
|
|
92
|
+
const src = this.getAttribute("src");
|
|
93
|
+
let spec;
|
|
94
|
+
if (inline != null) spec = JSON.parse(inline);
|
|
95
|
+
else if (src) {
|
|
96
|
+
const response = await fetch(new URL(src, this.baseURI), { signal: request.signal });
|
|
97
|
+
if (!response.ok) throw new Error(`motion-diagram: spec request failed (${response.status})`);
|
|
98
|
+
spec = await response.json();
|
|
99
|
+
} else throw new Error("motion-diagram: provide spec, inline application/json, or src");
|
|
100
|
+
if (!this.isConnected || revision !== this._revision) return;
|
|
101
|
+
const theme = this.getAttribute("theme");
|
|
102
|
+
if (theme && theme !== "light" && theme !== "dark") throw new Error('motion-diagram: theme must be "light" or "dark"');
|
|
103
|
+
const scene = createScene(spec, { format: this.getAttribute("format") ?? "16:9", locale: this.getAttribute("locale") ?? "en" });
|
|
104
|
+
const style = JSON.parse(this.getAttribute("style-spec") ?? "null");
|
|
105
|
+
player = this.ownerDocument.createElement("div");
|
|
106
|
+
// Mount inside a child so player CSS cannot overwrite the host's CSS.
|
|
107
|
+
this.appendChild(player);
|
|
108
|
+
this.controller = mountPlayer(player, scene, { id: this._id, style, theme: theme || undefined });
|
|
109
|
+
this._player = player;
|
|
110
|
+
this._fallback.hidden = true;
|
|
111
|
+
this.dispatchEvent(new CustomEvent("motion-ready", { detail: { controller: this.controller } }));
|
|
112
|
+
} catch (error) {
|
|
113
|
+
player?.remove();
|
|
114
|
+
if (this.isConnected && revision === this._revision) {
|
|
115
|
+
this.dispatchEvent(new CustomEvent("motion-error", { detail: { error } }));
|
|
116
|
+
}
|
|
117
|
+
} finally {
|
|
118
|
+
if (this._request === request) this._request = null;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (!customElements.get("motion-diagram")) customElements.define("motion-diagram", MotionDiagram);
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# Page adapter (self-contained HTML)
|
|
2
|
+
|
|
3
|
+
No component to copy: `motion-kit build` inlines the runtime and the player
|
|
4
|
+
into one HTML document with no network requests.
|
|
5
|
+
|
|
6
|
+
```bash
|
|
7
|
+
motion-kit build diagram.json --out diagram.html [--format wide] [--locale nl] [--style brand.json]
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
- **Craft Pages:** pass the file's content to `create_page` / `update_page`.
|
|
11
|
+
Pages block all network egress (`connect-src 'none'`), and this output makes
|
|
12
|
+
no requests.
|
|
13
|
+
- **Craft in-chat `html-preview`:** scripts are blocked there. Use
|
|
14
|
+
`motion-kit bake` (zero-JS animated SVG) or step PNGs via `image-preview`.
|
|
15
|
+
- **Static hosting / file://:** works as is.
|
|
16
|
+
|
|
17
|
+
The final frame is pre-rendered into the markup, so the diagram is complete
|
|
18
|
+
before JS runs. The player replays once on scroll-into-view, shows pause/play,
|
|
19
|
+
step buttons, a scrubber and captions, and respects `prefers-reduced-motion`
|
|
20
|
+
and `prefers-color-scheme`.
|
|
21
|
+
|
|
22
|
+
Capture hook for renderers: `window.__motionKit[sceneId].seek(t)`.
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
SPDX-License-Identifier: MIT
|
|
3
|
+
Copyright (c) 2026 Ophir Radnitz
|
|
4
|
+
|
|
5
|
+
motion-kit · Slidev adapter (verified against Slidev 53).
|
|
6
|
+
|
|
7
|
+
Each slide click advances one build step. The component registers its own
|
|
8
|
+
clicks with the slide (the same API Slidev's ShikiMagicMove uses), so no
|
|
9
|
+
`clicks:` frontmatter is needed and it composes with v-click elements.
|
|
10
|
+
|
|
11
|
+
- Step 1 shows on arrival (its entrance plays once); clicks 1…N−1 advance.
|
|
12
|
+
- Forward by one click: animates from the previous step's time to the next.
|
|
13
|
+
Back or jump: seeks with no animation.
|
|
14
|
+
- Overview, presenter next-preview, print/PDF/PNG export (`isPrintMode`) and
|
|
15
|
+
prefers-reduced-motion: always the settled step frame, so
|
|
16
|
+
`slidev export --with-clicks` writes one clean frame per click.
|
|
17
|
+
- Light/dark follows Slidev's dark mode unless `theme` is set.
|
|
18
|
+
|
|
19
|
+
Usage in a slide:
|
|
20
|
+
|
|
21
|
+
<script setup>
|
|
22
|
+
import spec from './diagrams/order-flow.json'
|
|
23
|
+
</script>
|
|
24
|
+
|
|
25
|
+
<AnimatedDiagram :spec="spec" format="16:9" locale="en" />
|
|
26
|
+
|
|
27
|
+
Props:
|
|
28
|
+
spec scene spec (JSON) for a registered scene type, or
|
|
29
|
+
scene a ready scene object (custom scene module output)
|
|
30
|
+
format "16:9" | "4:3" | "1:1" | "9:16" | "wide" | "tall" (default "16:9")
|
|
31
|
+
locale string (default "en")
|
|
32
|
+
at Slidev click position of the first advance (default "+1")
|
|
33
|
+
intro play the entrance up to step 1 when the slide opens (default true)
|
|
34
|
+
theme "light" | "dark" (default: follow Slidev dark mode)
|
|
35
|
+
styleSpec style object (tokens); default neutral
|
|
36
|
+
maxHeight CSS length; default "380px" fits under a slide title on Slidev's
|
|
37
|
+
980×552 canvas. Use "100%" (with a full-height parent) for
|
|
38
|
+
full-bleed diagram slides.
|
|
39
|
+
-->
|
|
40
|
+
<script setup>
|
|
41
|
+
import { useDarkMode, useNav, useSlideContext } from "@slidev/client";
|
|
42
|
+
import { computed, onMounted, onUnmounted, ref, unref, watch } from "vue";
|
|
43
|
+
import { renderSvg } from "../runtime/scene.mjs";
|
|
44
|
+
import { cssVars, normalizeStyle } from "../runtime/style.mjs";
|
|
45
|
+
import { createScene } from "../runtime/types/index.mjs";
|
|
46
|
+
|
|
47
|
+
const props = defineProps({
|
|
48
|
+
spec: { type: Object, default: null },
|
|
49
|
+
scene: { type: Object, default: null },
|
|
50
|
+
format: { type: String, default: "16:9" },
|
|
51
|
+
locale: { type: String, default: "en" },
|
|
52
|
+
at: { type: [String, Number], default: "+1" },
|
|
53
|
+
intro: { type: Boolean, default: true },
|
|
54
|
+
theme: { type: String, default: undefined },
|
|
55
|
+
styleSpec: { type: Object, default: null },
|
|
56
|
+
maxHeight: { type: String, default: "380px" },
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
const { $clicksContext: clicks, $renderContext } = useSlideContext();
|
|
60
|
+
const { isPrintMode } = useNav();
|
|
61
|
+
const { isDark } = useDarkMode();
|
|
62
|
+
|
|
63
|
+
const scene = computed(() => props.scene ?? createScene(props.spec, { format: props.format, locale: props.locale }));
|
|
64
|
+
const style = computed(() => normalizeStyle(props.styleSpec ?? "neutral"));
|
|
65
|
+
const n = scene.value.steps.length;
|
|
66
|
+
const id = `mk-${scene.value.id}-${Math.random().toString(36).slice(2, 8)}`;
|
|
67
|
+
const reduced = typeof matchMedia === "function" && matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
68
|
+
const animated = computed(() => ["slide", "presenter"].includes(unref($renderContext)) && !isPrintMode.value && !reduced);
|
|
69
|
+
|
|
70
|
+
// Register N−1 clicks (step 1 is shown on arrival).
|
|
71
|
+
const info = clicks && n > 1 ? clicks.calculateSince(props.at, n - 1) : null;
|
|
72
|
+
const stepIndex = computed(() => {
|
|
73
|
+
if (!info || !clicks) return n - 1;
|
|
74
|
+
return Math.min(Math.max(clicks.current - info.start + 1, 0), n - 1);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
const t = ref(scene.value.steps[stepIndex.value].at);
|
|
78
|
+
let raf = 0;
|
|
79
|
+
|
|
80
|
+
function animate(from, to) {
|
|
81
|
+
cancelAnimationFrame(raf);
|
|
82
|
+
if (!animated.value || to <= from) {
|
|
83
|
+
t.value = to;
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
const start = performance.now();
|
|
87
|
+
const tick = (now) => {
|
|
88
|
+
const v = Math.min(from + (now - start) / 1000, to);
|
|
89
|
+
t.value = v;
|
|
90
|
+
if (v < to) raf = requestAnimationFrame(tick);
|
|
91
|
+
};
|
|
92
|
+
t.value = from;
|
|
93
|
+
raf = requestAnimationFrame(tick);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
watch(stepIndex, (next, prev) => {
|
|
97
|
+
const steps = scene.value.steps;
|
|
98
|
+
if (next === prev + 1) animate(steps[prev].at, steps[next].at);
|
|
99
|
+
else {
|
|
100
|
+
cancelAnimationFrame(raf);
|
|
101
|
+
t.value = steps[next].at;
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
onMounted(() => {
|
|
106
|
+
if (info) clicks.register(id, info);
|
|
107
|
+
if (animated.value && props.intro && stepIndex.value === 0) animate(0, scene.value.steps[0].at);
|
|
108
|
+
});
|
|
109
|
+
onUnmounted(() => {
|
|
110
|
+
cancelAnimationFrame(raf);
|
|
111
|
+
clicks?.unregister(id);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
const dark = computed(() => (props.theme ? props.theme === "dark" : isDark.value));
|
|
115
|
+
const markup = computed(() => renderSvg(scene.value, t.value, { style: style.value, live: true, idPrefix: id }));
|
|
116
|
+
const vars = computed(() => `${cssVars(style.value, dark.value ? "dark" : "light")};--mk-max-h:${props.maxHeight}`);
|
|
117
|
+
</script>
|
|
118
|
+
|
|
119
|
+
<template>
|
|
120
|
+
<figure class="mk-slidev" :style="vars" :data-mk-step="stepIndex + 1">
|
|
121
|
+
<div class="mk-slidev-stage" v-html="markup" />
|
|
122
|
+
</figure>
|
|
123
|
+
</template>
|
|
124
|
+
|
|
125
|
+
<style scoped>
|
|
126
|
+
.mk-slidev {
|
|
127
|
+
margin: 0;
|
|
128
|
+
width: 100%;
|
|
129
|
+
}
|
|
130
|
+
.mk-slidev-stage :deep(svg) {
|
|
131
|
+
display: block;
|
|
132
|
+
width: 100%;
|
|
133
|
+
height: auto;
|
|
134
|
+
max-height: var(--mk-max-h, 380px);
|
|
135
|
+
margin: 0 auto;
|
|
136
|
+
}
|
|
137
|
+
</style>
|