@tinyvolt/pie 0.9.0 → 0.11.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/README.md +18 -1
- package/dist/components/SplitPane.d.ts +21 -0
- package/dist/components/SplitPane.jsx +113 -0
- package/dist/components/icons.d.ts +12 -0
- package/dist/components/icons.jsx +45 -0
- package/dist/lib/autodiff/index.d.ts +28 -0
- package/dist/lib/autodiff/index.js +39 -0
- package/dist/lib/customModules.d.ts +19 -0
- package/dist/lib/customModules.js +52 -0
- package/dist/lib/markdown/compile.d.ts +27 -0
- package/dist/lib/markdown/compile.js +26 -0
- package/dist/lib/markdown/components/CanvasPage.d.ts +9 -0
- package/dist/lib/markdown/components/CanvasPage.jsx +16 -0
- package/dist/lib/markdown/components/MarkdownBody.d.ts +11 -0
- package/dist/lib/markdown/components/MarkdownBody.jsx +37 -0
- package/dist/lib/markdown/components/PiePage.d.ts +14 -0
- package/dist/lib/markdown/components/PiePage.jsx +42 -0
- package/dist/lib/markdown/components/PythonEditor.d.ts +10 -0
- package/dist/lib/markdown/components/PythonEditor.jsx +209 -0
- package/dist/lib/markdown/features/build.d.ts +60 -0
- package/dist/lib/markdown/features/build.js +36 -0
- package/dist/lib/markdown/features/glossary/View.d.ts +9 -0
- package/dist/lib/markdown/features/glossary/View.jsx +62 -0
- package/dist/lib/markdown/features/glossary/build.d.ts +6 -0
- package/dist/lib/markdown/features/glossary/build.js +81 -0
- package/dist/lib/markdown/features/sidebar/View.d.ts +11 -0
- package/dist/lib/markdown/features/sidebar/View.jsx +45 -0
- package/dist/lib/markdown/features/sidebar/build.d.ts +3 -0
- package/dist/lib/markdown/features/sidebar/build.js +7 -0
- package/dist/lib/markdown/features/tags/build.d.ts +7 -0
- package/dist/lib/markdown/features/tags/build.js +22 -0
- package/dist/lib/markdown/features/tags/url.d.ts +6 -0
- package/dist/lib/markdown/features/tags/url.js +10 -0
- package/dist/lib/markdown/features/toc/build.d.ts +7 -0
- package/dist/lib/markdown/features/toc/build.js +62 -0
- package/dist/lib/markdown/features/views.d.ts +4 -0
- package/dist/lib/markdown/features/views.jsx +11 -0
- package/dist/lib/markdown/frontmatter.d.ts +44 -0
- package/dist/lib/markdown/frontmatter.js +109 -0
- package/dist/lib/markdown/render.d.ts +6 -0
- package/dist/lib/markdown/render.js +95 -0
- package/dist/lib/nodes/animation.d.ts +14 -0
- package/dist/lib/nodes/animation.js +34 -0
- package/dist/lib/nodes/components/CenteredGrid.d.ts +20 -0
- package/dist/lib/nodes/components/CenteredGrid.jsx +44 -0
- package/dist/lib/nodes/components/NodeCanvas.d.ts +7 -0
- package/dist/lib/nodes/components/NodeCanvas.jsx +50 -0
- package/dist/lib/nodes/execute.d.ts +27 -0
- package/dist/lib/nodes/execute.js +159 -0
- package/dist/lib/nodes/functions/Node2NodeFunc.d.ts +70 -0
- package/dist/lib/nodes/functions/Node2NodeFunc.js +223 -0
- package/dist/lib/nodes/functions/array.d.ts +12 -0
- package/dist/lib/nodes/functions/array.js +31 -0
- package/dist/lib/nodes/functions/autodiff.d.ts +12 -0
- package/dist/lib/nodes/functions/autodiff.js +60 -0
- package/dist/lib/nodes/functions/basic.d.ts +111 -0
- package/dist/lib/nodes/functions/basic.js +275 -0
- package/dist/lib/nodes/functions/registry.d.ts +9 -0
- package/dist/lib/nodes/functions/registry.js +45 -0
- package/dist/lib/nodes/functions/sideEffects.d.ts +78 -0
- package/dist/lib/nodes/functions/sideEffects.js +201 -0
- package/dist/lib/nodes/hooks/useCanvasGestures.d.ts +9 -0
- package/dist/lib/nodes/hooks/useCanvasGestures.js +120 -0
- package/dist/lib/nodes/hooks/useCanvasResize.d.ts +2 -0
- package/dist/lib/nodes/hooks/useCanvasResize.js +33 -0
- package/dist/lib/nodes/jax.d.ts +46 -0
- package/dist/lib/nodes/jax.js +120 -0
- package/dist/lib/nodes/render/renderLogic.d.ts +13 -0
- package/dist/lib/nodes/render/renderLogic.jsx +234 -0
- package/dist/lib/nodes/state/NameGenerator.d.ts +10 -0
- package/dist/lib/nodes/state/NameGenerator.js +29 -0
- package/dist/lib/nodes/state/Node.d.ts +134 -0
- package/dist/lib/nodes/state/Node.js +57 -0
- package/dist/lib/nodes/state/NodeStore.d.ts +49 -0
- package/dist/lib/nodes/state/NodeStore.js +138 -0
- package/dist/lib/nodes/tensors/broadcast.d.ts +18 -0
- package/dist/lib/nodes/tensors/broadcast.js +92 -0
- package/dist/lib/nodes/tensors/tensor.d.ts +13 -0
- package/dist/lib/nodes/tensors/tensor.js +34 -0
- package/dist/lib/python/lift.d.ts +51 -0
- package/dist/lib/python/lift.js +155 -0
- package/dist/lib/python/py/pie.py +303 -0
- package/dist/lib/python/py/pie_mpl.py +15 -0
- package/dist/lib/python/py/show_figures.py +5 -0
- package/dist/lib/python/runPage.d.ts +21 -0
- package/dist/lib/python/runPage.js +60 -0
- package/dist/lib/python/runner.d.ts +68 -0
- package/dist/lib/python/runner.js +243 -0
- package/dist/lib/python/worker.d.ts +1 -0
- package/dist/lib/python/worker.js +111 -0
- package/dist/lib/ui/UiLayer.d.ts +2 -0
- package/dist/lib/ui/UiLayer.jsx +15 -0
- package/dist/lib/ui/UiView.d.ts +23 -0
- package/dist/lib/ui/UiView.jsx +56 -0
- package/dist/lib/ui/components/checkboxes.d.ts +10 -0
- package/dist/lib/ui/components/checkboxes.jsx +30 -0
- package/dist/lib/ui/components/latex.d.ts +8 -0
- package/dist/lib/ui/components/latex.jsx +41 -0
- package/dist/lib/ui/components/radio.d.ts +6 -0
- package/dist/lib/ui/components/radio.jsx +19 -0
- package/dist/lib/ui/components/slider.d.ts +8 -0
- package/dist/lib/ui/components/slider.jsx +7 -0
- package/dist/lib/ui/components/textbox.d.ts +5 -0
- package/dist/lib/ui/components/textbox.jsx +21 -0
- package/dist/lib/ui/dragUi.d.ts +10 -0
- package/dist/lib/ui/dragUi.js +129 -0
- package/dist/lib/ui/nodeHandle.d.ts +15 -0
- package/dist/lib/ui/nodeHandle.js +37 -0
- package/dist/lib/ui/registry.d.ts +10 -0
- package/dist/lib/ui/registry.js +42 -0
- package/dist/runtime.css +696 -0
- package/dist/runtime.d.ts +10 -0
- package/dist/runtime.jsx +7 -0
- package/init/index.js +2 -2
- package/init/site-agents.md +2 -1
- package/package.json +15 -6
- package/runtime-agents.md +168 -0
- package/src/app.css +7 -665
- package/src/lib/customModules.ts +67 -0
- package/src/lib/markdown/compile.ts +49 -0
- package/src/lib/markdown/components/PiePage.tsx +49 -0
- package/src/lib/markdown/features/build.ts +15 -6
- package/src/lib/markdown/features/sidebar/View.tsx +4 -2
- package/src/lib/markdown/features/tags/build.ts +6 -3
- package/src/lib/markdown/pages.ts +1 -9
- package/src/lib/markdown/vitePlugin.ts +4 -8
- package/src/lib/nodes/functions/registry.ts +30 -21
- package/src/lib/nodes/functions/sideEffects.ts +14 -8
- package/src/lib/nodes/functions/vitePlugin.ts +8 -10
- package/src/lib/nodes/state/Node.ts +27 -19
- package/src/lib/python/runPage.ts +78 -0
- package/src/lib/python/runner.ts +189 -164
- package/src/lib/siteModules.ts +13 -0
- package/src/lib/ui/dragUi.ts +4 -2
- package/src/lib/ui/registry.ts +35 -14
- package/src/page.css +664 -0
- package/src/routes/[...slug].tsx +1 -0
- package/src/runtime.css +29 -0
- package/src/runtime.tsx +16 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { createMemo, createRoot, createSignal, onCleanup } from 'solid-js';
|
|
2
|
+
import { checkColor, getNodeTypes } from '../state/Node';
|
|
3
|
+
import { add, num, ref, scalar } from '../jax';
|
|
4
|
+
/**
|
|
5
|
+
* Saves `node` with `changes`, new values for some of its numbers, in place of `node`.
|
|
6
|
+
* It has no inputs, so it no longer follows the old node's.
|
|
7
|
+
* With `animate`, returns a track that moves the numbers from their old values to the new ones.
|
|
8
|
+
*/
|
|
9
|
+
export function replaceNode(store, node, changes, animate) {
|
|
10
|
+
const { __graph_deps, ...unchanged } = node;
|
|
11
|
+
const end = { ...unchanged, ...changes };
|
|
12
|
+
if (!animate) {
|
|
13
|
+
createRoot((dispose) => store.saveNode(node.id, createMemo(() => end), dispose));
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
// [key, start, end] for each changed number.
|
|
17
|
+
const moves = Object.entries(changes).map(([key, value]) => [key, num(node[key]), num(value)]);
|
|
18
|
+
const at = (t) => ({ ...end, ...Object.fromEntries(moves.map(([key, a, b]) => [key, scalar(a + (b - a) * t)])) });
|
|
19
|
+
return createRoot((dispose) => {
|
|
20
|
+
const [progress, setProgress] = createSignal(0);
|
|
21
|
+
let stopped = false;
|
|
22
|
+
store.saveNode(node.id, createMemo(() => (progress() < 1 ? at(progress()) : end)), dispose);
|
|
23
|
+
// Another change to this node, or `clear`, disposes this one: it stops moving.
|
|
24
|
+
onCleanup(() => {
|
|
25
|
+
stopped = true;
|
|
26
|
+
});
|
|
27
|
+
return { seek: setProgress, stopped: () => stopped };
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
// A parameter that takes any node, but not a whole Array: an Array is looped over, so each item is hidden or shown.
|
|
31
|
+
// A getter, so it includes custom node types set after this file loads.
|
|
32
|
+
const anyItem = (name) => ({
|
|
33
|
+
name,
|
|
34
|
+
get type() {
|
|
35
|
+
return getNodeTypes().filter(type => type !== 'Array');
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
/**
|
|
39
|
+
* Hide a node: it is not drawn on the canvas.
|
|
40
|
+
* @param node - Node to hide
|
|
41
|
+
*/
|
|
42
|
+
export const Hide = {
|
|
43
|
+
keyword: 'hide',
|
|
44
|
+
parameters: [anyItem('node')],
|
|
45
|
+
run(inputs, store) {
|
|
46
|
+
store.updateNode(inputs[0].id, { hidden: true });
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Show a hidden node.
|
|
51
|
+
* @param node - Node to show
|
|
52
|
+
*/
|
|
53
|
+
export const Show = {
|
|
54
|
+
keyword: 'show',
|
|
55
|
+
parameters: [anyItem('node')],
|
|
56
|
+
run(inputs, store) {
|
|
57
|
+
store.updateNode(inputs[0].id, { hidden: false });
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* Remove a node from the canvas.
|
|
62
|
+
* @param node - Node to remove
|
|
63
|
+
*/
|
|
64
|
+
export const Remove = {
|
|
65
|
+
keyword: 'remove',
|
|
66
|
+
parameters: [anyItem('node')],
|
|
67
|
+
run(inputs, store) {
|
|
68
|
+
store.removeNode(inputs[0].id);
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* Set a node's fill.
|
|
73
|
+
* @param node - Node to change
|
|
74
|
+
* @param color - A CSS color, like 'red' or '#ff0000', or 'none'
|
|
75
|
+
*/
|
|
76
|
+
export const SetFill = {
|
|
77
|
+
keyword: 'set_fill',
|
|
78
|
+
parameters: [anyItem('node'), { name: 'color', type: 'Text' }],
|
|
79
|
+
run(inputs, store) {
|
|
80
|
+
const fill = inputs[1].value;
|
|
81
|
+
checkColor('set_fill', 'fill', fill);
|
|
82
|
+
store.updateNode(inputs[0].id, { fill });
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
/**
|
|
86
|
+
* Set a node's stroke.
|
|
87
|
+
* @param node - Node to change
|
|
88
|
+
* @param color - A CSS color, like 'red' or '#ff0000', or 'none'
|
|
89
|
+
*/
|
|
90
|
+
export const SetStroke = {
|
|
91
|
+
keyword: 'set_stroke',
|
|
92
|
+
parameters: [anyItem('node'), { name: 'color', type: 'Text' }],
|
|
93
|
+
run(inputs, store) {
|
|
94
|
+
const stroke = inputs[1].value;
|
|
95
|
+
checkColor('set_stroke', 'stroke', stroke);
|
|
96
|
+
store.updateNode(inputs[0].id, { stroke });
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
/**
|
|
100
|
+
* Clear the canvas and reset the system nodes.
|
|
101
|
+
*/
|
|
102
|
+
export const Clear = {
|
|
103
|
+
keyword: 'clear',
|
|
104
|
+
parameters: [],
|
|
105
|
+
run(_inputs, store) {
|
|
106
|
+
store.clearNodes();
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
const CANVAS_SIDES = ['left', 'right', 'top', 'bottom'];
|
|
110
|
+
/**
|
|
111
|
+
* Show the canvas, and move it to `side` when given.
|
|
112
|
+
* @param side - 'left', 'right', 'top' or 'bottom' (default: where it is now)
|
|
113
|
+
*/
|
|
114
|
+
export const ShowCanvas = {
|
|
115
|
+
keyword: 'show_canvas',
|
|
116
|
+
parameters: [{ name: 'side', type: 'Text', defaultValue: null }],
|
|
117
|
+
broadcastable: false,
|
|
118
|
+
run(inputs, store) {
|
|
119
|
+
const side = inputs[0]?.value;
|
|
120
|
+
if (side !== undefined) {
|
|
121
|
+
if (!CANVAS_SIDES.includes(side)) {
|
|
122
|
+
throw new Error(`show_canvas: side must be one of ${CANVAS_SIDES.join(', ')}, got '${side}'`);
|
|
123
|
+
}
|
|
124
|
+
store.setCanvasSide(side);
|
|
125
|
+
}
|
|
126
|
+
if (store.collapsedPane() === 'first')
|
|
127
|
+
store.setCollapsedPane('none');
|
|
128
|
+
},
|
|
129
|
+
};
|
|
130
|
+
/**
|
|
131
|
+
* Hide the canvas. Its nodes stay.
|
|
132
|
+
*/
|
|
133
|
+
export const HideCanvas = {
|
|
134
|
+
keyword: 'hide_canvas',
|
|
135
|
+
parameters: [],
|
|
136
|
+
run(_inputs, store) {
|
|
137
|
+
store.setCollapsedPane('first');
|
|
138
|
+
},
|
|
139
|
+
};
|
|
140
|
+
/**
|
|
141
|
+
* Move a point to (x, y). With `animate=True` it moves there over half a second.
|
|
142
|
+
* @param point - Point to move
|
|
143
|
+
* @param x - New x coordinate
|
|
144
|
+
* @param y - New y coordinate
|
|
145
|
+
*/
|
|
146
|
+
export const TranslatePointTo = {
|
|
147
|
+
keyword: 'translate_point_to',
|
|
148
|
+
parameters: [{ name: 'point', type: 'Point' }, { name: 'x', type: 'Scalar' }, { name: 'y', type: 'Scalar' }],
|
|
149
|
+
animatable: true,
|
|
150
|
+
run(inputs, store, animate) {
|
|
151
|
+
const [point, x, y] = inputs;
|
|
152
|
+
return replaceNode(store, point, { x: ref(x.value), y: ref(y.value) }, animate);
|
|
153
|
+
},
|
|
154
|
+
};
|
|
155
|
+
/**
|
|
156
|
+
* Move a point by (dx, dy). With `animate=True` it moves there over half a second.
|
|
157
|
+
* @param point - Point to move
|
|
158
|
+
* @param dx - Change in x
|
|
159
|
+
* @param dy - Change in y
|
|
160
|
+
*/
|
|
161
|
+
export const TranslatePointBy = {
|
|
162
|
+
keyword: 'translate_point_by',
|
|
163
|
+
parameters: [{ name: 'point', type: 'Point' }, { name: 'dx', type: 'Scalar' }, { name: 'dy', type: 'Scalar' }],
|
|
164
|
+
animatable: true,
|
|
165
|
+
run(inputs, store, animate) {
|
|
166
|
+
const [point, dx, dy] = inputs;
|
|
167
|
+
const x = add(ref(point.x), ref(dx.value));
|
|
168
|
+
const y = add(ref(point.y), ref(dy.value));
|
|
169
|
+
return replaceNode(store, point, { x, y }, animate);
|
|
170
|
+
},
|
|
171
|
+
};
|
|
172
|
+
/**
|
|
173
|
+
* Set a scalar to a new value. With `animate=True` it gets there over half a second.
|
|
174
|
+
* @param target - Scalar to change
|
|
175
|
+
* @param value - New value
|
|
176
|
+
*/
|
|
177
|
+
export const TranslateScalarTo = {
|
|
178
|
+
keyword: 'translate_scalar_to',
|
|
179
|
+
parameters: [{ name: 'target', type: 'Scalar' }, { name: 'value', type: 'Scalar' }],
|
|
180
|
+
animatable: true,
|
|
181
|
+
run(inputs, store, animate) {
|
|
182
|
+
const [target, value] = inputs;
|
|
183
|
+
return replaceNode(store, target, { value: ref(value.value) }, animate);
|
|
184
|
+
},
|
|
185
|
+
};
|
|
186
|
+
/**
|
|
187
|
+
* Add `delta` to a scalar. With `animate=True` it gets there over half a second.
|
|
188
|
+
* @param target - Scalar to change
|
|
189
|
+
* @param delta - Amount to add
|
|
190
|
+
*/
|
|
191
|
+
export const TranslateScalarBy = {
|
|
192
|
+
keyword: 'translate_scalar_by',
|
|
193
|
+
parameters: [{ name: 'target', type: 'Scalar' }, { name: 'delta', type: 'Scalar' }],
|
|
194
|
+
animatable: true,
|
|
195
|
+
run(inputs, store, animate) {
|
|
196
|
+
const [target, delta] = inputs;
|
|
197
|
+
return replaceNode(store, target, { value: add(ref(target.value), ref(delta.value)) }, animate);
|
|
198
|
+
},
|
|
199
|
+
};
|
|
200
|
+
/** Docs category for the functions defined in this file. */
|
|
201
|
+
export const category = 'Side Effects';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { NodeStore } from '../state/NodeStore';
|
|
2
|
+
/**
|
|
3
|
+
* Lets the reader move the canvas with a pointer:
|
|
4
|
+
* - Dragging a point moves it, like `translate_point_to`: it stops following its inputs.
|
|
5
|
+
* - Dragging anywhere else pans the canvas, through `originX` and `originY`.
|
|
6
|
+
* - A pinch, a two-finger trackpad slide or a mouse wheel zooms through `unit`, and moves the origin so the spot
|
|
7
|
+
* under the cursor or fingers stays put.
|
|
8
|
+
*/
|
|
9
|
+
export declare function useCanvasGestures(containerRef: () => HTMLDivElement | undefined, store: NodeStore): void;
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { batch, onCleanup, onMount } from 'solid-js';
|
|
2
|
+
import { num, scalar } from '../jax';
|
|
3
|
+
import { replaceNode } from '../functions/sideEffects';
|
|
4
|
+
/** A trackpad pinch with a `deltaY` of 100 zooms by a factor of e. */
|
|
5
|
+
const PINCH_SPEED = 0.01;
|
|
6
|
+
/** Slower than a pinch: a scroll's `deltaY` is larger. */
|
|
7
|
+
const SCROLL_SPEED = 0.002;
|
|
8
|
+
/**
|
|
9
|
+
* Lets the reader move the canvas with a pointer:
|
|
10
|
+
* - Dragging a point moves it, like `translate_point_to`: it stops following its inputs.
|
|
11
|
+
* - Dragging anywhere else pans the canvas, through `originX` and `originY`.
|
|
12
|
+
* - A pinch, a two-finger trackpad slide or a mouse wheel zooms through `unit`, and moves the origin so the spot
|
|
13
|
+
* under the cursor or fingers stays put.
|
|
14
|
+
*/
|
|
15
|
+
export function useCanvasGestures(containerRef, store) {
|
|
16
|
+
// Pointers panning or pinching the canvas.
|
|
17
|
+
const pointers = new Map();
|
|
18
|
+
// The pointer dragging a point, and that point's id.
|
|
19
|
+
let drag;
|
|
20
|
+
/** Pixels from the canvas's top left. */
|
|
21
|
+
const toPixel = (e) => {
|
|
22
|
+
const rect = containerRef().getBoundingClientRect();
|
|
23
|
+
return { x: e.clientX - rect.left, y: e.clientY - rect.top };
|
|
24
|
+
};
|
|
25
|
+
/** Sets system scalar `id` to `change(its value)`. Skipped if it is no longer a scalar. */
|
|
26
|
+
const changeScalar = (id, change) => {
|
|
27
|
+
const node = store.getNodeValue(id);
|
|
28
|
+
if (node?.type === 'Scalar')
|
|
29
|
+
replaceNode(store, node, { value: scalar(change(num(node.value))) }, false);
|
|
30
|
+
};
|
|
31
|
+
const pan = (dx, dy) => {
|
|
32
|
+
const unit = store.unit();
|
|
33
|
+
changeScalar('originX', x => x + dx / unit);
|
|
34
|
+
// Pixels go down, grid units go up.
|
|
35
|
+
changeScalar('originY', y => y - dy / unit);
|
|
36
|
+
};
|
|
37
|
+
const zoomAt = (at, factor) => {
|
|
38
|
+
const unit = store.unit();
|
|
39
|
+
const next = Math.max(1, unit * factor);
|
|
40
|
+
const { width, height } = store.canvasBounds();
|
|
41
|
+
// The spot `at` is a fixed number of pixels from the center; keep it on the same grid position.
|
|
42
|
+
const shift = 1 / next - 1 / unit;
|
|
43
|
+
changeScalar('unit', () => next);
|
|
44
|
+
changeScalar('originX', x => x + (at.x - width / 2) * shift);
|
|
45
|
+
changeScalar('originY', y => y + (height / 2 - at.y) * shift);
|
|
46
|
+
};
|
|
47
|
+
const movePoint = (id, at) => {
|
|
48
|
+
const point = store.getNodeValue(id);
|
|
49
|
+
// Removed or replaced while being dragged.
|
|
50
|
+
if (point?.type !== 'Point')
|
|
51
|
+
return;
|
|
52
|
+
const unit = store.unit();
|
|
53
|
+
const origin = store.origin();
|
|
54
|
+
replaceNode(store, point, { x: scalar(at.x / unit - origin.x), y: scalar(origin.y - at.y / unit) }, false);
|
|
55
|
+
};
|
|
56
|
+
/** The pointers' center, and their mean distance from it. */
|
|
57
|
+
const spread = () => {
|
|
58
|
+
const all = [...pointers.values()];
|
|
59
|
+
const x = all.reduce((sum, p) => sum + p.x, 0) / all.length;
|
|
60
|
+
const y = all.reduce((sum, p) => sum + p.y, 0) / all.length;
|
|
61
|
+
const size = all.reduce((sum, p) => sum + Math.hypot(p.x - x, p.y - y), 0) / all.length;
|
|
62
|
+
return { x, y, size };
|
|
63
|
+
};
|
|
64
|
+
const onPointerDown = (e) => {
|
|
65
|
+
if (drag)
|
|
66
|
+
return;
|
|
67
|
+
// Keeps text from being selected while dragging with a mouse.
|
|
68
|
+
e.preventDefault();
|
|
69
|
+
containerRef().setPointerCapture(e.pointerId);
|
|
70
|
+
const nodeId = e.target.closest('[data-node-id]')?.getAttribute('data-node-id');
|
|
71
|
+
if (nodeId && pointers.size === 0 && store.getNodeValue(nodeId)?.type === 'Point') {
|
|
72
|
+
drag = { pointerId: e.pointerId, nodeId };
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
pointers.set(e.pointerId, toPixel(e));
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
const onPointerMove = (e) => {
|
|
79
|
+
if (drag?.pointerId === e.pointerId)
|
|
80
|
+
return movePoint(drag.nodeId, toPixel(e));
|
|
81
|
+
if (!pointers.has(e.pointerId))
|
|
82
|
+
return;
|
|
83
|
+
const before = spread();
|
|
84
|
+
pointers.set(e.pointerId, toPixel(e));
|
|
85
|
+
const after = spread();
|
|
86
|
+
batch(() => {
|
|
87
|
+
pan(after.x - before.x, after.y - before.y);
|
|
88
|
+
// One pointer has no spread: it only pans.
|
|
89
|
+
if (before.size > 0)
|
|
90
|
+
zoomAt(after, after.size / before.size);
|
|
91
|
+
});
|
|
92
|
+
};
|
|
93
|
+
const onPointerUp = (e) => {
|
|
94
|
+
if (drag?.pointerId === e.pointerId)
|
|
95
|
+
drag = undefined;
|
|
96
|
+
pointers.delete(e.pointerId);
|
|
97
|
+
};
|
|
98
|
+
const onWheel = (e) => {
|
|
99
|
+
// The browser reports a trackpad pinch as a wheel event with `ctrlKey`, and a scroll as one without.
|
|
100
|
+
e.preventDefault();
|
|
101
|
+
const speed = e.ctrlKey ? PINCH_SPEED : SCROLL_SPEED;
|
|
102
|
+
batch(() => zoomAt(toPixel(e), Math.exp(-e.deltaY * speed)));
|
|
103
|
+
};
|
|
104
|
+
onMount(() => {
|
|
105
|
+
const container = containerRef();
|
|
106
|
+
container.addEventListener('pointerdown', onPointerDown);
|
|
107
|
+
container.addEventListener('pointermove', onPointerMove);
|
|
108
|
+
container.addEventListener('pointerup', onPointerUp);
|
|
109
|
+
container.addEventListener('pointercancel', onPointerUp);
|
|
110
|
+
// Not passive, so `preventDefault` can stop the browser zooming or scrolling the page.
|
|
111
|
+
container.addEventListener('wheel', onWheel, { passive: false });
|
|
112
|
+
onCleanup(() => {
|
|
113
|
+
container.removeEventListener('pointerdown', onPointerDown);
|
|
114
|
+
container.removeEventListener('pointermove', onPointerMove);
|
|
115
|
+
container.removeEventListener('pointerup', onPointerUp);
|
|
116
|
+
container.removeEventListener('pointercancel', onPointerUp);
|
|
117
|
+
container.removeEventListener('wheel', onWheel);
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { onMount, onCleanup } from 'solid-js';
|
|
2
|
+
export function useCanvasResize(svgContainerRef, store) {
|
|
3
|
+
let resizeObserver;
|
|
4
|
+
const updateCanvasSize = () => {
|
|
5
|
+
const container = svgContainerRef();
|
|
6
|
+
if (container) {
|
|
7
|
+
store.setCanvasBounds({
|
|
8
|
+
width: container.clientWidth,
|
|
9
|
+
height: container.clientHeight
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
onMount(() => {
|
|
14
|
+
updateCanvasSize();
|
|
15
|
+
if (typeof window !== 'undefined') {
|
|
16
|
+
window.addEventListener('resize', updateCanvasSize);
|
|
17
|
+
}
|
|
18
|
+
// Observe the container directly so the canvas re-fits on any layout change
|
|
19
|
+
// (e.g. dragging the split pane), not just window resizes.
|
|
20
|
+
const container = svgContainerRef();
|
|
21
|
+
if (container && typeof ResizeObserver !== 'undefined') {
|
|
22
|
+
resizeObserver = new ResizeObserver(() => updateCanvasSize());
|
|
23
|
+
resizeObserver.observe(container);
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
onCleanup(() => {
|
|
27
|
+
if (typeof window !== 'undefined') {
|
|
28
|
+
window.removeEventListener('resize', updateCanvasSize);
|
|
29
|
+
}
|
|
30
|
+
resizeObserver?.disconnect();
|
|
31
|
+
});
|
|
32
|
+
return updateCanvasSize;
|
|
33
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { numpy as np, jit, blockUntilReady, vmap } from '@jax-js/jax';
|
|
2
|
+
export declare const deviceReady: Promise<void>;
|
|
3
|
+
export declare const getDevice: () => 'cpu' | 'wasm' | 'webgpu';
|
|
4
|
+
export declare const withCpuDevice: <T>(fn: () => T) => T;
|
|
5
|
+
export { np, jit, blockUntilReady, vmap };
|
|
6
|
+
declare const USE_JAX_TENSORS = true;
|
|
7
|
+
export type Dtype = typeof USE_JAX_TENSORS extends true ? np.Array : number;
|
|
8
|
+
export declare const setNumericMode: (on: boolean) => void;
|
|
9
|
+
export declare const isNumericMode: () => boolean;
|
|
10
|
+
export declare const withNumericMode: <T>(fn: () => T) => T;
|
|
11
|
+
/** Wrap a plain number as a Dtype value */
|
|
12
|
+
export declare const scalar: (v: number) => Dtype;
|
|
13
|
+
/** Extract a plain number from a Dtype value */
|
|
14
|
+
export declare const num: (a: Dtype) => number;
|
|
15
|
+
/**
|
|
16
|
+
* Gets a reference to a Dtype if it is a JAX tensor,
|
|
17
|
+
* so that it is not consumed when passed to another operation.
|
|
18
|
+
*/
|
|
19
|
+
export declare const ref: (a: Dtype) => Dtype;
|
|
20
|
+
export declare const add: (a: Dtype, b: Dtype) => Dtype;
|
|
21
|
+
export declare const sub: (a: Dtype, b: Dtype) => Dtype;
|
|
22
|
+
export declare const mul: (a: Dtype, b: Dtype) => Dtype;
|
|
23
|
+
export declare const div: (a: Dtype, b: Dtype) => Dtype;
|
|
24
|
+
export declare const sqrt: (a: Dtype) => Dtype;
|
|
25
|
+
export declare const pow: (a: Dtype, b: Dtype) => Dtype;
|
|
26
|
+
export declare const cos: (a: Dtype) => Dtype;
|
|
27
|
+
export declare const sin: (a: Dtype) => Dtype;
|
|
28
|
+
export declare const acos: (a: Dtype) => Dtype;
|
|
29
|
+
export declare const asin: (a: Dtype) => Dtype;
|
|
30
|
+
export declare const atan: (a: Dtype) => Dtype;
|
|
31
|
+
export declare const atan2: (a: Dtype, b: Dtype) => Dtype;
|
|
32
|
+
export declare const log: (a: Dtype) => Dtype;
|
|
33
|
+
export declare const exp: (a: Dtype) => Dtype;
|
|
34
|
+
export declare const log10: (a: Dtype) => Dtype;
|
|
35
|
+
export declare const abs: (a: Dtype) => Dtype;
|
|
36
|
+
export declare const sign: (a: Dtype) => Dtype;
|
|
37
|
+
export declare const floor: (a: Dtype) => Dtype;
|
|
38
|
+
export declare const ceil: (a: Dtype) => Dtype;
|
|
39
|
+
export declare const round: (a: Dtype) => Dtype;
|
|
40
|
+
export declare const tan: (a: Dtype) => Dtype;
|
|
41
|
+
export declare const mod: (a: Dtype, b: Dtype) => Dtype;
|
|
42
|
+
export declare const minimum: (a: Dtype, b: Dtype) => Dtype;
|
|
43
|
+
export declare const maximum: (a: Dtype, b: Dtype) => Dtype;
|
|
44
|
+
export declare const mean: (a: Dtype) => Dtype;
|
|
45
|
+
export declare const std: (a: Dtype) => Dtype;
|
|
46
|
+
export declare const var_: (a: Dtype) => Dtype;
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { numpy as np, defaultDevice, init, jit, blockUntilReady, vmap } from '@jax-js/jax';
|
|
2
|
+
let currentDevice = 'cpu';
|
|
3
|
+
const setDevice = (d) => {
|
|
4
|
+
currentDevice = d;
|
|
5
|
+
defaultDevice(d);
|
|
6
|
+
};
|
|
7
|
+
// Keep cpu as an immediate synchronous fallback until init() resolves.
|
|
8
|
+
setDevice('cpu');
|
|
9
|
+
export const deviceReady = (async () => {
|
|
10
|
+
const devices = await init();
|
|
11
|
+
if (devices.includes('webgpu')) {
|
|
12
|
+
setDevice('webgpu');
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
setDevice('wasm');
|
|
16
|
+
}
|
|
17
|
+
})();
|
|
18
|
+
export const getDevice = () => currentDevice;
|
|
19
|
+
export const withCpuDevice = (fn) => {
|
|
20
|
+
const prev = currentDevice;
|
|
21
|
+
if (prev === 'cpu')
|
|
22
|
+
return fn();
|
|
23
|
+
setDevice('cpu');
|
|
24
|
+
try {
|
|
25
|
+
return fn();
|
|
26
|
+
}
|
|
27
|
+
finally {
|
|
28
|
+
setDevice(prev);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
export { np, jit, blockUntilReady, vmap };
|
|
32
|
+
const USE_JAX_TENSORS = true;
|
|
33
|
+
let numericMode = false;
|
|
34
|
+
export const setNumericMode = (on) => { numericMode = on; };
|
|
35
|
+
export const isNumericMode = () => numericMode;
|
|
36
|
+
const useJax = () => USE_JAX_TENSORS && !numericMode;
|
|
37
|
+
export const withNumericMode = (fn) => {
|
|
38
|
+
if (numericMode)
|
|
39
|
+
return fn();
|
|
40
|
+
setNumericMode(true);
|
|
41
|
+
try {
|
|
42
|
+
return fn();
|
|
43
|
+
}
|
|
44
|
+
finally {
|
|
45
|
+
setNumericMode(false);
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
/** Wrap a plain number as a Dtype value */
|
|
49
|
+
export const scalar = (v) => (useJax() ? np.array(v) : v);
|
|
50
|
+
/** Extract a plain number from a Dtype value */
|
|
51
|
+
export const num = (a) => {
|
|
52
|
+
try {
|
|
53
|
+
if (typeof a === 'number')
|
|
54
|
+
return a;
|
|
55
|
+
const arr = a;
|
|
56
|
+
if (typeof arr.item === 'function') {
|
|
57
|
+
// If it's a real array and we call item(), we should do so without consuming it,
|
|
58
|
+
// so we should use its .ref if available
|
|
59
|
+
const arrRef = arr.ref;
|
|
60
|
+
if (arrRef && typeof arrRef.item === 'function') {
|
|
61
|
+
try {
|
|
62
|
+
return arrRef.item();
|
|
63
|
+
}
|
|
64
|
+
catch (e) {
|
|
65
|
+
// Fallback if ref is already freed or something
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return arr.item();
|
|
69
|
+
}
|
|
70
|
+
// If we're tracing and the underlying value has a primal
|
|
71
|
+
if (arr.primal && typeof arr.primal.pval?.val?.item === 'function') {
|
|
72
|
+
return arr.primal.pval.val.item();
|
|
73
|
+
}
|
|
74
|
+
// As a last resort, just cast it to number (which usually yields NaN if it's an object)
|
|
75
|
+
return Number(arr);
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
console.error(`Error extracting number from Dtype: ${error instanceof Error ? error.message : String(error)}`);
|
|
79
|
+
return NaN;
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
/** Polymorphic Math Layer */
|
|
83
|
+
// In numeric mode an operand may still be a JAX tensor (a value cached by a
|
|
84
|
+
// node that last recomputed in tensor mode); borrow-read it to a number.
|
|
85
|
+
const asNum = (a) => (typeof a === 'number' ? a : num(a));
|
|
86
|
+
// In tensor mode an operand may still be a plain number (a value cached by a
|
|
87
|
+
// node that last recomputed in numeric mode); promote it to a fresh array.
|
|
88
|
+
const asArr = (a) => (typeof a === 'number' ? np.array(a) : a);
|
|
89
|
+
/**
|
|
90
|
+
* Gets a reference to a Dtype if it is a JAX tensor,
|
|
91
|
+
* so that it is not consumed when passed to another operation.
|
|
92
|
+
*/
|
|
93
|
+
export const ref = (a) => (useJax() && typeof a !== 'number' ? a.ref : a);
|
|
94
|
+
export const add = (a, b) => (useJax() ? np.add(asArr(a), asArr(b)) : (asNum(a) + asNum(b)));
|
|
95
|
+
export const sub = (a, b) => (useJax() ? np.subtract(asArr(a), asArr(b)) : (asNum(a) - asNum(b)));
|
|
96
|
+
export const mul = (a, b) => (useJax() ? np.multiply(asArr(a), asArr(b)) : (asNum(a) * asNum(b)));
|
|
97
|
+
export const div = (a, b) => (useJax() ? np.divide(asArr(a), asArr(b)) : (asNum(a) / asNum(b)));
|
|
98
|
+
export const sqrt = (a) => (useJax() ? np.sqrt(asArr(a)) : Math.sqrt(asNum(a)));
|
|
99
|
+
export const pow = (a, b) => (useJax() ? np.power(asArr(a), asArr(b)) : Math.pow(asNum(a), asNum(b)));
|
|
100
|
+
export const cos = (a) => (useJax() ? np.cos(asArr(a)) : Math.cos(asNum(a)));
|
|
101
|
+
export const sin = (a) => (useJax() ? np.sin(asArr(a)) : Math.sin(asNum(a)));
|
|
102
|
+
export const acos = (a) => (useJax() ? np.arccos(asArr(a)) : Math.acos(asNum(a)));
|
|
103
|
+
export const asin = (a) => (useJax() ? np.arcsin(asArr(a)) : Math.asin(asNum(a)));
|
|
104
|
+
export const atan = (a) => (useJax() ? np.arctan(asArr(a)) : Math.atan(asNum(a)));
|
|
105
|
+
export const atan2 = (a, b) => (useJax() ? np.arctan2(asArr(a), asArr(b)) : Math.atan2(asNum(a), asNum(b)));
|
|
106
|
+
export const log = (a) => (useJax() ? np.log(asArr(a)) : Math.log(asNum(a)));
|
|
107
|
+
export const exp = (a) => (useJax() ? np.exp(asArr(a)) : Math.exp(asNum(a)));
|
|
108
|
+
export const log10 = (a) => (useJax() ? np.log10(asArr(a)) : Math.log10(asNum(a)));
|
|
109
|
+
export const abs = (a) => (useJax() ? np.abs(asArr(a)) : Math.abs(asNum(a)));
|
|
110
|
+
export const sign = (a) => (useJax() ? np.sign(asArr(a)) : Math.sign(asNum(a)));
|
|
111
|
+
export const floor = (a) => (useJax() ? np.floor(asArr(a)) : Math.floor(asNum(a)));
|
|
112
|
+
export const ceil = (a) => (useJax() ? np.ceil(asArr(a)) : Math.ceil(asNum(a)));
|
|
113
|
+
export const round = (a) => (useJax() ? np.round(asArr(a)) : Math.round(asNum(a)));
|
|
114
|
+
export const tan = (a) => (useJax() ? np.tan(asArr(a)) : Math.tan(asNum(a)));
|
|
115
|
+
export const mod = (a, b) => (useJax() ? np.fmod(asArr(a), asArr(b)) : (asNum(a) % asNum(b)));
|
|
116
|
+
export const minimum = (a, b) => (useJax() ? np.minimum(asArr(a), asArr(b)) : Math.min(asNum(a), asNum(b)));
|
|
117
|
+
export const maximum = (a, b) => (useJax() ? np.maximum(asArr(a), asArr(b)) : Math.max(asNum(a), asNum(b)));
|
|
118
|
+
export const mean = (a) => (useJax() ? np.mean(asArr(a)) : asNum(a));
|
|
119
|
+
export const std = (a) => (useJax() ? np.std(asArr(a)) : asNum(a));
|
|
120
|
+
export const var_ = (a) => (useJax() ? np.var_(asArr(a)) : asNum(a));
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { PointNode, LineNode, TriangleNode, CircleNode, EllipseNode, ArcNode, BezierQuadraticNode, BezierCubicNode, PointGradNode, TextNode } from "../state/Node";
|
|
2
|
+
import { Accessor } from "solid-js";
|
|
3
|
+
declare const renderPoint: (point: Accessor<PointNode>, unit: Accessor<number>) => import("solid-js").JSX.Element;
|
|
4
|
+
declare const renderLine: (line: Accessor<LineNode>) => import("solid-js").JSX.Element;
|
|
5
|
+
declare const renderTriangle: (triangle: Accessor<TriangleNode>) => import("solid-js").JSX.Element;
|
|
6
|
+
declare const renderCircle: (circle: Accessor<CircleNode>) => import("solid-js").JSX.Element;
|
|
7
|
+
declare const renderEllipse: (ellipse: Accessor<EllipseNode>) => import("solid-js").JSX.Element;
|
|
8
|
+
declare const renderArc: (arc: Accessor<ArcNode>) => import("solid-js").JSX.Element;
|
|
9
|
+
declare const renderBezierQuadratic: (bezier: Accessor<BezierQuadraticNode>) => import("solid-js").JSX.Element;
|
|
10
|
+
declare const renderBezierCubic: (bezier: Accessor<BezierCubicNode>) => import("solid-js").JSX.Element;
|
|
11
|
+
declare const renderPointGrad: (grad: Accessor<PointGradNode>, unit: Accessor<number>) => import("solid-js").JSX.Element;
|
|
12
|
+
declare const renderText: (text: Accessor<TextNode>, unit: Accessor<number>) => import("solid-js").JSX.Element;
|
|
13
|
+
export { renderPoint, renderLine, renderTriangle, renderCircle, renderEllipse, renderArc, renderBezierQuadratic, renderBezierCubic, renderPointGrad, renderText, };
|