@motionscript/math 0.1.0-alpha.7 → 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 (181) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/LICENSE +201 -201
  3. package/NOTICE +49 -48
  4. package/README.md +390 -391
  5. package/dist/annotate/angle-mark.d.ts +6 -6
  6. package/dist/annotate/angle-mark.js.map +1 -1
  7. package/dist/annotate/curly-brace.d.ts +9 -9
  8. package/dist/annotate/curly-brace.js.map +1 -1
  9. package/dist/annotate/emphasis-mark.d.ts +9 -9
  10. package/dist/annotate/emphasis-mark.js.map +1 -1
  11. package/dist/browser/chunks/{chunk-ABYBW64J.js → chunk-IRZCG6PM.js} +2 -2
  12. package/dist/browser/chunks/chunk-IRZCG6PM.js.map +7 -0
  13. package/dist/browser/chunks/{chunk-K77KLQX4.js → chunk-WMVW6ZIV.js} +2 -2
  14. package/dist/browser/chunks/chunk-WMVW6ZIV.js.map +7 -0
  15. package/dist/browser/chunks/{mathjax-DNP3YRES.js → mathjax-WV55AC2P.js} +1 -1
  16. package/dist/browser/chunks/{mathjax-DNP3YRES.js.map → mathjax-WV55AC2P.js.map} +1 -1
  17. package/dist/browser/index.js +1 -1
  18. package/dist/browser/index.js.map +3 -3
  19. package/dist/browser/kit.js +1 -1
  20. package/dist/browser/manifest.json +1 -1
  21. package/dist/browser/tex.js +1 -1
  22. package/dist/browser/tex.js.map +1 -1
  23. package/dist/engine.d.ts +1 -1
  24. package/dist/engine.js +1 -1
  25. package/dist/engine.js.map +1 -1
  26. package/dist/index.js.map +1 -1
  27. package/dist/kit/brace.js.map +1 -1
  28. package/dist/kit/cache.js.map +1 -1
  29. package/dist/kit/component.d.ts.map +1 -1
  30. package/dist/kit/component.js.map +1 -1
  31. package/dist/kit/contour.js.map +1 -1
  32. package/dist/kit/coords.js.map +1 -1
  33. package/dist/kit/curve.js.map +1 -1
  34. package/dist/kit/equation.js.map +1 -1
  35. package/dist/kit/equation3d.js.map +1 -1
  36. package/dist/kit/expression.js.map +1 -1
  37. package/dist/kit/field-mark.d.ts.map +1 -1
  38. package/dist/kit/field-mark.js.map +1 -1
  39. package/dist/kit/field.js.map +1 -1
  40. package/dist/kit/follow.d.ts +2 -2
  41. package/dist/kit/follow.d.ts.map +1 -1
  42. package/dist/kit/follow.js.map +1 -1
  43. package/dist/kit/furniture.js.map +1 -1
  44. package/dist/kit/geometry.js.map +1 -1
  45. package/dist/kit/graph.js.map +1 -1
  46. package/dist/kit/mark.js.map +1 -1
  47. package/dist/kit/part-fills.js.map +1 -1
  48. package/dist/kit/place.js.map +1 -1
  49. package/dist/kit/sampler.js +1 -1
  50. package/dist/kit/sampler.js.map +1 -1
  51. package/dist/kit/scale.js.map +1 -1
  52. package/dist/kit/source.js.map +1 -1
  53. package/dist/kit/system.js.map +1 -1
  54. package/dist/kit/ticks.js.map +1 -1
  55. package/dist/kit/withdrawn.js.map +1 -1
  56. package/dist/marks/curve-area.js.map +1 -1
  57. package/dist/marks/drop-lines.d.ts +6 -6
  58. package/dist/marks/drop-lines.js.map +1 -1
  59. package/dist/marks/plot-point.d.ts +3 -3
  60. package/dist/marks/plot-point.js.map +1 -1
  61. package/dist/marks/riemann-sum.d.ts +3 -3
  62. package/dist/marks/riemann-sum.js.map +1 -1
  63. package/dist/marks/tangent-line.d.ts +6 -6
  64. package/dist/marks/tangent-line.js.map +1 -1
  65. package/dist/marks/vector-arrow.d.ts +6 -6
  66. package/dist/marks/vector-arrow.js.map +1 -1
  67. package/dist/nodes.js.map +1 -1
  68. package/dist/plots/function-plot.d.ts +8 -8
  69. package/dist/plots/function-plot.d.ts.map +1 -1
  70. package/dist/plots/function-plot.js.map +1 -1
  71. package/dist/plots/points-plot.js.map +1 -1
  72. package/dist/plots/stream-lines.js.map +1 -1
  73. package/dist/plots/traced-path.js.map +1 -1
  74. package/dist/plots/vector-field.js.map +1 -1
  75. package/dist/structures/graph-layout.js.map +1 -1
  76. package/dist/structures/network-graph.js.map +1 -1
  77. package/dist/structures/sample-space.js.map +1 -1
  78. package/dist/systems/axes2d.d.ts.map +1 -1
  79. package/dist/systems/axes2d.js.map +1 -1
  80. package/dist/systems/number-line.js.map +1 -1
  81. package/dist/systems/number-plane.js.map +1 -1
  82. package/dist/systems/polar-plane.js.map +1 -1
  83. package/dist/tex/geometry.js.map +1 -1
  84. package/dist/tex/index.js.map +1 -1
  85. package/dist/tex/mathjax.js.map +1 -1
  86. package/dist/tex/matrix.d.ts +2 -2
  87. package/dist/tex/matrix.js.map +1 -1
  88. package/dist/tex/node.d.ts +2 -2
  89. package/dist/tex/node.js.map +1 -1
  90. package/dist/tex/tween.js.map +1 -1
  91. package/dist/tex/typeset.d.ts +4 -4
  92. package/dist/tex/typeset.d.ts.map +1 -1
  93. package/dist/tex/typeset.js.map +1 -1
  94. package/dist/three/axes3d.js.map +1 -1
  95. package/dist/three/curve-plot3d.d.ts +27 -27
  96. package/dist/three/curve-plot3d.d.ts.map +1 -1
  97. package/dist/three/curve-plot3d.js +3 -2
  98. package/dist/three/curve-plot3d.js.map +1 -1
  99. package/dist/three/entries.js.map +1 -1
  100. package/dist/three/label3d.d.ts.map +1 -1
  101. package/dist/three/label3d.js +3 -1
  102. package/dist/three/label3d.js.map +1 -1
  103. package/dist/three/plot-point3d.d.ts +27 -27
  104. package/dist/three/plot-point3d.js.map +1 -1
  105. package/dist/three/space.js.map +1 -1
  106. package/dist/three/surface-plot3d.d.ts +36 -36
  107. package/dist/three/surface-plot3d.js +6 -1
  108. package/dist/three/surface-plot3d.js.map +1 -1
  109. package/dist/three/vector-arrow3d.d.ts +27 -27
  110. package/dist/three/vector-arrow3d.js.map +1 -1
  111. package/package.json +5 -4
  112. package/registry.json +251 -251
  113. package/src/annotate/angle-mark.ts +137 -137
  114. package/src/annotate/curly-brace.ts +148 -148
  115. package/src/annotate/emphasis-mark.ts +118 -118
  116. package/src/engine.ts +86 -86
  117. package/src/index.ts +29 -29
  118. package/src/kit/brace.ts +37 -37
  119. package/src/kit/cache.ts +61 -61
  120. package/src/kit/component.ts +57 -57
  121. package/src/kit/contour.ts +94 -94
  122. package/src/kit/coords.ts +278 -278
  123. package/src/kit/curve.ts +47 -47
  124. package/src/kit/equation.ts +181 -181
  125. package/src/kit/equation3d.ts +137 -137
  126. package/src/kit/expression.ts +159 -159
  127. package/src/kit/field-mark.ts +171 -171
  128. package/src/kit/field.ts +50 -50
  129. package/src/kit/follow.ts +229 -229
  130. package/src/kit/furniture.ts +113 -113
  131. package/src/kit/geometry.ts +49 -49
  132. package/src/kit/graph.ts +116 -116
  133. package/src/kit/mark.ts +152 -152
  134. package/src/kit/part-fills.ts +61 -61
  135. package/src/kit/place.ts +77 -77
  136. package/src/kit/sampler.ts +281 -281
  137. package/src/kit/scale.ts +39 -39
  138. package/src/kit/source.ts +57 -57
  139. package/src/kit/system.ts +105 -105
  140. package/src/kit/ticks.ts +85 -85
  141. package/src/kit/withdrawn.ts +8 -8
  142. package/src/marks/curve-area.ts +89 -89
  143. package/src/marks/drop-lines.ts +71 -71
  144. package/src/marks/plot-point.ts +102 -102
  145. package/src/marks/riemann-sum.ts +99 -99
  146. package/src/marks/tangent-line.ts +119 -119
  147. package/src/marks/vector-arrow.ts +98 -98
  148. package/src/nodes.ts +99 -99
  149. package/src/plots/function-plot.ts +220 -220
  150. package/src/plots/points-plot.ts +103 -103
  151. package/src/plots/stream-lines.ts +160 -160
  152. package/src/plots/traced-path.ts +70 -70
  153. package/src/plots/vector-field.ts +107 -107
  154. package/src/structures/graph-layout.ts +145 -145
  155. package/src/structures/network-graph.ts +291 -291
  156. package/src/structures/sample-space.ts +210 -210
  157. package/src/systems/axes2d.ts +226 -226
  158. package/src/systems/number-line.ts +130 -130
  159. package/src/systems/number-plane.ts +232 -232
  160. package/src/systems/polar-plane.ts +177 -177
  161. package/src/tex/geometry.ts +703 -703
  162. package/src/tex/index.ts +15 -15
  163. package/src/tex/mathjax.ts +82 -82
  164. package/src/tex/matrix.ts +97 -97
  165. package/src/tex/node.ts +74 -74
  166. package/src/tex/tween.ts +308 -308
  167. package/src/tex/typeset.ts +461 -461
  168. package/src/three/axes3d.ts +267 -267
  169. package/src/three/curve-plot3d.ts +146 -144
  170. package/src/three/entries.ts +42 -42
  171. package/src/three/label3d.ts +52 -50
  172. package/src/three/plot-point3d.ts +63 -63
  173. package/src/three/space.ts +92 -92
  174. package/src/three/surface-plot3d.ts +318 -312
  175. package/src/three/vector-arrow3d.ts +88 -88
  176. package/dist/browser/chunks/chunk-ABYBW64J.js.map +0 -7
  177. package/dist/browser/chunks/chunk-K77KLQX4.js.map +0 -7
  178. package/dist/kit/placement.d.ts +0 -8
  179. package/dist/kit/placement.d.ts.map +0 -1
  180. package/dist/kit/placement.js +0 -8
  181. package/dist/kit/placement.js.map +0 -1
package/src/kit/mark.ts CHANGED
@@ -1,152 +1,152 @@
1
- import {
2
- Graphics2D,
3
- type ChildView,
4
- type Extents,
5
- type Fill,
6
- type RenderContext,
7
- type Shadow,
8
- type Size2D,
9
- type Stroke,
10
- type StrokeResolved,
11
- type Vector2,
12
- } from "@motionscript/sdk";
13
- import { painted } from "./component";
14
- import { PIXEL_MAP, type CoordinateMap } from "./coords";
15
- import { boundsOfRuns, runsToPath } from "./geometry";
16
- import type { GraphPlane } from "./graph";
17
-
18
- /**
19
- * A mark is a node drawn in graph coordinates: a plot, an area, an arrow
20
- * between two graph points. It answers with runs of pixel points and is
21
- * otherwise a shape, so its box is the tight box of its ink.
22
- *
23
- * Inside a coordinate system (or a plot) the holder lays it out on that ink,
24
- * calling the mark's own trace with its map, and the mark's `x`/`y` are a shift
25
- * in graph units. Outside one a graph unit is a pixel and `x`/`y` are the usual
26
- * offset.
27
- */
28
-
29
- /** What a mark is traced through: the map, its own `x`/`y` as a shift, and the plane it reads curves from. */
30
- export interface MarkReading {
31
- readonly map: CoordinateMap;
32
- /** Zero outside a system, where `x`/`y` are pixels. */
33
- readonly shift: Vector2;
34
- readonly plane: GraphPlane | null;
35
- }
36
-
37
- /** The runs a mark draws, in pixels, y-up from the system's centre. */
38
- export type Tracer<V> = (state: V, read: MarkReading) => Vector2[][];
39
-
40
- /** What every mark's state reads: its place, and the plane it is laid out on. */
41
- export interface MarkState {
42
- readonly x: number;
43
- readonly y: number;
44
- readonly graph: GraphPlane | null;
45
- }
46
-
47
- const NO_SHIFT: Vector2 = { x: 0, y: 0 };
48
-
49
- const tracers: { readonly is: (child: ChildView) => boolean; readonly trace: Tracer<never> }[] = [];
50
-
51
- /** `component`, declared a mark: a holder lays it out on the ink `trace` draws. Hand back the component. */
52
- export function traced<C extends { is(child: ChildView): boolean }>(component: C, trace: Tracer<never>): C {
53
- tracers.push({ is: (child) => component.is(child), trace });
54
- return component;
55
- }
56
-
57
- /** The runs `child` draws on `plane`, or `undefined` for a child that is no mark. */
58
- export function markRuns(child: ChildView, map: CoordinateMap, plane: GraphPlane): Vector2[][] | undefined {
59
- const mark = tracers.find((entry) => entry.is(child));
60
- if (!mark) return undefined;
61
- const state = child.state as unknown as MarkState;
62
- return (mark.trace as Tracer<MarkState>)(state, { map, shift: { x: state.x, y: state.y }, plane });
63
- }
64
-
65
- /** How a mark's own fields read it: through its plane's map when one lays it out, through pixels when none does. */
66
- export function reading(state: MarkState): MarkReading {
67
- const plane = state.graph;
68
- return plane ? { map: plane.map, shift: { x: state.x, y: state.y }, plane } : { map: PIXEL_MAP, shift: NO_SHIFT, plane: null };
69
- }
70
-
71
- /** The size of a mark's ink: its box when a node other than a system lays it out. */
72
- export function inkSize(runs: readonly (readonly Vector2[])[]): Size2D {
73
- const ink = boundsOfRuns(runs);
74
- return ink ? { width: ink.maxX - ink.minX, height: ink.maxY - ink.minY } : { width: 0, height: 0 };
75
- }
76
-
77
- /** The centre of `runs`' ink in the map's pixels: where a mark's children are placed from. */
78
- export function inkCentre(runs: readonly (readonly Vector2[])[]): Vector2 {
79
- const ink = boundsOfRuns(runs);
80
- return ink ? { x: (ink.minX + ink.maxX) / 2, y: (ink.minY + ink.maxY) / 2 } : NO_SHIFT;
81
- }
82
-
83
- /** A graph point in a mark's own frame, where a handle on it sits. */
84
- export function ownPoint(read: MarkReading, runs: readonly (readonly Vector2[])[], point: Vector2): Vector2 {
85
- const at = read.map.toPoint(point.x + read.shift.x, point.y + read.shift.y);
86
- const centre = inkCentre(runs);
87
- return { x: at.x - centre.x, y: at.y - centre.y };
88
- }
89
-
90
- /** The graph point at `at` in a mark's own frame: {@link ownPoint} undone. */
91
- export function graphPoint(read: MarkReading, runs: readonly (readonly Vector2[])[], at: Vector2): Vector2 {
92
- const centre = inkCentre(runs);
93
- const point = read.map.toCoords(at.x + centre.x, at.y + centre.y);
94
- return { x: point.x - read.shift.x, y: point.y - read.shift.y };
95
- }
96
-
97
- /** What a mark paints with: a shape's paints, any it withdrew left out. */
98
- export interface MarkPaint {
99
- readonly fill?: Fill;
100
- readonly stroke?: Stroke;
101
- readonly shadow: Shadow;
102
- readonly start?: number;
103
- readonly end?: number;
104
- }
105
-
106
- export interface MarkOptions {
107
- /** Whether each run is closed into an outline. */
108
- readonly closed?: boolean;
109
- /** Whether the fill is painted: a mark whose ink has no inside yet (an open line) says no. */
110
- readonly fills?: boolean;
111
- /** The stroke along the ink, when it is not the plain one. */
112
- readonly ink?: (ctx: RenderContext, ink: Extents) => void;
113
- }
114
-
115
- const NONE: never[] = [];
116
-
117
- /** One mark's outline, the same commands for each paint. */
118
- export function markShape(runs: readonly (readonly Vector2[])[], s: MarkPaint, closed = false): (() => Graphics2D) | null {
119
- const ink = boundsOfRuns(runs);
120
- const data = runsToPath(runs, closed);
121
- if (!ink || data.length === 0) return null;
122
- const centerBounds: [number, number, number, number] = [ink.minX, ink.minY, ink.maxX, ink.maxY];
123
- return () => new Graphics2D().path({ data, centerBounds, start: s.start ?? 0, end: s.end ?? 1 });
124
- }
125
-
126
- /**
127
- * A mark's paint: the fill under the stroke along its ink, both before its
128
- * children, so a point or a tangent riding a plot sits on the curve.
129
- */
130
- export function drawMark(ctx: RenderContext, s: MarkPaint, runs: readonly (readonly Vector2[])[], options: MarkOptions = {}): void {
131
- const shape = markShape(runs, s, options.closed);
132
- const fill = s.fill ?? NONE;
133
- painted(ctx, () => {
134
- if (shape && options.fills !== false) ctx.draw(shape().shadow(s.shadow).fill(fill));
135
- const ink = options.ink ? boundsOfRuns(runs) : null;
136
- if (ink && options.ink) options.ink(ctx, ink);
137
- else if (shape) strokeShape(ctx, shape, s);
138
- });
139
- }
140
-
141
- /** The stroke along a shape, casting the shadow when nothing fills it. */
142
- export function strokeShape(ctx: RenderContext, shape: () => Graphics2D, s: MarkPaint): void {
143
- const stroke = (s.stroke ?? NONE) as StrokeResolved[];
144
- if (stroke.length === 0) return;
145
- ctx.draw(castsShadow(s) ? shape().shadow(s.shadow).stroke(stroke) : shape().stroke(stroke));
146
- }
147
-
148
- /** A shadow is cast by the stroke when nothing visible fills the shape. */
149
- export function castsShadow(s: MarkPaint): boolean {
150
- if ((s.shadow as unknown[]).length === 0) return false;
151
- return !((s.fill ?? NONE) as { opacity?: number }[]).some((layer) => (layer.opacity ?? 1) > 0);
152
- }
1
+ import {
2
+ Graphics2D,
3
+ type ChildView,
4
+ type Extents,
5
+ type Fill,
6
+ type RenderContext,
7
+ type Shadow,
8
+ type Size2D,
9
+ type Stroke,
10
+ type StrokeResolved,
11
+ type Vector2,
12
+ } from "@motionscript/sdk";
13
+ import { painted } from "./component";
14
+ import { PIXEL_MAP, type CoordinateMap } from "./coords";
15
+ import { boundsOfRuns, runsToPath } from "./geometry";
16
+ import type { GraphPlane } from "./graph";
17
+
18
+ /**
19
+ * A mark is a node drawn in graph coordinates: a plot, an area, an arrow
20
+ * between two graph points. It answers with runs of pixel points and is
21
+ * otherwise a shape, so its box is the tight box of its ink.
22
+ *
23
+ * Inside a coordinate system (or a plot) the holder lays it out on that ink,
24
+ * calling the mark's own trace with its map, and the mark's `x`/`y` are a shift
25
+ * in graph units. Outside one a graph unit is a pixel and `x`/`y` are the usual
26
+ * offset.
27
+ */
28
+
29
+ /** What a mark is traced through: the map, its own `x`/`y` as a shift, and the plane it reads curves from. */
30
+ export interface MarkReading {
31
+ readonly map: CoordinateMap;
32
+ /** Zero outside a system, where `x`/`y` are pixels. */
33
+ readonly shift: Vector2;
34
+ readonly plane: GraphPlane | null;
35
+ }
36
+
37
+ /** The runs a mark draws, in pixels, y-up from the system's centre. */
38
+ export type Tracer<V> = (state: V, read: MarkReading) => Vector2[][];
39
+
40
+ /** What every mark's state reads: its place, and the plane it is laid out on. */
41
+ export interface MarkState {
42
+ readonly x: number;
43
+ readonly y: number;
44
+ readonly graph: GraphPlane | null;
45
+ }
46
+
47
+ const NO_SHIFT: Vector2 = { x: 0, y: 0 };
48
+
49
+ const tracers: { readonly is: (child: ChildView) => boolean; readonly trace: Tracer<never> }[] = [];
50
+
51
+ /** `component`, declared a mark: a holder lays it out on the ink `trace` draws. Hand back the component. */
52
+ export function traced<C extends { is(child: ChildView): boolean }>(component: C, trace: Tracer<never>): C {
53
+ tracers.push({ is: (child) => component.is(child), trace });
54
+ return component;
55
+ }
56
+
57
+ /** The runs `child` draws on `plane`, or `undefined` for a child that is no mark. */
58
+ export function markRuns(child: ChildView, map: CoordinateMap, plane: GraphPlane): Vector2[][] | undefined {
59
+ const mark = tracers.find((entry) => entry.is(child));
60
+ if (!mark) return undefined;
61
+ const state = child.state as unknown as MarkState;
62
+ return (mark.trace as Tracer<MarkState>)(state, { map, shift: { x: state.x, y: state.y }, plane });
63
+ }
64
+
65
+ /** How a mark's own fields read it: through its plane's map when one lays it out, through pixels when none does. */
66
+ export function reading(state: MarkState): MarkReading {
67
+ const plane = state.graph;
68
+ return plane ? { map: plane.map, shift: { x: state.x, y: state.y }, plane } : { map: PIXEL_MAP, shift: NO_SHIFT, plane: null };
69
+ }
70
+
71
+ /** The size of a mark's ink: its box when a node other than a system lays it out. */
72
+ export function inkSize(runs: readonly (readonly Vector2[])[]): Size2D {
73
+ const ink = boundsOfRuns(runs);
74
+ return ink ? { width: ink.maxX - ink.minX, height: ink.maxY - ink.minY } : { width: 0, height: 0 };
75
+ }
76
+
77
+ /** The centre of `runs`' ink in the map's pixels: where a mark's children are placed from. */
78
+ export function inkCentre(runs: readonly (readonly Vector2[])[]): Vector2 {
79
+ const ink = boundsOfRuns(runs);
80
+ return ink ? { x: (ink.minX + ink.maxX) / 2, y: (ink.minY + ink.maxY) / 2 } : NO_SHIFT;
81
+ }
82
+
83
+ /** A graph point in a mark's own frame, where a handle on it sits. */
84
+ export function ownPoint(read: MarkReading, runs: readonly (readonly Vector2[])[], point: Vector2): Vector2 {
85
+ const at = read.map.toPoint(point.x + read.shift.x, point.y + read.shift.y);
86
+ const centre = inkCentre(runs);
87
+ return { x: at.x - centre.x, y: at.y - centre.y };
88
+ }
89
+
90
+ /** The graph point at `at` in a mark's own frame: {@link ownPoint} undone. */
91
+ export function graphPoint(read: MarkReading, runs: readonly (readonly Vector2[])[], at: Vector2): Vector2 {
92
+ const centre = inkCentre(runs);
93
+ const point = read.map.toCoords(at.x + centre.x, at.y + centre.y);
94
+ return { x: point.x - read.shift.x, y: point.y - read.shift.y };
95
+ }
96
+
97
+ /** What a mark paints with: a shape's paints, any it withdrew left out. */
98
+ export interface MarkPaint {
99
+ readonly fill?: Fill;
100
+ readonly stroke?: Stroke;
101
+ readonly shadow: Shadow;
102
+ readonly start?: number;
103
+ readonly end?: number;
104
+ }
105
+
106
+ export interface MarkOptions {
107
+ /** Whether each run is closed into an outline. */
108
+ readonly closed?: boolean;
109
+ /** Whether the fill is painted: a mark whose ink has no inside yet (an open line) says no. */
110
+ readonly fills?: boolean;
111
+ /** The stroke along the ink, when it is not the plain one. */
112
+ readonly ink?: (ctx: RenderContext, ink: Extents) => void;
113
+ }
114
+
115
+ const NONE: never[] = [];
116
+
117
+ /** One mark's outline, the same commands for each paint. */
118
+ export function markShape(runs: readonly (readonly Vector2[])[], s: MarkPaint, closed = false): (() => Graphics2D) | null {
119
+ const ink = boundsOfRuns(runs);
120
+ const data = runsToPath(runs, closed);
121
+ if (!ink || data.length === 0) return null;
122
+ const centerBounds: [number, number, number, number] = [ink.minX, ink.minY, ink.maxX, ink.maxY];
123
+ return () => new Graphics2D().path({ data, centerBounds, start: s.start ?? 0, end: s.end ?? 1 });
124
+ }
125
+
126
+ /**
127
+ * A mark's paint: the fill under the stroke along its ink, both before its
128
+ * children, so a point or a tangent riding a plot sits on the curve.
129
+ */
130
+ export function drawMark(ctx: RenderContext, s: MarkPaint, runs: readonly (readonly Vector2[])[], options: MarkOptions = {}): void {
131
+ const shape = markShape(runs, s, options.closed);
132
+ const fill = s.fill ?? NONE;
133
+ painted(ctx, () => {
134
+ if (shape && options.fills !== false) ctx.draw(shape().shadow(s.shadow).fill(fill));
135
+ const ink = options.ink ? boundsOfRuns(runs) : null;
136
+ if (ink && options.ink) options.ink(ctx, ink);
137
+ else if (shape) strokeShape(ctx, shape, s);
138
+ });
139
+ }
140
+
141
+ /** The stroke along a shape, casting the shadow when nothing fills it. */
142
+ export function strokeShape(ctx: RenderContext, shape: () => Graphics2D, s: MarkPaint): void {
143
+ const stroke = (s.stroke ?? NONE) as StrokeResolved[];
144
+ if (stroke.length === 0) return;
145
+ ctx.draw(castsShadow(s) ? shape().shadow(s.shadow).stroke(stroke) : shape().stroke(stroke));
146
+ }
147
+
148
+ /** A shadow is cast by the stroke when nothing visible fills the shape. */
149
+ export function castsShadow(s: MarkPaint): boolean {
150
+ if ((s.shadow as unknown[]).length === 0) return false;
151
+ return !((s.fill ?? NONE) as { opacity?: number }[]).some((layer) => (layer.opacity ?? 1) > 0);
152
+ }
@@ -1,61 +1,61 @@
1
- import { fillOps, lerpNumber, type Fill, type FillResolved } from "@motionscript/sdk";
2
-
3
- /** A paint for one part of a node, named the way the node names its parts: a formula's term, a graph's vertex or edge. */
4
- export interface PartFill {
5
- part: string;
6
- fill: Fill;
7
- /** Which of the places the part is written, counted from 1; 0 is all of them. */
8
- occurrence?: number;
9
- /** How far from the node's own paint to `fill`, 0 to 1. A tween's state: an author leaves it out. */
10
- mix?: number;
11
- }
12
-
13
- export interface PartFillResolved {
14
- part: string;
15
- fill: FillResolved[];
16
- occurrence: number;
17
- mix: number;
18
- }
19
-
20
- // Every entry is kept as written, one with no part yet included, so an editor
21
- // showing the list shows the row it just added; the drawing skips it.
22
- export function readPartFills(value: readonly PartFill[] | null | undefined): PartFillResolved[] {
23
- if (!Array.isArray(value)) return [];
24
- return value.map((entry) => ({
25
- part: String(entry?.part ?? ""),
26
- fill: fillOps.resolve(entry?.fill ?? "#ffffff"),
27
- occurrence: Math.max(0, Math.round(entry?.occurrence ?? 0)),
28
- mix: Math.min(1, Math.max(0, entry?.mix ?? 1)),
29
- }));
30
- }
31
-
32
- const keyOf = (entry: PartFillResolved) => `${entry.occurrence}|${entry.part}`;
33
-
34
- // A paint that arrives grows from the node's own, and one that leaves fades back to it.
35
- export function lerpPartFills(from: PartFillResolved[], to: PartFillResolved[], t: number): PartFillResolved[] {
36
- if (t <= 0) return from;
37
- if (t >= 1) return to;
38
- const before = new Map(from.map((entry) => [keyOf(entry), entry]));
39
- const out = to.map((entry) => {
40
- const was = before.get(keyOf(entry));
41
- before.delete(keyOf(entry));
42
- return was
43
- ? { ...entry, fill: fillOps.lerp(was.fill, entry.fill, t), mix: lerpNumber(was.mix, entry.mix, t) }
44
- : { ...entry, mix: entry.mix * t };
45
- });
46
- for (const gone of before.values()) out.push({ ...gone, mix: gone.mix * (1 - t) });
47
- return out;
48
- }
49
-
50
- /** Each part fill with its own key, the one in place of an earlier one for the same part and occurrence. */
51
- export function withPartFill(fills: readonly PartFillResolved[], entry: PartFill): PartFill[] {
52
- const occurrence = Math.max(0, Math.round(entry.occurrence ?? 0));
53
- return [...fills.filter((was) => was.part !== entry.part || was.occurrence !== occurrence), { ...entry, occurrence }];
54
- }
55
-
56
- /** `base` with a part's fill laid over it while it arrives, in place of it once it has. */
57
- export function partPaint(base: FillResolved[], entry: PartFillResolved | undefined): FillResolved[] {
58
- if (!entry || entry.mix <= 0) return base;
59
- if (entry.mix >= 1) return entry.fill;
60
- return [...base, ...entry.fill.map((layer) => ({ ...layer, opacity: (layer.opacity ?? 1) * entry.mix }))];
61
- }
1
+ import { fillOps, lerpNumber, type Fill, type FillResolved } from "@motionscript/sdk";
2
+
3
+ /** A paint for one part of a node, named the way the node names its parts: a formula's term, a graph's vertex or edge. */
4
+ export interface PartFill {
5
+ part: string;
6
+ fill: Fill;
7
+ /** Which of the places the part is written, counted from 1; 0 is all of them. */
8
+ occurrence?: number;
9
+ /** How far from the node's own paint to `fill`, 0 to 1. A tween's state: an author leaves it out. */
10
+ mix?: number;
11
+ }
12
+
13
+ export interface PartFillResolved {
14
+ part: string;
15
+ fill: FillResolved[];
16
+ occurrence: number;
17
+ mix: number;
18
+ }
19
+
20
+ // Every entry is kept as written, one with no part yet included, so an editor
21
+ // showing the list shows the row it just added; the drawing skips it.
22
+ export function readPartFills(value: readonly PartFill[] | null | undefined): PartFillResolved[] {
23
+ if (!Array.isArray(value)) return [];
24
+ return value.map((entry) => ({
25
+ part: String(entry?.part ?? ""),
26
+ fill: fillOps.resolve(entry?.fill ?? "#ffffff"),
27
+ occurrence: Math.max(0, Math.round(entry?.occurrence ?? 0)),
28
+ mix: Math.min(1, Math.max(0, entry?.mix ?? 1)),
29
+ }));
30
+ }
31
+
32
+ const keyOf = (entry: PartFillResolved) => `${entry.occurrence}|${entry.part}`;
33
+
34
+ // A paint that arrives grows from the node's own, and one that leaves fades back to it.
35
+ export function lerpPartFills(from: PartFillResolved[], to: PartFillResolved[], t: number): PartFillResolved[] {
36
+ if (t <= 0) return from;
37
+ if (t >= 1) return to;
38
+ const before = new Map(from.map((entry) => [keyOf(entry), entry]));
39
+ const out = to.map((entry) => {
40
+ const was = before.get(keyOf(entry));
41
+ before.delete(keyOf(entry));
42
+ return was
43
+ ? { ...entry, fill: fillOps.lerp(was.fill, entry.fill, t), mix: lerpNumber(was.mix, entry.mix, t) }
44
+ : { ...entry, mix: entry.mix * t };
45
+ });
46
+ for (const gone of before.values()) out.push({ ...gone, mix: gone.mix * (1 - t) });
47
+ return out;
48
+ }
49
+
50
+ /** Each part fill with its own key, the one in place of an earlier one for the same part and occurrence. */
51
+ export function withPartFill(fills: readonly PartFillResolved[], entry: PartFill): PartFill[] {
52
+ const occurrence = Math.max(0, Math.round(entry.occurrence ?? 0));
53
+ return [...fills.filter((was) => was.part !== entry.part || was.occurrence !== occurrence), { ...entry, occurrence }];
54
+ }
55
+
56
+ /** `base` with a part's fill laid over it while it arrives, in place of it once it has. */
57
+ export function partPaint(base: FillResolved[], entry: PartFillResolved | undefined): FillResolved[] {
58
+ if (!entry || entry.mix <= 0) return base;
59
+ if (entry.mix >= 1) return entry.fill;
60
+ return [...base, ...entry.fill.map((layer) => ({ ...layer, opacity: (layer.opacity ?? 1) * entry.mix }))];
61
+ }
package/src/kit/place.ts CHANGED
@@ -1,77 +1,77 @@
1
- import type { ChildPlane, ChildView, Placement2D, Size2D, Vector2 } from "@motionscript/sdk";
2
- import type { CoordinateMap } from "./coords";
3
- import { boundsOfRuns } from "./geometry";
4
- import type { GraphPlane } from "./graph";
5
- import { markRuns } from "./mark";
6
-
7
- /** Where a child that sits somewhere other than its own `x`/`y` is (a point riding a curve), given its own `x`/`y`. */
8
- export type Anchor = (x: number, y: number) => Vector2;
9
-
10
- const anchors: { readonly is: (child: ChildView) => boolean; readonly anchor: (state: never, plane: GraphPlane) => Anchor | null }[] = [];
11
-
12
- /** `component`, declared to sit at `anchor` of its own `x`/`y` on a graph. Hand back the component. */
13
- export function anchored<C extends { is(child: ChildView): boolean }>(component: C, anchor: (state: never, plane: GraphPlane) => Anchor | null): C {
14
- anchors.push({ is: (child) => component.is(child), anchor });
15
- return component;
16
- }
17
-
18
- /**
19
- * Lay `children` out on `map`, for whichever node holds them: a coordinate
20
- * system, or a plot holding the marks that read it. A mark goes on its ink;
21
- * anything else keeps its own size and has its `x`/`y` read as graph
22
- * coordinates. `origin` is the holder's own centre in the map's pixels (y-up),
23
- * since a child's box is placed from there.
24
- */
25
- export function placeOnMap(children: readonly ChildView[], map: CoordinateMap, origin: Vector2, room: Size2D, plane: GraphPlane): Placement2D[] {
26
- return children.map((child) => {
27
- const runs = markRuns(child, map, plane);
28
- if (runs) {
29
- const ink = boundsOfRuns(runs);
30
- const cx = ink ? (ink.minX + ink.maxX) / 2 : origin.x;
31
- const cy = ink ? (ink.minY + ink.maxY) / 2 : origin.y;
32
- return {
33
- x: cx - origin.x,
34
- y: cy - origin.y,
35
- width: ink ? ink.maxX - ink.minX : 0,
36
- height: ink ? ink.maxY - ink.minY : 0,
37
- plane: markPlane(map, cx, cy),
38
- };
39
- }
40
- const size = child.measure({ width: room.width, height: room.height });
41
- const entry = anchors.find((a) => a.is(child));
42
- const anchor = entry ? (entry.anchor as (state: unknown, plane: GraphPlane) => Anchor | null)(child.state, plane) : null;
43
- // Its box is centred on the map's own centre, so its offset from there is its point.
44
- return { x: -origin.x, y: -origin.y, width: size.width, height: size.height, plane: pointPlane(map, anchor) };
45
- });
46
- }
47
-
48
- /** The plane of a child that is not there yet, under a holder centred on `origin`: where a host would drop one. */
49
- export function openPlane(map: CoordinateMap, origin: Vector2): ChildPlane {
50
- return {
51
- toOffset: (x, y) => {
52
- const at = map.toPoint(x, y);
53
- return { x: at.x - origin.x, y: at.y - origin.y };
54
- },
55
- toCoords: (x, y) => map.toCoords(x + origin.x, y + origin.y),
56
- };
57
- }
58
-
59
- function pointPlane(map: CoordinateMap, anchor: Anchor | null): ChildPlane {
60
- const point: Vector2 = { x: 0, y: 0 };
61
- return {
62
- toOffset: (x, y) => {
63
- const at = anchor?.(x, y);
64
- return at ? map.toPoint(at.x, at.y, point) : map.toPoint(x, y, point);
65
- },
66
- toCoords: (x, y) => map.toCoords(x, y),
67
- };
68
- }
69
-
70
- // A mark reads its own `x`/`y` as it draws, so its holder adds nothing; a drag still answers in graph units.
71
- function markPlane(map: CoordinateMap, cx: number, cy: number): ChildPlane {
72
- const none: Vector2 = { x: 0, y: 0 };
73
- return {
74
- toOffset: () => none,
75
- toCoords: (x, y) => map.toCoords(cx + x, cy + y),
76
- };
77
- }
1
+ import type { ChildPlane, ChildView, Placement2D, Size2D, Vector2 } from "@motionscript/sdk";
2
+ import type { CoordinateMap } from "./coords";
3
+ import { boundsOfRuns } from "./geometry";
4
+ import type { GraphPlane } from "./graph";
5
+ import { markRuns } from "./mark";
6
+
7
+ /** Where a child that sits somewhere other than its own `x`/`y` is (a point riding a curve), given its own `x`/`y`. */
8
+ export type Anchor = (x: number, y: number) => Vector2;
9
+
10
+ const anchors: { readonly is: (child: ChildView) => boolean; readonly anchor: (state: never, plane: GraphPlane) => Anchor | null }[] = [];
11
+
12
+ /** `component`, declared to sit at `anchor` of its own `x`/`y` on a graph. Hand back the component. */
13
+ export function anchored<C extends { is(child: ChildView): boolean }>(component: C, anchor: (state: never, plane: GraphPlane) => Anchor | null): C {
14
+ anchors.push({ is: (child) => component.is(child), anchor });
15
+ return component;
16
+ }
17
+
18
+ /**
19
+ * Lay `children` out on `map`, for whichever node holds them: a coordinate
20
+ * system, or a plot holding the marks that read it. A mark goes on its ink;
21
+ * anything else keeps its own size and has its `x`/`y` read as graph
22
+ * coordinates. `origin` is the holder's own centre in the map's pixels (y-up),
23
+ * since a child's box is placed from there.
24
+ */
25
+ export function placeOnMap(children: readonly ChildView[], map: CoordinateMap, origin: Vector2, room: Size2D, plane: GraphPlane): Placement2D[] {
26
+ return children.map((child) => {
27
+ const runs = markRuns(child, map, plane);
28
+ if (runs) {
29
+ const ink = boundsOfRuns(runs);
30
+ const cx = ink ? (ink.minX + ink.maxX) / 2 : origin.x;
31
+ const cy = ink ? (ink.minY + ink.maxY) / 2 : origin.y;
32
+ return {
33
+ x: cx - origin.x,
34
+ y: cy - origin.y,
35
+ width: ink ? ink.maxX - ink.minX : 0,
36
+ height: ink ? ink.maxY - ink.minY : 0,
37
+ plane: markPlane(map, cx, cy),
38
+ };
39
+ }
40
+ const size = child.measure({ width: room.width, height: room.height });
41
+ const entry = anchors.find((a) => a.is(child));
42
+ const anchor = entry ? (entry.anchor as (state: unknown, plane: GraphPlane) => Anchor | null)(child.state, plane) : null;
43
+ // Its box is centred on the map's own centre, so its offset from there is its point.
44
+ return { x: -origin.x, y: -origin.y, width: size.width, height: size.height, plane: pointPlane(map, anchor) };
45
+ });
46
+ }
47
+
48
+ /** The plane of a child that is not there yet, under a holder centred on `origin`: where a host would drop one. */
49
+ export function openPlane(map: CoordinateMap, origin: Vector2): ChildPlane {
50
+ return {
51
+ toOffset: (x, y) => {
52
+ const at = map.toPoint(x, y);
53
+ return { x: at.x - origin.x, y: at.y - origin.y };
54
+ },
55
+ toCoords: (x, y) => map.toCoords(x + origin.x, y + origin.y),
56
+ };
57
+ }
58
+
59
+ function pointPlane(map: CoordinateMap, anchor: Anchor | null): ChildPlane {
60
+ const point: Vector2 = { x: 0, y: 0 };
61
+ return {
62
+ toOffset: (x, y) => {
63
+ const at = anchor?.(x, y);
64
+ return at ? map.toPoint(at.x, at.y, point) : map.toPoint(x, y, point);
65
+ },
66
+ toCoords: (x, y) => map.toCoords(x, y),
67
+ };
68
+ }
69
+
70
+ // A mark reads its own `x`/`y` as it draws, so its holder adds nothing; a drag still answers in graph units.
71
+ function markPlane(map: CoordinateMap, cx: number, cy: number): ChildPlane {
72
+ const none: Vector2 = { x: 0, y: 0 };
73
+ return {
74
+ toOffset: () => none,
75
+ toCoords: (x, y) => map.toCoords(cx + x, cy + y),
76
+ };
77
+ }