@particle-academy/fancy-3d 0.1.6 → 0.3.3

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,791 @@
1
+ import { __require } from './chunk-DGUM43GV.js';
2
+ import { createContext, forwardRef, createElement, useRef, useState, useEffect, useMemo, Children, useCallback, useContext } from 'react';
3
+ import { jsx, jsxs } from 'react/jsx-runtime';
4
+
5
+ // src/utils/cn.ts
6
+ function flatten(value) {
7
+ if (!value) return "";
8
+ if (typeof value === "string" || typeof value === "number") return String(value);
9
+ if (Array.isArray(value)) return value.map(flatten).filter(Boolean).join(" ");
10
+ if (typeof value === "object") {
11
+ return Object.entries(value).filter(([, v]) => Boolean(v)).map(([k]) => k).join(" ");
12
+ }
13
+ return "";
14
+ }
15
+ function cn(...inputs) {
16
+ return inputs.map(flatten).filter(Boolean).join(" ");
17
+ }
18
+ function useControllableState(controlledValue, defaultValue, onChange) {
19
+ const [uncontrolledValue, setUncontrolledValue] = useState(defaultValue);
20
+ const isControlled = controlledValue !== void 0;
21
+ const value = isControlled ? controlledValue : uncontrolledValue;
22
+ const onChangeRef = useRef(onChange);
23
+ onChangeRef.current = onChange;
24
+ const setValue = useCallback(
25
+ (next) => {
26
+ const nextValue = typeof next === "function" ? next(value) : next;
27
+ if (!isControlled) {
28
+ setUncontrolledValue(nextValue);
29
+ }
30
+ onChangeRef.current?.(nextValue);
31
+ },
32
+ [isControlled, value]
33
+ );
34
+ return [value, setValue];
35
+ }
36
+ function usePanZoom({
37
+ viewport,
38
+ setViewport,
39
+ minZoom,
40
+ maxZoom,
41
+ pannable,
42
+ zoomable,
43
+ containerRef
44
+ }) {
45
+ const isPanningRef = useRef(false);
46
+ const startRef = useRef({ x: 0, y: 0, panX: 0, panY: 0 });
47
+ const viewportRef = useRef(viewport);
48
+ viewportRef.current = viewport;
49
+ const setViewportRef = useRef(setViewport);
50
+ setViewportRef.current = setViewport;
51
+ const zoomableRef = useRef(zoomable);
52
+ zoomableRef.current = zoomable;
53
+ const minZoomRef = useRef(minZoom);
54
+ minZoomRef.current = minZoom;
55
+ const maxZoomRef = useRef(maxZoom);
56
+ maxZoomRef.current = maxZoom;
57
+ useEffect(() => {
58
+ const container = containerRef.current;
59
+ if (!container) return;
60
+ function handleWheel(e) {
61
+ if (!zoomableRef.current) return;
62
+ if (!e.ctrlKey && !e.metaKey) return;
63
+ e.preventDefault();
64
+ const rect = container.getBoundingClientRect();
65
+ const mouseX = e.clientX - rect.left;
66
+ const mouseY = e.clientY - rect.top;
67
+ setViewportRef.current((prev) => {
68
+ const factor = 1 - e.deltaY * 1e-3;
69
+ const newZoom = Math.min(maxZoomRef.current, Math.max(minZoomRef.current, prev.zoom * factor));
70
+ const ratio = newZoom / prev.zoom;
71
+ return {
72
+ zoom: newZoom,
73
+ panX: mouseX - (mouseX - prev.panX) * ratio,
74
+ panY: mouseY - (mouseY - prev.panY) * ratio
75
+ };
76
+ });
77
+ }
78
+ container.addEventListener("wheel", handleWheel, { passive: false });
79
+ return () => {
80
+ container.removeEventListener("wheel", handleWheel);
81
+ };
82
+ }, [containerRef]);
83
+ const onPointerDown = useCallback(
84
+ (e) => {
85
+ if (!pannable) return;
86
+ if (e.button !== 0) return;
87
+ const target = e.target;
88
+ if (target !== containerRef.current && !target.hasAttribute("data-canvas-bg")) return;
89
+ isPanningRef.current = true;
90
+ startRef.current = { x: e.clientX, y: e.clientY, panX: viewport.panX, panY: viewport.panY };
91
+ e.target.setPointerCapture(e.pointerId);
92
+ e.preventDefault();
93
+ },
94
+ [pannable, viewport.panX, viewport.panY, containerRef]
95
+ );
96
+ const onPointerMove = useCallback(
97
+ (e) => {
98
+ if (!isPanningRef.current) return;
99
+ const dx = e.clientX - startRef.current.x;
100
+ const dy = e.clientY - startRef.current.y;
101
+ setViewport((prev) => ({
102
+ ...prev,
103
+ panX: startRef.current.panX + dx,
104
+ panY: startRef.current.panY + dy
105
+ }));
106
+ },
107
+ [setViewport]
108
+ );
109
+ const onPointerUp = useCallback(() => {
110
+ isPanningRef.current = false;
111
+ }, []);
112
+ return {
113
+ containerProps: { onPointerDown, onPointerMove, onPointerUp },
114
+ isPanning: isPanningRef.current
115
+ };
116
+ }
117
+ function useNodeRegistry() {
118
+ const rectsRef = useRef(/* @__PURE__ */ new Map());
119
+ const [version, setVersion] = useState(0);
120
+ const registerNode = useCallback((id, rect) => {
121
+ rectsRef.current.set(id, rect);
122
+ setVersion((v) => v + 1);
123
+ }, []);
124
+ const unregisterNode = useCallback((id) => {
125
+ rectsRef.current.delete(id);
126
+ setVersion((v) => v + 1);
127
+ }, []);
128
+ return { registerNode, unregisterNode, nodeRects: rectsRef.current, version };
129
+ }
130
+ var CanvasContext = createContext(null);
131
+ function useCanvas() {
132
+ const ctx = useContext(CanvasContext);
133
+ if (!ctx) throw new Error("useCanvas must be used within a Canvas component");
134
+ return ctx;
135
+ }
136
+ function CanvasNode({ children, id, x, y, draggable, onPositionChange, className, style }) {
137
+ const { registerNode, unregisterNode, viewport, gridSize, snapToGrid } = useCanvas();
138
+ const nodeRef = useRef(null);
139
+ const isDragging = useRef(false);
140
+ const dragStart = useRef({ mouseX: 0, mouseY: 0, nodeX: 0, nodeY: 0 });
141
+ useEffect(() => {
142
+ const el = nodeRef.current;
143
+ if (!el) return;
144
+ const updateRect = () => {
145
+ registerNode(id, { x, y, width: el.offsetWidth, height: el.offsetHeight });
146
+ };
147
+ updateRect();
148
+ const observer = new ResizeObserver(updateRect);
149
+ observer.observe(el);
150
+ return () => {
151
+ observer.disconnect();
152
+ unregisterNode(id);
153
+ };
154
+ }, [id, x, y, registerNode, unregisterNode]);
155
+ const handlePointerDown = useCallback(
156
+ (e) => {
157
+ if (!draggable || e.button !== 0) return;
158
+ e.stopPropagation();
159
+ isDragging.current = true;
160
+ dragStart.current = { mouseX: e.clientX, mouseY: e.clientY, nodeX: x, nodeY: y };
161
+ e.target.setPointerCapture(e.pointerId);
162
+ },
163
+ [draggable, x, y]
164
+ );
165
+ const handlePointerMove = useCallback(
166
+ (e) => {
167
+ if (!isDragging.current) return;
168
+ const dx = (e.clientX - dragStart.current.mouseX) / viewport.zoom;
169
+ const dy = (e.clientY - dragStart.current.mouseY) / viewport.zoom;
170
+ let nx = dragStart.current.nodeX + dx;
171
+ let ny = dragStart.current.nodeY + dy;
172
+ if (snapToGrid && gridSize > 0) {
173
+ nx = Math.round(nx / gridSize) * gridSize;
174
+ ny = Math.round(ny / gridSize) * gridSize;
175
+ }
176
+ onPositionChange?.(nx, ny);
177
+ },
178
+ [viewport.zoom, onPositionChange, snapToGrid, gridSize]
179
+ );
180
+ const handlePointerUp = useCallback(() => {
181
+ isDragging.current = false;
182
+ }, []);
183
+ return /* @__PURE__ */ jsx(
184
+ "div",
185
+ {
186
+ ref: nodeRef,
187
+ "data-react-fancy-canvas-node": "",
188
+ "data-node-id": id,
189
+ className: cn("absolute", draggable && "cursor-grab active:cursor-grabbing", className),
190
+ style: { left: x, top: y, ...style },
191
+ onPointerDown: handlePointerDown,
192
+ onPointerMove: handlePointerMove,
193
+ onPointerUp: handlePointerUp,
194
+ children
195
+ }
196
+ );
197
+ }
198
+ CanvasNode.displayName = "CanvasNode";
199
+
200
+ // src/canvas/canvas.utils.ts
201
+ function getAnchorPoint(rect, anchor, otherRect) {
202
+ const cx = rect.x + rect.width / 2;
203
+ const cy = rect.y + rect.height / 2;
204
+ if (anchor === "auto" && otherRect) {
205
+ const ocx = otherRect.x + otherRect.width / 2;
206
+ const ocy = otherRect.y + otherRect.height / 2;
207
+ const dx = ocx - cx;
208
+ const dy = ocy - cy;
209
+ if (Math.abs(dx) > Math.abs(dy)) {
210
+ return dx > 0 ? { x: rect.x + rect.width, y: cy } : { x: rect.x, y: cy };
211
+ }
212
+ return dy > 0 ? { x: cx, y: rect.y + rect.height } : { x: cx, y: rect.y };
213
+ }
214
+ switch (anchor) {
215
+ case "top":
216
+ return { x: cx, y: rect.y };
217
+ case "bottom":
218
+ return { x: cx, y: rect.y + rect.height };
219
+ case "left":
220
+ return { x: rect.x, y: cy };
221
+ case "right":
222
+ return { x: rect.x + rect.width, y: cy };
223
+ case "center":
224
+ return { x: cx, y: cy };
225
+ default:
226
+ return { x: cx, y: cy };
227
+ }
228
+ }
229
+ function bezierPath(from, to) {
230
+ const dx = Math.abs(to.x - from.x);
231
+ const dy = Math.abs(to.y - from.y);
232
+ if (dx > dy) {
233
+ const offset2 = dx * 0.5;
234
+ const cp1x = from.x + (to.x > from.x ? offset2 : -offset2);
235
+ const cp2x = to.x + (to.x > from.x ? -offset2 : offset2);
236
+ return `M${from.x},${from.y} C${cp1x},${from.y} ${cp2x},${to.y} ${to.x},${to.y}`;
237
+ }
238
+ const offset = Math.max(dy * 0.5, 30);
239
+ const cp1y = from.y + (to.y > from.y ? offset : -offset);
240
+ const cp2y = to.y + (to.y > from.y ? -offset : offset);
241
+ return `M${from.x},${from.y} C${from.x},${cp1y} ${to.x},${cp2y} ${to.x},${to.y}`;
242
+ }
243
+ function stepPath(from, to) {
244
+ const midX = (from.x + to.x) / 2;
245
+ return `M${from.x},${from.y} H${midX} V${to.y} H${to.x}`;
246
+ }
247
+ function straightPath(from, to) {
248
+ return `M${from.x},${from.y} L${to.x},${to.y}`;
249
+ }
250
+ function getEdgePath(from, to, curve = "bezier") {
251
+ switch (curve) {
252
+ case "bezier":
253
+ return bezierPath(from, to);
254
+ case "step":
255
+ return stepPath(from, to);
256
+ case "straight":
257
+ return straightPath(from, to);
258
+ }
259
+ }
260
+ function CanvasEdge({
261
+ from,
262
+ to,
263
+ fromAnchor = "auto",
264
+ toAnchor = "auto",
265
+ curve = "bezier",
266
+ color = "currentColor",
267
+ strokeWidth = 2,
268
+ dashed = false,
269
+ animated = false,
270
+ label,
271
+ className,
272
+ markerStart,
273
+ markerEnd
274
+ }) {
275
+ const { nodeRects, registryVersion } = useCanvas();
276
+ const path = useMemo(() => {
277
+ const fromRect = nodeRects.get(from);
278
+ const toRect = nodeRects.get(to);
279
+ if (!fromRect || !toRect) return null;
280
+ const fromPt = getAnchorPoint(fromRect, fromAnchor, toRect);
281
+ const toPt = getAnchorPoint(toRect, toAnchor, fromRect);
282
+ return {
283
+ d: getEdgePath(fromPt, toPt, curve),
284
+ midX: (fromPt.x + toPt.x) / 2,
285
+ midY: (fromPt.y + toPt.y) / 2
286
+ };
287
+ }, [from, to, fromAnchor, toAnchor, curve, nodeRects, registryVersion]);
288
+ if (!path) return null;
289
+ return /* @__PURE__ */ jsxs("g", { "data-react-fancy-canvas-edge": "", className: cn("text-zinc-300 dark:text-zinc-600", className), children: [
290
+ /* @__PURE__ */ jsx(
291
+ "path",
292
+ {
293
+ d: path.d,
294
+ fill: "none",
295
+ stroke: color,
296
+ strokeWidth,
297
+ strokeDasharray: dashed ? "6 4" : void 0,
298
+ markerStart: markerStart ? `url(#${markerStart})` : void 0,
299
+ markerEnd: markerEnd ? `url(#${markerEnd})` : void 0,
300
+ className: animated ? "animate-[dash_1s_linear_infinite]" : "",
301
+ style: animated ? { strokeDasharray: "8 4" } : void 0
302
+ }
303
+ ),
304
+ label && /* @__PURE__ */ jsx("foreignObject", { x: path.midX - 40, y: path.midY - 12, width: 80, height: 24, children: /* @__PURE__ */ jsx("div", { className: "flex items-center justify-center text-xs text-zinc-500", children: label }) })
305
+ ] });
306
+ }
307
+ CanvasEdge.displayName = "CanvasEdge";
308
+ function CanvasMinimap({ width = 150, height = 100, className }) {
309
+ const { nodeRects, registryVersion, viewport } = useCanvas();
310
+ const bounds = useMemo(() => {
311
+ if (nodeRects.size === 0) return { minX: 0, minY: 0, maxX: 500, maxY: 300 };
312
+ let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
313
+ nodeRects.forEach((r) => {
314
+ minX = Math.min(minX, r.x);
315
+ minY = Math.min(minY, r.y);
316
+ maxX = Math.max(maxX, r.x + r.width);
317
+ maxY = Math.max(maxY, r.y + r.height);
318
+ });
319
+ const padding = 50;
320
+ return { minX: minX - padding, minY: minY - padding, maxX: maxX + padding, maxY: maxY + padding };
321
+ }, [nodeRects, registryVersion]);
322
+ const scaleX = width / (bounds.maxX - bounds.minX || 1);
323
+ const scaleY = height / (bounds.maxY - bounds.minY || 1);
324
+ const scale = Math.min(scaleX, scaleY);
325
+ return /* @__PURE__ */ jsx(
326
+ "div",
327
+ {
328
+ "data-react-fancy-canvas-minimap": "",
329
+ className: cn(
330
+ "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",
331
+ className
332
+ ),
333
+ style: { width, height },
334
+ children: /* @__PURE__ */ jsxs("svg", { width, height, children: [
335
+ Array.from(nodeRects.entries()).map(([id, rect]) => /* @__PURE__ */ jsx(
336
+ "rect",
337
+ {
338
+ x: (rect.x - bounds.minX) * scale,
339
+ y: (rect.y - bounds.minY) * scale,
340
+ width: Math.max(rect.width * scale, 4),
341
+ height: Math.max(rect.height * scale, 3),
342
+ rx: 1,
343
+ className: "fill-blue-400/60"
344
+ },
345
+ id
346
+ )),
347
+ /* @__PURE__ */ jsx(
348
+ "rect",
349
+ {
350
+ x: (-viewport.panX / viewport.zoom - bounds.minX) * scale,
351
+ y: (-viewport.panY / viewport.zoom - bounds.minY) * scale,
352
+ width: (width / viewport.zoom / scale > 0 ? width / viewport.zoom : width) * scale / (bounds.maxX - bounds.minX || 1) * (bounds.maxX - bounds.minX),
353
+ height: (height / viewport.zoom / scale > 0 ? height / viewport.zoom : height) * scale / (bounds.maxY - bounds.minY || 1) * (bounds.maxY - bounds.minY),
354
+ fill: "none",
355
+ stroke: "currentColor",
356
+ strokeWidth: 1,
357
+ className: "text-blue-500"
358
+ }
359
+ )
360
+ ] })
361
+ }
362
+ );
363
+ }
364
+ CanvasMinimap.displayName = "CanvasMinimap";
365
+
366
+ // node_modules/lucide-react/dist/esm/shared/src/utils.js
367
+ var toKebabCase = (string) => string.replace(/([a-z0-9])([A-Z])/g, "$1-$2").toLowerCase();
368
+ var toCamelCase = (string) => string.replace(
369
+ /^([A-Z])|[\s-_]+(\w)/g,
370
+ (match, p1, p2) => p2 ? p2.toUpperCase() : p1.toLowerCase()
371
+ );
372
+ var toPascalCase = (string) => {
373
+ const camelCase = toCamelCase(string);
374
+ return camelCase.charAt(0).toUpperCase() + camelCase.slice(1);
375
+ };
376
+ var mergeClasses = (...classes) => classes.filter((className, index, array) => {
377
+ return Boolean(className) && className.trim() !== "" && array.indexOf(className) === index;
378
+ }).join(" ").trim();
379
+ var hasA11yProp = (props) => {
380
+ for (const prop in props) {
381
+ if (prop.startsWith("aria-") || prop === "role" || prop === "title") {
382
+ return true;
383
+ }
384
+ }
385
+ };
386
+
387
+ // node_modules/lucide-react/dist/esm/defaultAttributes.js
388
+ var defaultAttributes = {
389
+ xmlns: "http://www.w3.org/2000/svg",
390
+ width: 24,
391
+ height: 24,
392
+ viewBox: "0 0 24 24",
393
+ fill: "none",
394
+ stroke: "currentColor",
395
+ strokeWidth: 2,
396
+ strokeLinecap: "round",
397
+ strokeLinejoin: "round"
398
+ };
399
+
400
+ // node_modules/lucide-react/dist/esm/Icon.js
401
+ var Icon = forwardRef(
402
+ ({
403
+ color = "currentColor",
404
+ size = 24,
405
+ strokeWidth = 2,
406
+ absoluteStrokeWidth,
407
+ className = "",
408
+ children,
409
+ iconNode,
410
+ ...rest
411
+ }, ref) => createElement(
412
+ "svg",
413
+ {
414
+ ref,
415
+ ...defaultAttributes,
416
+ width: size,
417
+ height: size,
418
+ stroke: color,
419
+ strokeWidth: absoluteStrokeWidth ? Number(strokeWidth) * 24 / Number(size) : strokeWidth,
420
+ className: mergeClasses("lucide", className),
421
+ ...!children && !hasA11yProp(rest) && { "aria-hidden": "true" },
422
+ ...rest
423
+ },
424
+ [
425
+ ...iconNode.map(([tag, attrs]) => createElement(tag, attrs)),
426
+ ...Array.isArray(children) ? children : [children]
427
+ ]
428
+ )
429
+ );
430
+
431
+ // node_modules/lucide-react/dist/esm/createLucideIcon.js
432
+ var createLucideIcon = (iconName, iconNode) => {
433
+ const Component = forwardRef(
434
+ ({ className, ...props }, ref) => createElement(Icon, {
435
+ ref,
436
+ iconNode,
437
+ className: mergeClasses(
438
+ `lucide-${toKebabCase(toPascalCase(iconName))}`,
439
+ `lucide-${iconName}`,
440
+ className
441
+ ),
442
+ ...props
443
+ })
444
+ );
445
+ Component.displayName = toPascalCase(iconName);
446
+ return Component;
447
+ };
448
+
449
+ // node_modules/lucide-react/dist/esm/icons/maximize.js
450
+ var __iconNode = [
451
+ ["path", { d: "M8 3H5a2 2 0 0 0-2 2v3", key: "1dcmit" }],
452
+ ["path", { d: "M21 8V5a2 2 0 0 0-2-2h-3", key: "1e4gt3" }],
453
+ ["path", { d: "M3 16v3a2 2 0 0 0 2 2h3", key: "wsl5sc" }],
454
+ ["path", { d: "M16 21h3a2 2 0 0 0 2-2v-3", key: "18trek" }]
455
+ ];
456
+ var Maximize = createLucideIcon("maximize", __iconNode);
457
+
458
+ // node_modules/lucide-react/dist/esm/icons/rotate-ccw.js
459
+ var __iconNode2 = [
460
+ ["path", { d: "M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8", key: "1357e3" }],
461
+ ["path", { d: "M3 3v5h5", key: "1xhq8a" }]
462
+ ];
463
+ var RotateCcw = createLucideIcon("rotate-ccw", __iconNode2);
464
+
465
+ // node_modules/lucide-react/dist/esm/icons/zoom-in.js
466
+ var __iconNode3 = [
467
+ ["circle", { cx: "11", cy: "11", r: "8", key: "4ej97u" }],
468
+ ["line", { x1: "21", x2: "16.65", y1: "21", y2: "16.65", key: "13gj7c" }],
469
+ ["line", { x1: "11", x2: "11", y1: "8", y2: "14", key: "1vmskp" }],
470
+ ["line", { x1: "8", x2: "14", y1: "11", y2: "11", key: "durymu" }]
471
+ ];
472
+ var ZoomIn = createLucideIcon("zoom-in", __iconNode3);
473
+
474
+ // node_modules/lucide-react/dist/esm/icons/zoom-out.js
475
+ var __iconNode4 = [
476
+ ["circle", { cx: "11", cy: "11", r: "8", key: "4ej97u" }],
477
+ ["line", { x1: "21", x2: "16.65", y1: "21", y2: "16.65", key: "13gj7c" }],
478
+ ["line", { x1: "8", x2: "14", y1: "11", y2: "11", key: "durymu" }]
479
+ ];
480
+ var ZoomOut = createLucideIcon("zoom-out", __iconNode4);
481
+ function CanvasControls({
482
+ className,
483
+ showZoomIn = true,
484
+ showZoomOut = true,
485
+ showReset = true,
486
+ showFitAll = true
487
+ }) {
488
+ const { setViewport, nodeRects, containerRef } = useCanvas();
489
+ const zoomIn = () => setViewport((v) => ({ ...v, zoom: Math.min(3, v.zoom * 1.25) }));
490
+ const zoomOut = () => setViewport((v) => ({ ...v, zoom: Math.max(0.1, v.zoom / 1.25) }));
491
+ const reset = () => setViewport({ panX: 0, panY: 0, zoom: 1 });
492
+ const fitAll = () => {
493
+ const container = containerRef.current;
494
+ if (!container || nodeRects.size === 0) return reset();
495
+ let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
496
+ nodeRects.forEach((r) => {
497
+ minX = Math.min(minX, r.x);
498
+ minY = Math.min(minY, r.y);
499
+ maxX = Math.max(maxX, r.x + r.width);
500
+ maxY = Math.max(maxY, r.y + r.height);
501
+ });
502
+ const padding = 40;
503
+ const contentW = maxX - minX + padding * 2;
504
+ const contentH = maxY - minY + padding * 2;
505
+ const cw = container.clientWidth;
506
+ const ch = container.clientHeight;
507
+ const zoom = Math.min(cw / contentW, ch / contentH, 1.5);
508
+ const panX = (cw - contentW * zoom) / 2 - minX * zoom + padding * zoom;
509
+ const panY = (ch - contentH * zoom) / 2 - minY * zoom + padding * zoom;
510
+ setViewport({ panX, panY, zoom });
511
+ };
512
+ 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";
513
+ return /* @__PURE__ */ jsxs(
514
+ "div",
515
+ {
516
+ "data-react-fancy-canvas-controls": "",
517
+ className: cn(
518
+ "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",
519
+ className
520
+ ),
521
+ children: [
522
+ showZoomIn && /* @__PURE__ */ jsx("button", { type: "button", onClick: zoomIn, className: btnClass, "aria-label": "Zoom in", children: /* @__PURE__ */ jsx(ZoomIn, { size: 16 }) }),
523
+ showZoomOut && /* @__PURE__ */ jsx("button", { type: "button", onClick: zoomOut, className: btnClass, "aria-label": "Zoom out", children: /* @__PURE__ */ jsx(ZoomOut, { size: 16 }) }),
524
+ showReset && /* @__PURE__ */ jsx("button", { type: "button", onClick: reset, className: btnClass, "aria-label": "Reset view", children: /* @__PURE__ */ jsx(RotateCcw, { size: 16 }) }),
525
+ showFitAll && /* @__PURE__ */ jsx("button", { type: "button", onClick: fitAll, className: btnClass, "aria-label": "Fit all", children: /* @__PURE__ */ jsx(Maximize, { size: 16 }) })
526
+ ]
527
+ }
528
+ );
529
+ }
530
+ CanvasControls.displayName = "CanvasControls";
531
+
532
+ // src/canvas/engines/dom.ts
533
+ var domEngine = {
534
+ name: "dom",
535
+ mount(host, _viewport) {
536
+ return {
537
+ name: "dom",
538
+ root: host,
539
+ updateViewport: () => {
540
+ },
541
+ dispose: () => {
542
+ }
543
+ };
544
+ }
545
+ };
546
+
547
+ // src/canvas/engines/babylon.ts
548
+ var babylonEngine = {
549
+ name: "babylon",
550
+ mount(host, viewport) {
551
+ const BABYLON = __require("@babylonjs/core");
552
+ const overlay = document.createElement("canvas");
553
+ overlay.style.position = "absolute";
554
+ overlay.style.inset = "0";
555
+ overlay.style.width = "100%";
556
+ overlay.style.height = "100%";
557
+ overlay.style.pointerEvents = "none";
558
+ overlay.dataset.fancy3dCanvasEngine = "babylon";
559
+ host.appendChild(overlay);
560
+ const engine = new BABYLON.Engine(overlay, true, { preserveDrawingBuffer: true, stencil: true });
561
+ const scene = new BABYLON.Scene(engine);
562
+ scene.clearColor = new BABYLON.Color4(0, 0, 0, 0);
563
+ const camera = new BABYLON.ArcRotateCamera(
564
+ "fancy3d-canvas-camera",
565
+ Math.PI / 2,
566
+ Math.PI / 2.5,
567
+ 8,
568
+ BABYLON.Vector3.Zero(),
569
+ scene
570
+ );
571
+ camera.attachControl(overlay, false);
572
+ new BABYLON.HemisphericLight("fancy3d-canvas-light", new BABYLON.Vector3(0, 1, 0), scene);
573
+ const observer = engine.runRenderLoop(() => scene.render());
574
+ function updateViewport(_v) {
575
+ }
576
+ function dispose() {
577
+ try {
578
+ engine.stopRenderLoop(observer);
579
+ } catch {
580
+ }
581
+ scene.dispose();
582
+ engine.dispose();
583
+ if (overlay.parentElement === host) host.removeChild(overlay);
584
+ }
585
+ return {
586
+ name: "babylon",
587
+ root: scene,
588
+ updateViewport,
589
+ dispose
590
+ };
591
+ }
592
+ };
593
+ function resolveEngine(spec) {
594
+ if (!spec || spec === "dom") return domEngine;
595
+ if (spec === "babylon") return babylonEngine;
596
+ return spec;
597
+ }
598
+ var DEFAULT_VIEWPORT = { panX: 0, panY: 0, zoom: 1 };
599
+ function CanvasRoot({
600
+ children,
601
+ viewport: controlledViewport,
602
+ defaultViewport = DEFAULT_VIEWPORT,
603
+ onViewportChange,
604
+ minZoom = 0.1,
605
+ maxZoom = 3,
606
+ pannable = true,
607
+ zoomable = true,
608
+ showGrid = false,
609
+ gridStyle = "dots",
610
+ gridSize = 20,
611
+ gridColor = "rgb(161 161 170 / 0.3)",
612
+ snapToGrid = false,
613
+ fitOnMount = false,
614
+ engine: engineSpec,
615
+ className,
616
+ style
617
+ }) {
618
+ const containerRef = useRef(null);
619
+ const [viewport, setViewport] = useControllableState(controlledViewport, defaultViewport, onViewportChange);
620
+ const { registerNode, unregisterNode, nodeRects, version: registryVersion } = useNodeRegistry();
621
+ const [engineHandle, setEngineHandle] = useState(null);
622
+ useEffect(() => {
623
+ const host = containerRef.current;
624
+ if (!host) return;
625
+ const resolved = resolveEngine(engineSpec);
626
+ let handle = null;
627
+ try {
628
+ handle = resolved.mount(host, viewport);
629
+ setEngineHandle(handle);
630
+ } catch (err) {
631
+ console.error(`[fancy-3d] Canvas engine "${resolved.name}" failed to mount:`, err);
632
+ setEngineHandle(null);
633
+ }
634
+ return () => {
635
+ try {
636
+ handle?.dispose();
637
+ } catch (err) {
638
+ console.error(`[fancy-3d] Canvas engine "${handle?.name}" failed to dispose:`, err);
639
+ }
640
+ setEngineHandle(null);
641
+ };
642
+ }, [engineSpec]);
643
+ useEffect(() => {
644
+ engineHandle?.updateViewport(viewport);
645
+ }, [engineHandle, viewport]);
646
+ const { containerProps } = usePanZoom({
647
+ viewport,
648
+ setViewport,
649
+ minZoom,
650
+ maxZoom,
651
+ pannable,
652
+ zoomable,
653
+ containerRef
654
+ });
655
+ const ctx = useMemo(
656
+ () => ({ viewport, setViewport, registerNode, unregisterNode, nodeRects, registryVersion, containerRef, gridSize, snapToGrid, engine: engineHandle }),
657
+ [viewport, setViewport, registerNode, unregisterNode, nodeRects, registryVersion, gridSize, snapToGrid, engineHandle]
658
+ );
659
+ const hasFitted = useRef(false);
660
+ useEffect(() => {
661
+ if (!fitOnMount || hasFitted.current || nodeRects.size === 0) return;
662
+ const container = containerRef.current;
663
+ if (!container || container.clientWidth === 0) return;
664
+ hasFitted.current = true;
665
+ requestAnimationFrame(() => {
666
+ let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
667
+ nodeRects.forEach((r) => {
668
+ minX = Math.min(minX, r.x);
669
+ minY = Math.min(minY, r.y);
670
+ maxX = Math.max(maxX, r.x + r.width);
671
+ maxY = Math.max(maxY, r.y + r.height);
672
+ });
673
+ const padding = 40;
674
+ const contentW = maxX - minX + padding * 2;
675
+ const contentH = maxY - minY + padding * 2;
676
+ const cw = container.clientWidth;
677
+ const ch = container.clientHeight;
678
+ const zoom = Math.min(cw / contentW, ch / contentH, 1.5);
679
+ const panX = (cw - contentW * zoom) / 2 - minX * zoom + padding * zoom;
680
+ const panY = (ch - contentH * zoom) / 2 - minY * zoom + padding * zoom;
681
+ setViewport({ panX, panY, zoom });
682
+ });
683
+ }, [fitOnMount, nodeRects, registryVersion, setViewport]);
684
+ const edges = [];
685
+ const others = [];
686
+ const overlays = [];
687
+ Children.forEach(children, (child) => {
688
+ const el = child;
689
+ if (!el || !el.type) return;
690
+ const elType = el.type;
691
+ if (elType === CanvasEdge || elType?._isCanvasEdge) {
692
+ edges.push(el);
693
+ } else if (elType === CanvasMinimap || elType === CanvasControls) {
694
+ overlays.push(el);
695
+ } else {
696
+ others.push(el);
697
+ }
698
+ });
699
+ return /* @__PURE__ */ jsx(CanvasContext.Provider, { value: ctx, children: /* @__PURE__ */ jsxs(
700
+ "div",
701
+ {
702
+ ref: containerRef,
703
+ "data-react-fancy-canvas": "",
704
+ className: cn("relative overflow-hidden", className),
705
+ style: { touchAction: "none", ...style },
706
+ ...containerProps,
707
+ children: [
708
+ /* @__PURE__ */ jsx(
709
+ "div",
710
+ {
711
+ "data-canvas-bg": "",
712
+ className: "absolute inset-0",
713
+ style: showGrid && gridStyle !== "none" ? gridStyle === "lines" ? {
714
+ backgroundImage: `linear-gradient(to right, ${gridColor} 1px, transparent 1px), linear-gradient(to bottom, ${gridColor} 1px, transparent 1px)`,
715
+ backgroundSize: `${gridSize * viewport.zoom}px ${gridSize * viewport.zoom}px`,
716
+ backgroundPosition: `${viewport.panX}px ${viewport.panY}px`
717
+ } : {
718
+ backgroundImage: `radial-gradient(circle, ${gridColor} 1px, transparent 1px)`,
719
+ backgroundSize: `${gridSize * viewport.zoom}px ${gridSize * viewport.zoom}px`,
720
+ backgroundPosition: `${viewport.panX}px ${viewport.panY}px`
721
+ } : void 0
722
+ }
723
+ ),
724
+ /* @__PURE__ */ jsx(
725
+ "div",
726
+ {
727
+ className: "absolute origin-top-left",
728
+ style: {
729
+ transform: `translate(${viewport.panX}px, ${viewport.panY}px) scale(${viewport.zoom})`
730
+ },
731
+ children: others
732
+ }
733
+ ),
734
+ /* @__PURE__ */ jsxs(
735
+ "svg",
736
+ {
737
+ className: "pointer-events-none absolute inset-0 h-full w-full",
738
+ style: {
739
+ transform: `translate(${viewport.panX}px, ${viewport.panY}px) scale(${viewport.zoom})`,
740
+ transformOrigin: "0 0"
741
+ },
742
+ children: [
743
+ /* @__PURE__ */ jsxs("defs", { children: [
744
+ /* @__PURE__ */ jsx("marker", { id: "canvas-arrow", viewBox: "0 0 10 10", refX: "10", refY: "5", markerWidth: "8", markerHeight: "8", orient: "auto", children: /* @__PURE__ */ jsx("path", { d: "M0,0 L10,5 L0,10 Z", fill: "#71717a" }) }),
745
+ /* @__PURE__ */ jsx("marker", { id: "canvas-circle", viewBox: "0 0 10 10", refX: "5", refY: "5", markerWidth: "8", markerHeight: "8", orient: "auto", children: /* @__PURE__ */ jsx("circle", { cx: "5", cy: "5", r: "3.5", fill: "#71717a" }) }),
746
+ /* @__PURE__ */ jsx("marker", { id: "canvas-diamond", viewBox: "0 0 12 12", refX: "6", refY: "6", markerWidth: "10", markerHeight: "10", orient: "auto", children: /* @__PURE__ */ jsx("polygon", { points: "6,0 12,6 6,12 0,6", fill: "none", stroke: "#71717a", strokeWidth: "1.5" }) }),
747
+ /* @__PURE__ */ jsx("marker", { id: "canvas-one", viewBox: "0 0 2 16", refX: "1", refY: "8", markerWidth: "2", markerHeight: "14", orient: "auto", children: /* @__PURE__ */ jsx("line", { x1: "1", y1: "0", x2: "1", y2: "16", stroke: "#71717a", strokeWidth: "2" }) }),
748
+ /* @__PURE__ */ jsxs("marker", { id: "canvas-crow-foot", viewBox: "0 0 16 16", refX: "16", refY: "8", markerWidth: "14", markerHeight: "14", orient: "auto", children: [
749
+ /* @__PURE__ */ jsx("line", { x1: "16", y1: "8", x2: "0", y2: "0", stroke: "#71717a", strokeWidth: "2", strokeLinecap: "round" }),
750
+ /* @__PURE__ */ jsx("line", { x1: "16", y1: "8", x2: "0", y2: "8", stroke: "#71717a", strokeWidth: "2", strokeLinecap: "round" }),
751
+ /* @__PURE__ */ jsx("line", { x1: "16", y1: "8", x2: "0", y2: "16", stroke: "#71717a", strokeWidth: "2", strokeLinecap: "round" }),
752
+ /* @__PURE__ */ jsx("line", { x1: "16", y1: "0", x2: "16", y2: "16", stroke: "#71717a", strokeWidth: "2", strokeLinecap: "round" })
753
+ ] })
754
+ ] }),
755
+ edges
756
+ ]
757
+ }
758
+ ),
759
+ overlays
760
+ ]
761
+ }
762
+ ) });
763
+ }
764
+ var Canvas = Object.assign(CanvasRoot, {
765
+ Node: CanvasNode,
766
+ Edge: CanvasEdge,
767
+ Minimap: CanvasMinimap,
768
+ Controls: CanvasControls
769
+ });
770
+ /*! Bundled license information:
771
+
772
+ lucide-react/dist/esm/shared/src/utils.js:
773
+ lucide-react/dist/esm/defaultAttributes.js:
774
+ lucide-react/dist/esm/Icon.js:
775
+ lucide-react/dist/esm/createLucideIcon.js:
776
+ lucide-react/dist/esm/icons/maximize.js:
777
+ lucide-react/dist/esm/icons/rotate-ccw.js:
778
+ lucide-react/dist/esm/icons/zoom-in.js:
779
+ lucide-react/dist/esm/icons/zoom-out.js:
780
+ lucide-react/dist/esm/lucide-react.js:
781
+ (**
782
+ * @license lucide-react v0.511.0 - ISC
783
+ *
784
+ * This source code is licensed under the ISC license.
785
+ * See the LICENSE file in the root directory of this source tree.
786
+ *)
787
+ */
788
+
789
+ export { Canvas, babylonEngine, domEngine, useCanvas };
790
+ //# sourceMappingURL=chunk-USH43TOO.js.map
791
+ //# sourceMappingURL=chunk-USH43TOO.js.map