@motionscript/math 0.1.0-alpha.8 → 0.1.0-alpha.9

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.
Files changed (157) hide show
  1. package/CHANGELOG.md +2 -0
  2. package/LICENSE +201 -201
  3. package/NOTICE +49 -49
  4. package/README.md +390 -391
  5. package/dist/annotate/angle-mark.js.map +1 -1
  6. package/dist/annotate/curly-brace.js.map +1 -1
  7. package/dist/annotate/emphasis-mark.js.map +1 -1
  8. package/dist/browser/chunks/{chunk-FETQZEN2.js → chunk-IRZCG6PM.js} +2 -2
  9. package/dist/browser/chunks/chunk-IRZCG6PM.js.map +7 -0
  10. package/dist/browser/chunks/{chunk-K77KLQX4.js → chunk-WMVW6ZIV.js} +2 -2
  11. package/dist/browser/chunks/chunk-WMVW6ZIV.js.map +7 -0
  12. package/dist/browser/chunks/{mathjax-DNP3YRES.js → mathjax-WV55AC2P.js} +1 -1
  13. package/dist/browser/chunks/{mathjax-DNP3YRES.js.map → mathjax-WV55AC2P.js.map} +1 -1
  14. package/dist/browser/index.js +1 -1
  15. package/dist/browser/index.js.map +1 -1
  16. package/dist/browser/kit.js +1 -1
  17. package/dist/browser/tex.js +1 -1
  18. package/dist/browser/tex.js.map +1 -1
  19. package/dist/engine.d.ts +1 -1
  20. package/dist/engine.js +1 -1
  21. package/dist/engine.js.map +1 -1
  22. package/dist/index.js.map +1 -1
  23. package/dist/kit/brace.js.map +1 -1
  24. package/dist/kit/cache.js.map +1 -1
  25. package/dist/kit/component.d.ts.map +1 -1
  26. package/dist/kit/component.js.map +1 -1
  27. package/dist/kit/contour.js.map +1 -1
  28. package/dist/kit/coords.js.map +1 -1
  29. package/dist/kit/curve.js.map +1 -1
  30. package/dist/kit/equation.js.map +1 -1
  31. package/dist/kit/equation3d.js.map +1 -1
  32. package/dist/kit/expression.js.map +1 -1
  33. package/dist/kit/field-mark.d.ts.map +1 -1
  34. package/dist/kit/field-mark.js.map +1 -1
  35. package/dist/kit/field.js.map +1 -1
  36. package/dist/kit/follow.d.ts.map +1 -1
  37. package/dist/kit/follow.js.map +1 -1
  38. package/dist/kit/furniture.js.map +1 -1
  39. package/dist/kit/geometry.js.map +1 -1
  40. package/dist/kit/graph.js.map +1 -1
  41. package/dist/kit/mark.js.map +1 -1
  42. package/dist/kit/part-fills.js.map +1 -1
  43. package/dist/kit/place.js.map +1 -1
  44. package/dist/kit/sampler.js.map +1 -1
  45. package/dist/kit/scale.js.map +1 -1
  46. package/dist/kit/source.js.map +1 -1
  47. package/dist/kit/system.js.map +1 -1
  48. package/dist/kit/ticks.js.map +1 -1
  49. package/dist/kit/withdrawn.js.map +1 -1
  50. package/dist/marks/curve-area.js.map +1 -1
  51. package/dist/marks/drop-lines.js.map +1 -1
  52. package/dist/marks/plot-point.js.map +1 -1
  53. package/dist/marks/riemann-sum.js.map +1 -1
  54. package/dist/marks/tangent-line.js.map +1 -1
  55. package/dist/marks/vector-arrow.js.map +1 -1
  56. package/dist/nodes.js.map +1 -1
  57. package/dist/plots/function-plot.d.ts.map +1 -1
  58. package/dist/plots/function-plot.js.map +1 -1
  59. package/dist/plots/points-plot.js.map +1 -1
  60. package/dist/plots/stream-lines.js.map +1 -1
  61. package/dist/plots/traced-path.js.map +1 -1
  62. package/dist/plots/vector-field.js.map +1 -1
  63. package/dist/structures/graph-layout.js.map +1 -1
  64. package/dist/structures/network-graph.js.map +1 -1
  65. package/dist/structures/sample-space.js.map +1 -1
  66. package/dist/systems/axes2d.d.ts.map +1 -1
  67. package/dist/systems/axes2d.js.map +1 -1
  68. package/dist/systems/number-line.js.map +1 -1
  69. package/dist/systems/number-plane.js.map +1 -1
  70. package/dist/systems/polar-plane.js.map +1 -1
  71. package/dist/tex/geometry.js.map +1 -1
  72. package/dist/tex/index.js.map +1 -1
  73. package/dist/tex/mathjax.js.map +1 -1
  74. package/dist/tex/matrix.js.map +1 -1
  75. package/dist/tex/node.js.map +1 -1
  76. package/dist/tex/tween.js.map +1 -1
  77. package/dist/tex/typeset.d.ts.map +1 -1
  78. package/dist/tex/typeset.js.map +1 -1
  79. package/dist/three/axes3d.js.map +1 -1
  80. package/dist/three/curve-plot3d.js.map +1 -1
  81. package/dist/three/entries.js.map +1 -1
  82. package/dist/three/label3d.js.map +1 -1
  83. package/dist/three/plot-point3d.js.map +1 -1
  84. package/dist/three/space.js.map +1 -1
  85. package/dist/three/surface-plot3d.js.map +1 -1
  86. package/dist/three/vector-arrow3d.js.map +1 -1
  87. package/package.json +4 -4
  88. package/registry.json +251 -251
  89. package/src/annotate/angle-mark.ts +137 -137
  90. package/src/annotate/curly-brace.ts +148 -148
  91. package/src/annotate/emphasis-mark.ts +118 -118
  92. package/src/engine.ts +86 -86
  93. package/src/index.ts +29 -29
  94. package/src/kit/brace.ts +37 -37
  95. package/src/kit/cache.ts +61 -61
  96. package/src/kit/component.ts +57 -57
  97. package/src/kit/contour.ts +94 -94
  98. package/src/kit/coords.ts +278 -278
  99. package/src/kit/curve.ts +47 -47
  100. package/src/kit/equation.ts +181 -181
  101. package/src/kit/equation3d.ts +137 -137
  102. package/src/kit/expression.ts +159 -159
  103. package/src/kit/field-mark.ts +171 -171
  104. package/src/kit/field.ts +50 -50
  105. package/src/kit/follow.ts +229 -229
  106. package/src/kit/furniture.ts +113 -113
  107. package/src/kit/geometry.ts +49 -49
  108. package/src/kit/graph.ts +116 -116
  109. package/src/kit/mark.ts +152 -152
  110. package/src/kit/part-fills.ts +61 -61
  111. package/src/kit/place.ts +77 -77
  112. package/src/kit/sampler.ts +281 -281
  113. package/src/kit/scale.ts +39 -39
  114. package/src/kit/source.ts +57 -57
  115. package/src/kit/system.ts +105 -105
  116. package/src/kit/ticks.ts +85 -85
  117. package/src/kit/withdrawn.ts +8 -8
  118. package/src/marks/curve-area.ts +89 -89
  119. package/src/marks/drop-lines.ts +71 -71
  120. package/src/marks/plot-point.ts +102 -102
  121. package/src/marks/riemann-sum.ts +99 -99
  122. package/src/marks/tangent-line.ts +119 -119
  123. package/src/marks/vector-arrow.ts +98 -98
  124. package/src/nodes.ts +99 -99
  125. package/src/plots/function-plot.ts +220 -220
  126. package/src/plots/points-plot.ts +103 -103
  127. package/src/plots/stream-lines.ts +160 -160
  128. package/src/plots/traced-path.ts +70 -70
  129. package/src/plots/vector-field.ts +107 -107
  130. package/src/structures/graph-layout.ts +145 -145
  131. package/src/structures/network-graph.ts +291 -291
  132. package/src/structures/sample-space.ts +210 -210
  133. package/src/systems/axes2d.ts +226 -226
  134. package/src/systems/number-line.ts +130 -130
  135. package/src/systems/number-plane.ts +232 -232
  136. package/src/systems/polar-plane.ts +177 -177
  137. package/src/tex/geometry.ts +703 -703
  138. package/src/tex/index.ts +15 -15
  139. package/src/tex/mathjax.ts +82 -82
  140. package/src/tex/matrix.ts +97 -97
  141. package/src/tex/node.ts +74 -74
  142. package/src/tex/tween.ts +308 -308
  143. package/src/tex/typeset.ts +461 -461
  144. package/src/three/axes3d.ts +267 -267
  145. package/src/three/curve-plot3d.ts +146 -146
  146. package/src/three/entries.ts +42 -42
  147. package/src/three/label3d.ts +52 -52
  148. package/src/three/plot-point3d.ts +63 -63
  149. package/src/three/space.ts +92 -92
  150. package/src/three/surface-plot3d.ts +318 -318
  151. package/src/three/vector-arrow3d.ts +88 -88
  152. package/dist/browser/chunks/chunk-FETQZEN2.js.map +0 -7
  153. package/dist/browser/chunks/chunk-K77KLQX4.js.map +0 -7
  154. package/dist/kit/placement.d.ts +0 -8
  155. package/dist/kit/placement.d.ts.map +0 -1
  156. package/dist/kit/placement.js +0 -8
  157. package/dist/kit/placement.js.map +0 -1
@@ -1,232 +1,232 @@
1
- import {
2
- component2d,
3
- Graphics2D,
4
- Types,
5
- type Extents,
6
- type NodeOf,
7
- type Stroke,
8
- type StrokeResolved,
9
- // Named so the emitted declaration reaches these through the sdk, not core.
10
- type Color as _Color,
11
- type Fill as _Fill,
12
- type FillResolved as _FillResolved,
13
- type Node2D as _Node2D,
14
- type Node2DProps as _Node2DProps,
15
- type NormalizedColor as _NormalizedColor,
16
- type Shadow as _Shadow,
17
- type ShadowResolved as _ShadowResolved,
18
- } from "@motionscript/sdk";
19
- import {
20
- isIdentityMatrix,
21
- lerpMatrix,
22
- lineThrough,
23
- readMatrix,
24
- transformedMap,
25
- warpedMap,
26
- type CoordinateMap,
27
- type Matrix2,
28
- type Warp,
29
- } from "../kit/coords";
30
- import { fieldParams, parseField } from "../kit/field";
31
- import { AXIS_COLOR } from "../kit/furniture";
32
- import { lerpSource, readSource, type PlotSource } from "../kit/source";
33
- import { drawSystem, plotArea, systemLayout, systemOpenPlane, systemProvides, systemSize } from "../kit/system";
34
- import { AXES, AXES_COMMANDS, axesFurniture, axesMap, axesOf, loadAxes, type Axes2dProps, type AxesValues, type AxisLayout } from "./axes2d";
35
-
36
- export interface NumberPlaneProps extends Axes2dProps {
37
- grid?: boolean;
38
- gridStroke?: Stroke;
39
- minorStroke?: Stroke;
40
- minorDivisions?: number;
41
- matrix?: Matrix2 | readonly (readonly number[])[];
42
- warp?: string | PlotSource;
43
- ghost?: boolean;
44
- }
45
-
46
- const state = {
47
- ...AXES,
48
- grid: Types.boolean(true, { animatable: false }),
49
- /** The line at every tick. */
50
- gridStroke: Types.stroke({ weight: 1.5, fill: "#3b4658", align: "center" }),
51
- /** The lines between ticks. */
52
- minorStroke: Types.stroke({ weight: 1, fill: "#232b38", align: "center" }),
53
- /** How many parts the space between two ticks is cut into; one draws no line between them. */
54
- minorDivisions: Types.number(2, { min: 1, max: 10, step: 1 }),
55
- /** A linear map of the plane, row by row: `[a, b, c, d]` sends `(x, y)` to `(ax + by, cx + dy)`. */
56
- matrix: Types.custom<Matrix2 | readonly (readonly number[])[], Matrix2>([1, 0, 0, 1], { resolve: readMatrix, lerp: lerpMatrix }),
57
- /**
58
- * Where each point of the plane is sent, two parts in x and y:
59
- * `(x + sin(y), y + sin(x))`. Empty leaves the plane flat.
60
- */
61
- warp: Types.custom<string | PlotSource, PlotSource>("", { resolve: readSource, lerp: lerpSource, kind: "fieldEquation" }),
62
- /** Leave the plane as it was, faintly, behind one that has been transformed or warped. */
63
- ghost: Types.boolean(true, { animatable: false }),
64
- };
65
-
66
- type PlaneValues = AxesValues & {
67
- readonly grid: boolean;
68
- readonly gridStroke: StrokeResolved[];
69
- readonly minorStroke: StrokeResolved[];
70
- readonly minorDivisions: number;
71
- readonly matrix: Matrix2;
72
- readonly warp: PlotSource;
73
- readonly ghost: boolean;
74
- };
75
-
76
- /**
77
- * Axes over a full grid, with square units unless told otherwise: the plane
78
- * vectors and transformations are drawn on. Its `matrix` shears, turns or
79
- * squeezes the whole plane about its origin, and its `warp` bends it by a
80
- * function of its own points; the grid, the axes and everything on the plane
81
- * go with it.
82
- */
83
- export const NumberPlane = component2d({
84
- key: "numberPlane",
85
- state,
86
- defaults: {
87
- squareUnits: true,
88
- stroke: { weight: 2, fill: AXIS_COLOR, align: "center" },
89
- // A transformed plane reaches past its box, and so does what is on it: both are kept in unless told otherwise.
90
- clip: (s) => spills(s),
91
- },
92
- load: (loader, s) => loadAxes(loader, s),
93
- commands: {
94
- ...AXES_COMMANDS,
95
- /** Send the plane through a matrix: a shear, a turn, a squeeze, a flip. */
96
- transformTo: { args: ["matrix"] },
97
- /** Bend the plane, each point travelling to where the warp sends it; an empty one flattens it again. */
98
- warpTo: { args: ["warp"] },
99
- },
100
- provide: (s) => systemProvides(s),
101
- measure: (_measurer, s) => systemSize(s),
102
- layout: (box, s, children) => systemLayout(box, s, children, planeMap),
103
- plane: (box, s) => systemOpenPlane(box, s, planeMap),
104
- render: (ctx, s) => {
105
- const { flat, map } = planeMaps(plotArea(ctx.size, s.padding), s);
106
- drawSystem(ctx, s, map, (on) => axesFurniture(on, s, (x, y) => grid(s, flat, on, x, y)));
107
- },
108
- });
109
-
110
- export type NumberPlane = NodeOf<typeof NumberPlane>;
111
-
112
- /** The plane's map on a plot area: the flat one, sent through its matrix and its warp. */
113
- export function planeMap(area: Extents, s: PlaneValues): CoordinateMap {
114
- return planeMaps(area, s).map;
115
- }
116
-
117
- /** The flat plane on a plot area, and the plane as its matrix and its warp have it. */
118
- function planeMaps(area: Extents, s: PlaneValues): { flat: CoordinateMap; map: CoordinateMap } {
119
- const flat = axesMap(area, s);
120
- const map = isIdentityMatrix(s.matrix) ? flat : transformedMap(flat, s.matrix);
121
- const { source, blend } = s.warp;
122
- const to = warpOf(source);
123
- if (!blend) return { flat, map: to ? warpedMap(map, to, source) : map };
124
- // Mid-warp each point is part of the way from where the old warp had it to where the new one sends it.
125
- const from = warpOf(blend.from);
126
- if (!from && !to) return { flat, map };
127
- const start = { x: 0, y: 0 };
128
- return {
129
- flat,
130
- map: warpedMap(
131
- map,
132
- (x, y, out) => {
133
- if (!(from ?? STAY)(x, y, start) || !(to ?? STAY)(x, y, out)) return false;
134
- out.x = start.x + (out.x - start.x) * blend.t;
135
- out.y = start.y + (out.y - start.y) * blend.t;
136
- return true;
137
- },
138
- `${blend.from}>${source}@${blend.t}`,
139
- ),
140
- };
141
- }
142
-
143
- /** Whether the plane reaches past its box: it is transformed or warped. */
144
- function spills(s: PlaneValues): boolean {
145
- if (!isIdentityMatrix(s.matrix)) return true;
146
- const { source, blend } = s.warp;
147
- return warpOf(source) !== null || (blend !== undefined && warpOf(blend.from) !== null);
148
- }
149
-
150
- /** The grid behind the axes, with the flat plane faintly behind a transformed one. */
151
- function grid(s: PlaneValues, flat: CoordinateMap, map: CoordinateMap, x: AxisLayout, y: AxisLayout): Graphics2D[] {
152
- if (!s.grid) return [];
153
- const parts: Graphics2D[] = [];
154
- if (s.ghost && flat.revision !== map.revision) {
155
- const [gx, gy] = axesOf(flat, s);
156
- parts.push(...ruled(s, flat, gx, gy, true));
157
- }
158
- parts.push(...ruled(s, map, x, y, false));
159
- return parts;
160
- }
161
-
162
- /** The grid of `map`: every line, or only the ones at a tick, in the fainter stroke, for a ghost. */
163
- function ruled(s: PlaneValues, map: CoordinateMap, x: AxisLayout, y: AxisLayout, ghost: boolean): Graphics2D[] {
164
- const { start, end } = s;
165
- const [xMin, xMax] = map.xRange;
166
- const [yMin, yMax] = map.yRange;
167
- const lines = (values: readonly number[], vertical: boolean): Graphics2D => {
168
- const g = new Graphics2D();
169
- for (const value of values) {
170
- g.line({
171
- points: vertical ? lineThrough(map, value, yMin, value, yMax) : lineThrough(map, xMin, value, xMax, value),
172
- radius: 0,
173
- start,
174
- end,
175
- });
176
- }
177
- return g;
178
- };
179
- const parts: Graphics2D[] = [];
180
- const minor = s.minorStroke;
181
- if (ghost) {
182
- if (minor.length === 0) return parts;
183
- if (x.marks.values.length > 0) parts.push(lines(x.marks.values, true).stroke(minor));
184
- if (y.marks.values.length > 0) parts.push(lines(y.marks.values, false).stroke(minor));
185
- return parts;
186
- }
187
- const divisions = Math.round(s.minorDivisions);
188
- if (minor.length > 0 && divisions > 1) {
189
- const between = (axis: AxisLayout, [min, max]: readonly [number, number]): number[] => {
190
- const { step, values } = axis.marks;
191
- if (!(step > 0) || values.length === 0) return [];
192
- const out: number[] = [];
193
- for (let base = values[0] - step; base < max; base += step) {
194
- for (let i = 1; i < divisions; i++) {
195
- const value = base + (step * i) / divisions;
196
- if (value > min && value < max) out.push(value);
197
- }
198
- }
199
- return out;
200
- };
201
- const xs = between(x, map.xRange);
202
- const ys = between(y, map.yRange);
203
- if (xs.length > 0) parts.push(lines(xs, true).stroke(minor));
204
- if (ys.length > 0) parts.push(lines(ys, false).stroke(minor));
205
- }
206
- const major = s.gridStroke;
207
- if (major.length > 0) {
208
- if (x.marks.values.length > 0) parts.push(lines(x.marks.values, true).stroke(major));
209
- if (y.marks.values.length > 0) parts.push(lines(y.marks.values, false).stroke(major));
210
- }
211
- return parts;
212
- }
213
-
214
- const STAY: Warp = (x, y, out) => {
215
- out.x = x;
216
- out.y = y;
217
- return true;
218
- };
219
-
220
- /** `source` as a warp, or `null` for text that is empty or not a field: the plane is then left as it is. */
221
- function warpOf(source: string): Warp | null {
222
- const field = source.trim() === "" ? null : parseField(source, fieldParams(source));
223
- if (!field || field instanceof Error) return null;
224
- const { x, y } = field;
225
- return (gx, gy, out) => {
226
- const wx = x(gx, gy);
227
- const wy = y(gx, gy);
228
- out.x = wx;
229
- out.y = wy;
230
- return Number.isFinite(wx) && Number.isFinite(wy);
231
- };
232
- }
1
+ import {
2
+ component2d,
3
+ Graphics2D,
4
+ Types,
5
+ type Extents,
6
+ type NodeOf,
7
+ type Stroke,
8
+ type StrokeResolved,
9
+ // Named so the emitted declaration reaches these through the sdk, not core.
10
+ type Color as _Color,
11
+ type Fill as _Fill,
12
+ type FillResolved as _FillResolved,
13
+ type Node2D as _Node2D,
14
+ type Node2DProps as _Node2DProps,
15
+ type NormalizedColor as _NormalizedColor,
16
+ type Shadow as _Shadow,
17
+ type ShadowResolved as _ShadowResolved,
18
+ } from "@motionscript/sdk";
19
+ import {
20
+ isIdentityMatrix,
21
+ lerpMatrix,
22
+ lineThrough,
23
+ readMatrix,
24
+ transformedMap,
25
+ warpedMap,
26
+ type CoordinateMap,
27
+ type Matrix2,
28
+ type Warp,
29
+ } from "../kit/coords";
30
+ import { fieldParams, parseField } from "../kit/field";
31
+ import { AXIS_COLOR } from "../kit/furniture";
32
+ import { lerpSource, readSource, type PlotSource } from "../kit/source";
33
+ import { drawSystem, plotArea, systemLayout, systemOpenPlane, systemProvides, systemSize } from "../kit/system";
34
+ import { AXES, AXES_COMMANDS, axesFurniture, axesMap, axesOf, loadAxes, type Axes2dProps, type AxesValues, type AxisLayout } from "./axes2d";
35
+
36
+ export interface NumberPlaneProps extends Axes2dProps {
37
+ grid?: boolean;
38
+ gridStroke?: Stroke;
39
+ minorStroke?: Stroke;
40
+ minorDivisions?: number;
41
+ matrix?: Matrix2 | readonly (readonly number[])[];
42
+ warp?: string | PlotSource;
43
+ ghost?: boolean;
44
+ }
45
+
46
+ const state = {
47
+ ...AXES,
48
+ grid: Types.boolean(true, { animatable: false }),
49
+ /** The line at every tick. */
50
+ gridStroke: Types.stroke({ weight: 1.5, fill: "#3b4658", align: "center" }),
51
+ /** The lines between ticks. */
52
+ minorStroke: Types.stroke({ weight: 1, fill: "#232b38", align: "center" }),
53
+ /** How many parts the space between two ticks is cut into; one draws no line between them. */
54
+ minorDivisions: Types.number(2, { min: 1, max: 10, step: 1 }),
55
+ /** A linear map of the plane, row by row: `[a, b, c, d]` sends `(x, y)` to `(ax + by, cx + dy)`. */
56
+ matrix: Types.custom<Matrix2 | readonly (readonly number[])[], Matrix2>([1, 0, 0, 1], { resolve: readMatrix, lerp: lerpMatrix }),
57
+ /**
58
+ * Where each point of the plane is sent, two parts in x and y:
59
+ * `(x + sin(y), y + sin(x))`. Empty leaves the plane flat.
60
+ */
61
+ warp: Types.custom<string | PlotSource, PlotSource>("", { resolve: readSource, lerp: lerpSource, kind: "fieldEquation" }),
62
+ /** Leave the plane as it was, faintly, behind one that has been transformed or warped. */
63
+ ghost: Types.boolean(true, { animatable: false }),
64
+ };
65
+
66
+ type PlaneValues = AxesValues & {
67
+ readonly grid: boolean;
68
+ readonly gridStroke: StrokeResolved[];
69
+ readonly minorStroke: StrokeResolved[];
70
+ readonly minorDivisions: number;
71
+ readonly matrix: Matrix2;
72
+ readonly warp: PlotSource;
73
+ readonly ghost: boolean;
74
+ };
75
+
76
+ /**
77
+ * Axes over a full grid, with square units unless told otherwise: the plane
78
+ * vectors and transformations are drawn on. Its `matrix` shears, turns or
79
+ * squeezes the whole plane about its origin, and its `warp` bends it by a
80
+ * function of its own points; the grid, the axes and everything on the plane
81
+ * go with it.
82
+ */
83
+ export const NumberPlane = component2d({
84
+ key: "numberPlane",
85
+ state,
86
+ defaults: {
87
+ squareUnits: true,
88
+ stroke: { weight: 2, fill: AXIS_COLOR, align: "center" },
89
+ // A transformed plane reaches past its box, and so does what is on it: both are kept in unless told otherwise.
90
+ clip: (s) => spills(s),
91
+ },
92
+ load: (loader, s) => loadAxes(loader, s),
93
+ commands: {
94
+ ...AXES_COMMANDS,
95
+ /** Send the plane through a matrix: a shear, a turn, a squeeze, a flip. */
96
+ transformTo: { args: ["matrix"] },
97
+ /** Bend the plane, each point travelling to where the warp sends it; an empty one flattens it again. */
98
+ warpTo: { args: ["warp"] },
99
+ },
100
+ provide: (s) => systemProvides(s),
101
+ measure: (_measurer, s) => systemSize(s),
102
+ layout: (box, s, children) => systemLayout(box, s, children, planeMap),
103
+ plane: (box, s) => systemOpenPlane(box, s, planeMap),
104
+ render: (ctx, s) => {
105
+ const { flat, map } = planeMaps(plotArea(ctx.size, s.padding), s);
106
+ drawSystem(ctx, s, map, (on) => axesFurniture(on, s, (x, y) => grid(s, flat, on, x, y)));
107
+ },
108
+ });
109
+
110
+ export type NumberPlane = NodeOf<typeof NumberPlane>;
111
+
112
+ /** The plane's map on a plot area: the flat one, sent through its matrix and its warp. */
113
+ export function planeMap(area: Extents, s: PlaneValues): CoordinateMap {
114
+ return planeMaps(area, s).map;
115
+ }
116
+
117
+ /** The flat plane on a plot area, and the plane as its matrix and its warp have it. */
118
+ function planeMaps(area: Extents, s: PlaneValues): { flat: CoordinateMap; map: CoordinateMap } {
119
+ const flat = axesMap(area, s);
120
+ const map = isIdentityMatrix(s.matrix) ? flat : transformedMap(flat, s.matrix);
121
+ const { source, blend } = s.warp;
122
+ const to = warpOf(source);
123
+ if (!blend) return { flat, map: to ? warpedMap(map, to, source) : map };
124
+ // Mid-warp each point is part of the way from where the old warp had it to where the new one sends it.
125
+ const from = warpOf(blend.from);
126
+ if (!from && !to) return { flat, map };
127
+ const start = { x: 0, y: 0 };
128
+ return {
129
+ flat,
130
+ map: warpedMap(
131
+ map,
132
+ (x, y, out) => {
133
+ if (!(from ?? STAY)(x, y, start) || !(to ?? STAY)(x, y, out)) return false;
134
+ out.x = start.x + (out.x - start.x) * blend.t;
135
+ out.y = start.y + (out.y - start.y) * blend.t;
136
+ return true;
137
+ },
138
+ `${blend.from}>${source}@${blend.t}`,
139
+ ),
140
+ };
141
+ }
142
+
143
+ /** Whether the plane reaches past its box: it is transformed or warped. */
144
+ function spills(s: PlaneValues): boolean {
145
+ if (!isIdentityMatrix(s.matrix)) return true;
146
+ const { source, blend } = s.warp;
147
+ return warpOf(source) !== null || (blend !== undefined && warpOf(blend.from) !== null);
148
+ }
149
+
150
+ /** The grid behind the axes, with the flat plane faintly behind a transformed one. */
151
+ function grid(s: PlaneValues, flat: CoordinateMap, map: CoordinateMap, x: AxisLayout, y: AxisLayout): Graphics2D[] {
152
+ if (!s.grid) return [];
153
+ const parts: Graphics2D[] = [];
154
+ if (s.ghost && flat.revision !== map.revision) {
155
+ const [gx, gy] = axesOf(flat, s);
156
+ parts.push(...ruled(s, flat, gx, gy, true));
157
+ }
158
+ parts.push(...ruled(s, map, x, y, false));
159
+ return parts;
160
+ }
161
+
162
+ /** The grid of `map`: every line, or only the ones at a tick, in the fainter stroke, for a ghost. */
163
+ function ruled(s: PlaneValues, map: CoordinateMap, x: AxisLayout, y: AxisLayout, ghost: boolean): Graphics2D[] {
164
+ const { start, end } = s;
165
+ const [xMin, xMax] = map.xRange;
166
+ const [yMin, yMax] = map.yRange;
167
+ const lines = (values: readonly number[], vertical: boolean): Graphics2D => {
168
+ const g = new Graphics2D();
169
+ for (const value of values) {
170
+ g.line({
171
+ points: vertical ? lineThrough(map, value, yMin, value, yMax) : lineThrough(map, xMin, value, xMax, value),
172
+ radius: 0,
173
+ start,
174
+ end,
175
+ });
176
+ }
177
+ return g;
178
+ };
179
+ const parts: Graphics2D[] = [];
180
+ const minor = s.minorStroke;
181
+ if (ghost) {
182
+ if (minor.length === 0) return parts;
183
+ if (x.marks.values.length > 0) parts.push(lines(x.marks.values, true).stroke(minor));
184
+ if (y.marks.values.length > 0) parts.push(lines(y.marks.values, false).stroke(minor));
185
+ return parts;
186
+ }
187
+ const divisions = Math.round(s.minorDivisions);
188
+ if (minor.length > 0 && divisions > 1) {
189
+ const between = (axis: AxisLayout, [min, max]: readonly [number, number]): number[] => {
190
+ const { step, values } = axis.marks;
191
+ if (!(step > 0) || values.length === 0) return [];
192
+ const out: number[] = [];
193
+ for (let base = values[0] - step; base < max; base += step) {
194
+ for (let i = 1; i < divisions; i++) {
195
+ const value = base + (step * i) / divisions;
196
+ if (value > min && value < max) out.push(value);
197
+ }
198
+ }
199
+ return out;
200
+ };
201
+ const xs = between(x, map.xRange);
202
+ const ys = between(y, map.yRange);
203
+ if (xs.length > 0) parts.push(lines(xs, true).stroke(minor));
204
+ if (ys.length > 0) parts.push(lines(ys, false).stroke(minor));
205
+ }
206
+ const major = s.gridStroke;
207
+ if (major.length > 0) {
208
+ if (x.marks.values.length > 0) parts.push(lines(x.marks.values, true).stroke(major));
209
+ if (y.marks.values.length > 0) parts.push(lines(y.marks.values, false).stroke(major));
210
+ }
211
+ return parts;
212
+ }
213
+
214
+ const STAY: Warp = (x, y, out) => {
215
+ out.x = x;
216
+ out.y = y;
217
+ return true;
218
+ };
219
+
220
+ /** `source` as a warp, or `null` for text that is empty or not a field: the plane is then left as it is. */
221
+ function warpOf(source: string): Warp | null {
222
+ const field = source.trim() === "" ? null : parseField(source, fieldParams(source));
223
+ if (!field || field instanceof Error) return null;
224
+ const { x, y } = field;
225
+ return (gx, gy, out) => {
226
+ const wx = x(gx, gy);
227
+ const wy = y(gx, gy);
228
+ out.x = wx;
229
+ out.y = wy;
230
+ return Number.isFinite(wx) && Number.isFinite(wy);
231
+ };
232
+ }