@motionscript/math 0.1.0-alpha.4 → 0.1.0-alpha.6

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 (233) hide show
  1. package/CHANGELOG.md +132 -92
  2. package/README.md +39 -33
  3. package/dist/annotate/angle-mark.d.ts +52 -27
  4. package/dist/annotate/angle-mark.d.ts.map +1 -1
  5. package/dist/annotate/angle-mark.js +80 -115
  6. package/dist/annotate/angle-mark.js.map +1 -1
  7. package/dist/annotate/curly-brace.d.ts +59 -25
  8. package/dist/annotate/curly-brace.d.ts.map +1 -1
  9. package/dist/annotate/curly-brace.js +85 -100
  10. package/dist/annotate/curly-brace.js.map +1 -1
  11. package/dist/annotate/emphasis-mark.d.ts +43 -9
  12. package/dist/annotate/emphasis-mark.d.ts.map +1 -1
  13. package/dist/annotate/emphasis-mark.js +33 -34
  14. package/dist/annotate/emphasis-mark.js.map +1 -1
  15. package/dist/browser/chunks/chunk-ABYBW64J.js +2 -0
  16. package/dist/browser/chunks/chunk-ABYBW64J.js.map +7 -0
  17. package/dist/browser/chunks/chunk-K77KLQX4.js +2 -0
  18. package/dist/browser/chunks/chunk-K77KLQX4.js.map +7 -0
  19. package/dist/browser/index.js +1 -1
  20. package/dist/browser/index.js.map +3 -3
  21. package/dist/browser/kit.js +1 -1
  22. package/dist/browser/manifest.json +1 -1
  23. package/dist/browser/tex.js +1 -1
  24. package/dist/browser/tex.js.map +2 -2
  25. package/dist/engine.d.ts +26 -19
  26. package/dist/engine.d.ts.map +1 -1
  27. package/dist/engine.js +17 -14
  28. package/dist/engine.js.map +1 -1
  29. package/dist/kit/component.d.ts +51 -0
  30. package/dist/kit/component.d.ts.map +1 -0
  31. package/dist/kit/component.js +30 -0
  32. package/dist/kit/component.js.map +1 -0
  33. package/dist/kit/curve.d.ts +22 -11
  34. package/dist/kit/curve.d.ts.map +1 -1
  35. package/dist/kit/curve.js +25 -18
  36. package/dist/kit/curve.js.map +1 -1
  37. package/dist/kit/field-mark.d.ts +58 -35
  38. package/dist/kit/field-mark.d.ts.map +1 -1
  39. package/dist/kit/field-mark.js +100 -120
  40. package/dist/kit/field-mark.js.map +1 -1
  41. package/dist/kit/follow.d.ts +67 -29
  42. package/dist/kit/follow.d.ts.map +1 -1
  43. package/dist/kit/follow.js +114 -110
  44. package/dist/kit/follow.js.map +1 -1
  45. package/dist/kit/furniture.d.ts +3 -1
  46. package/dist/kit/furniture.d.ts.map +1 -1
  47. package/dist/kit/furniture.js +5 -0
  48. package/dist/kit/furniture.js.map +1 -1
  49. package/dist/kit/graph.d.ts +46 -0
  50. package/dist/kit/graph.d.ts.map +1 -0
  51. package/dist/kit/graph.js +90 -0
  52. package/dist/kit/graph.js.map +1 -0
  53. package/dist/kit/mark.d.ts +64 -39
  54. package/dist/kit/mark.d.ts.map +1 -1
  55. package/dist/kit/mark.js +80 -96
  56. package/dist/kit/mark.js.map +1 -1
  57. package/dist/kit/place.d.ts +10 -8
  58. package/dist/kit/place.d.ts.map +1 -1
  59. package/dist/kit/place.js +28 -30
  60. package/dist/kit/place.js.map +1 -1
  61. package/dist/kit/system.d.ts +27 -29
  62. package/dist/kit/system.d.ts.map +1 -1
  63. package/dist/kit/system.js +62 -58
  64. package/dist/kit/system.js.map +1 -1
  65. package/dist/kit/withdrawn.d.ts +3 -3
  66. package/dist/kit/withdrawn.js +3 -3
  67. package/dist/kit/withdrawn.js.map +1 -1
  68. package/dist/marks/curve-area.d.ts +24 -18
  69. package/dist/marks/curve-area.d.ts.map +1 -1
  70. package/dist/marks/curve-area.js +50 -73
  71. package/dist/marks/curve-area.js.map +1 -1
  72. package/dist/marks/drop-lines.d.ts +28 -10
  73. package/dist/marks/drop-lines.d.ts.map +1 -1
  74. package/dist/marks/drop-lines.js +37 -52
  75. package/dist/marks/drop-lines.js.map +1 -1
  76. package/dist/marks/plot-point.d.ts +37 -20
  77. package/dist/marks/plot-point.d.ts.map +1 -1
  78. package/dist/marks/plot-point.js +65 -91
  79. package/dist/marks/plot-point.js.map +1 -1
  80. package/dist/marks/riemann-sum.d.ts +30 -26
  81. package/dist/marks/riemann-sum.d.ts.map +1 -1
  82. package/dist/marks/riemann-sum.js +57 -85
  83. package/dist/marks/riemann-sum.js.map +1 -1
  84. package/dist/marks/tangent-line.d.ts +38 -14
  85. package/dist/marks/tangent-line.d.ts.map +1 -1
  86. package/dist/marks/tangent-line.js +73 -87
  87. package/dist/marks/tangent-line.js.map +1 -1
  88. package/dist/marks/vector-arrow.d.ts +30 -20
  89. package/dist/marks/vector-arrow.d.ts.map +1 -1
  90. package/dist/marks/vector-arrow.js +58 -78
  91. package/dist/marks/vector-arrow.js.map +1 -1
  92. package/dist/nodes.d.ts +13 -5
  93. package/dist/nodes.d.ts.map +1 -1
  94. package/dist/nodes.js +0 -6
  95. package/dist/nodes.js.map +1 -1
  96. package/dist/plots/function-plot.d.ts +107 -43
  97. package/dist/plots/function-plot.d.ts.map +1 -1
  98. package/dist/plots/function-plot.js +146 -177
  99. package/dist/plots/function-plot.js.map +1 -1
  100. package/dist/plots/points-plot.d.ts +34 -19
  101. package/dist/plots/points-plot.d.ts.map +1 -1
  102. package/dist/plots/points-plot.js +31 -58
  103. package/dist/plots/points-plot.js.map +1 -1
  104. package/dist/plots/stream-lines.d.ts +38 -19
  105. package/dist/plots/stream-lines.d.ts.map +1 -1
  106. package/dist/plots/stream-lines.js +60 -79
  107. package/dist/plots/stream-lines.js.map +1 -1
  108. package/dist/plots/traced-path.d.ts +34 -15
  109. package/dist/plots/traced-path.d.ts.map +1 -1
  110. package/dist/plots/traced-path.js +33 -48
  111. package/dist/plots/traced-path.js.map +1 -1
  112. package/dist/plots/vector-field.d.ts +31 -11
  113. package/dist/plots/vector-field.d.ts.map +1 -1
  114. package/dist/plots/vector-field.js +50 -64
  115. package/dist/plots/vector-field.js.map +1 -1
  116. package/dist/structures/network-graph.d.ts +99 -49
  117. package/dist/structures/network-graph.d.ts.map +1 -1
  118. package/dist/structures/network-graph.js +168 -217
  119. package/dist/structures/network-graph.js.map +1 -1
  120. package/dist/structures/sample-space.d.ts +69 -38
  121. package/dist/structures/sample-space.d.ts.map +1 -1
  122. package/dist/structures/sample-space.js +132 -170
  123. package/dist/structures/sample-space.js.map +1 -1
  124. package/dist/systems/axes2d.d.ts +126 -37
  125. package/dist/systems/axes2d.d.ts.map +1 -1
  126. package/dist/systems/axes2d.js +147 -183
  127. package/dist/systems/axes2d.js.map +1 -1
  128. package/dist/systems/number-line.d.ts +51 -28
  129. package/dist/systems/number-line.d.ts.map +1 -1
  130. package/dist/systems/number-line.js +82 -118
  131. package/dist/systems/number-line.js.map +1 -1
  132. package/dist/systems/number-plane.d.ts +92 -31
  133. package/dist/systems/number-plane.d.ts.map +1 -1
  134. package/dist/systems/number-plane.js +142 -149
  135. package/dist/systems/number-plane.js.map +1 -1
  136. package/dist/systems/polar-plane.d.ts +47 -26
  137. package/dist/systems/polar-plane.d.ts.map +1 -1
  138. package/dist/systems/polar-plane.js +107 -133
  139. package/dist/systems/polar-plane.js.map +1 -1
  140. package/dist/tex/index.d.ts.map +1 -1
  141. package/dist/tex/index.js +1 -0
  142. package/dist/tex/index.js.map +1 -1
  143. package/dist/tex/matrix.d.ts +56 -18
  144. package/dist/tex/matrix.d.ts.map +1 -1
  145. package/dist/tex/matrix.js +33 -50
  146. package/dist/tex/matrix.js.map +1 -1
  147. package/dist/tex/node.d.ts +55 -201
  148. package/dist/tex/node.d.ts.map +1 -1
  149. package/dist/tex/node.js +23 -593
  150. package/dist/tex/node.js.map +1 -1
  151. package/dist/tex/typeset.d.ts +133 -0
  152. package/dist/tex/typeset.d.ts.map +1 -0
  153. package/dist/tex/typeset.js +403 -0
  154. package/dist/tex/typeset.js.map +1 -0
  155. package/dist/three/axes3d.d.ts +140 -54
  156. package/dist/three/axes3d.d.ts.map +1 -1
  157. package/dist/three/axes3d.js +159 -204
  158. package/dist/three/axes3d.js.map +1 -1
  159. package/dist/three/curve-plot3d.d.ts +70 -42
  160. package/dist/three/curve-plot3d.d.ts.map +1 -1
  161. package/dist/three/curve-plot3d.js +97 -132
  162. package/dist/three/curve-plot3d.js.map +1 -1
  163. package/dist/three/entries.d.ts +17 -0
  164. package/dist/three/entries.d.ts.map +1 -0
  165. package/dist/three/entries.js +15 -0
  166. package/dist/three/entries.js.map +1 -0
  167. package/dist/three/plot-point3d.d.ts +57 -12
  168. package/dist/three/plot-point3d.d.ts.map +1 -1
  169. package/dist/three/plot-point3d.js +27 -53
  170. package/dist/three/plot-point3d.js.map +1 -1
  171. package/dist/three/space.d.ts +17 -2
  172. package/dist/three/space.d.ts.map +1 -1
  173. package/dist/three/space.js +19 -8
  174. package/dist/three/space.js.map +1 -1
  175. package/dist/three/surface-plot3d.d.ts +131 -49
  176. package/dist/three/surface-plot3d.d.ts.map +1 -1
  177. package/dist/three/surface-plot3d.js +227 -261
  178. package/dist/three/surface-plot3d.js.map +1 -1
  179. package/dist/three/vector-arrow3d.d.ts +58 -16
  180. package/dist/three/vector-arrow3d.d.ts.map +1 -1
  181. package/dist/three/vector-arrow3d.js +48 -67
  182. package/dist/three/vector-arrow3d.js.map +1 -1
  183. package/package.json +4 -4
  184. package/src/annotate/angle-mark.ts +95 -105
  185. package/src/annotate/curly-brace.ts +107 -85
  186. package/src/annotate/emphasis-mark.ts +55 -26
  187. package/src/engine.ts +35 -21
  188. package/src/kit/component.ts +57 -0
  189. package/src/kit/curve.ts +38 -20
  190. package/src/kit/field-mark.ts +124 -108
  191. package/src/kit/follow.ts +146 -120
  192. package/src/kit/furniture.ts +6 -1
  193. package/src/kit/graph.ts +116 -0
  194. package/src/kit/mark.ts +135 -114
  195. package/src/kit/place.ts +32 -56
  196. package/src/kit/system.ts +77 -71
  197. package/src/kit/withdrawn.ts +3 -3
  198. package/src/marks/curve-area.ts +65 -65
  199. package/src/marks/drop-lines.ts +50 -42
  200. package/src/marks/plot-point.ts +77 -82
  201. package/src/marks/riemann-sum.ts +73 -72
  202. package/src/marks/tangent-line.ts +87 -71
  203. package/src/marks/vector-arrow.ts +73 -73
  204. package/src/nodes.ts +41 -5
  205. package/src/plots/function-plot.ts +155 -171
  206. package/src/plots/points-plot.ts +46 -50
  207. package/src/plots/stream-lines.ts +75 -66
  208. package/src/plots/traced-path.ts +51 -33
  209. package/src/plots/vector-field.ts +68 -49
  210. package/src/structures/network-graph.ts +168 -185
  211. package/src/structures/sample-space.ts +149 -148
  212. package/src/systems/axes2d.ts +165 -156
  213. package/src/systems/number-line.ts +91 -92
  214. package/src/systems/number-plane.ts +149 -120
  215. package/src/systems/polar-plane.ts +117 -116
  216. package/src/tex/index.ts +2 -1
  217. package/src/tex/matrix.ts +62 -40
  218. package/src/tex/node.ts +54 -621
  219. package/src/tex/typeset.ts +461 -0
  220. package/src/three/axes3d.ts +182 -161
  221. package/src/three/curve-plot3d.ts +107 -110
  222. package/src/three/entries.ts +42 -0
  223. package/src/three/plot-point3d.ts +42 -45
  224. package/src/three/space.ts +34 -9
  225. package/src/three/surface-plot3d.ts +312 -323
  226. package/src/three/vector-arrow3d.ts +65 -54
  227. package/dist/browser/chunks/chunk-3GJB6AVO.js +0 -2
  228. package/dist/browser/chunks/chunk-3GJB6AVO.js.map +0 -7
  229. package/dist/browser/chunks/chunk-D5WHCVBC.js +0 -2
  230. package/dist/browser/chunks/chunk-D5WHCVBC.js.map +0 -7
  231. package/dist/browser/chunks/chunk-WL4KEX7Y.js +0 -2
  232. package/dist/browser/chunks/chunk-WL4KEX7Y.js.map +0 -7
  233. package/src/kit/placement.ts +0 -8
package/src/engine.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  * The math **kit**: what the nodes share, and what a copied one does not copy.
3
3
  *
4
4
  * `ms add` writes a node's own files into a project and rewrites its reach for
5
- * these to `@motionscript/math`, so MathJax, the morph and every shared class
5
+ * these to `@motionscript/math`, so MathJax, the morph and every shared module
6
6
  * stay a real dependency with one identity.
7
7
  */
8
8
  export {
@@ -13,19 +13,31 @@ export { defaultLatexMorph } from './tex/tween';
13
13
  export type { AnimatedToken, LatexMorphStrategy } from './tex/tween';
14
14
  export { Latex } from './tex/node';
15
15
  export type { LatexProps, LatexStrategies } from './tex/node';
16
+ export {
17
+ drawFormula, formula, formulaFrame, formulaOf, isFormula, latexPartBounds, layoutFormula, lerpMorph, loadFormula, measureFormula, Morph, morphOf,
18
+ partBoundsIn, settled, TYPESET, TYPESET_COMMANDS,
19
+ } from './tex/typeset';
20
+ export type { FormulaFrame, Morphing, TypesetValues } from './tex/typeset';
16
21
  export { lerpPartFills, partPaint, readPartFills, withPartFill } from './kit/part-fills';
17
22
  export type { PartFill, PartFillResolved } from './kit/part-fills';
18
23
  export { FILL_PAINT, OUTLINE_TRIM, STROKE_PAINT } from './kit/withdrawn';
19
24
  export { createBoundedCache } from './kit/cache';
25
+ export { listed, omit, PAINT, PAINT_COMMANDS, painted } from './kit/component';
20
26
  export type { BoundedCache } from './kit/cache';
21
27
 
22
- export { CoordinateSystem } from './kit/system';
23
- // A number plane is axes with a grid, so a copied one extends these rather than a copy of them.
24
- export { Axes2d } from './systems/axes2d';
25
- export type { Axes2dProps, AxisLayout } from './systems/axes2d';
26
- export type { Anchored } from './kit/place';
27
- export { openPlane, placeOnMap } from './kit/place';
28
- export { Mark } from './kit/mark';
28
+ export { drawSystem, plotArea, systemLayout, systemMap, systemOpenPlane, systemProvides, systemSize } from './kit/system';
29
+ export type { MapFor, SystemState } from './kit/system';
30
+ // A number plane is axes with a grid, so a copied one builds on these rather than a copy of them.
31
+ export { Axes2d, AXES, AXES_COMMANDS, axesFurniture, axesMap, axesOf, loadAxes } from './systems/axes2d';
32
+ export type { Axes2dProps, AxesValues, AxisLayout } from './systems/axes2d';
33
+ export type { Anchor } from './kit/place';
34
+ export { anchored, openPlane, placeOnMap } from './kit/place';
35
+ export {
36
+ castsShadow, drawMark, graphPoint, inkCentre, inkSize, markRuns, markShape, ownPoint, reading, strokeShape, traced,
37
+ } from './kit/mark';
38
+ export type { MarkOptions, MarkPaint, MarkReading, MarkState, Tracer } from './kit/mark';
39
+ export { curveFor, PLANE_ENTRY, PLOT, PLOT_REF, plotPlane, refId, settle, SYSTEM, SYSTEM_ENTRY, systemPlane } from './kit/graph';
40
+ export type { GraphPlane } from './kit/graph';
29
41
  export { axisScale, cartesianMap, resolveInterval, PIXEL_MAP } from './kit/coords';
30
42
  export type { AxisScale, AxisScaling, CartesianOptions, CoordinateMap, Interval } from './kit/coords';
31
43
  export { compileExpression, compileExpressionCached, expressionVocabulary, MATH_DIALECT } from './kit/expression';
@@ -39,30 +51,32 @@ export type { CurveProjection, SampleOptions } from './kit/sampler';
39
51
  export { formatTick, ticksFor } from './kit/ticks';
40
52
  export type { Ticks } from './kit/ticks';
41
53
  export { boundsOfRuns, clamp, inflate, runsToPath, stated } from './kit/geometry';
42
- export { curveNamed, curveOf } from './kit/curve';
43
- export type { Curve } from './kit/curve';
44
- export { along, AXIS_COLOR, drawLabels, drawn, lerpInterval, PLOT_COLOR, readInterval, withoutTips } from './kit/furniture';
54
+ export { CURVE_KEYS, curveValue, isCurve } from './kit/curve';
55
+ export type { CurveValues } from './kit/curve';
56
+ export { along, AXIS_COLOR, declareFormulas, drawLabels, drawn, lerpInterval, loadFormulas, PLOT_COLOR, readInterval, withoutTips } from './kit/furniture';
45
57
  export type { Label, LabelStyle } from './kit/furniture';
46
- export { add, between, followed, FollowMark, grown, heading, quadOf } from './kit/follow';
47
- export type { FollowProps, Parted, Quad, Retarget, Target } from './kit/follow';
58
+ export {
59
+ add, between, container, FOLLOW_COMMANDS, FOLLOW_STATE, followedQuad, grown, heading, outlineBounds, OWN_FRAME, resting,
60
+ } from './kit/follow';
61
+ export type { FollowProps, FollowValues, Parted, Quad, Retarget, Target } from './kit/follow';
48
62
  export {
49
63
  parseSpaceCurve, parseSurface, spaceCurveError, spaceCurveParams, surfaceError, surfaceParams, EQUATION_KINDS, EQUATION_VARIABLES_3D,
50
64
  } from './kit/equation3d';
51
65
  export type { EquationLanguage, SpaceCurveEquation, SurfaceEquation } from './kit/equation3d';
52
- export { aim, graphSpace, spaceOf } from './three/space';
53
- export type { GraphSpace } from './three/space';
66
+ export { aim, graphEntry, graphOf, graphSpace, spaceOf, GRAPH_3D } from './three/space';
67
+ export type { Graph3D, GraphSpace } from './three/space';
68
+ export { domainEntry, meshEntries } from './three/entries';
54
69
  export { drawLabel3D } from './three/label3d';
55
70
  export type { LabelStyle3D } from './three/label3d';
56
- // A traced path is a plot whose span follows a value, so a copied one extends this rather than a copy of it.
57
- export { FunctionPlot } from './plots/function-plot';
58
- export type { FunctionPlotProps } from './plots/function-plot';
59
- export { FieldMark } from './kit/field-mark';
60
- export type { FieldProps, FieldSampler, FieldStroke } from './kit/field-mark';
71
+ // A traced path is a plot whose span follows a value, so a copied one builds on these rather than a copy of them.
72
+ export { FunctionPlot, PLOT_COMMANDS, PLOT_STATE, PLOT_STROKE, tracePlot } from './plots/function-plot';
73
+ export type { FunctionPlotProps, PlotValues } from './plots/function-plot';
74
+ export { drawField, FIELD_COMMANDS, FIELD_STATE, traceField } from './kit/field-mark';
75
+ export type { FieldDrawing, FieldProps, FieldSampler, FieldStroke, FieldValues } from './kit/field-mark';
61
76
  export { fieldError, fieldParams, parseField, FIELD_VARIABLES } from './kit/field';
62
77
  export type { FieldEquation } from './kit/field';
63
78
  export { traceContour } from './kit/contour';
64
79
  export { colorAt, fillAt, lerpFillScale, lerpScale, readFillScale, readScale } from './kit/scale';
65
- export { SYSTEMS, SYSTEMS_AND_PLOT } from './kit/placement';
66
80
  export {
67
81
  IDENTITY_MATRIX, isIdentityMatrix, lerpMatrix, lineThrough, readMatrix, transformedMap, warpedMap,
68
82
  } from './kit/coords';
@@ -0,0 +1,57 @@
1
+ import {
2
+ Paint,
3
+ Types,
4
+ type EntryOptions,
5
+ type RenderContext,
6
+ type Type,
7
+ // Named so the emitted declaration reaches these through the sdk, not core.
8
+ type Fill as _Fill,
9
+ type FillResolved as _FillResolved,
10
+ type Shadow as _Shadow,
11
+ type ShadowResolved as _ShadowResolved,
12
+ type Stroke as _Stroke,
13
+ type StrokeResolved as _StrokeResolved,
14
+ } from "@motionscript/sdk";
15
+
16
+ /**
17
+ * What a component in this kit shares beyond the factory: the paints a math
18
+ * node draws with, listed choices, and the order a node paints in.
19
+ */
20
+
21
+ const { overlay: _overlay, ...paint } = Paint.state;
22
+ const { overlayTo: _overlayTo, ...paintCommands } = Paint.commands;
23
+
24
+ /** A math node's paints: fill, stroke, shadow and the outline trim, with no overlay. */
25
+ export const PAINT = paint;
26
+ export const PAINT_COMMANDS = paintCommands;
27
+
28
+ /**
29
+ * One of `values`, held as text with its choices listed (no `kind: "enum"`).
30
+ */
31
+ export function listed<const V extends readonly string[]>(values: V, initial: V[number], options?: ListedOptions): Type<V[number]>;
32
+ /** With no default: an arg left out is one the command was not given. */
33
+ export function listed<const V extends readonly string[]>(values: V, initial: undefined, options?: ListedOptions): Type<V[number] | undefined>;
34
+ export function listed(values: readonly string[], initial: string | undefined, options: ListedOptions = {}): Type<string | undefined> {
35
+ return Types.custom<string | undefined, string | undefined, EntryOptions & { readonly options: readonly string[] }>(initial, { ...options, options: values });
36
+ }
37
+
38
+ interface ListedOptions {
39
+ readonly animatable?: boolean;
40
+ }
41
+
42
+ /** `record` without `keys`: the paints a node has no use for. */
43
+ export function omit<T extends object, const K extends keyof T>(record: T, ...keys: K[]): Omit<T, K> {
44
+ const out = { ...record };
45
+ for (const key of keys) delete out[key];
46
+ return out;
47
+ }
48
+
49
+ /**
50
+ * A node's paint order: its own paint under its children, then what it lays
51
+ * over them, each cut at the near plane of a space it crosses.
52
+ */
53
+ export function painted(ctx: RenderContext, own: () => void, over: () => void = () => { }): void {
54
+ ctx.inFront(own);
55
+ ctx.children();
56
+ ctx.inFront(over);
57
+ }
package/src/kit/curve.ts CHANGED
@@ -1,29 +1,47 @@
1
- import { findNode2D, type Node2D } from "@motionscript/sdk";
1
+ import type { NodeView } from "@motionscript/sdk";
2
+ import { equationParams, parseEquation } from "./equation";
3
+ import type { ExpressionScope } from "./expression";
4
+ import { scopeFor, type PlotSource } from "./source";
2
5
 
3
6
  /**
4
- * A node with a height at each `x`: what a point, a tangent or an area rides.
5
- * A protocol rather than a class, so a copied plot is ridden like the kit's own.
7
+ * A curve as a mark reads it: what a plot is drawn from, its own `x`/`y`, and
8
+ * whether a system laid it out, which is when those are a shift in graph units.
9
+ * Data rather than a class, so a copied plot is ridden like the kit's own.
6
10
  */
7
- export interface Curve {
8
- /** The curve's height at graph `x`, or `NaN` where it has none. */
9
- valueAt(x: number): number;
11
+ export interface CurveValues {
12
+ readonly expression: PlotSource;
13
+ readonly params: ExpressionScope;
14
+ readonly x: number;
15
+ readonly y: number;
16
+ /** The plane the plot was laid out on; `null` outside any system. */
17
+ readonly graph?: object | null;
10
18
  }
11
19
 
12
- /** The curve `id` names in `from`'s tree, or `null`. */
13
- export function curveNamed(from: Node2D, id: string): Curve | null {
14
- if (id === "") return null;
15
- const found = findNode2D(from, id) as (Node2D & Partial<Curve>) | null;
16
- return found && typeof found.valueAt === "function" ? (found as Node2D & Curve) : null;
17
- }
20
+ /** The node types a point, a tangent or an area can ride: they have a height at each `x`. */
21
+ export const CURVE_KEYS: readonly string[] = ["functionPlot", "tracedPath"];
18
22
 
19
23
  /**
20
- * The curve a mark reads: the one `on` names, or, naming none, the nearest
21
- * curve the mark sits inside. Putting an area in a plot is how it is told which.
24
+ * The curve's height at graph `x`, or `NaN` where it has none or is not a
25
+ * function of `x`. What a point, a tangent or an area riding a plot reads.
22
26
  */
23
- export function curveOf(mark: Node2D, on: string): Curve | null {
24
- if (on !== "") return curveNamed(mark, on);
25
- for (let at: Node2D | null = mark.parent; at; at = at.parent) {
26
- if (typeof (at as Partial<Curve>).valueAt === "function") return at as Node2D & Curve;
27
- }
28
- return null;
27
+ export function curveValue(curve: CurveValues, x: number): number {
28
+ const { source, blend } = curve.expression;
29
+ const shift = curve.graph ? curve : NO_SHIFT;
30
+ const value = heightOf(source, curve.params, x - shift.x) + shift.y;
31
+ if (!blend) return value;
32
+ const from = heightOf(blend.from, curve.params, x - shift.x) + shift.y;
33
+ return from + (value - from) * blend.t;
34
+ }
35
+
36
+ /** Whether `view` is a node a mark can ride. */
37
+ export function isCurve(view: unknown): view is NodeView & { readonly state: CurveValues } {
38
+ return typeof view === "object" && view !== null && CURVE_KEYS.includes((view as { key?: string }).key ?? "");
39
+ }
40
+
41
+ const NO_SHIFT = { x: 0, y: 0 };
42
+
43
+ function heightOf(source: string, params: ExpressionScope, x: number): number {
44
+ const equation = parseEquation(source, equationParams(source));
45
+ if (equation instanceof Error || equation.kind !== "function" || equation.of !== "x") return NaN;
46
+ return equation.f(x, 0, scopeFor(equationParams(source), params));
29
47
  }
@@ -1,25 +1,34 @@
1
1
  import {
2
- command,
3
2
  Graphics2D,
4
- property,
5
- withdraw,
6
- type Command,
7
- type CommandArgs,
3
+ Types,
8
4
  type Fill,
9
5
  type FillResolved,
10
6
  type PaintProps,
11
- type RenderContext2D,
7
+ type RenderContext,
12
8
  type StrokeResolved,
13
9
  type Vector2,
10
+ // Named so the emitted declaration reaches these through the sdk, not core.
11
+ type Shadow as _Shadow,
12
+ type ShadowResolved as _ShadowResolved,
13
+ type Stroke as _Stroke,
14
14
  } from "@motionscript/sdk";
15
+ import { createBoundedCache } from "./cache";
16
+ import { omit, PAINT, PAINT_COMMANDS } from "./component";
15
17
  import type { CoordinateMap } from "./coords";
16
18
  import type { ExpressionScope } from "./expression";
17
19
  import { fieldParams, parseField } from "./field";
18
- import { boundsOfRuns, runsToPath, stated } from "./geometry";
19
- import { Mark } from "./mark";
20
+ import { runsToPath } from "./geometry";
21
+ import { PLANE_ENTRY } from "./graph";
22
+ import { drawMark, type MarkPaint, type MarkReading } from "./mark";
20
23
  import { fillAt, lerpFillScale, readFillScale } from "./scale";
21
24
  import { lerpParams, lerpSource, readSource, scopeFor, type PlotSource } from "./source";
22
- import { FILL_PAINT, OUTLINE_TRIM } from "./withdrawn";
25
+
26
+ /**
27
+ * What a mark drawn from a vector field, `(P(x, y), Q(x, y))`, shares: arrows,
28
+ * or the lines that follow it, all in one stroke. Without strength fills it is
29
+ * one path in the stroke's paint; with them, what it draws is sorted by the
30
+ * field's strength into a few shades.
31
+ */
23
32
 
24
33
  export interface FieldProps extends PaintProps {
25
34
  expression?: string | PlotSource;
@@ -40,116 +49,123 @@ export interface FieldStroke {
40
49
  /** How many shades the strength fills are drawn in: each is one path. */
41
50
  const SHADES = 12;
42
51
 
43
- /**
44
- * A mark drawn from a vector field, `(P(x, y), Q(x, y))`: arrows, or the lines
45
- * that follow it, all in one stroke. Without strength fills it is one path in
46
- * the stroke's paint; with them, what it draws is sorted by the field's
47
- * strength into a few shades.
48
- */
49
- @withdraw(...FILL_PAINT, ...OUTLINE_TRIM)
50
- export abstract class FieldMark<P extends FieldProps = FieldProps> extends Mark<P> {
51
- @property({ default: "", mapper: readSource, tween: lerpSource, kind: "fieldEquation" })
52
- declare expression: PlotSource;
53
-
52
+ /** A field mark's entries. It is only strokes, revealed its own way: it has no fill and trims no outline. */
53
+ export const FIELD_STATE = {
54
+ ...omit(PAINT, "fill", "start", "end"),
55
+ expression: Types.custom<string | PlotSource, PlotSource>("", { resolve: readSource, lerp: lerpSource, kind: "fieldEquation" }),
54
56
  /** The equation's named values: any name in it that is not `x`, `y`, a function or a constant. */
55
- @property({ default: {}, tween: lerpParams }) declare params: ExpressionScope;
56
-
57
+ params: Types.custom<ExpressionScope>({}, { lerp: lerpParams }),
57
58
  /** Paints by the field's strength, weakest first. Empty draws everything in the stroke's own paint. */
58
- @property({ default: [], mapper: readFillScale, tween: lerpFillScale, kind: "vector" }) declare strengthFills: Fill[];
59
+ strengthFills: Types.custom<Fill[], FillResolved[][]>([], { resolve: readFillScale, lerp: lerpFillScale, kind: "vector" }),
59
60
  /** The strength the last paint stands for; zero takes the strongest the field is where it is drawn. */
60
- @property({ default: 0, min: 0, step: 0.1 }) declare maxMagnitude: number;
61
+ maxMagnitude: Types.number(0, { min: 0, step: 0.1 }),
62
+ graph: PLANE_ENTRY,
63
+ };
61
64
 
65
+ export const FIELD_COMMANDS = {
66
+ ...omit(PAINT_COMMANDS, "fillTo", "progressTo", "flash"),
62
67
  /** Change the field, each vector turning from the one into the other. */
63
- @command({ args: ["expression"] })
64
- morphTo(args: CommandArgs<{ expression: string }> & { duration: number }): Command<P> {
65
- return this.to({ data: stated(args.data) as Partial<P>, duration: args.duration, easing: args.easing });
66
- }
67
-
68
+ morphTo: { args: ["expression"] as const },
68
69
  /** Animate the equation's named values. */
69
- @command({ args: ["params"] })
70
- paramsTo(args: CommandArgs<{ params: ExpressionScope }> & { duration: number }): Command<P> {
71
- return this.to({ data: stated(args.data) as Partial<P>, duration: args.duration, easing: args.easing });
72
- }
70
+ paramsTo: { args: ["params"] as const },
71
+ };
73
72
 
74
- /** What is drawn of `field` through `map`. */
75
- protected abstract strokes(map: CoordinateMap, field: FieldSampler): FieldStroke[];
73
+ /** What a field mark is drawn from. */
74
+ export interface FieldValues {
75
+ readonly expression: PlotSource;
76
+ readonly params: ExpressionScope;
77
+ }
76
78
 
77
- /** The props of its own that change what {@link strokes} returns. */
78
- protected abstract own(): string;
79
+ /** What one kind of field mark draws of `field` through `map`, and the entries of its own that change it. */
80
+ export interface FieldDrawing<S> {
81
+ strokes(s: S, map: CoordinateMap, field: FieldSampler): FieldStroke[];
82
+ /** The entries of its own that change what `strokes` returns, as a key. */
83
+ own(s: S): string;
84
+ }
79
85
 
80
- protected trace(map: CoordinateMap): Vector2[][] {
81
- const { area } = map;
82
- if (!Number.isFinite(area.maxX - area.minX)) return [];
83
- const { source, blend } = this.expression;
84
- const shift = this.shift();
85
- const key = [source, blend?.from, blend?.t, map.revision, JSON.stringify(this.params), shift.x, shift.y, this.own()].join("|");
86
- if (key !== this._key) {
87
- this._key = key;
88
- const field = this._sampler(shift);
89
- this._drawn = field ? this.strokes(map, field) : [];
90
- this._runs = this._drawn.flatMap((stroke) => stroke.runs);
91
- }
92
- return this._runs;
93
- }
86
+ interface Traced {
87
+ readonly drawn: FieldStroke[];
88
+ readonly runs: Vector2[][];
89
+ }
94
90
 
95
- protected override renderInk(ctx: RenderContext2D): void {
96
- const scale = this.strengthFills as unknown as FillResolved[][];
97
- const stroke = this.stroke as StrokeResolved[];
98
- const ink = scale.length > 0 && stroke.length > 0 ? boundsOfRuns(this._runs) : null;
99
- if (!ink) {
100
- super.renderInk(ctx);
101
- return;
102
- }
103
- const top = this.maxMagnitude > 0 ? this.maxMagnitude : this._drawn.reduce((most, drawn) => Math.max(most, drawn.magnitude), 0);
104
- const shades: Vector2[][][] = Array.from({ length: SHADES }, () => []);
105
- for (const drawn of this._drawn) {
106
- const share = top > 0 ? Math.min(1, drawn.magnitude / top) : 0;
107
- shades[Math.min(SHADES - 1, Math.floor(share * SHADES))].push(...drawn.runs);
108
- }
109
- shades.forEach((runs, index) => {
110
- if (runs.length === 0) return;
111
- const fill = fillAt(scale, (index + 0.5) / SHADES);
112
- ctx.draw(
113
- new Graphics2D()
114
- .path({ data: runsToPath(runs), centerBounds: [ink.minX, ink.minY, ink.maxX, ink.maxY], start: this.start, end: this.end })
115
- .shadow(this.shadow)
116
- .stroke(stroke.map((layer) => ({ ...layer, fill }))),
117
- );
118
- });
119
- }
91
+ const traces = createBoundedCache<Traced>(64);
92
+ const EMPTY: Traced = { drawn: [], runs: [] };
93
+
94
+ /** What a field mark of `kind` draws through `read`: each stroke with its strength, and all their runs. */
95
+ export function traceField<S extends FieldValues>(kind: string, drawing: FieldDrawing<S>, s: S, { map, shift }: MarkReading): Traced {
96
+ const { area } = map;
97
+ if (!Number.isFinite(area.maxX - area.minX)) return EMPTY;
98
+ const { source, blend } = s.expression;
99
+ const key = [kind, source, blend?.from, blend?.t, map.revision, JSON.stringify(s.params), shift.x, shift.y, drawing.own(s)].join("|");
100
+ const known = traces.get(key);
101
+ if (known) return known;
102
+ const field = sampler(s, shift);
103
+ const drawn = field ? drawing.strokes(s, map, field) : [];
104
+ const traced = { drawn, runs: drawn.flatMap((stroke) => stroke.runs) };
105
+ traces.set(key, traced);
106
+ return traced;
107
+ }
120
108
 
121
- private _key = "";
122
- private _drawn: FieldStroke[] = [];
123
- private _runs: Vector2[][] = [];
109
+ /** A field mark's paint: one stroke, or a shade per strength when it has strength fills. */
110
+ export function drawField(
111
+ ctx: RenderContext,
112
+ s: MarkPaint & { readonly strengthFills: FillResolved[][]; readonly maxMagnitude: number },
113
+ { drawn, runs }: Traced,
114
+ ): void {
115
+ const scale = s.strengthFills;
116
+ const stroke = s.stroke as StrokeResolved[];
117
+ const shaded = scale.length > 0 && stroke.length > 0;
118
+ drawMark(ctx, s, runs, {
119
+ ink: shaded
120
+ ? (ctx, ink) => {
121
+ const top = s.maxMagnitude > 0 ? s.maxMagnitude : drawn.reduce((most, d) => Math.max(most, d.magnitude), 0);
122
+ const shades: Vector2[][][] = Array.from({ length: SHADES }, () => []);
123
+ for (const d of drawn) {
124
+ const share = top > 0 ? Math.min(1, d.magnitude / top) : 0;
125
+ shades[Math.min(SHADES - 1, Math.floor(share * SHADES))].push(...d.runs);
126
+ }
127
+ shades.forEach((shade, index) => {
128
+ if (shade.length === 0) return;
129
+ const fill = fillAt(scale, (index + 0.5) / SHADES);
130
+ ctx.draw(
131
+ new Graphics2D()
132
+ .path({ data: runsToPath(shade), centerBounds: [ink.minX, ink.minY, ink.maxX, ink.maxY], start: s.start ?? 0, end: s.end ?? 1 })
133
+ .shadow(s.shadow)
134
+ .stroke(stroke.map((layer) => ({ ...layer, fill }))),
135
+ );
136
+ });
137
+ }
138
+ : undefined,
139
+ });
140
+ }
124
141
 
125
- /** The field at a graph point, measured from this mark's own shift, mid-morph a blend of two. */
126
- private _sampler(shift: Vector2): FieldSampler | null {
127
- const { source, blend } = this.expression;
128
- const to = this._field(source, shift);
129
- if (!to || !blend) return to;
130
- const from = this._field(blend.from, shift);
131
- if (!from) return to;
132
- const start: Vector2 = { x: 0, y: 0 };
133
- return (x, y, out) => {
134
- if (!from(x, y, start) || !to(x, y, out)) return false;
135
- out.x = start.x + (out.x - start.x) * blend.t;
136
- out.y = start.y + (out.y - start.y) * blend.t;
137
- return true;
138
- };
139
- }
142
+ /** The field at a graph point, measured from the mark's own shift, mid-morph a blend of two. */
143
+ function sampler(s: FieldValues, shift: Vector2): FieldSampler | null {
144
+ const { source, blend } = s.expression;
145
+ const to = fieldOf(source, s.params, shift);
146
+ if (!to || !blend) return to;
147
+ const from = fieldOf(blend.from, s.params, shift);
148
+ if (!from) return to;
149
+ const start: Vector2 = { x: 0, y: 0 };
150
+ return (x, y, out) => {
151
+ if (!from(x, y, start) || !to(x, y, out)) return false;
152
+ out.x = start.x + (out.x - start.x) * blend.t;
153
+ out.y = start.y + (out.y - start.y) * blend.t;
154
+ return true;
155
+ };
156
+ }
140
157
 
141
- private _field(source: string, shift: Vector2): FieldSampler | null {
142
- const names = fieldParams(source);
143
- const field = parseField(source, names);
144
- if (field instanceof Error) return null;
145
- const params = scopeFor(names, this.params);
146
- const { x, y } = field;
147
- return (gx, gy, out) => {
148
- const vx = x(gx - shift.x, gy - shift.y, params);
149
- const vy = y(gx - shift.x, gy - shift.y, params);
150
- out.x = vx;
151
- out.y = vy;
152
- return Number.isFinite(vx) && Number.isFinite(vy);
153
- };
154
- }
158
+ function fieldOf(source: string, params: ExpressionScope, shift: Vector2): FieldSampler | null {
159
+ const names = fieldParams(source);
160
+ const field = parseField(source, names);
161
+ if (field instanceof Error) return null;
162
+ const scope = scopeFor(names, params);
163
+ const { x, y } = field;
164
+ return (gx, gy, out) => {
165
+ const vx = x(gx - shift.x, gy - shift.y, scope);
166
+ const vy = y(gx - shift.x, gy - shift.y, scope);
167
+ out.x = vx;
168
+ out.y = vy;
169
+ return Number.isFinite(vx) && Number.isFinite(vy);
170
+ };
155
171
  }