motionary 10.6.0 → 10.8.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.
Files changed (153) hide show
  1. package/AGENTS.md +2 -0
  2. package/CHANGELOG.md +36 -0
  3. package/README.md +77 -4
  4. package/bin/motionary-mcp.mjs +160 -11
  5. package/dist/chunks/{anim-image-DECLkPH4.cjs → anim-image-BzTQa2k3.cjs} +2 -2
  6. package/dist/chunks/{anim-image-DECLkPH4.cjs.map → anim-image-BzTQa2k3.cjs.map} +1 -1
  7. package/dist/chunks/{anim-image-DNghy017.js → anim-image-iD646Q7i.js} +2 -2
  8. package/dist/chunks/{anim-image-DNghy017.js.map → anim-image-iD646Q7i.js.map} +1 -1
  9. package/dist/chunks/{keyframes-F6vp6tZZ.js → keyframes-B4zefz0k.js} +2 -2
  10. package/dist/chunks/{keyframes-F6vp6tZZ.js.map → keyframes-B4zefz0k.js.map} +1 -1
  11. package/dist/chunks/{keyframes-CbUZjD_5.cjs → keyframes-BlOyB1PP.cjs} +2 -2
  12. package/dist/chunks/{keyframes-CbUZjD_5.cjs.map → keyframes-BlOyB1PP.cjs.map} +1 -1
  13. package/dist/chunks/{registry-dr1CPTk1.cjs → registry-BZxDVL3h.cjs} +2 -2
  14. package/dist/chunks/{registry-dr1CPTk1.cjs.map → registry-BZxDVL3h.cjs.map} +1 -1
  15. package/dist/chunks/{registry-D8LAoXI-.js → registry-Bkt3-3ET.js} +3 -3
  16. package/dist/chunks/{registry-D8LAoXI-.js.map → registry-Bkt3-3ET.js.map} +1 -1
  17. package/dist/chunks/runtime-link-CUiZ8lbE.cjs +38 -0
  18. package/dist/chunks/runtime-link-CUiZ8lbE.cjs.map +1 -0
  19. package/dist/chunks/runtime-link-c13gskBZ.js +36 -0
  20. package/dist/chunks/runtime-link-c13gskBZ.js.map +1 -0
  21. package/dist/chunks/{ticker-DaAlEyOd.cjs → ticker-BfxAECfO.cjs} +2 -2
  22. package/dist/chunks/{ticker-DaAlEyOd.cjs.map → ticker-BfxAECfO.cjs.map} +1 -1
  23. package/dist/chunks/{ticker-Do9bGsaV.js → ticker-ZtweD3U4.js} +2 -2
  24. package/dist/chunks/{ticker-Do9bGsaV.js.map → ticker-ZtweD3U4.js.map} +1 -1
  25. package/dist/chunks/{tween-CmkdSyaC.cjs → tween--MD-39y0.cjs} +2 -2
  26. package/dist/chunks/{tween-CmkdSyaC.cjs.map → tween--MD-39y0.cjs.map} +1 -1
  27. package/dist/chunks/{tween-BzCGq_Ba.js → tween-DACdkLyO.js} +2 -2
  28. package/dist/chunks/{tween-BzCGq_Ba.js.map → tween-DACdkLyO.js.map} +1 -1
  29. package/dist/components/ai.cjs +314 -0
  30. package/dist/components/ai.cjs.map +1 -0
  31. package/dist/components/ai.d.cts +58 -0
  32. package/dist/components/ai.d.ts +58 -0
  33. package/dist/components/ai.js +311 -0
  34. package/dist/components/ai.js.map +1 -0
  35. package/dist/components/snap-carousel.cjs +216 -0
  36. package/dist/components/snap-carousel.cjs.map +1 -0
  37. package/dist/components/snap-carousel.d.cts +80 -0
  38. package/dist/components/snap-carousel.d.ts +80 -0
  39. package/dist/components/snap-carousel.js +214 -0
  40. package/dist/components/snap-carousel.js.map +1 -0
  41. package/dist/components/widgets.cjs +626 -268
  42. package/dist/components/widgets.cjs.map +1 -1
  43. package/dist/components/widgets.d.cts +515 -39
  44. package/dist/components/widgets.d.ts +515 -39
  45. package/dist/components/widgets.js +617 -262
  46. package/dist/components/widgets.js.map +1 -1
  47. package/dist/llms-full.txt +191 -6
  48. package/dist/llms.txt +10 -3
  49. package/dist/manifest.json +497 -19
  50. package/dist/runtime/drag-snap.cjs +269 -0
  51. package/dist/runtime/drag-snap.cjs.map +1 -0
  52. package/dist/runtime/drag-snap.d.cts +109 -0
  53. package/dist/runtime/drag-snap.d.ts +109 -0
  54. package/dist/runtime/drag-snap.iife.js +2 -0
  55. package/dist/runtime/drag-snap.iife.js.map +1 -0
  56. package/dist/runtime/drag-snap.js +261 -0
  57. package/dist/runtime/drag-snap.js.map +1 -0
  58. package/dist/runtime/format-apng.cjs +4 -4
  59. package/dist/runtime/format-apng.js +4 -4
  60. package/dist/runtime/format-css.cjs +4 -4
  61. package/dist/runtime/format-css.js +4 -4
  62. package/dist/runtime/format-gif.cjs +4 -4
  63. package/dist/runtime/format-gif.js +4 -4
  64. package/dist/runtime/format-gltf.cjs +37 -18
  65. package/dist/runtime/format-gltf.cjs.map +1 -1
  66. package/dist/runtime/format-gltf.d.cts +18 -2
  67. package/dist/runtime/format-gltf.d.ts +18 -2
  68. package/dist/runtime/format-gltf.iife.js +1 -1
  69. package/dist/runtime/format-gltf.iife.js.map +1 -1
  70. package/dist/runtime/format-gltf.js +37 -18
  71. package/dist/runtime/format-gltf.js.map +1 -1
  72. package/dist/runtime/format-motion.cjs +4 -4
  73. package/dist/runtime/format-motion.js +4 -4
  74. package/dist/runtime/format-obj.cjs +1 -1
  75. package/dist/runtime/format-obj.d.cts +2 -0
  76. package/dist/runtime/format-obj.d.ts +2 -0
  77. package/dist/runtime/format-obj.iife.js.map +1 -1
  78. package/dist/runtime/format-obj.js +1 -1
  79. package/dist/runtime/format-scene.cjs +241 -0
  80. package/dist/runtime/format-scene.cjs.map +1 -0
  81. package/dist/runtime/format-scene.d.cts +349 -0
  82. package/dist/runtime/format-scene.d.ts +349 -0
  83. package/dist/runtime/format-scene.iife.js +2 -0
  84. package/dist/runtime/format-scene.iife.js.map +1 -0
  85. package/dist/runtime/format-scene.js +233 -0
  86. package/dist/runtime/format-scene.js.map +1 -0
  87. package/dist/runtime/format-sprite.cjs +3 -3
  88. package/dist/runtime/format-sprite.js +3 -3
  89. package/dist/runtime/format-svg.cjs +3 -3
  90. package/dist/runtime/format-svg.js +3 -3
  91. package/dist/runtime/format-webp.cjs +4 -4
  92. package/dist/runtime/format-webp.js +4 -4
  93. package/dist/runtime/gl.cjs +17 -4
  94. package/dist/runtime/gl.cjs.map +1 -1
  95. package/dist/runtime/gl.d.cts +2 -0
  96. package/dist/runtime/gl.d.ts +2 -0
  97. package/dist/runtime/gl.iife.js +1 -1
  98. package/dist/runtime/gl.iife.js.map +1 -1
  99. package/dist/runtime/gl.js +17 -4
  100. package/dist/runtime/gl.js.map +1 -1
  101. package/dist/runtime/gltf-anim.cjs +251 -0
  102. package/dist/runtime/gltf-anim.cjs.map +1 -0
  103. package/dist/runtime/gltf-anim.d.cts +216 -0
  104. package/dist/runtime/gltf-anim.d.ts +216 -0
  105. package/dist/runtime/gltf-anim.iife.js +2 -0
  106. package/dist/runtime/gltf-anim.iife.js.map +1 -0
  107. package/dist/runtime/gltf-anim.js +243 -0
  108. package/dist/runtime/gltf-anim.js.map +1 -0
  109. package/dist/runtime/physics.cjs +739 -0
  110. package/dist/runtime/physics.cjs.map +1 -0
  111. package/dist/runtime/physics.d.cts +250 -0
  112. package/dist/runtime/physics.d.ts +250 -0
  113. package/dist/runtime/physics.iife.js +2 -0
  114. package/dist/runtime/physics.iife.js.map +1 -0
  115. package/dist/runtime/physics.js +731 -0
  116. package/dist/runtime/physics.js.map +1 -0
  117. package/dist/runtime/scroll.cjs +2 -2
  118. package/dist/runtime/scroll.js +2 -2
  119. package/dist/runtime/smooth.cjs +1 -1
  120. package/dist/runtime/smooth.js +1 -1
  121. package/dist/runtime/text.cjs +1 -1
  122. package/dist/runtime/text.js +1 -1
  123. package/dist/runtime/vector.cjs +421 -11
  124. package/dist/runtime/vector.cjs.map +1 -1
  125. package/dist/runtime/vector.d.cts +20 -4
  126. package/dist/runtime/vector.d.ts +20 -4
  127. package/dist/runtime/vector.iife.js +1 -1
  128. package/dist/runtime/vector.iife.js.map +1 -1
  129. package/dist/runtime/vector.js +421 -12
  130. package/dist/runtime/vector.js.map +1 -1
  131. package/dist/runtime.cjs +3 -3
  132. package/dist/runtime.d.cts +1 -1
  133. package/dist/runtime.d.ts +1 -1
  134. package/dist/runtime.iife.js +1 -1
  135. package/dist/runtime.iife.js.map +1 -1
  136. package/dist/runtime.js +5 -5
  137. package/dist/widgets.umd.js +1 -1
  138. package/dist/widgets.umd.js.map +1 -1
  139. package/docs/ROADMAP.md +2 -2
  140. package/docs/components/README.md +5 -2
  141. package/docs/components/usa-gl-scene.md +27 -6
  142. package/docs/components/usa-lottie-player.md +18 -0
  143. package/docs/components/usa-motion-prompt.md +32 -0
  144. package/docs/components/usa-physics-playground.md +63 -0
  145. package/docs/components/usa-snap-carousel.md +68 -0
  146. package/docs/components.md +95 -3
  147. package/docs/runtime/drag-snap.md +60 -0
  148. package/docs/runtime/format-gltf.md +3 -2
  149. package/docs/runtime/format-scene.md +59 -0
  150. package/docs/runtime/gltf-anim.md +62 -0
  151. package/docs/runtime/physics.md +61 -0
  152. package/docs/runtime/vector.md +3 -2
  153. package/package.json +61 -1
@@ -0,0 +1,241 @@
1
+ 'use strict';
2
+
3
+ var registry = require('../chunks/registry-BZxDVL3h.cjs');
4
+
5
+ /**
6
+ * `motionary/runtime/format-scene` (10.7) — the versioned scene format
7
+ * `motionary-scene@1`: a JSON description of a 2D physics scene (world,
8
+ * named materials, bodies, constraints, per-body style) that
9
+ * `<usa-physics-playground>` and your own code can load, validate,
10
+ * migrate and save.
11
+ *
12
+ * ```json
13
+ * { "format": "motionary-scene@1",
14
+ * "world": { "width": 600, "height": 360, "gravity": [0, 980], "walls": true },
15
+ * "materials": { "rubber": { "restitution": 0.8, "friction": 0.6, "density": 0.001 } },
16
+ * "bodies": [
17
+ * { "id": "ball", "shape": "circle", "radius": 20, "position": [120, 40], "material": "rubber", "style": { "fill": "#f472b6" } },
18
+ * { "id": "floor-plank", "shape": "box", "size": [300, 16], "position": [300, 300], "angle": 0.1, "static": true }
19
+ * ],
20
+ * "constraints": [ { "type": "distance", "a": "ball", "b": "floor-plank", "length": 120, "stiffness": 0.3 } ] }
21
+ * ```
22
+ *
23
+ * - `validateScene(json)` lists problems (never throws);
24
+ * - `migrateScene(json)` upgrades older shapes (an unversioned scene, the
25
+ * `motionary-scene@0` draft) to `@1` and reports what it changed;
26
+ * - `parseScene(json)` = migrate + validate, throws on errors;
27
+ * - `sceneToWorld(scene)` builds a `motionary/runtime/physics` world
28
+ * (bodies by id); `worldToScene(world)` saves one back.
29
+ *
30
+ * Needs `motionary/runtime/physics` registered for `sceneToWorld` /
31
+ * `worldToScene`; validation and migration are pure.
32
+ */
33
+ const SCENE_FORMAT = 'motionary-scene@1';
34
+ const isNum = (x) => typeof x === 'number' && isFinite(x);
35
+ const isVec = (x) => Array.isArray(x) && x.length === 2 && x.every(isNum);
36
+ /** Upgrade older scene shapes to `motionary-scene@1`. Returns the scene and what changed. */
37
+ function migrateScene(input) {
38
+ const changes = [];
39
+ const s = typeof input === 'string' ? JSON.parse(input) : JSON.parse(JSON.stringify(input ?? {}));
40
+ if (s.format === SCENE_FORMAT)
41
+ return { scene: s, changes };
42
+ if (s.format && s.format !== 'motionary-scene@0')
43
+ throw new Error(`[motionary] scene: unknown format "${s.format}" (this build reads ${SCENE_FORMAT} and migrates motionary-scene@0 / unversioned scenes)`);
44
+ changes.push(`${s.format || 'unversioned'} → ${SCENE_FORMAT}`);
45
+ s.format = SCENE_FORMAT;
46
+ // @0 / unversioned: { width, height, gravity: number } at the top level, bodies with x / y / w / h / type
47
+ if (!s.world) {
48
+ s.world = { width: s.width ?? 600, height: s.height ?? 400 };
49
+ if (s.gravity != null)
50
+ s.world.gravity = isNum(s.gravity) ? [0, s.gravity] : s.gravity;
51
+ if (s.walls != null)
52
+ s.world.walls = s.walls;
53
+ delete s.width, delete s.height, delete s.gravity, delete s.walls;
54
+ changes.push('top-level width / height / gravity moved into "world"');
55
+ }
56
+ if (isNum(s.world.gravity)) {
57
+ s.world.gravity = [0, s.world.gravity];
58
+ changes.push('world.gravity number → [0, g]');
59
+ }
60
+ s.bodies = (s.bodies || []).map((b, i) => {
61
+ const o = { ...b };
62
+ if (!o.id)
63
+ (o.id = `body-${i + 1}`), changes.push(`bodies[${i}]: id "${o.id}" added`);
64
+ if (o.position == null && (isNum(o.x) || isNum(o.y))) {
65
+ o.position = [o.x ?? 0, o.y ?? 0];
66
+ delete o.x, delete o.y;
67
+ changes.push(`bodies[${i}]: x / y → position`);
68
+ }
69
+ if (o.size == null && (isNum(o.w) || isNum(o.h))) {
70
+ o.size = [o.w ?? 40, o.h ?? 40];
71
+ delete o.w, delete o.h;
72
+ changes.push(`bodies[${i}]: w / h → size`);
73
+ }
74
+ if (o.type === 'static' || o.type === 'kinematic') {
75
+ o[o.type] = true;
76
+ delete o.type;
77
+ changes.push(`bodies[${i}]: type → ${o.static ? 'static' : 'kinematic'}: true`);
78
+ }
79
+ else if (o.type === 'dynamic')
80
+ delete o.type;
81
+ if (!o.shape)
82
+ o.shape = o.radius != null ? 'circle' : o.vertices ? 'polygon' : 'box';
83
+ return o;
84
+ });
85
+ return { scene: s, changes };
86
+ }
87
+ /** Problems in a `motionary-scene@1` document (after migration). Never throws. */
88
+ function validateScene(input) {
89
+ const out = [];
90
+ let s;
91
+ try {
92
+ s = migrateScene(input).scene;
93
+ }
94
+ catch (e) {
95
+ return [String(e.message).replace('[motionary] scene: ', '')];
96
+ }
97
+ if (!s.world || !isNum(s.world.width) || !isNum(s.world.height) || s.world.width <= 0 || s.world.height <= 0)
98
+ out.push('world: width and height must be positive numbers');
99
+ if (s.world?.gravity != null && !isVec(s.world.gravity))
100
+ out.push('world.gravity must be [x, y]');
101
+ const mats = s.materials || {};
102
+ for (const [k, m] of Object.entries(mats))
103
+ for (const p of ['restitution', 'friction', 'density'])
104
+ if (m[p] != null && (!isNum(m[p]) || m[p] < 0))
105
+ out.push(`materials.${k}.${p} must be a number ≥ 0`);
106
+ if (!Array.isArray(s.bodies))
107
+ out.push('bodies must be an array');
108
+ const ids = new Set();
109
+ (s.bodies || []).forEach((b, i) => {
110
+ const w = `bodies[${i}]${b.id ? ` ("${b.id}")` : ''}`;
111
+ if (ids.has(b.id))
112
+ out.push(`${w}: duplicate id`);
113
+ ids.add(b.id);
114
+ if (!['circle', 'box', 'polygon'].includes(b.shape))
115
+ out.push(`${w}: shape must be circle, box or polygon`);
116
+ if (!isVec(b.position))
117
+ out.push(`${w}: position must be [x, y]`);
118
+ if (b.shape === 'circle' && !(isNum(b.radius) && b.radius > 0))
119
+ out.push(`${w}: circle needs radius > 0`);
120
+ if (b.shape === 'box' && !(isVec(b.size) && b.size[0] > 0 && b.size[1] > 0))
121
+ out.push(`${w}: box needs size [w, h] > 0`);
122
+ if (b.shape === 'polygon' && !(Array.isArray(b.vertices) && b.vertices.length >= 3 && b.vertices.every(isVec)))
123
+ out.push(`${w}: polygon needs ≥ 3 vertices [x, y]`);
124
+ if (b.material && !mats[b.material])
125
+ out.push(`${w}: unknown material "${b.material}"`);
126
+ if (b.static && b.kinematic)
127
+ out.push(`${w}: static and kinematic are exclusive`);
128
+ });
129
+ (s.constraints || []).forEach((c, i) => {
130
+ const w = `constraints[${i}]`;
131
+ if (!['distance', 'pin'].includes(c.type))
132
+ out.push(`${w}: type must be distance or pin`);
133
+ if (!ids.has(c.a))
134
+ out.push(`${w}: unknown body "${c.a}"`);
135
+ if (c.type === 'distance' && !ids.has(c.b))
136
+ out.push(`${w}: distance needs body b (unknown "${c.b}")`);
137
+ if (c.type === 'pin' && c.point != null && !isVec(c.point))
138
+ out.push(`${w}: point must be [x, y]`);
139
+ if (c.stiffness != null && !(isNum(c.stiffness) && c.stiffness > 0 && c.stiffness <= 1))
140
+ out.push(`${w}: stiffness must be in (0, 1]`);
141
+ });
142
+ return out;
143
+ }
144
+ /** Migrate + validate; throws one error listing every problem. */
145
+ function parseScene(input) {
146
+ const { scene } = migrateScene(input);
147
+ const problems = validateScene(scene);
148
+ if (problems.length)
149
+ throw new Error(`[motionary] scene: ${problems.length} problem(s): ${problems.join('; ')}`);
150
+ return scene;
151
+ }
152
+ /** Build a physics world from a scene (needs `use(physics)`). */
153
+ function sceneToWorld(input) {
154
+ const scene = parseScene(input);
155
+ const P = registry.requireModule('physics', 'motionary/runtime/format-scene');
156
+ const w = scene.world;
157
+ const world = P.createWorld({ gravity: w.gravity || [0, 980], iterations: w.iterations });
158
+ if (w.walls)
159
+ world.bounds(w.width, w.height, 50, typeof w.walls === 'object' ? w.walls : {});
160
+ const bodies = {};
161
+ for (const b of scene.bodies) {
162
+ const m = (b.material && scene.materials?.[b.material]) || {};
163
+ const o = {
164
+ shape: b.shape,
165
+ x: b.position[0],
166
+ y: b.position[1],
167
+ angle: b.angle,
168
+ radius: b.radius,
169
+ width: b.size?.[0],
170
+ height: b.size?.[1],
171
+ vertices: b.vertices,
172
+ type: b.static ? 'static' : b.kinematic ? 'kinematic' : 'dynamic',
173
+ restitution: b.restitution ?? m.restitution,
174
+ friction: b.friction ?? m.friction,
175
+ density: b.density ?? m.density,
176
+ vx: b.velocity?.[0],
177
+ vy: b.velocity?.[1],
178
+ angularVelocity: b.angularVelocity,
179
+ sensor: b.sensor,
180
+ label: b.id,
181
+ data: { id: b.id, style: b.style || {}, material: b.material },
182
+ };
183
+ bodies[b.id] = world.body(o);
184
+ }
185
+ for (const c of scene.constraints || [])
186
+ world.constraint({ type: c.type, a: bodies[c.a], b: c.b ? bodies[c.b] : undefined, point: c.point, anchorA: c.anchorA, anchorB: c.anchorB, length: c.length, stiffness: c.stiffness, damping: c.damping });
187
+ return { world, bodies, scene };
188
+ }
189
+ /** Save the current state of a world as a scene (walls made by `bounds()` are left out; `w` / `h` give the world size). */
190
+ function worldToScene(world, o = { width: 600, height: 400 }) {
191
+ const r = (x) => Math.round(x * 1000) / 1000;
192
+ const wallLabels = new Set(['floor', 'wall-left', 'wall-right', 'ceiling']);
193
+ const kept = world.bodies.filter((b) => !(b.isStatic && wallLabels.has(b.label)));
194
+ const id = (b) => b.data?.id || b.label + '-' + b.id;
195
+ const bodies = kept.map((b) => {
196
+ const sb = { id: id(b), shape: b.shape, position: [r(b.position.x), r(b.position.y)] };
197
+ if (b.angle)
198
+ sb.angle = r(b.angle);
199
+ if (b.shape === 'circle')
200
+ sb.radius = r(b.radius);
201
+ else {
202
+ const xs = b.verts.map((p) => p.x), ys = b.verts.map((p) => p.y);
203
+ const isBox = b.shape === 'box';
204
+ if (isBox)
205
+ sb.size = [r(Math.max(...xs) - Math.min(...xs)), r(Math.max(...ys) - Math.min(...ys))];
206
+ else
207
+ sb.vertices = b.verts.map((p) => [r(p.x), r(p.y)]);
208
+ }
209
+ if (b.type === 'static')
210
+ sb.static = true;
211
+ if (b.type === 'kinematic')
212
+ sb.kinematic = true;
213
+ sb.restitution = b.restitution;
214
+ sb.friction = b.friction;
215
+ if (b.type === 'dynamic' && (b.velocity.x || b.velocity.y))
216
+ sb.velocity = [r(b.velocity.x), r(b.velocity.y)];
217
+ if (b.angularVelocity)
218
+ sb.angularVelocity = r(b.angularVelocity);
219
+ if (b.sensor)
220
+ sb.sensor = true;
221
+ const st = b.data?.style;
222
+ if (st && Object.keys(st).length)
223
+ sb.style = st;
224
+ return sb;
225
+ });
226
+ const constraints = world.constraints
227
+ .filter((c) => c.label !== 'drag' && kept.includes(c.a) && (!c.b || kept.includes(c.b)))
228
+ .map((c) => ({ type: c.type, a: id(c.a), ...(c.b ? { b: id(c.b) } : { point: [r(c.point.x), r(c.point.y)] }), anchorA: [r(c.anchorA.x), r(c.anchorA.y)], ...(c.b ? { anchorB: [r(c.anchorB.x), r(c.anchorB.y)] } : {}), length: r(c.length), stiffness: c.stiffness, damping: c.damping }));
229
+ return { format: SCENE_FORMAT, ...(o.name ? { name: o.name } : {}), world: { width: o.width, height: o.height, gravity: [world.gravity.x, world.gravity.y], walls: o.walls ?? world.bodies.some((b) => wallLabels.has(b.label)) }, bodies, constraints };
230
+ }
231
+ /** The module object for `use(formatScene)` (needs `physics`). */
232
+ const formatScene = { id: 'format-scene', version: registry.RUNTIME_VERSION, requires: ['core', 'physics'], api: { SCENE_FORMAT, migrateScene, validateScene, parseScene, sceneToWorld, worldToScene } };
233
+
234
+ exports.SCENE_FORMAT = SCENE_FORMAT;
235
+ exports.formatScene = formatScene;
236
+ exports.migrateScene = migrateScene;
237
+ exports.parseScene = parseScene;
238
+ exports.sceneToWorld = sceneToWorld;
239
+ exports.validateScene = validateScene;
240
+ exports.worldToScene = worldToScene;
241
+ //# sourceMappingURL=format-scene.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"format-scene.cjs","sources":["../../src/runtime/format-scene.ts"],"sourcesContent":["/**\n * `motionary/runtime/format-scene` (10.7) — the versioned scene format\n * `motionary-scene@1`: a JSON description of a 2D physics scene (world,\n * named materials, bodies, constraints, per-body style) that\n * `<usa-physics-playground>` and your own code can load, validate,\n * migrate and save.\n *\n * ```json\n * { \"format\": \"motionary-scene@1\",\n * \"world\": { \"width\": 600, \"height\": 360, \"gravity\": [0, 980], \"walls\": true },\n * \"materials\": { \"rubber\": { \"restitution\": 0.8, \"friction\": 0.6, \"density\": 0.001 } },\n * \"bodies\": [\n * { \"id\": \"ball\", \"shape\": \"circle\", \"radius\": 20, \"position\": [120, 40], \"material\": \"rubber\", \"style\": { \"fill\": \"#f472b6\" } },\n * { \"id\": \"floor-plank\", \"shape\": \"box\", \"size\": [300, 16], \"position\": [300, 300], \"angle\": 0.1, \"static\": true }\n * ],\n * \"constraints\": [ { \"type\": \"distance\", \"a\": \"ball\", \"b\": \"floor-plank\", \"length\": 120, \"stiffness\": 0.3 } ] }\n * ```\n *\n * - `validateScene(json)` lists problems (never throws);\n * - `migrateScene(json)` upgrades older shapes (an unversioned scene, the\n * `motionary-scene@0` draft) to `@1` and reports what it changed;\n * - `parseScene(json)` = migrate + validate, throws on errors;\n * - `sceneToWorld(scene)` builds a `motionary/runtime/physics` world\n * (bodies by id); `worldToScene(world)` saves one back.\n *\n * Needs `motionary/runtime/physics` registered for `sceneToWorld` /\n * `worldToScene`; validation and migration are pure.\n */\nimport { RUNTIME_VERSION, requireModule, type RuntimeModule } from './registry';\nimport type { PhysicsApi, World, Body, BodyOptions } from './physics';\n\ntype Num = number;\nexport const SCENE_FORMAT = 'motionary-scene@1';\n\nexport interface SceneMaterial { restitution?: Num; friction?: Num; density?: Num }\nexport interface SceneBody {\n id: string;\n shape: 'circle' | 'box' | 'polygon';\n position: [Num, Num];\n angle?: Num;\n radius?: Num;\n size?: [Num, Num];\n vertices?: [Num, Num][];\n static?: boolean;\n kinematic?: boolean;\n material?: string;\n restitution?: Num;\n friction?: Num;\n density?: Num;\n velocity?: [Num, Num];\n angularVelocity?: Num;\n sensor?: boolean;\n style?: { fill?: string; stroke?: string; label?: string };\n}\nexport interface SceneConstraint {\n type: 'distance' | 'pin';\n a: string;\n b?: string;\n point?: [Num, Num];\n anchorA?: [Num, Num];\n anchorB?: [Num, Num];\n length?: Num;\n stiffness?: Num;\n damping?: Num;\n}\nexport interface Scene {\n format: typeof SCENE_FORMAT;\n name?: string;\n world: { width: Num; height: Num; gravity?: [Num, Num]; walls?: boolean | { top?: boolean }; iterations?: Num };\n materials?: Record<string, SceneMaterial>;\n bodies: SceneBody[];\n constraints?: SceneConstraint[];\n}\n\nconst isNum = (x: unknown): x is Num => typeof x === 'number' && isFinite(x);\nconst isVec = (x: unknown): x is [Num, Num] => Array.isArray(x) && x.length === 2 && x.every(isNum);\n\n/** Upgrade older scene shapes to `motionary-scene@1`. Returns the scene and what changed. */\nexport function migrateScene(input: unknown): { scene: any; changes: string[] } {\n const changes: string[] = [];\n const s: any = typeof input === 'string' ? JSON.parse(input) : JSON.parse(JSON.stringify(input ?? {}));\n if (s.format === SCENE_FORMAT) return { scene: s, changes };\n if (s.format && s.format !== 'motionary-scene@0') throw new Error(`[motionary] scene: unknown format \"${s.format}\" (this build reads ${SCENE_FORMAT} and migrates motionary-scene@0 / unversioned scenes)`);\n changes.push(`${s.format || 'unversioned'} → ${SCENE_FORMAT}`);\n s.format = SCENE_FORMAT;\n // @0 / unversioned: { width, height, gravity: number } at the top level, bodies with x / y / w / h / type\n if (!s.world) {\n s.world = { width: s.width ?? 600, height: s.height ?? 400 };\n if (s.gravity != null) s.world.gravity = isNum(s.gravity) ? [0, s.gravity] : s.gravity;\n if (s.walls != null) s.world.walls = s.walls;\n delete s.width, delete s.height, delete s.gravity, delete s.walls;\n changes.push('top-level width / height / gravity moved into \"world\"');\n }\n if (isNum(s.world.gravity)) {\n s.world.gravity = [0, s.world.gravity];\n changes.push('world.gravity number → [0, g]');\n }\n s.bodies = (s.bodies || []).map((b: any, i: number) => {\n const o: any = { ...b };\n if (!o.id) (o.id = `body-${i + 1}`), changes.push(`bodies[${i}]: id \"${o.id}\" added`);\n if (o.position == null && (isNum(o.x) || isNum(o.y))) {\n o.position = [o.x ?? 0, o.y ?? 0];\n delete o.x, delete o.y;\n changes.push(`bodies[${i}]: x / y → position`);\n }\n if (o.size == null && (isNum(o.w) || isNum(o.h))) {\n o.size = [o.w ?? 40, o.h ?? 40];\n delete o.w, delete o.h;\n changes.push(`bodies[${i}]: w / h → size`);\n }\n if (o.type === 'static' || o.type === 'kinematic') {\n o[o.type] = true;\n delete o.type;\n changes.push(`bodies[${i}]: type → ${o.static ? 'static' : 'kinematic'}: true`);\n } else if (o.type === 'dynamic') delete o.type;\n if (!o.shape) o.shape = o.radius != null ? 'circle' : o.vertices ? 'polygon' : 'box';\n return o;\n });\n return { scene: s, changes };\n}\n\n/** Problems in a `motionary-scene@1` document (after migration). Never throws. */\nexport function validateScene(input: unknown): string[] {\n const out: string[] = [];\n let s: any;\n try {\n s = migrateScene(input).scene;\n } catch (e) {\n return [String((e as Error).message).replace('[motionary] scene: ', '')];\n }\n if (!s.world || !isNum(s.world.width) || !isNum(s.world.height) || s.world.width <= 0 || s.world.height <= 0) out.push('world: width and height must be positive numbers');\n if (s.world?.gravity != null && !isVec(s.world.gravity)) out.push('world.gravity must be [x, y]');\n const mats = s.materials || {};\n for (const [k, m] of Object.entries<any>(mats)) for (const p of ['restitution', 'friction', 'density']) if (m[p] != null && (!isNum(m[p]) || m[p] < 0)) out.push(`materials.${k}.${p} must be a number ≥ 0`);\n if (!Array.isArray(s.bodies)) out.push('bodies must be an array');\n const ids = new Set<string>();\n (s.bodies || []).forEach((b: any, i: number) => {\n const w = `bodies[${i}]${b.id ? ` (\"${b.id}\")` : ''}`;\n if (ids.has(b.id)) out.push(`${w}: duplicate id`);\n ids.add(b.id);\n if (!['circle', 'box', 'polygon'].includes(b.shape)) out.push(`${w}: shape must be circle, box or polygon`);\n if (!isVec(b.position)) out.push(`${w}: position must be [x, y]`);\n if (b.shape === 'circle' && !(isNum(b.radius) && b.radius > 0)) out.push(`${w}: circle needs radius > 0`);\n if (b.shape === 'box' && !(isVec(b.size) && b.size[0] > 0 && b.size[1] > 0)) out.push(`${w}: box needs size [w, h] > 0`);\n if (b.shape === 'polygon' && !(Array.isArray(b.vertices) && b.vertices.length >= 3 && b.vertices.every(isVec))) out.push(`${w}: polygon needs ≥ 3 vertices [x, y]`);\n if (b.material && !mats[b.material]) out.push(`${w}: unknown material \"${b.material}\"`);\n if (b.static && b.kinematic) out.push(`${w}: static and kinematic are exclusive`);\n });\n (s.constraints || []).forEach((c: any, i: number) => {\n const w = `constraints[${i}]`;\n if (!['distance', 'pin'].includes(c.type)) out.push(`${w}: type must be distance or pin`);\n if (!ids.has(c.a)) out.push(`${w}: unknown body \"${c.a}\"`);\n if (c.type === 'distance' && !ids.has(c.b)) out.push(`${w}: distance needs body b (unknown \"${c.b}\")`);\n if (c.type === 'pin' && c.point != null && !isVec(c.point)) out.push(`${w}: point must be [x, y]`);\n if (c.stiffness != null && !(isNum(c.stiffness) && c.stiffness > 0 && c.stiffness <= 1)) out.push(`${w}: stiffness must be in (0, 1]`);\n });\n return out;\n}\n\n/** Migrate + validate; throws one error listing every problem. */\nexport function parseScene(input: unknown): Scene {\n const { scene } = migrateScene(input);\n const problems = validateScene(scene);\n if (problems.length) throw new Error(`[motionary] scene: ${problems.length} problem(s): ${problems.join('; ')}`);\n return scene as Scene;\n}\n\n/** Build a physics world from a scene (needs `use(physics)`). */\nexport function sceneToWorld(input: Scene | unknown): { world: World; bodies: Record<string, Body>; scene: Scene } {\n const scene = parseScene(input);\n const P = requireModule<PhysicsApi>('physics', 'motionary/runtime/format-scene');\n const w = scene.world;\n const world = P.createWorld({ gravity: w.gravity || [0, 980], iterations: w.iterations });\n if (w.walls) world.bounds(w.width, w.height, 50, typeof w.walls === 'object' ? w.walls : {});\n const bodies: Record<string, Body> = {};\n for (const b of scene.bodies) {\n const m = (b.material && scene.materials?.[b.material]) || {};\n const o: BodyOptions = {\n shape: b.shape,\n x: b.position[0],\n y: b.position[1],\n angle: b.angle,\n radius: b.radius,\n width: b.size?.[0],\n height: b.size?.[1],\n vertices: b.vertices,\n type: b.static ? 'static' : b.kinematic ? 'kinematic' : 'dynamic',\n restitution: b.restitution ?? m.restitution,\n friction: b.friction ?? m.friction,\n density: b.density ?? m.density,\n vx: b.velocity?.[0],\n vy: b.velocity?.[1],\n angularVelocity: b.angularVelocity,\n sensor: b.sensor,\n label: b.id,\n data: { id: b.id, style: b.style || {}, material: b.material },\n };\n bodies[b.id] = world.body(o);\n }\n for (const c of scene.constraints || []) world.constraint({ type: c.type, a: bodies[c.a], b: c.b ? bodies[c.b] : undefined, point: c.point, anchorA: c.anchorA, anchorB: c.anchorB, length: c.length, stiffness: c.stiffness, damping: c.damping });\n return { world, bodies, scene };\n}\n\n/** Save the current state of a world as a scene (walls made by `bounds()` are left out; `w` / `h` give the world size). */\nexport function worldToScene(world: World, o: { width: Num; height: Num; name?: string; walls?: boolean } = { width: 600, height: 400 }): Scene {\n const r = (x: Num) => Math.round(x * 1000) / 1000;\n const wallLabels = new Set(['floor', 'wall-left', 'wall-right', 'ceiling']);\n const kept = world.bodies.filter((b) => !(b.isStatic && wallLabels.has(b.label)));\n const id = (b: Body) => (b.data as any)?.id || b.label + '-' + b.id;\n const bodies: SceneBody[] = kept.map((b) => {\n const sb: SceneBody = { id: id(b), shape: b.shape, position: [r(b.position.x), r(b.position.y)] };\n if (b.angle) sb.angle = r(b.angle);\n if (b.shape === 'circle') sb.radius = r(b.radius);\n else {\n const xs = b.verts.map((p) => p.x), ys = b.verts.map((p) => p.y);\n const isBox = b.shape === 'box';\n if (isBox) sb.size = [r(Math.max(...xs) - Math.min(...xs)), r(Math.max(...ys) - Math.min(...ys))];\n else sb.vertices = b.verts.map((p) => [r(p.x), r(p.y)]);\n }\n if (b.type === 'static') sb.static = true;\n if (b.type === 'kinematic') sb.kinematic = true;\n sb.restitution = b.restitution;\n sb.friction = b.friction;\n if (b.type === 'dynamic' && (b.velocity.x || b.velocity.y)) sb.velocity = [r(b.velocity.x), r(b.velocity.y)];\n if (b.angularVelocity) sb.angularVelocity = r(b.angularVelocity);\n if (b.sensor) sb.sensor = true;\n const st = (b.data as any)?.style;\n if (st && Object.keys(st).length) sb.style = st;\n return sb;\n });\n const constraints: SceneConstraint[] = world.constraints\n .filter((c) => c.label !== 'drag' && kept.includes(c.a) && (!c.b || kept.includes(c.b)))\n .map((c) => ({ type: c.type, a: id(c.a), ...(c.b ? { b: id(c.b) } : { point: [r(c.point.x), r(c.point.y)] as [Num, Num] }), anchorA: [r(c.anchorA.x), r(c.anchorA.y)] as [Num, Num], ...(c.b ? { anchorB: [r(c.anchorB.x), r(c.anchorB.y)] as [Num, Num] } : {}), length: r(c.length), stiffness: c.stiffness, damping: c.damping }));\n return { format: SCENE_FORMAT, ...(o.name ? { name: o.name } : {}), world: { width: o.width, height: o.height, gravity: [world.gravity.x, world.gravity.y], walls: o.walls ?? world.bodies.some((b) => wallLabels.has(b.label)) }, bodies, constraints };\n}\n\nexport interface FormatSceneApi {\n SCENE_FORMAT: typeof SCENE_FORMAT;\n migrateScene: typeof migrateScene;\n validateScene: typeof validateScene;\n parseScene: typeof parseScene;\n sceneToWorld: typeof sceneToWorld;\n worldToScene: typeof worldToScene;\n}\n\n/** The module object for `use(formatScene)` (needs `physics`). */\nexport const formatScene: RuntimeModule<FormatSceneApi> = { id: 'format-scene', version: RUNTIME_VERSION, requires: ['core', 'physics'], api: { SCENE_FORMAT, migrateScene, validateScene, parseScene, sceneToWorld, worldToScene } };\n"],"names":["requireModule","RUNTIME_VERSION"],"mappings":";;;;AAAA;;;;;;;;;;;;;;;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,EAAE,YAAY,EAAE,aAAa,EAAE,UAAU,EAAE,YAAY,EAAE,YAAY,EAAE;;;;;;;;"}
@@ -0,0 +1,349 @@
1
+ /** A runtime module: `{ id, version, api }`, registered with `use()`. */
2
+ interface RuntimeModule<A = unknown> {
3
+ /** Module id: 'core', 'format-css', 'scroll', … (import path `motionary/runtime/<id>`). */
4
+ id: string;
5
+ version: string;
6
+ /** Other modules this one needs (registered first by `use()` callers). */
7
+ requires?: string[];
8
+ /** The module's public API (what `requireModule(id)` returns). */
9
+ api: A;
10
+ /** Optional one-time setup, called on first registration. */
11
+ setup?(registry: RuntimeRegistry): void;
12
+ }
13
+ interface RuntimeRegistry {
14
+ version: string;
15
+ modules: Map<string, RuntimeModule>;
16
+ /** Shared per-page state slots (the ticker lives here). */
17
+ slots: Record<string, unknown>;
18
+ }
19
+
20
+ /**
21
+ * `motionary/runtime/physics` (10.7) — a small 2D rigid-body engine written
22
+ * for Motionary (own implementation and API; no Matter.js / Box2D code):
23
+ *
24
+ * - bodies: circles, boxes and convex polygons (static, dynamic or
25
+ * kinematic), density → mass + moment of inertia, restitution, friction,
26
+ * sensors, collision groups / masks, sleeping;
27
+ * - collisions: sort-and-sweep broad phase, separating-axis narrow phase
28
+ * with clipped contact manifolds (up to 2 points), sequential, accumulated and
29
+ * clamped impulses (normal + Coulomb friction) with a Baumgarte position bias;
30
+ * - constraints: `distance` (rigid or a spring with `stiffness` /
31
+ * `damping`), `pin` (a body point to a world point — also the pointer
32
+ * drag constraint) and `weld`-like stiff distance pairs;
33
+ * - a fixed time step (default 1/60 s) with an accumulator, so results are
34
+ * the same at any frame rate; `world.run()` drives it from the shared
35
+ * runtime ticker (pauses while the ticker sleeps).
36
+ *
37
+ * Pure maths — no DOM: safe in SSR and workers. Units are up to you (the
38
+ * defaults assume pixels: gravity 980 px/s²).
39
+ */
40
+
41
+ type Num$1 = number;
42
+ interface Vec2 {
43
+ x: Num$1;
44
+ y: Num$1;
45
+ }
46
+ type ShapeKind = 'circle' | 'box' | 'polygon';
47
+ interface BodyOptions {
48
+ shape?: ShapeKind;
49
+ x?: Num$1;
50
+ y?: Num$1;
51
+ angle?: Num$1;
52
+ /** circle */
53
+ radius?: Num$1;
54
+ /** box */
55
+ width?: Num$1;
56
+ height?: Num$1;
57
+ /** polygon: local vertices (any winding; made convex-ccw) */
58
+ vertices?: [Num$1, Num$1][];
59
+ /** 'dynamic' (default), 'static' (never moves) or 'kinematic' (moves by its velocity, ignores forces / collisions). */
60
+ type?: 'dynamic' | 'static' | 'kinematic';
61
+ /** Mass per unit area (default 0.001 → a 40 px box weighs 1.6). */
62
+ density?: Num$1;
63
+ /** Overrides density. */
64
+ mass?: Num$1;
65
+ restitution?: Num$1;
66
+ friction?: Num$1;
67
+ vx?: Num$1;
68
+ vy?: Num$1;
69
+ angularVelocity?: Num$1;
70
+ /** Linear / angular damping per second (0–1). */
71
+ damping?: Num$1;
72
+ angularDamping?: Num$1;
73
+ /** Collides with nothing, reports overlaps only. */
74
+ sensor?: boolean;
75
+ /** Collision filtering: bodies collide when (a.category & b.mask) && (b.category & a.mask). */
76
+ category?: Num$1;
77
+ mask?: Num$1;
78
+ /** No rotation (infinite inertia). */
79
+ fixedRotation?: boolean;
80
+ label?: string;
81
+ /** Anything (e.g. a fill colour for your renderer). */
82
+ data?: unknown;
83
+ }
84
+ declare class Body {
85
+ readonly id: number;
86
+ label: string;
87
+ shape: ShapeKind;
88
+ type: 'dynamic' | 'static' | 'kinematic';
89
+ position: Vec2;
90
+ velocity: Vec2;
91
+ angle: Num$1;
92
+ angularVelocity: Num$1;
93
+ force: Vec2;
94
+ torque: number;
95
+ radius: number;
96
+ /** Local convex vertices (counter-clockwise) for boxes / polygons. */
97
+ verts: Vec2[];
98
+ normals: Vec2[];
99
+ mass: number;
100
+ invMass: number;
101
+ inertia: number;
102
+ invInertia: number;
103
+ restitution: Num$1;
104
+ friction: Num$1;
105
+ damping: Num$1;
106
+ angularDamping: Num$1;
107
+ sensor: boolean;
108
+ category: Num$1;
109
+ mask: Num$1;
110
+ sleeping: boolean;
111
+ sleepTime: number;
112
+ data: unknown;
113
+ /** Bounding box, refreshed every step. */
114
+ aabb: {
115
+ minX: number;
116
+ minY: number;
117
+ maxX: number;
118
+ maxY: number;
119
+ };
120
+ private wv;
121
+ private wvAngle;
122
+ private wvPos;
123
+ constructor(o?: BodyOptions);
124
+ get isStatic(): boolean;
125
+ /** Vertices in world space (cached per pose). */
126
+ worldVerts(): Vec2[];
127
+ worldNormal(i: Num$1): Vec2;
128
+ updateAabb(): void;
129
+ applyImpulse(j: Vec2, at?: Vec2): void;
130
+ applyForce(f: Vec2, at?: Vec2): void;
131
+ wake(): void;
132
+ /** Is the world point inside this body? */
133
+ contains(x: Num$1, y: Num$1): boolean;
134
+ }
135
+ interface Contact {
136
+ point: Vec2;
137
+ depth: Num$1;
138
+ }
139
+ interface Manifold {
140
+ a: Body;
141
+ b: Body;
142
+ normal: Vec2;
143
+ contacts: Contact[];
144
+ }
145
+ interface ConstraintOptions {
146
+ type: 'distance' | 'pin';
147
+ a: Body;
148
+ /** distance: the other body; pin: omitted. */
149
+ b?: Body;
150
+ /** Anchor on a / b in body-local coordinates (default the centre). */
151
+ anchorA?: [Num$1, Num$1];
152
+ anchorB?: [Num$1, Num$1];
153
+ /** pin: the world point. */
154
+ point?: [Num$1, Num$1];
155
+ /** Rest length (default: the current distance). */
156
+ length?: Num$1;
157
+ /** 1 = rigid; < 1 = spring (fraction of the error corrected per step). */
158
+ stiffness?: Num$1;
159
+ /** Spring damping (0–1). */
160
+ damping?: Num$1;
161
+ label?: string;
162
+ }
163
+ declare class Constraint {
164
+ type: 'distance' | 'pin';
165
+ a: Body;
166
+ b: Body | null;
167
+ anchorA: Vec2;
168
+ anchorB: Vec2;
169
+ point: Vec2;
170
+ length: Num$1;
171
+ stiffness: Num$1;
172
+ damping: Num$1;
173
+ label: string;
174
+ constructor(o: ConstraintOptions);
175
+ worldA(): Vec2;
176
+ worldB(): Vec2;
177
+ /** One velocity iteration (impulse along the constraint axis, with a position bias). */
178
+ solve(h: Num$1): void;
179
+ }
180
+ interface WorldOptions {
181
+ gravity?: [Num$1, Num$1];
182
+ /** Velocity iterations per step (default 10). */
183
+ iterations?: Num$1;
184
+ /** Fixed step in seconds (default 1/60). */
185
+ step?: Num$1;
186
+ /** Let resting bodies sleep (default true). */
187
+ sleeping?: boolean;
188
+ }
189
+ type CollisionListener = (m: Manifold) => void;
190
+ declare class World {
191
+ gravity: Vec2;
192
+ iterations: Num$1;
193
+ fixedStep: Num$1;
194
+ sleeping: boolean;
195
+ bodies: Body[];
196
+ constraints: Constraint[];
197
+ /** Manifolds of the last step (incl. sensor overlaps, which have no response). */
198
+ contacts: Manifold[];
199
+ time: number;
200
+ steps: number;
201
+ private acc;
202
+ private warm;
203
+ private listeners;
204
+ private stop;
205
+ constructor(o?: WorldOptions);
206
+ add<T extends Body | Constraint>(...items: T[]): T;
207
+ body(o: BodyOptions): Body;
208
+ constraint(o: ConstraintOptions): Constraint;
209
+ remove(...items: (Body | Constraint)[]): void;
210
+ clear(): void;
211
+ /** Static walls around a w × h box (thickness t outside it). */
212
+ bounds(w: Num$1, h: Num$1, t?: number, o?: {
213
+ top?: boolean;
214
+ }): Body[];
215
+ onCollision(fn: CollisionListener): () => void;
216
+ /** Bodies under a world point (topmost = last added first). */
217
+ query(x: Num$1, y: Num$1): Body[];
218
+ /** Advance by `seconds` of real time in fixed steps (max 8 steps per call). */
219
+ update(seconds: Num$1): Num$1;
220
+ /** One fixed step of h seconds. */
221
+ step(h?: Num$1): void;
222
+ /** Drive the world from the shared runtime ticker (needs the core). Returns a stopper. */
223
+ run(): () => void;
224
+ get running(): boolean;
225
+ /** Total kinetic energy (handy for tests / "settled" checks). */
226
+ energy(): Num$1;
227
+ }
228
+
229
+ /**
230
+ * `motionary/runtime/format-scene` (10.7) — the versioned scene format
231
+ * `motionary-scene@1`: a JSON description of a 2D physics scene (world,
232
+ * named materials, bodies, constraints, per-body style) that
233
+ * `<usa-physics-playground>` and your own code can load, validate,
234
+ * migrate and save.
235
+ *
236
+ * ```json
237
+ * { "format": "motionary-scene@1",
238
+ * "world": { "width": 600, "height": 360, "gravity": [0, 980], "walls": true },
239
+ * "materials": { "rubber": { "restitution": 0.8, "friction": 0.6, "density": 0.001 } },
240
+ * "bodies": [
241
+ * { "id": "ball", "shape": "circle", "radius": 20, "position": [120, 40], "material": "rubber", "style": { "fill": "#f472b6" } },
242
+ * { "id": "floor-plank", "shape": "box", "size": [300, 16], "position": [300, 300], "angle": 0.1, "static": true }
243
+ * ],
244
+ * "constraints": [ { "type": "distance", "a": "ball", "b": "floor-plank", "length": 120, "stiffness": 0.3 } ] }
245
+ * ```
246
+ *
247
+ * - `validateScene(json)` lists problems (never throws);
248
+ * - `migrateScene(json)` upgrades older shapes (an unversioned scene, the
249
+ * `motionary-scene@0` draft) to `@1` and reports what it changed;
250
+ * - `parseScene(json)` = migrate + validate, throws on errors;
251
+ * - `sceneToWorld(scene)` builds a `motionary/runtime/physics` world
252
+ * (bodies by id); `worldToScene(world)` saves one back.
253
+ *
254
+ * Needs `motionary/runtime/physics` registered for `sceneToWorld` /
255
+ * `worldToScene`; validation and migration are pure.
256
+ */
257
+
258
+ type Num = number;
259
+ declare const SCENE_FORMAT = "motionary-scene@1";
260
+ interface SceneMaterial {
261
+ restitution?: Num;
262
+ friction?: Num;
263
+ density?: Num;
264
+ }
265
+ interface SceneBody {
266
+ id: string;
267
+ shape: 'circle' | 'box' | 'polygon';
268
+ position: [Num, Num];
269
+ angle?: Num;
270
+ radius?: Num;
271
+ size?: [Num, Num];
272
+ vertices?: [Num, Num][];
273
+ static?: boolean;
274
+ kinematic?: boolean;
275
+ material?: string;
276
+ restitution?: Num;
277
+ friction?: Num;
278
+ density?: Num;
279
+ velocity?: [Num, Num];
280
+ angularVelocity?: Num;
281
+ sensor?: boolean;
282
+ style?: {
283
+ fill?: string;
284
+ stroke?: string;
285
+ label?: string;
286
+ };
287
+ }
288
+ interface SceneConstraint {
289
+ type: 'distance' | 'pin';
290
+ a: string;
291
+ b?: string;
292
+ point?: [Num, Num];
293
+ anchorA?: [Num, Num];
294
+ anchorB?: [Num, Num];
295
+ length?: Num;
296
+ stiffness?: Num;
297
+ damping?: Num;
298
+ }
299
+ interface Scene {
300
+ format: typeof SCENE_FORMAT;
301
+ name?: string;
302
+ world: {
303
+ width: Num;
304
+ height: Num;
305
+ gravity?: [Num, Num];
306
+ walls?: boolean | {
307
+ top?: boolean;
308
+ };
309
+ iterations?: Num;
310
+ };
311
+ materials?: Record<string, SceneMaterial>;
312
+ bodies: SceneBody[];
313
+ constraints?: SceneConstraint[];
314
+ }
315
+ /** Upgrade older scene shapes to `motionary-scene@1`. Returns the scene and what changed. */
316
+ declare function migrateScene(input: unknown): {
317
+ scene: any;
318
+ changes: string[];
319
+ };
320
+ /** Problems in a `motionary-scene@1` document (after migration). Never throws. */
321
+ declare function validateScene(input: unknown): string[];
322
+ /** Migrate + validate; throws one error listing every problem. */
323
+ declare function parseScene(input: unknown): Scene;
324
+ /** Build a physics world from a scene (needs `use(physics)`). */
325
+ declare function sceneToWorld(input: Scene | unknown): {
326
+ world: World;
327
+ bodies: Record<string, Body>;
328
+ scene: Scene;
329
+ };
330
+ /** Save the current state of a world as a scene (walls made by `bounds()` are left out; `w` / `h` give the world size). */
331
+ declare function worldToScene(world: World, o?: {
332
+ width: Num;
333
+ height: Num;
334
+ name?: string;
335
+ walls?: boolean;
336
+ }): Scene;
337
+ interface FormatSceneApi {
338
+ SCENE_FORMAT: typeof SCENE_FORMAT;
339
+ migrateScene: typeof migrateScene;
340
+ validateScene: typeof validateScene;
341
+ parseScene: typeof parseScene;
342
+ sceneToWorld: typeof sceneToWorld;
343
+ worldToScene: typeof worldToScene;
344
+ }
345
+ /** The module object for `use(formatScene)` (needs `physics`). */
346
+ declare const formatScene: RuntimeModule<FormatSceneApi>;
347
+
348
+ export { SCENE_FORMAT, formatScene, migrateScene, parseScene, sceneToWorld, validateScene, worldToScene };
349
+ export type { FormatSceneApi, Scene, SceneBody, SceneConstraint, SceneMaterial };