@lovo/matter-react 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +60 -0
- package/dist/.tsbuildinfo +1 -0
- package/dist/index.cjs +445 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +164 -0
- package/dist/index.d.ts +164 -0
- package/dist/index.js +409 -0
- package/dist/index.js.map +1 -0
- package/package.json +76 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,445 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
FallbackBoundary: () => FallbackBoundary,
|
|
24
|
+
MatterMonitor: () => MatterMonitor,
|
|
25
|
+
MatterScene: () => MatterScene,
|
|
26
|
+
useAnimatableUniform: () => useAnimatableUniform,
|
|
27
|
+
useCursor: () => useCursor,
|
|
28
|
+
useMatterContext: () => useMatterContext,
|
|
29
|
+
useResize: () => useResize,
|
|
30
|
+
useScroll: () => useScroll,
|
|
31
|
+
useShaderMaterial: () => useShaderMaterial,
|
|
32
|
+
useStaticHint: () => useStaticHint
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(index_exports);
|
|
35
|
+
|
|
36
|
+
// src/MatterScene.tsx
|
|
37
|
+
var import_react2 = require("react");
|
|
38
|
+
var import_three = require("three");
|
|
39
|
+
var import_matter = require("@lovo/matter");
|
|
40
|
+
|
|
41
|
+
// src/matter-context.ts
|
|
42
|
+
var import_react = require("react");
|
|
43
|
+
var MatterContext = (0, import_react.createContext)(null);
|
|
44
|
+
|
|
45
|
+
// src/MatterScene.tsx
|
|
46
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
47
|
+
var defaultStyle = {
|
|
48
|
+
position: "absolute",
|
|
49
|
+
inset: 0,
|
|
50
|
+
display: "block",
|
|
51
|
+
width: "100%",
|
|
52
|
+
height: "100%"
|
|
53
|
+
};
|
|
54
|
+
function MatterScene(props) {
|
|
55
|
+
const { children, fallback, className, style, maxDPR } = props;
|
|
56
|
+
const canvasRef = (0, import_react2.useRef)(null);
|
|
57
|
+
const [ctx, setCtx] = (0, import_react2.useState)(null);
|
|
58
|
+
const [error, setError] = (0, import_react2.useState)(null);
|
|
59
|
+
(0, import_react2.useEffect)(() => {
|
|
60
|
+
const canvas = canvasRef.current;
|
|
61
|
+
if (!canvas) return;
|
|
62
|
+
let cancelled = false;
|
|
63
|
+
let cleanup = null;
|
|
64
|
+
const setup = async () => {
|
|
65
|
+
try {
|
|
66
|
+
const renderer = await (0, import_matter.createRenderer)(canvas, { maxDPR });
|
|
67
|
+
if (cancelled) {
|
|
68
|
+
renderer.dispose();
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
const scene = new import_three.Scene();
|
|
72
|
+
const camera = new import_three.OrthographicCamera(-1, 1, 1, -1, 0.1, 10);
|
|
73
|
+
camera.position.z = 1;
|
|
74
|
+
const scheduler = new import_matter.MatterScheduler();
|
|
75
|
+
scheduler.add(() => renderer.three.render(scene, camera));
|
|
76
|
+
scheduler.start();
|
|
77
|
+
const visibility = (0, import_matter.createVisibilityWatcher)();
|
|
78
|
+
const intersection = (0, import_matter.createIntersectionWatcher)(canvas);
|
|
79
|
+
const updatePauseState = () => {
|
|
80
|
+
const shouldRun = visibility.isVisible() && intersection.isInView();
|
|
81
|
+
if (shouldRun) scheduler.resume();
|
|
82
|
+
else scheduler.pause();
|
|
83
|
+
};
|
|
84
|
+
updatePauseState();
|
|
85
|
+
const unsubVisibility = visibility.subscribe(updatePauseState);
|
|
86
|
+
const unsubIntersection = intersection.subscribe(updatePauseState);
|
|
87
|
+
const onResize = () => renderer.resize();
|
|
88
|
+
window.addEventListener("resize", onResize);
|
|
89
|
+
cleanup = () => {
|
|
90
|
+
unsubVisibility();
|
|
91
|
+
unsubIntersection();
|
|
92
|
+
visibility.dispose();
|
|
93
|
+
intersection.dispose();
|
|
94
|
+
window.removeEventListener("resize", onResize);
|
|
95
|
+
scheduler.dispose();
|
|
96
|
+
renderer.dispose();
|
|
97
|
+
};
|
|
98
|
+
setCtx({ renderer, scene, camera, scheduler });
|
|
99
|
+
} catch (err) {
|
|
100
|
+
if (cancelled) return;
|
|
101
|
+
const e = err instanceof Error ? err : new Error(String(err));
|
|
102
|
+
console.error("[MatterScene] renderer init failed:", e);
|
|
103
|
+
setError(e);
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
void setup();
|
|
107
|
+
return () => {
|
|
108
|
+
cancelled = true;
|
|
109
|
+
cleanup?.();
|
|
110
|
+
cleanup = null;
|
|
111
|
+
setCtx(null);
|
|
112
|
+
};
|
|
113
|
+
}, [maxDPR]);
|
|
114
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className, style: { ...defaultStyle, ...style }, children: [
|
|
115
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("canvas", { ref: canvasRef, style: { width: "100%", height: "100%", display: "block" } }),
|
|
116
|
+
error ? /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
|
|
117
|
+
"div",
|
|
118
|
+
{
|
|
119
|
+
style: {
|
|
120
|
+
position: "absolute",
|
|
121
|
+
inset: 0,
|
|
122
|
+
display: "flex",
|
|
123
|
+
alignItems: "center",
|
|
124
|
+
justifyContent: "center",
|
|
125
|
+
padding: "1rem",
|
|
126
|
+
color: "#fff",
|
|
127
|
+
background: "rgba(120, 30, 30, 0.85)",
|
|
128
|
+
font: "0.85rem ui-monospace, monospace",
|
|
129
|
+
whiteSpace: "pre-wrap",
|
|
130
|
+
textAlign: "center"
|
|
131
|
+
},
|
|
132
|
+
children: [
|
|
133
|
+
"MatterScene init failed:",
|
|
134
|
+
"\n",
|
|
135
|
+
error.message
|
|
136
|
+
]
|
|
137
|
+
}
|
|
138
|
+
) : ctx ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(MatterContext.Provider, { value: ctx, children }) : fallback ?? null
|
|
139
|
+
] });
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// src/useMatterContext.ts
|
|
143
|
+
var import_react3 = require("react");
|
|
144
|
+
function useMatterContext() {
|
|
145
|
+
return (0, import_react3.useContext)(MatterContext);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// src/useShaderMaterial.ts
|
|
149
|
+
var import_react4 = require("react");
|
|
150
|
+
var import_webgpu = require("three/webgpu");
|
|
151
|
+
function useShaderMaterial(build) {
|
|
152
|
+
const material = (0, import_react4.useMemo)(() => {
|
|
153
|
+
const m = new import_webgpu.MeshBasicNodeMaterial();
|
|
154
|
+
m.colorNode = build();
|
|
155
|
+
return m;
|
|
156
|
+
}, [build]);
|
|
157
|
+
(0, import_react4.useEffect)(() => {
|
|
158
|
+
return () => material.dispose();
|
|
159
|
+
}, [material]);
|
|
160
|
+
return material;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// src/useCursor.ts
|
|
164
|
+
var import_react5 = require("react");
|
|
165
|
+
var import_matter2 = require("@lovo/matter");
|
|
166
|
+
var STUB_SIGNAL = {
|
|
167
|
+
get: () => [0.5, 0.5],
|
|
168
|
+
on: () => () => void 0
|
|
169
|
+
};
|
|
170
|
+
function useCursor(opts = {}) {
|
|
171
|
+
const ctx = useMatterContext();
|
|
172
|
+
const [input, setInput] = (0, import_react5.useState)(null);
|
|
173
|
+
(0, import_react5.useEffect)(() => {
|
|
174
|
+
const canvas = ctx?.renderer.three.domElement;
|
|
175
|
+
const elementOpt = opts.element ?? (canvas instanceof HTMLElement ? canvas : void 0);
|
|
176
|
+
const fresh = new import_matter2.CursorInput({ ...opts, element: elementOpt });
|
|
177
|
+
setInput(fresh);
|
|
178
|
+
let detach = null;
|
|
179
|
+
if (ctx?.scheduler) {
|
|
180
|
+
const client = ({ delta }) => fresh.tick(delta);
|
|
181
|
+
ctx.scheduler.add(client);
|
|
182
|
+
detach = () => ctx.scheduler.remove(client);
|
|
183
|
+
} else {
|
|
184
|
+
let raf = null;
|
|
185
|
+
let lastNow = performance.now();
|
|
186
|
+
const loop = (now) => {
|
|
187
|
+
const delta = (now - lastNow) / 1e3;
|
|
188
|
+
lastNow = now;
|
|
189
|
+
fresh.tick(delta);
|
|
190
|
+
raf = requestAnimationFrame(loop);
|
|
191
|
+
};
|
|
192
|
+
raf = requestAnimationFrame(loop);
|
|
193
|
+
detach = () => {
|
|
194
|
+
if (raf !== null) cancelAnimationFrame(raf);
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
return () => {
|
|
198
|
+
detach?.();
|
|
199
|
+
fresh.dispose();
|
|
200
|
+
setInput(null);
|
|
201
|
+
};
|
|
202
|
+
}, [ctx]);
|
|
203
|
+
return input ?? STUB_SIGNAL;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// src/useResize.ts
|
|
207
|
+
var import_react6 = require("react");
|
|
208
|
+
var STUB_SIGNAL2 = {
|
|
209
|
+
get: () => [0, 0, 1],
|
|
210
|
+
on: () => () => void 0
|
|
211
|
+
};
|
|
212
|
+
function useResize() {
|
|
213
|
+
const ctx = useMatterContext();
|
|
214
|
+
const [signal, setSignal] = (0, import_react6.useState)(null);
|
|
215
|
+
(0, import_react6.useEffect)(() => {
|
|
216
|
+
if (!ctx) return void 0;
|
|
217
|
+
const canvas = ctx.renderer.three.domElement;
|
|
218
|
+
if (!(canvas instanceof HTMLCanvasElement)) return void 0;
|
|
219
|
+
let value = [
|
|
220
|
+
canvas.clientWidth,
|
|
221
|
+
canvas.clientHeight,
|
|
222
|
+
typeof window !== "undefined" ? window.devicePixelRatio : 1
|
|
223
|
+
];
|
|
224
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
225
|
+
const fresh = {
|
|
226
|
+
get: () => value,
|
|
227
|
+
on: (_event, cb) => {
|
|
228
|
+
listeners.add(cb);
|
|
229
|
+
return () => {
|
|
230
|
+
listeners.delete(cb);
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
setSignal(fresh);
|
|
235
|
+
const emit = () => {
|
|
236
|
+
const next = [
|
|
237
|
+
canvas.clientWidth,
|
|
238
|
+
canvas.clientHeight,
|
|
239
|
+
typeof window !== "undefined" ? window.devicePixelRatio : 1
|
|
240
|
+
];
|
|
241
|
+
if (next[0] === value[0] && next[1] === value[1] && next[2] === value[2]) return;
|
|
242
|
+
value = next;
|
|
243
|
+
for (const cb of listeners) cb(next);
|
|
244
|
+
};
|
|
245
|
+
const observer = new ResizeObserver(emit);
|
|
246
|
+
observer.observe(canvas);
|
|
247
|
+
let mql = null;
|
|
248
|
+
let mqlHandler = null;
|
|
249
|
+
const setupDprWatch = () => {
|
|
250
|
+
if (typeof window === "undefined") return;
|
|
251
|
+
const dpr = window.devicePixelRatio;
|
|
252
|
+
const next = window.matchMedia(`(resolution: ${dpr}dppx)`);
|
|
253
|
+
const handler = () => {
|
|
254
|
+
emit();
|
|
255
|
+
if (mql && mqlHandler) mql.removeEventListener("change", mqlHandler);
|
|
256
|
+
setupDprWatch();
|
|
257
|
+
};
|
|
258
|
+
next.addEventListener("change", handler);
|
|
259
|
+
mql = next;
|
|
260
|
+
mqlHandler = handler;
|
|
261
|
+
};
|
|
262
|
+
setupDprWatch();
|
|
263
|
+
return () => {
|
|
264
|
+
observer.disconnect();
|
|
265
|
+
if (mql && mqlHandler) mql.removeEventListener("change", mqlHandler);
|
|
266
|
+
mql = null;
|
|
267
|
+
mqlHandler = null;
|
|
268
|
+
listeners.clear();
|
|
269
|
+
setSignal(null);
|
|
270
|
+
};
|
|
271
|
+
}, [ctx]);
|
|
272
|
+
return signal ?? STUB_SIGNAL2;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// src/useScroll.ts
|
|
276
|
+
var import_react7 = require("react");
|
|
277
|
+
var STUB_SIGNAL3 = {
|
|
278
|
+
get: () => [0, 0],
|
|
279
|
+
on: () => () => void 0
|
|
280
|
+
};
|
|
281
|
+
function useScroll() {
|
|
282
|
+
const [signal, setSignal] = (0, import_react7.useState)(null);
|
|
283
|
+
(0, import_react7.useEffect)(() => {
|
|
284
|
+
if (typeof window === "undefined") return void 0;
|
|
285
|
+
const compute = () => {
|
|
286
|
+
const y = window.scrollY;
|
|
287
|
+
const max = Math.max(document.documentElement.scrollHeight - window.innerHeight, 1);
|
|
288
|
+
const progress = Math.max(0, Math.min(1, y / max));
|
|
289
|
+
return [y, progress];
|
|
290
|
+
};
|
|
291
|
+
let value = compute();
|
|
292
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
293
|
+
const fresh = {
|
|
294
|
+
get: () => value,
|
|
295
|
+
on: (_event, cb) => {
|
|
296
|
+
listeners.add(cb);
|
|
297
|
+
return () => {
|
|
298
|
+
listeners.delete(cb);
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
};
|
|
302
|
+
setSignal(fresh);
|
|
303
|
+
let rafPending = false;
|
|
304
|
+
const onScroll = () => {
|
|
305
|
+
if (rafPending) return;
|
|
306
|
+
rafPending = true;
|
|
307
|
+
requestAnimationFrame(() => {
|
|
308
|
+
rafPending = false;
|
|
309
|
+
const next = compute();
|
|
310
|
+
if (next[0] === value[0] && next[1] === value[1]) return;
|
|
311
|
+
value = next;
|
|
312
|
+
for (const cb of listeners) cb(next);
|
|
313
|
+
});
|
|
314
|
+
};
|
|
315
|
+
window.addEventListener("scroll", onScroll, { passive: true });
|
|
316
|
+
return () => {
|
|
317
|
+
window.removeEventListener("scroll", onScroll);
|
|
318
|
+
listeners.clear();
|
|
319
|
+
setSignal(null);
|
|
320
|
+
};
|
|
321
|
+
}, []);
|
|
322
|
+
return signal ?? STUB_SIGNAL3;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// src/useAnimatableUniform.ts
|
|
326
|
+
var import_react8 = require("react");
|
|
327
|
+
var import_tsl = require("three/tsl");
|
|
328
|
+
var isSignal = (value) => {
|
|
329
|
+
return typeof value === "object" && value !== null && typeof value.get === "function" && typeof value.on === "function";
|
|
330
|
+
};
|
|
331
|
+
function useAnimatableUniform(value) {
|
|
332
|
+
const uniformNode = (0, import_react8.useMemo)(() => {
|
|
333
|
+
const initial = isSignal(value) ? value.get() : value;
|
|
334
|
+
return (0, import_tsl.uniform)(initial);
|
|
335
|
+
}, []);
|
|
336
|
+
(0, import_react8.useEffect)(() => {
|
|
337
|
+
if (isSignal(value)) {
|
|
338
|
+
const unsub = value.on("change", (next) => {
|
|
339
|
+
;
|
|
340
|
+
uniformNode.value = next;
|
|
341
|
+
});
|
|
342
|
+
return unsub;
|
|
343
|
+
}
|
|
344
|
+
;
|
|
345
|
+
uniformNode.value = value;
|
|
346
|
+
return void 0;
|
|
347
|
+
}, [value, uniformNode]);
|
|
348
|
+
return uniformNode;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
// src/FallbackBoundary.tsx
|
|
352
|
+
var import_react9 = require("react");
|
|
353
|
+
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
354
|
+
function FallbackBoundary({ fallback, children }) {
|
|
355
|
+
const [mounted, setMounted] = (0, import_react9.useState)(false);
|
|
356
|
+
(0, import_react9.useEffect)(() => {
|
|
357
|
+
setMounted(true);
|
|
358
|
+
}, []);
|
|
359
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_jsx_runtime2.Fragment, { children: mounted ? children : fallback ?? null });
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
// src/useStaticHint.ts
|
|
363
|
+
var import_react10 = require("react");
|
|
364
|
+
function useStaticHint(hint) {
|
|
365
|
+
const ctx = useMatterContext();
|
|
366
|
+
(0, import_react10.useEffect)(() => {
|
|
367
|
+
if (!ctx) return;
|
|
368
|
+
ctx.scheduler.setIdle(hint);
|
|
369
|
+
return () => ctx.scheduler.setIdle(false);
|
|
370
|
+
}, [ctx, hint]);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
// src/MatterMonitor.tsx
|
|
374
|
+
var import_react11 = require("react");
|
|
375
|
+
var import_jsx_runtime3 = require("react/jsx-runtime");
|
|
376
|
+
var anchorStyle = {
|
|
377
|
+
"top-left": { top: 8, left: 8 },
|
|
378
|
+
"top-right": { top: 8, right: 8 },
|
|
379
|
+
"bottom-left": { bottom: 8, left: 8 },
|
|
380
|
+
"bottom-right": { bottom: 8, right: 8 }
|
|
381
|
+
};
|
|
382
|
+
var baseStyle = {
|
|
383
|
+
position: "absolute",
|
|
384
|
+
zIndex: 10,
|
|
385
|
+
padding: "6px 8px",
|
|
386
|
+
borderRadius: 6,
|
|
387
|
+
background: "rgba(0, 0, 0, 0.6)",
|
|
388
|
+
color: "#fff",
|
|
389
|
+
font: "11px ui-monospace, monospace",
|
|
390
|
+
lineHeight: 1.4,
|
|
391
|
+
pointerEvents: "none",
|
|
392
|
+
whiteSpace: "pre"
|
|
393
|
+
};
|
|
394
|
+
function MatterMonitor({ anchor = "top-right" }) {
|
|
395
|
+
const ctx = (0, import_react11.useContext)(MatterContext);
|
|
396
|
+
const [stats, setStats] = (0, import_react11.useState)({ fps: 0, ticks: 0, frames: 0 });
|
|
397
|
+
const ticksRef = (0, import_react11.useRef)(0);
|
|
398
|
+
const fpsAccumRef = (0, import_react11.useRef)({ frames: 0, lastSampleAt: 0, fps: 0 });
|
|
399
|
+
(0, import_react11.useEffect)(() => {
|
|
400
|
+
if (!ctx) return;
|
|
401
|
+
const client = (tick) => {
|
|
402
|
+
ticksRef.current += 1;
|
|
403
|
+
const acc = fpsAccumRef.current;
|
|
404
|
+
acc.frames += 1;
|
|
405
|
+
if (acc.lastSampleAt === 0) acc.lastSampleAt = tick.now;
|
|
406
|
+
const dt = tick.now - acc.lastSampleAt;
|
|
407
|
+
if (dt >= 500) {
|
|
408
|
+
acc.fps = Math.round(acc.frames * 1e3 / dt);
|
|
409
|
+
acc.frames = 0;
|
|
410
|
+
acc.lastSampleAt = tick.now;
|
|
411
|
+
}
|
|
412
|
+
setStats({ fps: acc.fps, ticks: ticksRef.current, frames: acc.frames });
|
|
413
|
+
};
|
|
414
|
+
ctx.scheduler.add(client);
|
|
415
|
+
return () => ctx.scheduler.remove(client);
|
|
416
|
+
}, [ctx]);
|
|
417
|
+
if (!ctx) {
|
|
418
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { "data-testid": "matter-monitor", style: { ...baseStyle, ...anchorStyle[anchor] }, children: "no scene" });
|
|
419
|
+
}
|
|
420
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { "data-testid": "matter-monitor", style: { ...baseStyle, ...anchorStyle[anchor] }, children: [
|
|
421
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("span", { "data-testid": "matter-monitor-fps", children: [
|
|
422
|
+
"fps: ",
|
|
423
|
+
stats.fps || "\u2014"
|
|
424
|
+
] }),
|
|
425
|
+
"\n",
|
|
426
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("span", { "data-testid": "matter-monitor-ticks", children: [
|
|
427
|
+
"ticks: ",
|
|
428
|
+
stats.ticks
|
|
429
|
+
] })
|
|
430
|
+
] });
|
|
431
|
+
}
|
|
432
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
433
|
+
0 && (module.exports = {
|
|
434
|
+
FallbackBoundary,
|
|
435
|
+
MatterMonitor,
|
|
436
|
+
MatterScene,
|
|
437
|
+
useAnimatableUniform,
|
|
438
|
+
useCursor,
|
|
439
|
+
useMatterContext,
|
|
440
|
+
useResize,
|
|
441
|
+
useScroll,
|
|
442
|
+
useShaderMaterial,
|
|
443
|
+
useStaticHint
|
|
444
|
+
});
|
|
445
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/MatterScene.tsx","../src/matter-context.ts","../src/useMatterContext.ts","../src/useShaderMaterial.ts","../src/useCursor.ts","../src/useResize.ts","../src/useScroll.ts","../src/useAnimatableUniform.ts","../src/FallbackBoundary.tsx","../src/useStaticHint.ts","../src/MatterMonitor.tsx"],"sourcesContent":["// @lovo/matter-react — React binding for Matter.\n\nexport { MatterScene } from './MatterScene.js'\nexport type { MatterSceneProps } from './MatterScene.js'\n\nexport { useMatterContext } from './useMatterContext.js'\nexport type { MatterContextValue } from './matter-context.js'\n\nexport { useShaderMaterial } from './useShaderMaterial.js'\n\nexport { useCursor } from './useCursor.js'\nexport type { CursorSignal } from './useCursor.js'\n\nexport { useResize } from './useResize.js'\nexport type { ResizeSignal, ResizeValue } from './useResize.js'\n\nexport { useScroll } from './useScroll.js'\nexport type { ScrollSignal, ScrollValue } from './useScroll.js'\n\nexport { useAnimatableUniform } from './useAnimatableUniform.js'\nexport type { AnimatableProp, MatterSignal } from './useAnimatableUniform.js'\n\nexport { FallbackBoundary } from './FallbackBoundary.js'\nexport type { FallbackBoundaryProps } from './FallbackBoundary.js'\n\nexport { useStaticHint } from './useStaticHint.js'\n\nexport { MatterMonitor } from './MatterMonitor.js'\nexport type { MatterMonitorProps, MonitorAnchor } from './MatterMonitor.js'\n","'use client'\n\nimport { useEffect, useRef, useState, type CSSProperties, type ReactNode } from 'react'\nimport { Scene, OrthographicCamera } from 'three'\nimport { createRenderer, MatterScheduler, createVisibilityWatcher, createIntersectionWatcher } from '@lovo/matter'\nimport { MatterContext, type MatterContextValue } from './matter-context.js'\n\nexport interface MatterSceneProps {\n children?: ReactNode\n /** Rendered server-side and during WebGPU init. Default: empty. */\n fallback?: ReactNode\n className?: string\n style?: CSSProperties\n /** Cap on devicePixelRatio. Default: 2. */\n maxDPR?: number\n}\n\nconst defaultStyle: CSSProperties = {\n position: 'absolute',\n inset: 0,\n display: 'block',\n width: '100%',\n height: '100%',\n}\n\n/**\n * Owns a canvas, a Three.js renderer (WebGPU + WebGL2 fallback), an\n * orthographic camera covering the canvas, an empty Scene, and a\n * MatterScheduler. Children consume these via useMatterContext().\n */\nexport function MatterScene(props: MatterSceneProps) {\n const { children, fallback, className, style, maxDPR } = props\n const canvasRef = useRef<HTMLCanvasElement>(null)\n const [ctx, setCtx] = useState<MatterContextValue | null>(null)\n const [error, setError] = useState<Error | null>(null)\n\n useEffect(() => {\n const canvas = canvasRef.current\n if (!canvas) return\n\n let cancelled = false\n let cleanup: (() => void) | null = null\n\n const setup = async () => {\n try {\n const renderer = await createRenderer(canvas, { maxDPR })\n if (cancelled) {\n renderer.dispose()\n return\n }\n const scene = new Scene()\n const camera = new OrthographicCamera(-1, 1, 1, -1, 0.1, 10)\n camera.position.z = 1\n const scheduler = new MatterScheduler()\n\n scheduler.add(() => renderer.three.render(scene, camera))\n scheduler.start()\n\n const visibility = createVisibilityWatcher()\n const intersection = createIntersectionWatcher(canvas)\n\n const updatePauseState = () => {\n const shouldRun = visibility.isVisible() && intersection.isInView()\n if (shouldRun) scheduler.resume()\n else scheduler.pause()\n }\n updatePauseState()\n\n const unsubVisibility = visibility.subscribe(updatePauseState)\n const unsubIntersection = intersection.subscribe(updatePauseState)\n\n const onResize = () => renderer.resize()\n window.addEventListener('resize', onResize)\n\n cleanup = () => {\n unsubVisibility()\n unsubIntersection()\n visibility.dispose()\n intersection.dispose()\n window.removeEventListener('resize', onResize)\n scheduler.dispose()\n renderer.dispose()\n }\n\n setCtx({ renderer, scene, camera, scheduler })\n } catch (err) {\n if (cancelled) return\n const e = err instanceof Error ? err : new Error(String(err))\n // eslint-disable-next-line no-console\n console.error('[MatterScene] renderer init failed:', e)\n setError(e)\n }\n }\n\n void setup()\n return () => {\n cancelled = true\n cleanup?.()\n cleanup = null\n setCtx(null)\n }\n }, [maxDPR])\n\n return (\n <div className={className} style={{ ...defaultStyle, ...style }}>\n <canvas ref={canvasRef} style={{ width: '100%', height: '100%', display: 'block' }} />\n {error ? (\n <div\n style={{\n position: 'absolute',\n inset: 0,\n display: 'flex',\n alignItems: 'center',\n justifyContent: 'center',\n padding: '1rem',\n color: '#fff',\n background: 'rgba(120, 30, 30, 0.85)',\n font: '0.85rem ui-monospace, monospace',\n whiteSpace: 'pre-wrap',\n textAlign: 'center',\n }}\n >\n MatterScene init failed:\n {'\\n'}\n {error.message}\n </div>\n ) : ctx ? (\n <MatterContext.Provider value={ctx}>{children}</MatterContext.Provider>\n ) : (\n fallback ?? null\n )}\n </div>\n )\n}\n","import { createContext } from 'react'\nimport type { Scene, Camera } from 'three'\nimport type { MatterRenderer, MatterScheduler } from '@lovo/matter'\n\nexport interface MatterContextValue {\n renderer: MatterRenderer\n scene: Scene\n camera: Camera\n scheduler: MatterScheduler\n}\n\nexport const MatterContext = createContext<MatterContextValue | null>(null)\n","import { useContext } from 'react'\nimport { MatterContext, type MatterContextValue } from './matter-context.js'\n\n/**\n * Read the matter scene context. Returns null when called outside a\n * <MatterScene>; useShaderMaterial and similar hooks check this and\n * auto-provision a scene if missing (auto-wrap behavior).\n */\nexport function useMatterContext(): MatterContextValue | null {\n return useContext(MatterContext)\n}\n","'use client'\n\nimport { useEffect, useMemo } from 'react'\nimport { MeshBasicNodeMaterial } from 'three/webgpu'\nimport type { Node } from 'three/webgpu'\nimport type { ShaderNodeObject } from 'three/tsl'\n\n/** A TSL fragment that produces a color. Accept any Node or TSL-wrapped node. */\nexport type ColorTSL = Node | ShaderNodeObject<Node>\n\n/**\n * Bind a TSL color expression to a NodeMaterial. Returns the material;\n * caller is responsible for adding it to a mesh and disposing when done.\n *\n * The TSL fragment is computed once via `useMemo` and re-applied if the\n * factory function changes. For dynamic uniforms, mutate `.value` on the\n * uniform nodes — don't recreate the TSL fragment per render.\n */\nexport function useShaderMaterial(build: () => ColorTSL): MeshBasicNodeMaterial {\n const material = useMemo(() => {\n const m = new MeshBasicNodeMaterial()\n m.colorNode = build() as Node\n return m\n }, [build])\n\n useEffect(() => {\n return () => material.dispose()\n }, [material])\n\n return material\n}\n","'use client'\n\nimport { useEffect, useState } from 'react'\nimport { CursorInput, type CursorInputOptions, type Vec2 } from '@lovo/matter'\nimport { useMatterContext } from './useMatterContext.js'\n\nexport interface CursorSignal {\n /** Current smoothed cursor position (Vec2 in 0..1 viewport space). */\n get(): Vec2\n /** Subscribe to change events. Returns unsubscribe. */\n on(event: 'change', cb: (value: Vec2) => void): () => void\n}\n\n// Inert stub returned on the first render before the lifecycle effect\n// has created the real CursorInput. Calling .on returns an unsub no-op.\nconst STUB_SIGNAL: CursorSignal = {\n get: () => [0.5, 0.5] as const,\n on: () => () => undefined,\n}\n\n/**\n * React wrapper for CursorInput. Auto-attaches to the parent <MatterScene>'s\n * scheduler if available; otherwise creates a free-running rAF tick.\n *\n * Lifecycle is in a single effect so React 19 Strict Mode's intentional\n * mount→unmount→mount cycle creates a *fresh* CursorInput per real mount\n * instead of disposing a long-lived one (which would silently break the\n * window mousemove listener and the smoothing tick).\n */\nexport function useCursor(opts: CursorInputOptions = {}): CursorSignal {\n const ctx = useMatterContext()\n const [input, setInput] = useState<CursorInput | null>(null)\n\n useEffect(() => {\n // Plumb the parent <MatterScene>'s canvas as the cursor's normalization\n // element. Without this, cursor coords are viewport-normalized — fine for\n // a full-page scene but visibly offset when the canvas sits inside a\n // smaller wrapper (e.g., 70vh hero). DotField's cell tiling makes the\n // mismatch obvious; LinearGradient mostly gets away with it. Caller can\n // override by passing `opts.element` explicitly.\n const canvas = ctx?.renderer.three.domElement\n const elementOpt = opts.element ?? (canvas instanceof HTMLElement ? canvas : undefined)\n const fresh = new CursorInput({ ...opts, element: elementOpt })\n setInput(fresh)\n\n let detach: (() => void) | null = null\n if (ctx?.scheduler) {\n const client = ({ delta }: { delta: number }) => fresh.tick(delta)\n ctx.scheduler.add(client)\n detach = () => ctx.scheduler.remove(client)\n } else {\n let raf: number | null = null\n let lastNow = performance.now()\n const loop = (now: number) => {\n const delta = (now - lastNow) / 1000\n lastNow = now\n fresh.tick(delta)\n raf = requestAnimationFrame(loop)\n }\n raf = requestAnimationFrame(loop)\n detach = () => {\n if (raf !== null) cancelAnimationFrame(raf)\n }\n }\n\n return () => {\n detach?.()\n fresh.dispose()\n setInput(null)\n }\n // We intentionally only re-create on ctx change, not opts (which is a\n // fresh object literal each render). Smoothing tweaks during dev are\n // applied by remounting the parent component.\n }, [ctx])\n\n return input ?? STUB_SIGNAL\n}\n","'use client'\n\nimport { useEffect, useState } from 'react'\nimport { useMatterContext } from './useMatterContext.js'\n\nexport type ResizeValue = readonly [width: number, height: number, dpr: number]\n\nexport interface ResizeSignal {\n /** Current size in CSS pixels + devicePixelRatio. */\n get(): ResizeValue\n on(event: 'change', cb: (value: ResizeValue) => void): () => void\n}\n\n// Inert stub returned on the first render before the lifecycle effect has\n// observed the canvas. Subscribing to it returns a no-op unsubscribe.\nconst STUB_SIGNAL: ResizeSignal = {\n get: () => [0, 0, 1] as const,\n on: () => () => undefined,\n}\n\n/**\n * Track the parent <MatterScene>'s canvas size + DPR. Exposes a MatterSignal\n * that components can pass into a TSL uniform to make pixel-aware effects\n * (e.g., DotField's pixel-spacing math).\n *\n * Strict-Mode-safe: lifecycle is in one effect, so React 19's intentional\n * mount→unmount→mount cycle creates a fresh ResizeObserver per real mount\n * (CLAUDE.md gotcha #14).\n *\n * Falls back to the stub signal until the parent context is ready.\n */\nexport function useResize(): ResizeSignal {\n const ctx = useMatterContext()\n const [signal, setSignal] = useState<ResizeSignal | null>(null)\n\n useEffect(() => {\n if (!ctx) return undefined\n\n const canvas = ctx.renderer.three.domElement\n if (!(canvas instanceof HTMLCanvasElement)) return undefined\n\n let value: ResizeValue = [\n canvas.clientWidth,\n canvas.clientHeight,\n typeof window !== 'undefined' ? window.devicePixelRatio : 1,\n ]\n const listeners = new Set<(v: ResizeValue) => void>()\n const fresh: ResizeSignal = {\n get: () => value,\n on: (_event, cb) => {\n listeners.add(cb)\n return () => {\n listeners.delete(cb)\n }\n },\n }\n setSignal(fresh)\n\n const emit = () => {\n const next: ResizeValue = [\n canvas.clientWidth,\n canvas.clientHeight,\n typeof window !== 'undefined' ? window.devicePixelRatio : 1,\n ]\n if (next[0] === value[0] && next[1] === value[1] && next[2] === value[2]) return\n value = next\n for (const cb of listeners) cb(next)\n }\n\n const observer = new ResizeObserver(emit)\n observer.observe(canvas)\n\n // Cross-browser DPR-change watch. matchMedia(`(resolution: <dpr>dppx)`)\n // matches at the *current* DPR; when the user zooms the page the query\n // stops matching, fires `change`, and we re-arm the watch at the new DPR.\n // We track the current MQL + handler so we can fully detach in cleanup\n // (the handler is captured by the listener — passing a fresh closure to\n // removeEventListener wouldn't actually unregister it).\n let mql: MediaQueryList | null = null\n let mqlHandler: (() => void) | null = null\n const setupDprWatch = () => {\n if (typeof window === 'undefined') return\n const dpr = window.devicePixelRatio\n const next = window.matchMedia(`(resolution: ${dpr}dppx)`)\n const handler = () => {\n emit()\n if (mql && mqlHandler) mql.removeEventListener('change', mqlHandler)\n setupDprWatch()\n }\n next.addEventListener('change', handler)\n mql = next\n mqlHandler = handler\n }\n setupDprWatch()\n\n return () => {\n observer.disconnect()\n if (mql && mqlHandler) mql.removeEventListener('change', mqlHandler)\n mql = null\n mqlHandler = null\n listeners.clear()\n setSignal(null)\n }\n }, [ctx])\n\n return signal ?? STUB_SIGNAL\n}\n","'use client'\n\nimport { useEffect, useState } from 'react'\n\nexport type ScrollValue = readonly [scrollY: number, progress: number]\n\nexport interface ScrollSignal {\n /** Current scroll Y (px) and normalized progress in [0,1]. */\n get(): ScrollValue\n on(event: 'change', cb: (value: ScrollValue) => void): () => void\n}\n\n// Inert stub returned during SSR + on the first client render before the\n// lifecycle effect attaches. Subscribing to it returns a no-op unsubscribe.\nconst STUB_SIGNAL: ScrollSignal = {\n get: () => [0, 0] as const,\n on: () => () => undefined,\n}\n\n/**\n * Track window scroll position. Exposes a MatterSignal of `[scrollY, progress]`\n * where `progress` is `scrollY / max(documentHeight - innerHeight, 1)` clamped\n * to [0, 1]. Listener is rAF-throttled and `passive: true` so it never blocks\n * scrolling.\n *\n * No v1 Tier 1 component consumes this hook; it ships so users can pass\n * `inputs={{ scroll: useScroll() }}` to any Matter component.\n *\n * Strict-Mode-safe: lifecycle is in one effect, so React 19's intentional\n * mount→unmount→mount cycle in dev creates a fresh listener pair per real\n * mount and tears down cleanly on each pseudo-unmount (CLAUDE.md gotcha #14).\n *\n * **Known limitation (v1):** `progress` is computed against whichever\n * `documentHeight` was current when the last scroll fired. If the page grows\n * after mount (async content, font load reflow, expanding panels) without\n * the user scrolling, the denominator goes stale. A future ResizeObserver/\n * MutationObserver pass would close the gap; deferred until a v1 component\n * consumes scroll input.\n */\nexport function useScroll(): ScrollSignal {\n const [signal, setSignal] = useState<ScrollSignal | null>(null)\n\n useEffect(() => {\n if (typeof window === 'undefined') return undefined\n\n const compute = (): ScrollValue => {\n const y = window.scrollY\n // For pages shorter than the viewport, `documentHeight - innerHeight` is\n // <= 0; clamp to 1 to avoid div-by-zero. Progress stays at 0 in that\n // case because scrollY is also 0.\n const max = Math.max(document.documentElement.scrollHeight - window.innerHeight, 1)\n const progress = Math.max(0, Math.min(1, y / max))\n return [y, progress]\n }\n\n let value: ScrollValue = compute()\n const listeners = new Set<(v: ScrollValue) => void>()\n const fresh: ScrollSignal = {\n get: () => value,\n on: (_event, cb) => {\n listeners.add(cb)\n return () => {\n listeners.delete(cb)\n }\n },\n }\n setSignal(fresh)\n\n let rafPending = false\n const onScroll = () => {\n if (rafPending) return\n rafPending = true\n requestAnimationFrame(() => {\n rafPending = false\n const next = compute()\n if (next[0] === value[0] && next[1] === value[1]) return\n value = next\n for (const cb of listeners) cb(next)\n })\n }\n window.addEventListener('scroll', onScroll, { passive: true })\n\n return () => {\n window.removeEventListener('scroll', onScroll)\n listeners.clear()\n setSignal(null)\n }\n }, [])\n\n return signal ?? STUB_SIGNAL\n}\n","'use client'\n\nimport { useEffect, useMemo } from 'react'\nimport { uniform } from 'three/tsl'\nimport type { ShaderNodeObject } from 'three/tsl'\nimport type { Node } from 'three/webgpu'\n\nexport interface MatterSignal<T> {\n get(): T\n on(event: 'change', cb: (value: T) => void): () => void\n}\n\nexport type AnimatableProp<T> = T | MatterSignal<T>\n\nconst isSignal = <T>(value: AnimatableProp<T>): value is MatterSignal<T> => {\n return (\n typeof value === 'object' &&\n value !== null &&\n typeof (value as MatterSignal<T>).get === 'function' &&\n typeof (value as MatterSignal<T>).on === 'function'\n )\n}\n\n/**\n * Bind an AnimatableProp<T> to a TSL uniform. Plain values create a\n * static uniform that updates only when the prop changes (React render\n * path). Signals subscribe via .on('change') and write into the uniform\n * imperatively without re-rendering.\n */\nexport function useAnimatableUniform<T>(value: AnimatableProp<T>): ShaderNodeObject<Node> {\n // Create the uniform once with the initial value; subsequent updates flow\n // through the effect below (either via signal subscription or direct write).\n const uniformNode = useMemo(() => {\n const initial = isSignal(value) ? value.get() : value\n return uniform(initial) as unknown as ShaderNodeObject<Node>\n }, [])\n\n useEffect(() => {\n if (isSignal(value)) {\n const unsub = value.on('change', (next) => {\n ;(uniformNode as unknown as { value: T }).value = next\n })\n return unsub\n }\n ;(uniformNode as unknown as { value: T }).value = value\n return undefined\n }, [value, uniformNode])\n\n return uniformNode\n}\n","'use client'\n\nimport { useEffect, useState, type ReactNode } from 'react'\n\nexport interface FallbackBoundaryProps {\n /** Rendered until WebGPU/WebGL is available on the client. */\n fallback?: ReactNode\n children: ReactNode\n}\n\n/**\n * Render `fallback` until the component mounts on the client. Gates the\n * children behind client-only mounting so SSR/no-WebGPU users see a\n * sensible static placeholder rather than a flash of nothing.\n */\nexport function FallbackBoundary({ fallback, children }: FallbackBoundaryProps) {\n const [mounted, setMounted] = useState(false)\n useEffect(() => {\n setMounted(true)\n }, [])\n return <>{mounted ? children : fallback ?? null}</>\n}\n","'use client'\n\nimport { useEffect } from 'react'\nimport { useMatterContext } from './useMatterContext.js'\n\n/**\n * Opt a component out of the rAF loop while it has no dynamic uniforms.\n *\n * When `hint` is true, the scheduler runs one final flush tick (so any\n * uniform changes since the last frame are rendered) and then halts the\n * rAF loop until either `hint` becomes false or another component in the\n * same scene calls `scheduler.requestRender()`.\n *\n * Use for components whose animation is fully derived from props that don't\n * include `time`, e.g. `<LinearGradient speed={0}>` with no `interactive`.\n */\nexport function useStaticHint(hint: boolean): void {\n const ctx = useMatterContext()\n useEffect(() => {\n if (!ctx) return\n ctx.scheduler.setIdle(hint)\n return () => ctx.scheduler.setIdle(false)\n }, [ctx, hint])\n}\n","'use client'\n\nimport { useEffect, useRef, useState, useContext, type CSSProperties } from 'react'\nimport { MatterContext } from './matter-context.js'\n\nexport type MonitorAnchor = 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right'\n\nconst anchorStyle: Record<MonitorAnchor, CSSProperties> = {\n 'top-left': { top: 8, left: 8 },\n 'top-right': { top: 8, right: 8 },\n 'bottom-left': { bottom: 8, left: 8 },\n 'bottom-right': { bottom: 8, right: 8 },\n}\n\nconst baseStyle: CSSProperties = {\n position: 'absolute',\n zIndex: 10,\n padding: '6px 8px',\n borderRadius: 6,\n background: 'rgba(0, 0, 0, 0.6)',\n color: '#fff',\n font: '11px ui-monospace, monospace',\n lineHeight: 1.4,\n pointerEvents: 'none',\n whiteSpace: 'pre',\n}\n\nexport interface MatterMonitorProps {\n anchor?: MonitorAnchor\n}\n\n/**\n * Dev-only overlay that displays the current scene's FPS, tick count, and\n * paused/idle state. Reads from the surrounding `<MatterScene>` via context\n * and subscribes to its scheduler. Renders nothing useful if mounted outside\n * a scene.\n */\nexport function MatterMonitor({ anchor = 'top-right' }: MatterMonitorProps) {\n const ctx = useContext(MatterContext)\n const [stats, setStats] = useState({ fps: 0, ticks: 0, frames: 0 })\n const ticksRef = useRef(0)\n const fpsAccumRef = useRef({ frames: 0, lastSampleAt: 0, fps: 0 })\n\n useEffect(() => {\n if (!ctx) return\n const client = (tick: { now: number }) => {\n ticksRef.current += 1\n const acc = fpsAccumRef.current\n acc.frames += 1\n if (acc.lastSampleAt === 0) acc.lastSampleAt = tick.now\n const dt = tick.now - acc.lastSampleAt\n if (dt >= 500) {\n acc.fps = Math.round((acc.frames * 1000) / dt)\n acc.frames = 0\n acc.lastSampleAt = tick.now\n }\n setStats({ fps: acc.fps, ticks: ticksRef.current, frames: acc.frames })\n }\n ctx.scheduler.add(client)\n return () => ctx.scheduler.remove(client)\n }, [ctx])\n\n if (!ctx) {\n return (\n <div data-testid=\"matter-monitor\" style={{ ...baseStyle, ...anchorStyle[anchor] }}>\n no scene\n </div>\n )\n }\n\n return (\n <div data-testid=\"matter-monitor\" style={{ ...baseStyle, ...anchorStyle[anchor] }}>\n <span data-testid=\"matter-monitor-fps\">fps: {stats.fps || '—'}</span>\n {'\\n'}\n <span data-testid=\"matter-monitor-ticks\">ticks: {stats.ticks}</span>\n </div>\n )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACEA,IAAAA,gBAAgF;AAChF,mBAA0C;AAC1C,oBAAoG;;;ACJpG,mBAA8B;AAWvB,IAAM,oBAAgB,4BAAyC,IAAI;;;AD8FpE;AAxFN,IAAM,eAA8B;AAAA,EAClC,UAAU;AAAA,EACV,OAAO;AAAA,EACP,SAAS;AAAA,EACT,OAAO;AAAA,EACP,QAAQ;AACV;AAOO,SAAS,YAAY,OAAyB;AACnD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AK,KAAK,MAAM,IAAI;AAC1B,UAAI,MAAM,KAAK;AACb,YAAI,MAAM,KAAK,MAAO,IAAI,SAAS,MAAQ,EAAE;AAC7C,YAAI,SAAS;AACb,YAAI,eAAe,KAAK;AAAA,MAC1B;AACA,eAAS,EAAE,KAAK,IAAI,KAAK,OAAO,SAAS,SAAS,QAAQ,IAAI,OAAO,CAAC;AAAA,IACxE;AACA,QAAI,UAAU,IAAI,MAAM;AACxB,WAAO,MAAM,IAAI,UAAU,OAAO,MAAM;AAAA,EAC1C,GAAG,CAAC,GAAG,CAAC;AAER,MAAI,CAAC,KAAK;AACR,WACE,6CAAC,SAAI,eAAY,kBAAiB,OAAO,EAAE,GAAG,WAAW,GAAG,YAAY,MAAM,EAAE,GAAG,sBAEnF;AAAA,EAEJ;AAEA,SACE,8CAAC,SAAI,eAAY,kBAAiB,OAAO,EAAE,GAAG,WAAW,GAAG,YAAY,MAAM,EAAE,GAC9E;AAAA,kDAAC,UAAK,eAAY,sBAAqB;AAAA;AAAA,MAAM,MAAM,OAAO;AAAA,OAAI;AAAA,IAC7D;AAAA,IACD,8CAAC,UAAK,eAAY,wBAAuB;AAAA;AAAA,MAAQ,MAAM;AAAA,OAAM;AAAA,KAC/D;AAEJ;","names":["import_react","import_react","import_react","import_react","import_matter","import_react","STUB_SIGNAL","import_react","STUB_SIGNAL","import_react","import_react","import_jsx_runtime","import_react","import_react","import_jsx_runtime"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import { ReactNode, CSSProperties } from 'react';
|
|
3
|
+
import { Scene, Camera } from 'three';
|
|
4
|
+
import { MatterRenderer, MatterScheduler, Vec2, CursorInputOptions } from '@lovo/matter';
|
|
5
|
+
import { Node, MeshBasicNodeMaterial } from 'three/webgpu';
|
|
6
|
+
import { ShaderNodeObject } from 'three/tsl';
|
|
7
|
+
|
|
8
|
+
interface MatterSceneProps {
|
|
9
|
+
children?: ReactNode;
|
|
10
|
+
/** Rendered server-side and during WebGPU init. Default: empty. */
|
|
11
|
+
fallback?: ReactNode;
|
|
12
|
+
className?: string;
|
|
13
|
+
style?: CSSProperties;
|
|
14
|
+
/** Cap on devicePixelRatio. Default: 2. */
|
|
15
|
+
maxDPR?: number;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Owns a canvas, a Three.js renderer (WebGPU + WebGL2 fallback), an
|
|
19
|
+
* orthographic camera covering the canvas, an empty Scene, and a
|
|
20
|
+
* MatterScheduler. Children consume these via useMatterContext().
|
|
21
|
+
*/
|
|
22
|
+
declare function MatterScene(props: MatterSceneProps): react_jsx_runtime.JSX.Element;
|
|
23
|
+
|
|
24
|
+
interface MatterContextValue {
|
|
25
|
+
renderer: MatterRenderer;
|
|
26
|
+
scene: Scene;
|
|
27
|
+
camera: Camera;
|
|
28
|
+
scheduler: MatterScheduler;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Read the matter scene context. Returns null when called outside a
|
|
33
|
+
* <MatterScene>; useShaderMaterial and similar hooks check this and
|
|
34
|
+
* auto-provision a scene if missing (auto-wrap behavior).
|
|
35
|
+
*/
|
|
36
|
+
declare function useMatterContext(): MatterContextValue | null;
|
|
37
|
+
|
|
38
|
+
/** A TSL fragment that produces a color. Accept any Node or TSL-wrapped node. */
|
|
39
|
+
type ColorTSL = Node | ShaderNodeObject<Node>;
|
|
40
|
+
/**
|
|
41
|
+
* Bind a TSL color expression to a NodeMaterial. Returns the material;
|
|
42
|
+
* caller is responsible for adding it to a mesh and disposing when done.
|
|
43
|
+
*
|
|
44
|
+
* The TSL fragment is computed once via `useMemo` and re-applied if the
|
|
45
|
+
* factory function changes. For dynamic uniforms, mutate `.value` on the
|
|
46
|
+
* uniform nodes — don't recreate the TSL fragment per render.
|
|
47
|
+
*/
|
|
48
|
+
declare function useShaderMaterial(build: () => ColorTSL): MeshBasicNodeMaterial;
|
|
49
|
+
|
|
50
|
+
interface CursorSignal {
|
|
51
|
+
/** Current smoothed cursor position (Vec2 in 0..1 viewport space). */
|
|
52
|
+
get(): Vec2;
|
|
53
|
+
/** Subscribe to change events. Returns unsubscribe. */
|
|
54
|
+
on(event: 'change', cb: (value: Vec2) => void): () => void;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* React wrapper for CursorInput. Auto-attaches to the parent <MatterScene>'s
|
|
58
|
+
* scheduler if available; otherwise creates a free-running rAF tick.
|
|
59
|
+
*
|
|
60
|
+
* Lifecycle is in a single effect so React 19 Strict Mode's intentional
|
|
61
|
+
* mount→unmount→mount cycle creates a *fresh* CursorInput per real mount
|
|
62
|
+
* instead of disposing a long-lived one (which would silently break the
|
|
63
|
+
* window mousemove listener and the smoothing tick).
|
|
64
|
+
*/
|
|
65
|
+
declare function useCursor(opts?: CursorInputOptions): CursorSignal;
|
|
66
|
+
|
|
67
|
+
type ResizeValue = readonly [width: number, height: number, dpr: number];
|
|
68
|
+
interface ResizeSignal {
|
|
69
|
+
/** Current size in CSS pixels + devicePixelRatio. */
|
|
70
|
+
get(): ResizeValue;
|
|
71
|
+
on(event: 'change', cb: (value: ResizeValue) => void): () => void;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Track the parent <MatterScene>'s canvas size + DPR. Exposes a MatterSignal
|
|
75
|
+
* that components can pass into a TSL uniform to make pixel-aware effects
|
|
76
|
+
* (e.g., DotField's pixel-spacing math).
|
|
77
|
+
*
|
|
78
|
+
* Strict-Mode-safe: lifecycle is in one effect, so React 19's intentional
|
|
79
|
+
* mount→unmount→mount cycle creates a fresh ResizeObserver per real mount
|
|
80
|
+
* (CLAUDE.md gotcha #14).
|
|
81
|
+
*
|
|
82
|
+
* Falls back to the stub signal until the parent context is ready.
|
|
83
|
+
*/
|
|
84
|
+
declare function useResize(): ResizeSignal;
|
|
85
|
+
|
|
86
|
+
type ScrollValue = readonly [scrollY: number, progress: number];
|
|
87
|
+
interface ScrollSignal {
|
|
88
|
+
/** Current scroll Y (px) and normalized progress in [0,1]. */
|
|
89
|
+
get(): ScrollValue;
|
|
90
|
+
on(event: 'change', cb: (value: ScrollValue) => void): () => void;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Track window scroll position. Exposes a MatterSignal of `[scrollY, progress]`
|
|
94
|
+
* where `progress` is `scrollY / max(documentHeight - innerHeight, 1)` clamped
|
|
95
|
+
* to [0, 1]. Listener is rAF-throttled and `passive: true` so it never blocks
|
|
96
|
+
* scrolling.
|
|
97
|
+
*
|
|
98
|
+
* No v1 Tier 1 component consumes this hook; it ships so users can pass
|
|
99
|
+
* `inputs={{ scroll: useScroll() }}` to any Matter component.
|
|
100
|
+
*
|
|
101
|
+
* Strict-Mode-safe: lifecycle is in one effect, so React 19's intentional
|
|
102
|
+
* mount→unmount→mount cycle in dev creates a fresh listener pair per real
|
|
103
|
+
* mount and tears down cleanly on each pseudo-unmount (CLAUDE.md gotcha #14).
|
|
104
|
+
*
|
|
105
|
+
* **Known limitation (v1):** `progress` is computed against whichever
|
|
106
|
+
* `documentHeight` was current when the last scroll fired. If the page grows
|
|
107
|
+
* after mount (async content, font load reflow, expanding panels) without
|
|
108
|
+
* the user scrolling, the denominator goes stale. A future ResizeObserver/
|
|
109
|
+
* MutationObserver pass would close the gap; deferred until a v1 component
|
|
110
|
+
* consumes scroll input.
|
|
111
|
+
*/
|
|
112
|
+
declare function useScroll(): ScrollSignal;
|
|
113
|
+
|
|
114
|
+
interface MatterSignal<T> {
|
|
115
|
+
get(): T;
|
|
116
|
+
on(event: 'change', cb: (value: T) => void): () => void;
|
|
117
|
+
}
|
|
118
|
+
type AnimatableProp<T> = T | MatterSignal<T>;
|
|
119
|
+
/**
|
|
120
|
+
* Bind an AnimatableProp<T> to a TSL uniform. Plain values create a
|
|
121
|
+
* static uniform that updates only when the prop changes (React render
|
|
122
|
+
* path). Signals subscribe via .on('change') and write into the uniform
|
|
123
|
+
* imperatively without re-rendering.
|
|
124
|
+
*/
|
|
125
|
+
declare function useAnimatableUniform<T>(value: AnimatableProp<T>): ShaderNodeObject<Node>;
|
|
126
|
+
|
|
127
|
+
interface FallbackBoundaryProps {
|
|
128
|
+
/** Rendered until WebGPU/WebGL is available on the client. */
|
|
129
|
+
fallback?: ReactNode;
|
|
130
|
+
children: ReactNode;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Render `fallback` until the component mounts on the client. Gates the
|
|
134
|
+
* children behind client-only mounting so SSR/no-WebGPU users see a
|
|
135
|
+
* sensible static placeholder rather than a flash of nothing.
|
|
136
|
+
*/
|
|
137
|
+
declare function FallbackBoundary({ fallback, children }: FallbackBoundaryProps): react_jsx_runtime.JSX.Element;
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Opt a component out of the rAF loop while it has no dynamic uniforms.
|
|
141
|
+
*
|
|
142
|
+
* When `hint` is true, the scheduler runs one final flush tick (so any
|
|
143
|
+
* uniform changes since the last frame are rendered) and then halts the
|
|
144
|
+
* rAF loop until either `hint` becomes false or another component in the
|
|
145
|
+
* same scene calls `scheduler.requestRender()`.
|
|
146
|
+
*
|
|
147
|
+
* Use for components whose animation is fully derived from props that don't
|
|
148
|
+
* include `time`, e.g. `<LinearGradient speed={0}>` with no `interactive`.
|
|
149
|
+
*/
|
|
150
|
+
declare function useStaticHint(hint: boolean): void;
|
|
151
|
+
|
|
152
|
+
type MonitorAnchor = 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right';
|
|
153
|
+
interface MatterMonitorProps {
|
|
154
|
+
anchor?: MonitorAnchor;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Dev-only overlay that displays the current scene's FPS, tick count, and
|
|
158
|
+
* paused/idle state. Reads from the surrounding `<MatterScene>` via context
|
|
159
|
+
* and subscribes to its scheduler. Renders nothing useful if mounted outside
|
|
160
|
+
* a scene.
|
|
161
|
+
*/
|
|
162
|
+
declare function MatterMonitor({ anchor }: MatterMonitorProps): react_jsx_runtime.JSX.Element;
|
|
163
|
+
|
|
164
|
+
export { type AnimatableProp, type CursorSignal, FallbackBoundary, type FallbackBoundaryProps, type MatterContextValue, MatterMonitor, type MatterMonitorProps, MatterScene, type MatterSceneProps, type MatterSignal, type MonitorAnchor, type ResizeSignal, type ResizeValue, type ScrollSignal, type ScrollValue, useAnimatableUniform, useCursor, useMatterContext, useResize, useScroll, useShaderMaterial, useStaticHint };
|