@particle-academy/fancy-3d 0.5.0 → 0.7.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.
@@ -0,0 +1,694 @@
1
+ 'use strict';
2
+
3
+ var chunkPBKOUAUY_cjs = require('./chunk-PBKOUAUY.cjs');
4
+ var react = require('react');
5
+ var jsxRuntime = require('react/jsx-runtime');
6
+ var lucideReact = require('lucide-react');
7
+
8
+ // src/utils/cn.ts
9
+ function flatten(value) {
10
+ if (!value) return "";
11
+ if (typeof value === "string" || typeof value === "number") return String(value);
12
+ if (Array.isArray(value)) return value.map(flatten).filter(Boolean).join(" ");
13
+ if (typeof value === "object") {
14
+ return Object.entries(value).filter(([, v]) => Boolean(v)).map(([k]) => k).join(" ");
15
+ }
16
+ return "";
17
+ }
18
+ function cn(...inputs) {
19
+ return inputs.map(flatten).filter(Boolean).join(" ");
20
+ }
21
+ function useControllableState(controlledValue, defaultValue, onChange) {
22
+ const [uncontrolledValue, setUncontrolledValue] = react.useState(defaultValue);
23
+ const isControlled = controlledValue !== void 0;
24
+ const value = isControlled ? controlledValue : uncontrolledValue;
25
+ const onChangeRef = react.useRef(onChange);
26
+ onChangeRef.current = onChange;
27
+ const setValue = react.useCallback(
28
+ (next) => {
29
+ const nextValue = typeof next === "function" ? next(value) : next;
30
+ if (!isControlled) {
31
+ setUncontrolledValue(nextValue);
32
+ }
33
+ onChangeRef.current?.(nextValue);
34
+ },
35
+ [isControlled, value]
36
+ );
37
+ return [value, setValue];
38
+ }
39
+ function usePanZoom({
40
+ viewport,
41
+ setViewport,
42
+ minZoom,
43
+ maxZoom,
44
+ pannable,
45
+ zoomable,
46
+ containerRef
47
+ }) {
48
+ const isPanningRef = react.useRef(false);
49
+ const startRef = react.useRef({ x: 0, y: 0, panX: 0, panY: 0 });
50
+ const viewportRef = react.useRef(viewport);
51
+ viewportRef.current = viewport;
52
+ const setViewportRef = react.useRef(setViewport);
53
+ setViewportRef.current = setViewport;
54
+ const zoomableRef = react.useRef(zoomable);
55
+ zoomableRef.current = zoomable;
56
+ const minZoomRef = react.useRef(minZoom);
57
+ minZoomRef.current = minZoom;
58
+ const maxZoomRef = react.useRef(maxZoom);
59
+ maxZoomRef.current = maxZoom;
60
+ react.useEffect(() => {
61
+ const container = containerRef.current;
62
+ if (!container) return;
63
+ function handleWheel(e) {
64
+ if (!zoomableRef.current) return;
65
+ if (!e.ctrlKey && !e.metaKey) return;
66
+ e.preventDefault();
67
+ const rect = container.getBoundingClientRect();
68
+ const mouseX = e.clientX - rect.left;
69
+ const mouseY = e.clientY - rect.top;
70
+ setViewportRef.current((prev) => {
71
+ const factor = 1 - e.deltaY * 1e-3;
72
+ const newZoom = Math.min(maxZoomRef.current, Math.max(minZoomRef.current, prev.zoom * factor));
73
+ const ratio = newZoom / prev.zoom;
74
+ return {
75
+ zoom: newZoom,
76
+ panX: mouseX - (mouseX - prev.panX) * ratio,
77
+ panY: mouseY - (mouseY - prev.panY) * ratio
78
+ };
79
+ });
80
+ }
81
+ container.addEventListener("wheel", handleWheel, { passive: false });
82
+ return () => {
83
+ container.removeEventListener("wheel", handleWheel);
84
+ };
85
+ }, [containerRef]);
86
+ const onPointerDown = react.useCallback(
87
+ (e) => {
88
+ if (!pannable) return;
89
+ if (e.button !== 0) return;
90
+ const target = e.target;
91
+ if (target !== containerRef.current && !target.hasAttribute("data-canvas-bg")) return;
92
+ isPanningRef.current = true;
93
+ startRef.current = { x: e.clientX, y: e.clientY, panX: viewport.panX, panY: viewport.panY };
94
+ e.target.setPointerCapture(e.pointerId);
95
+ e.preventDefault();
96
+ },
97
+ [pannable, viewport.panX, viewport.panY, containerRef]
98
+ );
99
+ const onPointerMove = react.useCallback(
100
+ (e) => {
101
+ if (!isPanningRef.current) return;
102
+ const dx = e.clientX - startRef.current.x;
103
+ const dy = e.clientY - startRef.current.y;
104
+ setViewport((prev) => ({
105
+ ...prev,
106
+ panX: startRef.current.panX + dx,
107
+ panY: startRef.current.panY + dy
108
+ }));
109
+ },
110
+ [setViewport]
111
+ );
112
+ const onPointerUp = react.useCallback(() => {
113
+ isPanningRef.current = false;
114
+ }, []);
115
+ return {
116
+ containerProps: { onPointerDown, onPointerMove, onPointerUp },
117
+ isPanning: isPanningRef.current
118
+ };
119
+ }
120
+ function useNodeRegistry() {
121
+ const rectsRef = react.useRef(/* @__PURE__ */ new Map());
122
+ const [version, setVersion] = react.useState(0);
123
+ const registerNode = react.useCallback((id, rect) => {
124
+ rectsRef.current.set(id, rect);
125
+ setVersion((v) => v + 1);
126
+ }, []);
127
+ const unregisterNode = react.useCallback((id) => {
128
+ rectsRef.current.delete(id);
129
+ setVersion((v) => v + 1);
130
+ }, []);
131
+ return { registerNode, unregisterNode, nodeRects: rectsRef.current, version };
132
+ }
133
+ var CanvasContext = react.createContext(null);
134
+ function useCanvas() {
135
+ const ctx = react.useContext(CanvasContext);
136
+ if (!ctx) throw new Error("useCanvas must be used within a Canvas component");
137
+ return ctx;
138
+ }
139
+ function CanvasNode({ children, id, x, y, draggable, onPositionChange, className, style }) {
140
+ const { registerNode, unregisterNode, viewport, gridSize, snapToGrid } = useCanvas();
141
+ const nodeRef = react.useRef(null);
142
+ const isDragging = react.useRef(false);
143
+ const dragStart = react.useRef({ mouseX: 0, mouseY: 0, nodeX: 0, nodeY: 0 });
144
+ react.useEffect(() => {
145
+ const el = nodeRef.current;
146
+ if (!el) return;
147
+ const updateRect = () => {
148
+ registerNode(id, { x, y, width: el.offsetWidth, height: el.offsetHeight });
149
+ };
150
+ updateRect();
151
+ const observer = new ResizeObserver(updateRect);
152
+ observer.observe(el);
153
+ return () => {
154
+ observer.disconnect();
155
+ unregisterNode(id);
156
+ };
157
+ }, [id, x, y, registerNode, unregisterNode]);
158
+ const handlePointerDown = react.useCallback(
159
+ (e) => {
160
+ if (!draggable || e.button !== 0) return;
161
+ e.stopPropagation();
162
+ isDragging.current = true;
163
+ dragStart.current = { mouseX: e.clientX, mouseY: e.clientY, nodeX: x, nodeY: y };
164
+ e.target.setPointerCapture(e.pointerId);
165
+ },
166
+ [draggable, x, y]
167
+ );
168
+ const handlePointerMove = react.useCallback(
169
+ (e) => {
170
+ if (!isDragging.current) return;
171
+ const dx = (e.clientX - dragStart.current.mouseX) / viewport.zoom;
172
+ const dy = (e.clientY - dragStart.current.mouseY) / viewport.zoom;
173
+ let nx = dragStart.current.nodeX + dx;
174
+ let ny = dragStart.current.nodeY + dy;
175
+ if (snapToGrid && gridSize > 0) {
176
+ nx = Math.round(nx / gridSize) * gridSize;
177
+ ny = Math.round(ny / gridSize) * gridSize;
178
+ }
179
+ onPositionChange?.(nx, ny);
180
+ },
181
+ [viewport.zoom, onPositionChange, snapToGrid, gridSize]
182
+ );
183
+ const handlePointerUp = react.useCallback(() => {
184
+ isDragging.current = false;
185
+ }, []);
186
+ return /* @__PURE__ */ jsxRuntime.jsx(
187
+ "div",
188
+ {
189
+ ref: nodeRef,
190
+ "data-react-fancy-canvas-node": "",
191
+ "data-node-id": id,
192
+ className: cn("absolute", draggable && "cursor-grab active:cursor-grabbing", className),
193
+ style: { left: x, top: y, ...style },
194
+ onPointerDown: handlePointerDown,
195
+ onPointerMove: handlePointerMove,
196
+ onPointerUp: handlePointerUp,
197
+ children
198
+ }
199
+ );
200
+ }
201
+ CanvasNode.displayName = "CanvasNode";
202
+
203
+ // src/canvas/canvas.utils.ts
204
+ function getAnchorPoint(rect, anchor, otherRect) {
205
+ const cx = rect.x + rect.width / 2;
206
+ const cy = rect.y + rect.height / 2;
207
+ if (anchor === "auto" && otherRect) {
208
+ const ocx = otherRect.x + otherRect.width / 2;
209
+ const ocy = otherRect.y + otherRect.height / 2;
210
+ const dx = ocx - cx;
211
+ const dy = ocy - cy;
212
+ if (Math.abs(dx) > Math.abs(dy)) {
213
+ return dx > 0 ? { x: rect.x + rect.width, y: cy } : { x: rect.x, y: cy };
214
+ }
215
+ return dy > 0 ? { x: cx, y: rect.y + rect.height } : { x: cx, y: rect.y };
216
+ }
217
+ switch (anchor) {
218
+ case "top":
219
+ return { x: cx, y: rect.y };
220
+ case "bottom":
221
+ return { x: cx, y: rect.y + rect.height };
222
+ case "left":
223
+ return { x: rect.x, y: cy };
224
+ case "right":
225
+ return { x: rect.x + rect.width, y: cy };
226
+ case "center":
227
+ return { x: cx, y: cy };
228
+ default:
229
+ return { x: cx, y: cy };
230
+ }
231
+ }
232
+ function bezierPath(from, to) {
233
+ const dx = Math.abs(to.x - from.x);
234
+ const dy = Math.abs(to.y - from.y);
235
+ if (dx > dy) {
236
+ const offset2 = dx * 0.5;
237
+ const cp1x = from.x + (to.x > from.x ? offset2 : -offset2);
238
+ const cp2x = to.x + (to.x > from.x ? -offset2 : offset2);
239
+ return `M${from.x},${from.y} C${cp1x},${from.y} ${cp2x},${to.y} ${to.x},${to.y}`;
240
+ }
241
+ const offset = Math.max(dy * 0.5, 30);
242
+ const cp1y = from.y + (to.y > from.y ? offset : -offset);
243
+ const cp2y = to.y + (to.y > from.y ? -offset : offset);
244
+ return `M${from.x},${from.y} C${from.x},${cp1y} ${to.x},${cp2y} ${to.x},${to.y}`;
245
+ }
246
+ function stepPath(from, to) {
247
+ const midX = (from.x + to.x) / 2;
248
+ return `M${from.x},${from.y} H${midX} V${to.y} H${to.x}`;
249
+ }
250
+ function straightPath(from, to) {
251
+ return `M${from.x},${from.y} L${to.x},${to.y}`;
252
+ }
253
+ function getEdgePath(from, to, curve = "bezier") {
254
+ switch (curve) {
255
+ case "bezier":
256
+ return bezierPath(from, to);
257
+ case "step":
258
+ return stepPath(from, to);
259
+ case "straight":
260
+ return straightPath(from, to);
261
+ }
262
+ }
263
+ function CanvasEdge({
264
+ from,
265
+ to,
266
+ fromAnchor = "auto",
267
+ toAnchor = "auto",
268
+ curve = "bezier",
269
+ color = "currentColor",
270
+ strokeWidth = 2,
271
+ dashed = false,
272
+ animated = false,
273
+ label,
274
+ className,
275
+ markerStart,
276
+ markerEnd
277
+ }) {
278
+ const { nodeRects, registryVersion } = useCanvas();
279
+ const path = react.useMemo(() => {
280
+ const fromRect = nodeRects.get(from);
281
+ const toRect = nodeRects.get(to);
282
+ if (!fromRect || !toRect) return null;
283
+ const fromPt = getAnchorPoint(fromRect, fromAnchor, toRect);
284
+ const toPt = getAnchorPoint(toRect, toAnchor, fromRect);
285
+ return {
286
+ d: getEdgePath(fromPt, toPt, curve),
287
+ midX: (fromPt.x + toPt.x) / 2,
288
+ midY: (fromPt.y + toPt.y) / 2
289
+ };
290
+ }, [from, to, fromAnchor, toAnchor, curve, nodeRects, registryVersion]);
291
+ if (!path) return null;
292
+ return /* @__PURE__ */ jsxRuntime.jsxs("g", { "data-react-fancy-canvas-edge": "", className: cn("text-zinc-300 dark:text-zinc-600", className), children: [
293
+ /* @__PURE__ */ jsxRuntime.jsx(
294
+ "path",
295
+ {
296
+ d: path.d,
297
+ fill: "none",
298
+ stroke: color,
299
+ strokeWidth,
300
+ strokeDasharray: dashed ? "6 4" : void 0,
301
+ markerStart: markerStart ? `url(#${markerStart})` : void 0,
302
+ markerEnd: markerEnd ? `url(#${markerEnd})` : void 0,
303
+ className: animated ? "animate-[dash_1s_linear_infinite]" : "",
304
+ style: animated ? { strokeDasharray: "8 4" } : void 0
305
+ }
306
+ ),
307
+ label && /* @__PURE__ */ jsxRuntime.jsx("foreignObject", { x: path.midX - 40, y: path.midY - 12, width: 80, height: 24, children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex items-center justify-center text-xs text-zinc-500", children: label }) })
308
+ ] });
309
+ }
310
+ CanvasEdge.displayName = "CanvasEdge";
311
+ function CanvasMinimap({ width = 150, height = 100, className }) {
312
+ const { nodeRects, registryVersion, viewport } = useCanvas();
313
+ const bounds = react.useMemo(() => {
314
+ if (nodeRects.size === 0) return { minX: 0, minY: 0, maxX: 500, maxY: 300 };
315
+ let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
316
+ nodeRects.forEach((r) => {
317
+ minX = Math.min(minX, r.x);
318
+ minY = Math.min(minY, r.y);
319
+ maxX = Math.max(maxX, r.x + r.width);
320
+ maxY = Math.max(maxY, r.y + r.height);
321
+ });
322
+ const padding = 50;
323
+ return { minX: minX - padding, minY: minY - padding, maxX: maxX + padding, maxY: maxY + padding };
324
+ }, [nodeRects, registryVersion]);
325
+ const scaleX = width / (bounds.maxX - bounds.minX || 1);
326
+ const scaleY = height / (bounds.maxY - bounds.minY || 1);
327
+ const scale = Math.min(scaleX, scaleY);
328
+ return /* @__PURE__ */ jsxRuntime.jsx(
329
+ "div",
330
+ {
331
+ "data-react-fancy-canvas-minimap": "",
332
+ className: cn(
333
+ "absolute right-3 bottom-3 overflow-hidden rounded-lg border border-zinc-200 bg-white/90 dark:border-zinc-700 dark:bg-zinc-900/90",
334
+ className
335
+ ),
336
+ style: { width, height },
337
+ children: /* @__PURE__ */ jsxRuntime.jsxs("svg", { width, height, children: [
338
+ Array.from(nodeRects.entries()).map(([id, rect]) => /* @__PURE__ */ jsxRuntime.jsx(
339
+ "rect",
340
+ {
341
+ x: (rect.x - bounds.minX) * scale,
342
+ y: (rect.y - bounds.minY) * scale,
343
+ width: Math.max(rect.width * scale, 4),
344
+ height: Math.max(rect.height * scale, 3),
345
+ rx: 1,
346
+ className: "fill-blue-400/60"
347
+ },
348
+ id
349
+ )),
350
+ /* @__PURE__ */ jsxRuntime.jsx(
351
+ "rect",
352
+ {
353
+ x: (-viewport.panX / viewport.zoom - bounds.minX) * scale,
354
+ y: (-viewport.panY / viewport.zoom - bounds.minY) * scale,
355
+ width: (width / viewport.zoom / scale > 0 ? width / viewport.zoom : width) * scale / (bounds.maxX - bounds.minX || 1) * (bounds.maxX - bounds.minX),
356
+ height: (height / viewport.zoom / scale > 0 ? height / viewport.zoom : height) * scale / (bounds.maxY - bounds.minY || 1) * (bounds.maxY - bounds.minY),
357
+ fill: "none",
358
+ stroke: "currentColor",
359
+ strokeWidth: 1,
360
+ className: "text-blue-500"
361
+ }
362
+ )
363
+ ] })
364
+ }
365
+ );
366
+ }
367
+ CanvasMinimap.displayName = "CanvasMinimap";
368
+ function CanvasControls({
369
+ className,
370
+ showZoomIn = true,
371
+ showZoomOut = true,
372
+ showReset = true,
373
+ showFitAll = true,
374
+ showFullscreen = true
375
+ }) {
376
+ const { setViewport, nodeRects, containerRef } = useCanvas();
377
+ const zoomIn = () => setViewport((v) => ({ ...v, zoom: Math.min(3, v.zoom * 1.25) }));
378
+ const zoomOut = () => setViewport((v) => ({ ...v, zoom: Math.max(0.1, v.zoom / 1.25) }));
379
+ const reset = () => setViewport({ panX: 0, panY: 0, zoom: 1 });
380
+ const fitAll = () => {
381
+ const container = containerRef.current;
382
+ if (!container || nodeRects.size === 0) return reset();
383
+ let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
384
+ nodeRects.forEach((r) => {
385
+ minX = Math.min(minX, r.x);
386
+ minY = Math.min(minY, r.y);
387
+ maxX = Math.max(maxX, r.x + r.width);
388
+ maxY = Math.max(maxY, r.y + r.height);
389
+ });
390
+ const padding = 40;
391
+ const contentW = maxX - minX + padding * 2;
392
+ const contentH = maxY - minY + padding * 2;
393
+ const cw = container.clientWidth;
394
+ const ch = container.clientHeight;
395
+ const zoom = Math.min(cw / contentW, ch / contentH, 1.5);
396
+ const panX = (cw - contentW * zoom) / 2 - minX * zoom + padding * zoom;
397
+ const panY = (ch - contentH * zoom) / 2 - minY * zoom + padding * zoom;
398
+ setViewport({ panX, panY, zoom });
399
+ };
400
+ const btnClass = "flex h-8 w-8 items-center justify-center rounded-md text-zinc-500 hover:bg-zinc-100 hover:text-zinc-700 dark:hover:bg-zinc-800 dark:hover:text-zinc-300 transition-colors";
401
+ return /* @__PURE__ */ jsxRuntime.jsxs(
402
+ "div",
403
+ {
404
+ "data-react-fancy-canvas-controls": "",
405
+ className: cn(
406
+ "absolute bottom-3 left-3 flex gap-1 rounded-lg border border-zinc-200 bg-white/90 p-1 shadow-sm dark:border-zinc-700 dark:bg-zinc-900/90",
407
+ className
408
+ ),
409
+ children: [
410
+ showZoomIn && /* @__PURE__ */ jsxRuntime.jsx("button", { type: "button", onClick: zoomIn, className: btnClass, "aria-label": "Zoom in", children: /* @__PURE__ */ jsxRuntime.jsx(lucideReact.ZoomIn, { size: 16 }) }),
411
+ showZoomOut && /* @__PURE__ */ jsxRuntime.jsx("button", { type: "button", onClick: zoomOut, className: btnClass, "aria-label": "Zoom out", children: /* @__PURE__ */ jsxRuntime.jsx(lucideReact.ZoomOut, { size: 16 }) }),
412
+ showReset && /* @__PURE__ */ jsxRuntime.jsx("button", { type: "button", onClick: reset, className: btnClass, "aria-label": "Reset view", children: /* @__PURE__ */ jsxRuntime.jsx(lucideReact.RotateCcw, { size: 16 }) }),
413
+ showFitAll && /* @__PURE__ */ jsxRuntime.jsx("button", { type: "button", onClick: fitAll, className: btnClass, "aria-label": "Fit all", children: /* @__PURE__ */ jsxRuntime.jsx(lucideReact.Scan, { size: 16 }) }),
414
+ showFullscreen && /* @__PURE__ */ jsxRuntime.jsx(
415
+ chunkPBKOUAUY_cjs.FullscreenButton,
416
+ {
417
+ placement: "inline",
418
+ className: btnClass,
419
+ style: { border: "none", background: "transparent", color: "inherit" }
420
+ }
421
+ )
422
+ ]
423
+ }
424
+ );
425
+ }
426
+ CanvasControls.displayName = "CanvasControls";
427
+
428
+ // src/canvas/engines/dom.ts
429
+ var domEngine = {
430
+ name: "dom",
431
+ mount(host, _viewport) {
432
+ return {
433
+ name: "dom",
434
+ root: host,
435
+ updateViewport: () => {
436
+ },
437
+ dispose: () => {
438
+ }
439
+ };
440
+ }
441
+ };
442
+
443
+ // src/frame.ts
444
+ var PX_PER_UNIT = 100;
445
+ var DEFAULT_NODE_SIZE = { w: 240, h: 150 };
446
+ var FIT_PADDING = 40;
447
+ var FIT_MAX_ZOOM = 1.5;
448
+ function nodeSize(node) {
449
+ return node.size ?? DEFAULT_NODE_SIZE;
450
+ }
451
+ function sceneBounds(scene) {
452
+ if (scene.nodes.length === 0) return null;
453
+ let minX = Infinity;
454
+ let minY = Infinity;
455
+ let maxX = -Infinity;
456
+ let maxY = -Infinity;
457
+ for (const node of scene.nodes) {
458
+ const { w, h } = nodeSize(node);
459
+ minX = Math.min(minX, node.position.x);
460
+ minY = Math.min(minY, node.position.y);
461
+ maxX = Math.max(maxX, node.position.x + w);
462
+ maxY = Math.max(maxY, node.position.y + h);
463
+ }
464
+ return { minX, minY, maxX, maxY };
465
+ }
466
+ function fitZoom(bounds, width, height) {
467
+ const contentW = bounds.maxX - bounds.minX + FIT_PADDING * 2;
468
+ const contentH = bounds.maxY - bounds.minY + FIT_PADDING * 2;
469
+ if (width <= 0 || height <= 0 || contentW <= 0 || contentH <= 0) return 1;
470
+ return Math.min(width / contentW, height / contentH, FIT_MAX_ZOOM);
471
+ }
472
+ function sceneToWorld(scene, pxPerUnit = PX_PER_UNIT) {
473
+ const bounds = sceneBounds(scene);
474
+ if (!bounds) return [];
475
+ const midX = (bounds.minX + bounds.maxX) / 2;
476
+ const midY = (bounds.minY + bounds.maxY) / 2;
477
+ return scene.nodes.map((node) => {
478
+ const { w, h } = nodeSize(node);
479
+ return {
480
+ id: node.id,
481
+ x: (node.position.x + w / 2 - midX) / pxPerUnit,
482
+ y: (midY - (node.position.y + h / 2)) / pxPerUnit,
483
+ z: (node.position.z ?? 0) / pxPerUnit,
484
+ width: w / pxPerUnit,
485
+ height: h / pxPerUnit,
486
+ pixelWidth: w,
487
+ pixelHeight: h
488
+ };
489
+ });
490
+ }
491
+ function frameScene(scene, opts) {
492
+ const pxPerUnit = opts.pxPerUnit ?? PX_PER_UNIT;
493
+ const bounds = sceneBounds(scene);
494
+ const zoom = bounds ? fitZoom(bounds, opts.width, opts.height) : 1;
495
+ const height = opts.height > 0 ? opts.height : 1;
496
+ const distance = height / (2 * Math.tan(opts.fovY / 2) * zoom * pxPerUnit);
497
+ return { target: [0, 0, 0], distance, beta: Math.PI / 2, zoom };
498
+ }
499
+ function resolveEngine(spec) {
500
+ if (!spec || spec === "dom") return domEngine;
501
+ return spec;
502
+ }
503
+ var DEFAULT_VIEWPORT = { panX: 0, panY: 0, zoom: 1 };
504
+ function CanvasRoot({
505
+ children,
506
+ viewport: controlledViewport,
507
+ defaultViewport = DEFAULT_VIEWPORT,
508
+ onViewportChange,
509
+ minZoom = 0.1,
510
+ maxZoom = 3,
511
+ pannable = true,
512
+ zoomable = true,
513
+ showGrid = false,
514
+ gridStyle = "dots",
515
+ gridSize = 20,
516
+ gridColor = "rgb(161 161 170 / 0.3)",
517
+ snapToGrid = false,
518
+ fitOnMount = false,
519
+ engine: engineSpec,
520
+ fullscreen,
521
+ defaultFullscreen,
522
+ onFullscreenChange,
523
+ fullscreenLabels,
524
+ className,
525
+ style
526
+ }) {
527
+ const containerRef = react.useRef(null);
528
+ const [viewport, setViewport] = useControllableState(controlledViewport, defaultViewport, onViewportChange);
529
+ const { registerNode, unregisterNode, nodeRects, version: registryVersion } = useNodeRegistry();
530
+ const [engineHandle, setEngineHandle] = react.useState(null);
531
+ const fs = chunkPBKOUAUY_cjs.useFullscreen(containerRef, { fullscreen, defaultFullscreen, onFullscreenChange, labels: fullscreenLabels });
532
+ react.useEffect(() => {
533
+ const host = containerRef.current;
534
+ if (!host) return;
535
+ const resolved = resolveEngine(engineSpec);
536
+ let handle = null;
537
+ try {
538
+ handle = resolved.mount(host, viewport);
539
+ setEngineHandle(handle);
540
+ } catch (err) {
541
+ console.error(`[fancy-3d] Canvas engine "${resolved.name}" failed to mount:`, err);
542
+ setEngineHandle(null);
543
+ }
544
+ return () => {
545
+ try {
546
+ handle?.dispose();
547
+ } catch (err) {
548
+ console.error(`[fancy-3d] Canvas engine "${handle?.name}" failed to dispose:`, err);
549
+ }
550
+ setEngineHandle(null);
551
+ };
552
+ }, [engineSpec]);
553
+ react.useEffect(() => {
554
+ engineHandle?.updateViewport(viewport);
555
+ }, [engineHandle, viewport]);
556
+ const { containerProps } = usePanZoom({
557
+ viewport,
558
+ setViewport,
559
+ minZoom,
560
+ maxZoom,
561
+ pannable,
562
+ zoomable,
563
+ containerRef
564
+ });
565
+ const ctx = react.useMemo(
566
+ () => ({ viewport, setViewport, registerNode, unregisterNode, nodeRects, registryVersion, containerRef, gridSize, snapToGrid, engine: engineHandle }),
567
+ [viewport, setViewport, registerNode, unregisterNode, nodeRects, registryVersion, gridSize, snapToGrid, engineHandle]
568
+ );
569
+ const hasFitted = react.useRef(false);
570
+ react.useEffect(() => {
571
+ if (!fitOnMount || hasFitted.current || nodeRects.size === 0) return;
572
+ const container = containerRef.current;
573
+ if (!container || container.clientWidth === 0) return;
574
+ hasFitted.current = true;
575
+ requestAnimationFrame(() => {
576
+ let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
577
+ nodeRects.forEach((r) => {
578
+ minX = Math.min(minX, r.x);
579
+ minY = Math.min(minY, r.y);
580
+ maxX = Math.max(maxX, r.x + r.width);
581
+ maxY = Math.max(maxY, r.y + r.height);
582
+ });
583
+ const padding = FIT_PADDING;
584
+ const contentW = maxX - minX + padding * 2;
585
+ const contentH = maxY - minY + padding * 2;
586
+ const cw = container.clientWidth;
587
+ const ch = container.clientHeight;
588
+ const zoom = fitZoom({ minX, minY, maxX, maxY }, cw, ch);
589
+ const panX = (cw - contentW * zoom) / 2 - minX * zoom + padding * zoom;
590
+ const panY = (ch - contentH * zoom) / 2 - minY * zoom + padding * zoom;
591
+ setViewport({ panX, panY, zoom });
592
+ });
593
+ }, [fitOnMount, nodeRects, registryVersion, setViewport]);
594
+ const edges = [];
595
+ const others = [];
596
+ const overlays = [];
597
+ react.Children.forEach(children, (child) => {
598
+ const el = child;
599
+ if (!el || !el.type) return;
600
+ const elType = el.type;
601
+ if (elType === CanvasEdge || elType?._isCanvasEdge) {
602
+ edges.push(el);
603
+ } else if (elType === CanvasMinimap || elType === CanvasControls) {
604
+ overlays.push(el);
605
+ } else {
606
+ others.push(el);
607
+ }
608
+ });
609
+ return /* @__PURE__ */ jsxRuntime.jsx(chunkPBKOUAUY_cjs.FullscreenContext.Provider, { value: fs, children: /* @__PURE__ */ jsxRuntime.jsx(CanvasContext.Provider, { value: ctx, children: /* @__PURE__ */ jsxRuntime.jsxs(
610
+ "div",
611
+ {
612
+ ref: containerRef,
613
+ "data-react-fancy-canvas": "",
614
+ className: cn("relative overflow-hidden", className),
615
+ style: { touchAction: "none", ...style },
616
+ ...containerProps,
617
+ children: [
618
+ /* @__PURE__ */ jsxRuntime.jsx(
619
+ "div",
620
+ {
621
+ "data-canvas-bg": "",
622
+ className: "absolute inset-0",
623
+ style: showGrid && gridStyle !== "none" ? gridStyle === "lines" ? {
624
+ backgroundImage: `linear-gradient(to right, ${gridColor} 1px, transparent 1px), linear-gradient(to bottom, ${gridColor} 1px, transparent 1px)`,
625
+ backgroundSize: `${gridSize * viewport.zoom}px ${gridSize * viewport.zoom}px`,
626
+ backgroundPosition: `${viewport.panX}px ${viewport.panY}px`
627
+ } : {
628
+ backgroundImage: `radial-gradient(circle, ${gridColor} 1px, transparent 1px)`,
629
+ backgroundSize: `${gridSize * viewport.zoom}px ${gridSize * viewport.zoom}px`,
630
+ backgroundPosition: `${viewport.panX}px ${viewport.panY}px`
631
+ } : void 0
632
+ }
633
+ ),
634
+ /* @__PURE__ */ jsxRuntime.jsx(
635
+ "div",
636
+ {
637
+ className: "absolute origin-top-left",
638
+ style: {
639
+ transform: `translate(${viewport.panX}px, ${viewport.panY}px) scale(${viewport.zoom})`
640
+ },
641
+ children: others
642
+ }
643
+ ),
644
+ /* @__PURE__ */ jsxRuntime.jsxs(
645
+ "svg",
646
+ {
647
+ className: "pointer-events-none absolute inset-0 h-full w-full",
648
+ style: {
649
+ transform: `translate(${viewport.panX}px, ${viewport.panY}px) scale(${viewport.zoom})`,
650
+ transformOrigin: "0 0"
651
+ },
652
+ children: [
653
+ /* @__PURE__ */ jsxRuntime.jsxs("defs", { children: [
654
+ /* @__PURE__ */ jsxRuntime.jsx("marker", { id: "canvas-arrow", viewBox: "0 0 10 10", refX: "10", refY: "5", markerWidth: "8", markerHeight: "8", orient: "auto", children: /* @__PURE__ */ jsxRuntime.jsx("path", { d: "M0,0 L10,5 L0,10 Z", fill: "#71717a" }) }),
655
+ /* @__PURE__ */ jsxRuntime.jsx("marker", { id: "canvas-circle", viewBox: "0 0 10 10", refX: "5", refY: "5", markerWidth: "8", markerHeight: "8", orient: "auto", children: /* @__PURE__ */ jsxRuntime.jsx("circle", { cx: "5", cy: "5", r: "3.5", fill: "#71717a" }) }),
656
+ /* @__PURE__ */ jsxRuntime.jsx("marker", { id: "canvas-diamond", viewBox: "0 0 12 12", refX: "6", refY: "6", markerWidth: "10", markerHeight: "10", orient: "auto", children: /* @__PURE__ */ jsxRuntime.jsx("polygon", { points: "6,0 12,6 6,12 0,6", fill: "none", stroke: "#71717a", strokeWidth: "1.5" }) }),
657
+ /* @__PURE__ */ jsxRuntime.jsx("marker", { id: "canvas-one", viewBox: "0 0 2 16", refX: "1", refY: "8", markerWidth: "2", markerHeight: "14", orient: "auto", children: /* @__PURE__ */ jsxRuntime.jsx("line", { x1: "1", y1: "0", x2: "1", y2: "16", stroke: "#71717a", strokeWidth: "2" }) }),
658
+ /* @__PURE__ */ jsxRuntime.jsxs("marker", { id: "canvas-crow-foot", viewBox: "0 0 16 16", refX: "16", refY: "8", markerWidth: "14", markerHeight: "14", orient: "auto", children: [
659
+ /* @__PURE__ */ jsxRuntime.jsx("line", { x1: "16", y1: "8", x2: "0", y2: "0", stroke: "#71717a", strokeWidth: "2", strokeLinecap: "round" }),
660
+ /* @__PURE__ */ jsxRuntime.jsx("line", { x1: "16", y1: "8", x2: "0", y2: "8", stroke: "#71717a", strokeWidth: "2", strokeLinecap: "round" }),
661
+ /* @__PURE__ */ jsxRuntime.jsx("line", { x1: "16", y1: "8", x2: "0", y2: "16", stroke: "#71717a", strokeWidth: "2", strokeLinecap: "round" }),
662
+ /* @__PURE__ */ jsxRuntime.jsx("line", { x1: "16", y1: "0", x2: "16", y2: "16", stroke: "#71717a", strokeWidth: "2", strokeLinecap: "round" })
663
+ ] })
664
+ ] }),
665
+ edges
666
+ ]
667
+ }
668
+ ),
669
+ overlays
670
+ ]
671
+ }
672
+ ) }) });
673
+ }
674
+ var Canvas = Object.assign(CanvasRoot, {
675
+ Node: CanvasNode,
676
+ Edge: CanvasEdge,
677
+ Minimap: CanvasMinimap,
678
+ Controls: CanvasControls
679
+ });
680
+
681
+ exports.Canvas = Canvas;
682
+ exports.DEFAULT_NODE_SIZE = DEFAULT_NODE_SIZE;
683
+ exports.FIT_MAX_ZOOM = FIT_MAX_ZOOM;
684
+ exports.FIT_PADDING = FIT_PADDING;
685
+ exports.PX_PER_UNIT = PX_PER_UNIT;
686
+ exports.domEngine = domEngine;
687
+ exports.fitZoom = fitZoom;
688
+ exports.frameScene = frameScene;
689
+ exports.nodeSize = nodeSize;
690
+ exports.sceneBounds = sceneBounds;
691
+ exports.sceneToWorld = sceneToWorld;
692
+ exports.useCanvas = useCanvas;
693
+ //# sourceMappingURL=chunk-IU7B7Y63.cjs.map
694
+ //# sourceMappingURL=chunk-IU7B7Y63.cjs.map