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