@neoloopy/cld-canvas 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.
- package/README.md +253 -0
- package/package.json +4 -7
package/README.md
ADDED
|
@@ -0,0 +1,253 @@
|
|
|
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
|
+
## Engine Concepts
|
|
119
|
+
|
|
120
|
+
### Storage
|
|
121
|
+
|
|
122
|
+
`NativeEngine` works against a small filesystem-like `VaultStorage` interface:
|
|
123
|
+
|
|
124
|
+
```ts
|
|
125
|
+
interface VaultStorage {
|
|
126
|
+
exists(path: string): Promise<boolean>;
|
|
127
|
+
read(path: string): Promise<string>;
|
|
128
|
+
write(path: string, data: string): Promise<void>;
|
|
129
|
+
remove(path: string): Promise<void>;
|
|
130
|
+
mkdirs(path: string): Promise<void>;
|
|
131
|
+
rmdir(path: string): Promise<void>;
|
|
132
|
+
move(from: string, to: string): Promise<void>;
|
|
133
|
+
list(path: string): Promise<{ files: string[]; folders: string[] }>;
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
Use `MemoryStorage` for tests, demos, and server-side transforms. In production,
|
|
138
|
+
adapt this interface to your host filesystem, browser persistence layer.
|
|
139
|
+
|
|
140
|
+
All paths are vault-relative and use `/` separators.
|
|
141
|
+
|
|
142
|
+
### Model Format
|
|
143
|
+
|
|
144
|
+
A model is stored as:
|
|
145
|
+
|
|
146
|
+
- `model.json` for model metadata and viewport
|
|
147
|
+
- `Nodes/*.md` for variable notes with YAML frontmatter and Markdown body
|
|
148
|
+
- `Loops/*.md` for feedback loop notes
|
|
149
|
+
- optional system notes such as `System.md`
|
|
150
|
+
|
|
151
|
+
The engine preserves unknown frontmatter keys so other neoloopy tools can add
|
|
152
|
+
metadata without being clobbered.
|
|
153
|
+
|
|
154
|
+
### Main Engine Methods
|
|
155
|
+
|
|
156
|
+
```ts
|
|
157
|
+
const model = await engine.createModel("Model name");
|
|
158
|
+
const models = await engine.listModels();
|
|
159
|
+
const graph = await engine.loadGraph(model.folder);
|
|
160
|
+
|
|
161
|
+
const node = await engine.addVariable(model.folder, {
|
|
162
|
+
label: "Inventory",
|
|
163
|
+
type: "stock",
|
|
164
|
+
x: 120,
|
|
165
|
+
y: 80,
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
await engine.updateVariable(model.folder, node.id, {
|
|
169
|
+
label: "Available inventory",
|
|
170
|
+
tags: ["operations"],
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
await engine.addLink(model.folder, node.id, "other-node-id", {
|
|
174
|
+
polarity: "-",
|
|
175
|
+
delay: true,
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
await engine.relayout(model.folder);
|
|
179
|
+
await engine.setViewport(model.folder, { x: 0, y: 0, zoom: 1 });
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
Bulk build is useful when importing generated or external CLD specs:
|
|
183
|
+
|
|
184
|
+
```ts
|
|
185
|
+
await engine.buildModel(model.folder, {
|
|
186
|
+
variables: [
|
|
187
|
+
{ id: "demand", label: "Demand" },
|
|
188
|
+
{ id: "capacity", label: "Capacity", type: "stock" },
|
|
189
|
+
],
|
|
190
|
+
links: [
|
|
191
|
+
{ from: "demand", to: "capacity", polarity: "+" },
|
|
192
|
+
{ from: "capacity", to: "demand", polarity: "-" },
|
|
193
|
+
],
|
|
194
|
+
layout: true,
|
|
195
|
+
});
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
## Exporting
|
|
199
|
+
|
|
200
|
+
`NativeEngine.export` supports `json`, `mermaid`, and `markdown`.
|
|
201
|
+
|
|
202
|
+
```ts
|
|
203
|
+
const mermaid = await engine.export(model.folder, "mermaid");
|
|
204
|
+
|
|
205
|
+
console.log(mermaid.ext); // "mmd"
|
|
206
|
+
console.log(mermaid.mime); // "text/plain"
|
|
207
|
+
console.log(mermaid.content); // graph LR...
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
You can also use the lower-level renderer functions directly:
|
|
211
|
+
|
|
212
|
+
```ts
|
|
213
|
+
import { buildMermaid, render } from "@neoloopy/cld-canvas";
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
## Analysis Helpers
|
|
217
|
+
|
|
218
|
+
```ts
|
|
219
|
+
import { LoopGraph, endogeneity } from "@neoloopy/cld-canvas";
|
|
220
|
+
|
|
221
|
+
const loopGraph = new LoopGraph(graph.nodes);
|
|
222
|
+
const loops = loopGraph.detectLoops();
|
|
223
|
+
const metrics = loopGraph.metrics();
|
|
224
|
+
const summary = endogeneity(graph.nodes, loops);
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
## What Is Exported
|
|
228
|
+
|
|
229
|
+
The package exports the public modules from `src/index.ts`, including:
|
|
230
|
+
|
|
231
|
+
- Engine: `NativeEngine`, `NeoloopyEngine`, `MemoryStorage`, `VaultStorage`
|
|
232
|
+
- Domain types: `VariableFile`, `VaultLink`, `ModelManifest`, `GraphView`
|
|
233
|
+
- Graph logic: `LoopGraph`, `DetectedLoop`, `LoopType`, `labelLoopsByKey`
|
|
234
|
+
- Rendering: `paint`, `SceneCache`, `Camera`, `LIGHT`, `DARK`, geometry helpers
|
|
235
|
+
- File codecs: `parseNote`, `serializeNote`, `manifestFromJson`
|
|
236
|
+
- Exporters: `render`, `buildMermaid`, `loopNoteKey`
|
|
237
|
+
- Analysis: `endogeneity`
|
|
238
|
+
|
|
239
|
+
## Notes for Integrators
|
|
240
|
+
|
|
241
|
+
- The package is ESM-only. Use `import`, not `require`.
|
|
242
|
+
- The engine is asynchronous because real storage adapters usually perform I/O.
|
|
243
|
+
- `move` should preserve or update links when your host platform supports it.
|
|
244
|
+
The Obsidian adapter, for example, should route folder moves through Obsidian's
|
|
245
|
+
file manager so wikilinks remain valid.
|
|
246
|
+
- Canvas rendering is stateless. Keep selection, hover, animation clocks, bow
|
|
247
|
+
caches, and badge overrides in your application state and pass them to
|
|
248
|
+
`SceneCache` or `paint`.
|
|
249
|
+
- `MemoryStorage` is not persistent; it is intended for tests and examples.
|
|
250
|
+
|
|
251
|
+
## License
|
|
252
|
+
|
|
253
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,17 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@neoloopy/cld-canvas",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
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
|
-
"license": "UNLICENSED",
|
|
6
7
|
"type": "module",
|
|
7
8
|
"main": "dist/index.js",
|
|
8
9
|
"module": "dist/index.js",
|
|
9
10
|
"types": "dist/index.d.ts",
|
|
10
11
|
"exports": { ".": { "types": "./dist/index.d.ts", "import": "./dist/index.js" } },
|
|
11
12
|
"files": ["dist"],
|
|
12
|
-
"
|
|
13
|
-
"scripts": {
|
|
14
|
-
"build": "tsc -p tsconfig.json",
|
|
15
|
-
"prepublishOnly": "tsc -p tsconfig.json"
|
|
16
|
-
}
|
|
13
|
+
"scripts": { "build": "tsc -p tsconfig.json" }
|
|
17
14
|
}
|