@particle-academy/fancy-3d 0.1.0 → 0.1.1

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.
@@ -1,127 +1,127 @@
1
- # Babylon adapter
2
-
3
- Renders a fancy-3d `Scene` as BabylonJS meshes. Each widget kind paints onto a 2D canvas that's uploaded to a `DynamicTexture` and applied to a plane. Edges become line meshes. Same `Scene` JSON, 3D output.
4
-
5
- ## Import
6
-
7
- ```ts
8
- import {
9
- createBabylonAdapter,
10
- paintWidget,
11
- placeOnCylinder,
12
- sceneBounds,
13
- TEX_SCALE,
14
- } from "@particle-academy/fancy-3d/babylon";
15
- ```
16
-
17
- **Peer dependency:** `@babylonjs/core` (optional — only required when you use this subpath).
18
-
19
- ## Basic Usage
20
-
21
- ```tsx
22
- import { Engine, Scene as BJScene, ArcRotateCamera, HemisphericLight, Vector3 } from "@babylonjs/core";
23
- import { createBabylonAdapter, placeOnCylinder, sceneBounds } from "@particle-academy/fancy-3d/babylon";
24
-
25
- const engine = new Engine(canvas, true);
26
- const bj = new BJScene(engine);
27
- new ArcRotateCamera("cam", Math.PI / 2, Math.PI / 2.2, 11, new Vector3(0, 0, 0), bj).attachControl(canvas, true);
28
- new HemisphericLight("hemi", new Vector3(0, 1, 0), bj);
29
-
30
- const adapter = createBabylonAdapter({
31
- scene3D: bj,
32
- sizeFor: (spec) => ({ w: 300, h: 200 }),
33
- });
34
-
35
- const bounds = sceneBounds(scene);
36
- scene.nodes.forEach((node) => {
37
- const mesh = adapter.render(node.widget, { nodeId: node.id, selected: false });
38
- placeOnCylinder(node, mesh, bounds, { radius: 6, arc: Math.PI * 0.9 });
39
- });
40
-
41
- engine.runRenderLoop(() => bj.render());
42
- ```
43
-
44
- ## API
45
-
46
- ### createBabylonAdapter
47
-
48
- ```ts
49
- function createBabylonAdapter(deps: BabylonAdapterDeps): WidgetAdapter<Mesh>;
50
-
51
- interface BabylonAdapterDeps {
52
- scene3D: Scene; // your Babylon Scene
53
- sizeFor(spec: WidgetSpec): { w: number; h: number };
54
- }
55
- ```
56
-
57
- | Field | Description |
58
- |------|-------------|
59
- | scene3D | The Babylon `Scene` the meshes should be added to |
60
- | sizeFor | Returns the target pixel size for a widget's texture. The plane mesh is sized world-units = `pixels * (1/180)` |
61
-
62
- The adapter's `render(spec, ctx)` returns a Babylon `Mesh` whose `metadata.nodeId` matches `ctx.nodeId` — wire `scene.onPointerObservable` to read `pickedMesh.metadata.nodeId` for selection.
63
-
64
- Selected nodes render a 2px indigo border around the painted texture.
65
-
66
- ### sceneBounds
67
-
68
- ```ts
69
- function sceneBounds(scene: Scene): {
70
- minX: number; maxX: number; minY: number; maxY: number;
71
- };
72
- ```
73
-
74
- Computes the bounding box of all nodes' world-space rectangles. Feed into `placeOnCylinder` (or your own layout function) to map 2D scene coordinates onto a 3D layout.
75
-
76
- ### placeOnCylinder
77
-
78
- ```ts
79
- function placeOnCylinder(
80
- node: SceneNode,
81
- mesh: Mesh,
82
- bounds: { minX, maxX, minY, maxY },
83
- opts?: { radius?: number; arc?: number }
84
- ): void;
85
- ```
86
-
87
- Wraps a node's 2D position onto a partial cylinder facing the camera. Default `radius=6`, `arc=π * 0.9` (~160°). DOM `x` becomes angle around the cylinder; DOM `y` becomes vertical position. Sibling layout helpers (`placeOnGrid`, `placeOnArc`, `placeOnPath`) are planned.
88
-
89
- ### paintWidget
90
-
91
- ```ts
92
- function paintWidget(
93
- ctx: CanvasRenderingContext2D,
94
- spec: WidgetSpec,
95
- w: number,
96
- h: number,
97
- selected: boolean
98
- ): void;
99
- ```
100
-
101
- Low-level escape hatch — paints any `WidgetSpec` onto a 2D canvas you supply. Used internally by the adapter and by [`createWidgetTexture`](./primitives.md#createwidgettexture). Call this when you want widget content on a custom mesh shape that primitives don't yet expose.
102
-
103
- ### TEX_SCALE
104
-
105
- ```ts
106
- const TEX_SCALE: number; // = 2
107
- ```
108
-
109
- Texture super-sampling factor — primitives create textures `TEX_SCALE×` larger than their pixel size and pre-scale the canvas context, producing crisp output on high-DPI displays.
110
-
111
- ## Selection & interaction
112
-
113
- ```ts
114
- import { PointerEventTypes } from "@babylonjs/core";
115
-
116
- bj.onPointerObservable.add((pi) => {
117
- if (pi.type !== PointerEventTypes.POINTERPICK) return;
118
- const id = (pi.pickInfo?.pickedMesh?.metadata as { nodeId?: string })?.nodeId;
119
- if (id) handleSelect(id);
120
- });
121
- ```
122
-
123
- Scale or outline the picked mesh on hover for feedback (`mesh.scaling.setAll(1.04)`, `mesh.renderOutline = true`).
124
-
125
- ## V-flip
126
-
127
- Babylon plane UVs run `V=0` at the bottom; canvas Y runs top-to-bottom. The adapter sets `tex.vScale = -1; tex.vOffset = 1` so painted content reads right-side-up. If you build textures by hand (without `createWidgetTexture` or the primitives), apply this flip yourself.
1
+ # Babylon adapter
2
+
3
+ Renders a fancy-3d `Scene` as BabylonJS meshes. Each widget kind paints onto a 2D canvas that's uploaded to a `DynamicTexture` and applied to a plane. Edges become line meshes. Same `Scene` JSON, 3D output.
4
+
5
+ ## Import
6
+
7
+ ```ts
8
+ import {
9
+ createBabylonAdapter,
10
+ paintWidget,
11
+ placeOnCylinder,
12
+ sceneBounds,
13
+ TEX_SCALE,
14
+ } from "@particle-academy/fancy-3d/babylon";
15
+ ```
16
+
17
+ **Peer dependency:** `@babylonjs/core` (optional — only required when you use this subpath).
18
+
19
+ ## Basic Usage
20
+
21
+ ```tsx
22
+ import { Engine, Scene as BJScene, ArcRotateCamera, HemisphericLight, Vector3 } from "@babylonjs/core";
23
+ import { createBabylonAdapter, placeOnCylinder, sceneBounds } from "@particle-academy/fancy-3d/babylon";
24
+
25
+ const engine = new Engine(canvas, true);
26
+ const bj = new BJScene(engine);
27
+ new ArcRotateCamera("cam", Math.PI / 2, Math.PI / 2.2, 11, new Vector3(0, 0, 0), bj).attachControl(canvas, true);
28
+ new HemisphericLight("hemi", new Vector3(0, 1, 0), bj);
29
+
30
+ const adapter = createBabylonAdapter({
31
+ scene3D: bj,
32
+ sizeFor: (spec) => ({ w: 300, h: 200 }),
33
+ });
34
+
35
+ const bounds = sceneBounds(scene);
36
+ scene.nodes.forEach((node) => {
37
+ const mesh = adapter.render(node.widget, { nodeId: node.id, selected: false });
38
+ placeOnCylinder(node, mesh, bounds, { radius: 6, arc: Math.PI * 0.9 });
39
+ });
40
+
41
+ engine.runRenderLoop(() => bj.render());
42
+ ```
43
+
44
+ ## API
45
+
46
+ ### createBabylonAdapter
47
+
48
+ ```ts
49
+ function createBabylonAdapter(deps: BabylonAdapterDeps): WidgetAdapter<Mesh>;
50
+
51
+ interface BabylonAdapterDeps {
52
+ scene3D: Scene; // your Babylon Scene
53
+ sizeFor(spec: WidgetSpec): { w: number; h: number };
54
+ }
55
+ ```
56
+
57
+ | Field | Description |
58
+ |------|-------------|
59
+ | scene3D | The Babylon `Scene` the meshes should be added to |
60
+ | sizeFor | Returns the target pixel size for a widget's texture. The plane mesh is sized world-units = `pixels * (1/180)` |
61
+
62
+ The adapter's `render(spec, ctx)` returns a Babylon `Mesh` whose `metadata.nodeId` matches `ctx.nodeId` — wire `scene.onPointerObservable` to read `pickedMesh.metadata.nodeId` for selection.
63
+
64
+ Selected nodes render a 2px indigo border around the painted texture.
65
+
66
+ ### sceneBounds
67
+
68
+ ```ts
69
+ function sceneBounds(scene: Scene): {
70
+ minX: number; maxX: number; minY: number; maxY: number;
71
+ };
72
+ ```
73
+
74
+ Computes the bounding box of all nodes' world-space rectangles. Feed into `placeOnCylinder` (or your own layout function) to map 2D scene coordinates onto a 3D layout.
75
+
76
+ ### placeOnCylinder
77
+
78
+ ```ts
79
+ function placeOnCylinder(
80
+ node: SceneNode,
81
+ mesh: Mesh,
82
+ bounds: { minX, maxX, minY, maxY },
83
+ opts?: { radius?: number; arc?: number }
84
+ ): void;
85
+ ```
86
+
87
+ Wraps a node's 2D position onto a partial cylinder facing the camera. Default `radius=6`, `arc=π * 0.9` (~160°). DOM `x` becomes angle around the cylinder; DOM `y` becomes vertical position. Sibling layout helpers (`placeOnGrid`, `placeOnArc`, `placeOnPath`) are planned.
88
+
89
+ ### paintWidget
90
+
91
+ ```ts
92
+ function paintWidget(
93
+ ctx: CanvasRenderingContext2D,
94
+ spec: WidgetSpec,
95
+ w: number,
96
+ h: number,
97
+ selected: boolean
98
+ ): void;
99
+ ```
100
+
101
+ Low-level escape hatch — paints any `WidgetSpec` onto a 2D canvas you supply. Used internally by the adapter and by [`createWidgetTexture`](./primitives.md#createwidgettexture). Call this when you want widget content on a custom mesh shape that primitives don't yet expose.
102
+
103
+ ### TEX_SCALE
104
+
105
+ ```ts
106
+ const TEX_SCALE: number; // = 2
107
+ ```
108
+
109
+ Texture super-sampling factor — primitives create textures `TEX_SCALE×` larger than their pixel size and pre-scale the canvas context, producing crisp output on high-DPI displays.
110
+
111
+ ## Selection & interaction
112
+
113
+ ```ts
114
+ import { PointerEventTypes } from "@babylonjs/core";
115
+
116
+ bj.onPointerObservable.add((pi) => {
117
+ if (pi.type !== PointerEventTypes.POINTERPICK) return;
118
+ const id = (pi.pickInfo?.pickedMesh?.metadata as { nodeId?: string })?.nodeId;
119
+ if (id) handleSelect(id);
120
+ });
121
+ ```
122
+
123
+ Scale or outline the picked mesh on hover for feedback (`mesh.scaling.setAll(1.04)`, `mesh.renderOutline = true`).
124
+
125
+ ## V-flip
126
+
127
+ Babylon plane UVs run `V=0` at the bottom; canvas Y runs top-to-bottom. The adapter sets `tex.vScale = -1; tex.vOffset = 1` so painted content reads right-side-up. If you build textures by hand (without `createWidgetTexture` or the primitives), apply this flip yourself.
@@ -1,97 +1,97 @@
1
- # DOM adapter
2
-
3
- Renders a fancy-3d `Scene` to React elements composed from `@particle-academy/react-fancy`. The reference 2D adapter — useful when you want the same scene data to power a flat dashboard alongside its 3D rendering, or as a graceful fallback when WebGL isn't available.
4
-
5
- ## Import
6
-
7
- ```ts
8
- import { domAdapter } from "@particle-academy/fancy-3d/dom";
9
- ```
10
-
11
- **Peer dependency:** `@particle-academy/react-fancy` (optional — only required when you use this subpath).
12
-
13
- ## Basic Usage
14
-
15
- ```tsx
16
- import { Canvas } from "@particle-academy/react-fancy";
17
- import { domAdapter } from "@particle-academy/fancy-3d/dom";
18
- import type { Scene } from "@particle-academy/fancy-3d";
19
-
20
- const scene: Scene = {
21
- nodes: [
22
- { id: "rev", position: { x: 60, y: 60 }, size: { w: 220, h: 110 },
23
- widget: { kind: "kpi", label: "Revenue", value: "$48k", delta: "+12%", trend: "up" } },
24
- { id: "chart", position: { x: 60, y: 200 }, size: { w: 460, h: 220 },
25
- widget: { kind: "chart", title: "Trend", variant: "area",
26
- series: [12, 18, 14, 22, 28, 24, 32], color: "#6366f1" } },
27
- ],
28
- edges: [{ id: "e1", from: "rev", to: "chart", curve: "bezier" }],
29
- };
30
-
31
- export function Dashboard() {
32
- return (
33
- <Canvas showGrid fitOnMount className="h-[600px] w-full">
34
- {scene.nodes.map((n) => (
35
- <Canvas.Node key={n.id} id={n.id} x={n.position.x} y={n.position.y}
36
- style={{ width: n.size?.w, height: n.size?.h }}>
37
- {domAdapter.render(n.widget, { nodeId: n.id, selected: false })}
38
- </Canvas.Node>
39
- ))}
40
- {scene.edges.map((e) => (
41
- <Canvas.Edge key={e.id} from={e.from} to={e.to} curve={e.curve} />
42
- ))}
43
- </Canvas>
44
- );
45
- }
46
- ```
47
-
48
- ## API
49
-
50
- ### domAdapter
51
-
52
- ```ts
53
- const domAdapter: WidgetAdapter<ReactNode>;
54
- ```
55
-
56
- Singleton adapter — no construction needed. Call `domAdapter.render(spec, ctx)` to get a React element for any `WidgetSpec`.
57
-
58
- ### AdapterContext
59
-
60
- ```ts
61
- interface AdapterContext {
62
- nodeId: string;
63
- selected: boolean;
64
- onSelect?: (id: string) => void;
65
- }
66
- ```
67
-
68
- | Field | Description |
69
- |------|-------------|
70
- | nodeId | Forwarded to the rendered element so click handlers know which node was clicked |
71
- | selected | When `true`, the element gets an indigo ring outline |
72
- | onSelect | Called on `mousedown` with the `nodeId` — wire selection state in the host |
73
-
74
- ## Widget coverage
75
-
76
- The DOM adapter handles every kind in [Scene types](./scene.md#widgetspec):
77
-
78
- | Widget kind | Rendered as |
79
- |-------------|-------------|
80
- | kpi | `<Card>` with label + big value + delta |
81
- | chart | `<Card>` with inline SVG sparkline (line / bar / area) |
82
- | kanban | `<Card>` with column tracks + card chips |
83
- | table | `<Card>` with `<table>` rows |
84
- | profile | `<Profile>` inside `<Card>` |
85
- | callout | `<Callout>` with tone-mapped color |
86
- | form | `<Card>` with `<Switch>` and native inputs |
87
- | action | `<Card>` with `<Action>` buttons |
88
- | timeline | `<Card>` with `<Timeline events={...}>` |
89
- | code | `<Card>` with `<pre>` block + language badge |
90
- | image | `<Card>` wrapping `<img>` |
91
- | text | `<Card>` with heading + body |
92
- | demoPage | Card with accent header bar, category chip, path, title, description, CTA |
93
- | screen | Inner `<canvas>` (when `paint` provided) or static label/image, framed by a styled bezel + power LED |
94
-
95
- ## Adding more kinds
96
-
97
- Adding a new `WidgetSpec` discriminant requires updates in three places: `scene.ts` (type), `painters.ts` (Babylon paint case), and `dom.tsx` (React render case). Both adapters are kept in lock-step so any consumer can swap engines without losing widgets.
1
+ # DOM adapter
2
+
3
+ Renders a fancy-3d `Scene` to React elements composed from `@particle-academy/react-fancy`. The reference 2D adapter — useful when you want the same scene data to power a flat dashboard alongside its 3D rendering, or as a graceful fallback when WebGL isn't available.
4
+
5
+ ## Import
6
+
7
+ ```ts
8
+ import { domAdapter } from "@particle-academy/fancy-3d/dom";
9
+ ```
10
+
11
+ **Peer dependency:** `@particle-academy/react-fancy` (optional — only required when you use this subpath).
12
+
13
+ ## Basic Usage
14
+
15
+ ```tsx
16
+ import { Canvas } from "@particle-academy/react-fancy";
17
+ import { domAdapter } from "@particle-academy/fancy-3d/dom";
18
+ import type { Scene } from "@particle-academy/fancy-3d";
19
+
20
+ const scene: Scene = {
21
+ nodes: [
22
+ { id: "rev", position: { x: 60, y: 60 }, size: { w: 220, h: 110 },
23
+ widget: { kind: "kpi", label: "Revenue", value: "$48k", delta: "+12%", trend: "up" } },
24
+ { id: "chart", position: { x: 60, y: 200 }, size: { w: 460, h: 220 },
25
+ widget: { kind: "chart", title: "Trend", variant: "area",
26
+ series: [12, 18, 14, 22, 28, 24, 32], color: "#6366f1" } },
27
+ ],
28
+ edges: [{ id: "e1", from: "rev", to: "chart", curve: "bezier" }],
29
+ };
30
+
31
+ export function Dashboard() {
32
+ return (
33
+ <Canvas showGrid fitOnMount className="h-[600px] w-full">
34
+ {scene.nodes.map((n) => (
35
+ <Canvas.Node key={n.id} id={n.id} x={n.position.x} y={n.position.y}
36
+ style={{ width: n.size?.w, height: n.size?.h }}>
37
+ {domAdapter.render(n.widget, { nodeId: n.id, selected: false })}
38
+ </Canvas.Node>
39
+ ))}
40
+ {scene.edges.map((e) => (
41
+ <Canvas.Edge key={e.id} from={e.from} to={e.to} curve={e.curve} />
42
+ ))}
43
+ </Canvas>
44
+ );
45
+ }
46
+ ```
47
+
48
+ ## API
49
+
50
+ ### domAdapter
51
+
52
+ ```ts
53
+ const domAdapter: WidgetAdapter<ReactNode>;
54
+ ```
55
+
56
+ Singleton adapter — no construction needed. Call `domAdapter.render(spec, ctx)` to get a React element for any `WidgetSpec`.
57
+
58
+ ### AdapterContext
59
+
60
+ ```ts
61
+ interface AdapterContext {
62
+ nodeId: string;
63
+ selected: boolean;
64
+ onSelect?: (id: string) => void;
65
+ }
66
+ ```
67
+
68
+ | Field | Description |
69
+ |------|-------------|
70
+ | nodeId | Forwarded to the rendered element so click handlers know which node was clicked |
71
+ | selected | When `true`, the element gets an indigo ring outline |
72
+ | onSelect | Called on `mousedown` with the `nodeId` — wire selection state in the host |
73
+
74
+ ## Widget coverage
75
+
76
+ The DOM adapter handles every kind in [Scene types](./scene.md#widgetspec):
77
+
78
+ | Widget kind | Rendered as |
79
+ |-------------|-------------|
80
+ | kpi | `<Card>` with label + big value + delta |
81
+ | chart | `<Card>` with inline SVG sparkline (line / bar / area) |
82
+ | kanban | `<Card>` with column tracks + card chips |
83
+ | table | `<Card>` with `<table>` rows |
84
+ | profile | `<Profile>` inside `<Card>` |
85
+ | callout | `<Callout>` with tone-mapped color |
86
+ | form | `<Card>` with `<Switch>` and native inputs |
87
+ | action | `<Card>` with `<Action>` buttons |
88
+ | timeline | `<Card>` with `<Timeline events={...}>` |
89
+ | code | `<Card>` with `<pre>` block + language badge |
90
+ | image | `<Card>` wrapping `<img>` |
91
+ | text | `<Card>` with heading + body |
92
+ | demoPage | Card with accent header bar, category chip, path, title, description, CTA |
93
+ | screen | Inner `<canvas>` (when `paint` provided) or static label/image, framed by a styled bezel + power LED |
94
+
95
+ ## Adding more kinds
96
+
97
+ Adding a new `WidgetSpec` discriminant requires updates in three places: `scene.ts` (type), `painters.ts` (Babylon paint case), and `dom.tsx` (React render case). Both adapters are kept in lock-step so any consumer can swap engines without losing widgets.