@neoloopy/cld-canvas 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +330 -0
- package/package.json +24 -7
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 neoloopy
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
# @neoloopy/cld-canvas
|
|
2
|
+
|
|
3
|
+
Framework-agnostic causal loop diagram (CLD) canvas renderer and in-memory edit
|
|
4
|
+
engine used by neoloopy surfaces, including the Obsidian plugin and web app.
|
|
5
|
+
|
|
6
|
+
The package is pure TypeScript, ESM-only, and has no runtime dependency on
|
|
7
|
+
Obsidian, React, Vue, or a server. It gives you two pieces:
|
|
8
|
+
|
|
9
|
+
- A vault-compatible CLD engine for creating, editing, loading, exporting, and
|
|
10
|
+
analyzing neoloopy models.
|
|
11
|
+
- A canvas rendering layer for drawing nodes, causal links, polarity chips,
|
|
12
|
+
loop badges, selection state, and pan/zoom views.
|
|
13
|
+
|
|
14
|
+
## Install
|
|
15
|
+
|
|
16
|
+
```sh
|
|
17
|
+
npm install @neoloopy/cld-canvas
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
If you use `NativeEngine`, provide a YAML parser for note frontmatter. The
|
|
21
|
+
package does not bundle one:
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
npm install yaml
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Requirements
|
|
28
|
+
|
|
29
|
+
- TypeScript or JavaScript with ESM imports
|
|
30
|
+
- `ES2020` runtime support
|
|
31
|
+
- A `CanvasRenderingContext2D` if you use the painter
|
|
32
|
+
|
|
33
|
+
## Quick Start
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
import { parse } from "yaml";
|
|
37
|
+
import {
|
|
38
|
+
MemoryStorage,
|
|
39
|
+
NativeEngine,
|
|
40
|
+
} from "@neoloopy/cld-canvas";
|
|
41
|
+
|
|
42
|
+
const storage = new MemoryStorage();
|
|
43
|
+
const engine = new NativeEngine(storage, parse, {
|
|
44
|
+
modelsRoot: "Models",
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const model = await engine.createModel("Growth Loop");
|
|
48
|
+
|
|
49
|
+
await engine.buildModel(model.folder, {
|
|
50
|
+
variables: [
|
|
51
|
+
{ label: "Users" },
|
|
52
|
+
{ label: "Word of mouth" },
|
|
53
|
+
{ label: "Signups" },
|
|
54
|
+
],
|
|
55
|
+
links: [
|
|
56
|
+
{ from: "Users", to: "Word of mouth", polarity: "+" },
|
|
57
|
+
{ from: "Word of mouth", to: "Signups", polarity: "+" },
|
|
58
|
+
{ from: "Signups", to: "Users", polarity: "+" },
|
|
59
|
+
],
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
const graph = await engine.loadGraph(model.folder);
|
|
63
|
+
|
|
64
|
+
console.log(graph.nodes.map((node) => node.label));
|
|
65
|
+
console.log(graph.loops.length);
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Rendering to Canvas
|
|
69
|
+
|
|
70
|
+
The renderer is intentionally UI-framework neutral. Build a renderable scene
|
|
71
|
+
from a `GraphView`, configure a `Camera`, then call `paint`.
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
import {
|
|
75
|
+
Camera,
|
|
76
|
+
LIGHT,
|
|
77
|
+
SceneCache,
|
|
78
|
+
paint,
|
|
79
|
+
} from "@neoloopy/cld-canvas";
|
|
80
|
+
|
|
81
|
+
const canvas = document.querySelector("canvas")!;
|
|
82
|
+
const ctx = canvas.getContext("2d")!;
|
|
83
|
+
const dpr = window.devicePixelRatio || 1;
|
|
84
|
+
|
|
85
|
+
const width = canvas.clientWidth;
|
|
86
|
+
const height = canvas.clientHeight;
|
|
87
|
+
canvas.width = Math.floor(width * dpr);
|
|
88
|
+
canvas.height = Math.floor(height * dpr);
|
|
89
|
+
|
|
90
|
+
const camera = new Camera();
|
|
91
|
+
const sceneCache = new SceneCache();
|
|
92
|
+
const scene = sceneCache.build(graph, new Map(), new Map());
|
|
93
|
+
|
|
94
|
+
if (scene) {
|
|
95
|
+
sceneCache.fit(camera, width, height);
|
|
96
|
+
|
|
97
|
+
paint(ctx, scene, camera, LIGHT, {
|
|
98
|
+
cssWidth: width,
|
|
99
|
+
cssHeight: height,
|
|
100
|
+
dpr,
|
|
101
|
+
selectedNodeId: null,
|
|
102
|
+
selectedEdgeId: null,
|
|
103
|
+
selectedLoopKey: null,
|
|
104
|
+
liveNodeIds: new Set(),
|
|
105
|
+
linkPreview: null,
|
|
106
|
+
connectNodeId: null,
|
|
107
|
+
loopHighlight: null,
|
|
108
|
+
pulsePhase: 0,
|
|
109
|
+
flowPhase: 0,
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
Use `Camera.panBy`, `Camera.zoomAt`, `Camera.setScaleAt`, and
|
|
115
|
+
`Camera.centerOn` to wire your own pointer, wheel, trackpad, minimap, or toolbar
|
|
116
|
+
controls.
|
|
117
|
+
|
|
118
|
+
## Using It in React (or any framework)
|
|
119
|
+
|
|
120
|
+
There is no framework-specific component — `paint` only needs a `<canvas>` 2D
|
|
121
|
+
context, so a thin wrapper is all it takes. A minimal React component that turns
|
|
122
|
+
a spec into a rendered diagram:
|
|
123
|
+
|
|
124
|
+
```tsx
|
|
125
|
+
import { useEffect, useRef } from "react";
|
|
126
|
+
import { parse } from "yaml";
|
|
127
|
+
import {
|
|
128
|
+
MemoryStorage,
|
|
129
|
+
NativeEngine,
|
|
130
|
+
Camera,
|
|
131
|
+
SceneCache,
|
|
132
|
+
paint,
|
|
133
|
+
LIGHT,
|
|
134
|
+
} from "@neoloopy/cld-canvas";
|
|
135
|
+
import type { BuildSpec } from "@neoloopy/cld-canvas";
|
|
136
|
+
|
|
137
|
+
export function CldCanvas({ spec }: { spec: BuildSpec }) {
|
|
138
|
+
const ref = useRef<HTMLCanvasElement>(null);
|
|
139
|
+
|
|
140
|
+
useEffect(() => {
|
|
141
|
+
let alive = true;
|
|
142
|
+
(async () => {
|
|
143
|
+
const engine = new NativeEngine(new MemoryStorage(), parse, {
|
|
144
|
+
modelsRoot: "Models",
|
|
145
|
+
});
|
|
146
|
+
const model = await engine.createModel("model");
|
|
147
|
+
await engine.buildModel(model.folder, spec);
|
|
148
|
+
const graph = await engine.loadGraph(model.folder);
|
|
149
|
+
|
|
150
|
+
const canvas = ref.current;
|
|
151
|
+
const ctx = canvas?.getContext("2d");
|
|
152
|
+
if (!alive || !canvas || !ctx) return;
|
|
153
|
+
|
|
154
|
+
const dpr = window.devicePixelRatio || 1;
|
|
155
|
+
const w = canvas.clientWidth;
|
|
156
|
+
const h = canvas.clientHeight;
|
|
157
|
+
canvas.width = Math.floor(w * dpr);
|
|
158
|
+
canvas.height = Math.floor(h * dpr);
|
|
159
|
+
|
|
160
|
+
const camera = new Camera();
|
|
161
|
+
const sceneCache = new SceneCache();
|
|
162
|
+
const scene = sceneCache.build(graph, new Map(), new Map());
|
|
163
|
+
if (!scene) return;
|
|
164
|
+
sceneCache.fit(camera, w, h);
|
|
165
|
+
|
|
166
|
+
paint(ctx, scene, camera, LIGHT, {
|
|
167
|
+
cssWidth: w,
|
|
168
|
+
cssHeight: h,
|
|
169
|
+
dpr,
|
|
170
|
+
selectedNodeId: null,
|
|
171
|
+
selectedEdgeId: null,
|
|
172
|
+
selectedLoopKey: null,
|
|
173
|
+
liveNodeIds: new Set(),
|
|
174
|
+
linkPreview: null,
|
|
175
|
+
connectNodeId: null,
|
|
176
|
+
loopHighlight: null,
|
|
177
|
+
pulsePhase: 0,
|
|
178
|
+
flowPhase: 0,
|
|
179
|
+
});
|
|
180
|
+
})();
|
|
181
|
+
return () => {
|
|
182
|
+
alive = false;
|
|
183
|
+
};
|
|
184
|
+
}, [spec]);
|
|
185
|
+
|
|
186
|
+
return <canvas ref={ref} style={{ width: "100%", height: 400 }} />;
|
|
187
|
+
}
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
To make it interactive, keep one `Camera` and one `SceneCache` across renders,
|
|
191
|
+
wire `Camera.panBy` / `Camera.zoomAt` to pointer and wheel events, and repaint.
|
|
192
|
+
The same wrapper shape works in Vue, Svelte, or plain DOM — only the lifecycle
|
|
193
|
+
hook changes.
|
|
194
|
+
|
|
195
|
+
## Engine Concepts
|
|
196
|
+
|
|
197
|
+
### Storage
|
|
198
|
+
|
|
199
|
+
`NativeEngine` works against a small filesystem-like `VaultStorage` interface:
|
|
200
|
+
|
|
201
|
+
```ts
|
|
202
|
+
interface VaultStorage {
|
|
203
|
+
exists(path: string): Promise<boolean>;
|
|
204
|
+
read(path: string): Promise<string>;
|
|
205
|
+
write(path: string, data: string): Promise<void>;
|
|
206
|
+
remove(path: string): Promise<void>;
|
|
207
|
+
mkdirs(path: string): Promise<void>;
|
|
208
|
+
rmdir(path: string): Promise<void>;
|
|
209
|
+
move(from: string, to: string): Promise<void>;
|
|
210
|
+
list(path: string): Promise<{ files: string[]; folders: string[] }>;
|
|
211
|
+
}
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
Use `MemoryStorage` for tests, demos, and server-side transforms. In production,
|
|
215
|
+
adapt this interface to your host filesystem, browser persistence layer.
|
|
216
|
+
|
|
217
|
+
All paths are vault-relative and use `/` separators.
|
|
218
|
+
|
|
219
|
+
### Model Format
|
|
220
|
+
|
|
221
|
+
A model is stored as:
|
|
222
|
+
|
|
223
|
+
- `model.json` for model metadata and viewport
|
|
224
|
+
- `Nodes/*.md` for variable notes with YAML frontmatter and Markdown body
|
|
225
|
+
- `Loops/*.md` for feedback loop notes
|
|
226
|
+
- optional system notes such as `System.md`
|
|
227
|
+
|
|
228
|
+
The engine preserves unknown frontmatter keys so other neoloopy tools can add
|
|
229
|
+
metadata without being clobbered.
|
|
230
|
+
|
|
231
|
+
### Main Engine Methods
|
|
232
|
+
|
|
233
|
+
```ts
|
|
234
|
+
const model = await engine.createModel("Model name");
|
|
235
|
+
const models = await engine.listModels();
|
|
236
|
+
const graph = await engine.loadGraph(model.folder);
|
|
237
|
+
|
|
238
|
+
const node = await engine.addVariable(model.folder, {
|
|
239
|
+
label: "Inventory",
|
|
240
|
+
type: "stock",
|
|
241
|
+
x: 120,
|
|
242
|
+
y: 80,
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
await engine.updateVariable(model.folder, node.id, {
|
|
246
|
+
label: "Available inventory",
|
|
247
|
+
tags: ["operations"],
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
await engine.addLink(model.folder, node.id, "other-node-id", {
|
|
251
|
+
polarity: "-",
|
|
252
|
+
delay: true,
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
await engine.relayout(model.folder);
|
|
256
|
+
await engine.setViewport(model.folder, { x: 0, y: 0, zoom: 1 });
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
Bulk build is useful when importing generated or external CLD specs:
|
|
260
|
+
|
|
261
|
+
```ts
|
|
262
|
+
await engine.buildModel(model.folder, {
|
|
263
|
+
variables: [
|
|
264
|
+
{ id: "demand", label: "Demand" },
|
|
265
|
+
{ id: "capacity", label: "Capacity", type: "stock" },
|
|
266
|
+
],
|
|
267
|
+
links: [
|
|
268
|
+
{ from: "demand", to: "capacity", polarity: "+" },
|
|
269
|
+
{ from: "capacity", to: "demand", polarity: "-" },
|
|
270
|
+
],
|
|
271
|
+
layout: true,
|
|
272
|
+
});
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
## Exporting
|
|
276
|
+
|
|
277
|
+
`NativeEngine.export` supports `json`, `mermaid`, and `markdown`.
|
|
278
|
+
|
|
279
|
+
```ts
|
|
280
|
+
const mermaid = await engine.export(model.folder, "mermaid");
|
|
281
|
+
|
|
282
|
+
console.log(mermaid.ext); // "mmd"
|
|
283
|
+
console.log(mermaid.mime); // "text/plain"
|
|
284
|
+
console.log(mermaid.content); // graph LR...
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
You can also use the lower-level renderer functions directly:
|
|
288
|
+
|
|
289
|
+
```ts
|
|
290
|
+
import { buildMermaid, render } from "@neoloopy/cld-canvas";
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
## Analysis Helpers
|
|
294
|
+
|
|
295
|
+
```ts
|
|
296
|
+
import { LoopGraph, endogeneity } from "@neoloopy/cld-canvas";
|
|
297
|
+
|
|
298
|
+
const loopGraph = new LoopGraph(graph.nodes);
|
|
299
|
+
const loops = loopGraph.detectLoops();
|
|
300
|
+
const metrics = loopGraph.metrics();
|
|
301
|
+
const summary = endogeneity(graph.nodes, loops);
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
## What Is Exported
|
|
305
|
+
|
|
306
|
+
The package exports the public modules from `src/index.ts`, including:
|
|
307
|
+
|
|
308
|
+
- Engine: `NativeEngine`, `NeoloopyEngine`, `MemoryStorage`, `VaultStorage`
|
|
309
|
+
- Domain types: `VariableFile`, `VaultLink`, `ModelManifest`, `GraphView`
|
|
310
|
+
- Graph logic: `LoopGraph`, `DetectedLoop`, `LoopType`, `labelLoopsByKey`
|
|
311
|
+
- Rendering: `paint`, `SceneCache`, `Camera`, `LIGHT`, `DARK`, geometry helpers
|
|
312
|
+
- File codecs: `parseNote`, `serializeNote`, `manifestFromJson`
|
|
313
|
+
- Exporters: `render`, `buildMermaid`, `loopNoteKey`
|
|
314
|
+
- Analysis: `endogeneity`
|
|
315
|
+
|
|
316
|
+
## Notes for Integrators
|
|
317
|
+
|
|
318
|
+
- The package is ESM-only. Use `import`, not `require`.
|
|
319
|
+
- The engine is asynchronous because real storage adapters usually perform I/O.
|
|
320
|
+
- `move` should preserve or update links when your host platform supports it.
|
|
321
|
+
The Obsidian adapter, for example, should route folder moves through Obsidian's
|
|
322
|
+
file manager so wikilinks remain valid.
|
|
323
|
+
- Canvas rendering is stateless. Keep selection, hover, animation clocks, bow
|
|
324
|
+
caches, and badge overrides in your application state and pass them to
|
|
325
|
+
`SceneCache` or `paint`.
|
|
326
|
+
- `MemoryStorage` is not persistent; it is intended for tests and examples.
|
|
327
|
+
|
|
328
|
+
## License
|
|
329
|
+
|
|
330
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,17 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@neoloopy/cld-canvas",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"author": "neoloopy",
|
|
5
|
+
"license": "MIT",
|
|
4
6
|
"description": "Framework-agnostic CLD canvas renderer + in-memory edit engine (shared by the neoloopy Obsidian plugin and neoloopy.com).",
|
|
5
|
-
"
|
|
7
|
+
"keywords": [
|
|
8
|
+
"cld",
|
|
9
|
+
"causal-loop-diagram",
|
|
10
|
+
"systems-thinking",
|
|
11
|
+
"system-dynamics",
|
|
12
|
+
"feedback-loops",
|
|
13
|
+
"canvas",
|
|
14
|
+
"diagram",
|
|
15
|
+
"visualization",
|
|
16
|
+
"graph",
|
|
17
|
+
"neoloopy"
|
|
18
|
+
],
|
|
6
19
|
"type": "module",
|
|
7
20
|
"main": "dist/index.js",
|
|
8
21
|
"module": "dist/index.js",
|
|
9
22
|
"types": "dist/index.d.ts",
|
|
10
23
|
"exports": { ".": { "types": "./dist/index.d.ts", "import": "./dist/index.js" } },
|
|
24
|
+
"sideEffects": false,
|
|
11
25
|
"files": ["dist"],
|
|
12
|
-
"
|
|
13
|
-
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
}
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "git+https://github.com/frozenabe/neoloopy-obsidian.git",
|
|
29
|
+
"directory": "packages/cld-canvas"
|
|
30
|
+
},
|
|
31
|
+
"homepage": "https://github.com/frozenabe/neoloopy-obsidian/tree/main/packages/cld-canvas#readme",
|
|
32
|
+
"bugs": { "url": "https://github.com/frozenabe/neoloopy-obsidian/issues" },
|
|
33
|
+
"scripts": { "build": "tsc -p tsconfig.json" }
|
|
17
34
|
}
|