@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
@@ -1,29 +1,24 @@
1
1
  import {
2
- colorProperty,
3
- command,
2
+ component2d,
4
3
  Graphics2D,
5
- node,
6
- PaintNode,
7
- pathOps,
8
- property,
9
- strokeProperty,
10
- withdraw,
11
- type AssetScope,
12
- type BoxBounds,
4
+ Types,
13
5
  type Color,
14
- type Command,
15
- type CommandArgs,
16
- type NodeConfig,
6
+ type NodeOf,
17
7
  type NormalizedColor,
18
8
  type PaintProps,
19
9
  type PathCommand,
20
- type RenderContext2D,
21
- type Size2D,
22
- type Stroke,
10
+ // Named so the emitted declaration reaches these through the sdk, not core.
11
+ type Node2D as _Node2D,
12
+ type Node2DProps as _Node2DProps,
13
+ type Shadow as _Shadow,
14
+ type ShadowResolved as _ShadowResolved,
15
+ type Stroke as _Stroke,
16
+ type StrokeResolved as _StrokeResolved,
23
17
  } from "@motionscript/sdk";
24
- import { declareFormulas, drawLabels } from "../kit/furniture";
25
- import { stated } from "../kit/geometry";
26
- import { FILL_PAINT } from "../kit/withdrawn";
18
+ import { listed, omit, PAINT, PAINT_COMMANDS, painted } from "../kit/component";
19
+ import { outlineBounds, OWN_FRAME } from "../kit/follow";
20
+ import { drawLabels, loadFormulas } from "../kit/furniture";
21
+ import { strokeShape } from "../kit/mark";
27
22
 
28
23
  export interface AngleMarkProps extends PaintProps {
29
24
  fromAngle?: number;
@@ -36,7 +31,6 @@ export interface AngleMarkProps extends PaintProps {
36
31
  fontFamily?: string;
37
32
  }
38
33
 
39
- const OWN_FRAME: [number, number, number, number] = [-1, -1, 1, 1];
40
34
  /** Degrees of arc per drawn segment. */
41
35
  const ARC_STEP = 4;
42
36
  /** How close to a right angle `auto` draws the square. */
@@ -47,101 +41,97 @@ const point = (radius: number, degrees: number) => {
47
41
  return { x: radius * Math.cos(turn), y: radius * Math.sin(turn) };
48
42
  };
49
43
 
44
+ const state = {
45
+ // It is only an outline.
46
+ ...omit(PAINT, "fill"),
47
+ fromAngle: Types.number(0, { unit: "°" }),
48
+ toAngle: Types.number(60, { unit: "°" }),
49
+ radius: Types.number(48, { min: 0, unit: "px" }),
50
+ /** `auto` draws the square where the angle is a right one. */
51
+ corner: listed(["auto", "arc", "square"], "auto", { animatable: false }),
52
+ /** Text, a formula between dollar signs, or `°` for the angle's own size. */
53
+ label: Types.text("", { animatable: false }),
54
+ labelSize: Types.number(24, { min: 1, unit: "px" }),
55
+ labelColor: Types.color("#e5e7eb"),
56
+ fontFamily: Types.text("", { animatable: false }),
57
+ /** The angle marked, in degrees: positive counter-clockwise. A formula reads it back as `[key.value]`. */
58
+ value: Types.number(0, { private: true }),
59
+ };
60
+
61
+ interface AngleValues {
62
+ readonly fromAngle: number;
63
+ readonly toAngle: number;
64
+ readonly radius: number;
65
+ readonly corner: "arc" | "square" | "auto";
66
+ }
67
+
50
68
  /**
51
69
  * The mark of an angle at the node's own position: an arc from one direction
52
70
  * to another, or the small square of a right angle, with an optional label.
53
71
  * Directions are degrees counter-clockwise from the right, so binding one to a
54
72
  * variable opens and closes the angle. A label of `°` shows the angle's size.
55
73
  */
56
- @node({
74
+ export const AngleMark = component2d({
57
75
  key: "angleMark",
58
- parentKey: "shape",
59
- forkable: true,
60
- layout: { children: "freeform", defaultWidthMode: "hug", defaultHeightMode: "hug", acceptsChildren: false },
61
- })
62
- @withdraw(...FILL_PAINT)
63
- export class AngleMark extends PaintNode<AngleMarkProps> {
64
- @property({ default: 0, unit: "°" }) declare fromAngle: number;
65
- @property({ default: 60, unit: "°" }) declare toAngle: number;
66
- @property({ default: 48, min: 0, unit: "px" }) declare radius: number;
67
- /** `auto` draws the square where the angle is a right one. */
68
- @property({ default: "auto", options: ["auto", "arc", "square"], animatable: false }) declare corner: "arc" | "square" | "auto";
69
-
70
- /** Text, a formula between dollar signs, or `°` for the angle's own size. */
71
- @property({ default: "", animatable: false }) declare label: string;
72
- @property({ default: 24, min: 1, unit: "px" }) declare labelSize: number;
73
- @colorProperty({ default: "#e5e7eb" }) declare labelColor: Color;
74
- @property({ default: "", animatable: false }) declare fontFamily: string;
75
-
76
- @strokeProperty({ default: { weight: 3, fill: "#e5e7eb", align: "center", cap: "round", join: "round" } })
77
- declare stroke: Stroke;
78
-
79
- constructor(props?: NodeConfig<AngleMark, AngleMarkProps>) {
80
- super(props ?? {});
81
- }
76
+ state,
77
+ defaults: { stroke: { weight: 3, fill: "#e5e7eb", align: "center", cap: "round", join: "round" }, width: "hug", height: "hug", value: (s) => angleOf(s) },
78
+ load: (loader, s) => {
79
+ if (s.label !== "" && s.fontFamily !== "") loader.font(s.fontFamily);
80
+ loadFormulas(loader, [s.label]);
81
+ },
82
+ commands: {
83
+ ...omit(PAINT_COMMANDS, "fillTo"),
84
+ /** Grow or shrink the mark. */
85
+ radiusTo: { args: ["radius"] },
86
+ },
87
+ measure: () => ({ width: 0, height: 0 }),
88
+ bounds: (_size, s) => outlineBounds(outline(s)),
89
+ render: (ctx, s) => {
90
+ const data = outline(s);
91
+ const shape = data.length > 0 ? () => new Graphics2D().path({ data, centerBounds: OWN_FRAME, start: s.start, end: s.end }) : null;
92
+ painted(
93
+ ctx,
94
+ () => {
95
+ if (shape) ctx.draw(shape().shadow(s.shadow).fill([]));
96
+ if (s.label === "" || s.radius <= 0) return;
97
+ const text = s.label === "°" ? `${Math.round(Math.abs(s.value) * 10) / 10}°` : s.label;
98
+ const at = point(s.radius + s.labelSize * 0.9, (s.fromAngle + s.toAngle) / 2);
99
+ const style = { fontSize: s.labelSize, fontFamily: s.fontFamily, color: s.labelColor as NormalizedColor };
100
+ ctx.draw(drawLabels([{ text, at, align: "center" }], style));
101
+ },
102
+ () => {
103
+ if (shape) strokeShape(ctx, shape, s);
104
+ },
105
+ );
106
+ },
107
+ });
82
108
 
83
- /** Grow or shrink the mark. */
84
- @command({ args: ["radius"] })
85
- radiusTo(args: CommandArgs<{ radius: number }> & { duration: number }): Command<AngleMarkProps> {
86
- return this.to({ data: stated(args.data), duration: args.duration, easing: args.easing });
87
- }
88
-
89
- /** The angle marked, in degrees: positive counter-clockwise. */
90
- get value(): number {
91
- return this.toAngle - this.fromAngle;
92
- }
93
-
94
- override declareAssets(assets: AssetScope): void {
95
- super.declareAssets(assets);
96
- if (this.label !== "" && this.fontFamily !== "") assets.font(this.fontFamily);
97
- declareFormulas(assets, [this.label]);
98
- }
99
-
100
- override measure(): Partial<Size2D> {
101
- return { width: 0, height: 0 };
102
- }
103
-
104
- protected override shapeGraphics(): Graphics2D | null {
105
- const data = this._outline();
106
- return data.length === 0 ? null : new Graphics2D().path({ data, centerBounds: OWN_FRAME, start: this.start, end: this.end });
107
- }
109
+ export type AngleMark = NodeOf<typeof AngleMark>;
108
110
 
109
- protected override renderSelf(ctx: RenderContext2D): void {
110
- super.renderSelf(ctx);
111
- if (this.label === "" || this.radius <= 0) return;
112
- const text = this.label === "°" ? `${Math.round(Math.abs(this.value) * 10) / 10}°` : this.label;
113
- const at = point(this.radius + this.labelSize * 0.9, (this.fromAngle + this.toAngle) / 2);
114
- const style = { fontSize: this.labelSize, fontFamily: this.fontFamily, color: this.labelColor as NormalizedColor };
115
- for (const part of drawLabels([{ text, at, align: "center" }], style)) ctx.draw(part);
116
- }
111
+ /** The angle a mark marks, in degrees: positive counter-clockwise. */
112
+ export function angleOf(s: Pick<AngleValues, "fromAngle" | "toAngle">): number {
113
+ return s.toAngle - s.fromAngle;
114
+ }
117
115
 
118
- override _localBounds(): BoxBounds {
119
- const ink = pathOps.bounds(this._outline());
120
- if (!ink) return { x: 0, y: 0, width: 0, height: 0 };
121
- return { x: (ink.minX + ink.maxX) / 2, y: (ink.minY + ink.maxY) / 2, width: ink.maxX - ink.minX, height: ink.maxY - ink.minY };
116
+ function outline({ fromAngle, toAngle, radius, corner }: AngleValues): PathCommand[] {
117
+ const sweep = toAngle - fromAngle;
118
+ if (radius <= 0 || sweep === 0) return [];
119
+ const square = corner === "square" || (corner === "auto" && Math.abs(Math.abs(sweep) - 90) < RIGHT_SLOP);
120
+ if (square) {
121
+ const side = radius * 0.6;
122
+ const a = point(side, fromAngle);
123
+ const b = point(side, toAngle);
124
+ return [
125
+ { type: "M", x: a.x, y: a.y },
126
+ { type: "L", x: a.x + b.x, y: a.y + b.y },
127
+ { type: "L", x: b.x, y: b.y },
128
+ ];
122
129
  }
123
-
124
- private _outline(): PathCommand[] {
125
- const { fromAngle, toAngle, radius } = this;
126
- const sweep = toAngle - fromAngle;
127
- if (radius <= 0 || sweep === 0) return [];
128
- const square = this.corner === "square" || (this.corner === "auto" && Math.abs(Math.abs(sweep) - 90) < RIGHT_SLOP);
129
- if (square) {
130
- const side = radius * 0.6;
131
- const a = point(side, fromAngle);
132
- const b = point(side, toAngle);
133
- return [
134
- { type: "M", x: a.x, y: a.y },
135
- { type: "L", x: a.x + b.x, y: a.y + b.y },
136
- { type: "L", x: b.x, y: b.y },
137
- ];
138
- }
139
- const steps = Math.max(2, Math.ceil(Math.abs(sweep) / ARC_STEP));
140
- const out: PathCommand[] = [];
141
- for (let i = 0; i <= steps; i++) {
142
- const p = point(radius, fromAngle + (sweep * i) / steps);
143
- out.push({ type: i === 0 ? "M" : "L", x: p.x, y: p.y });
144
- }
145
- return out;
130
+ const steps = Math.max(2, Math.ceil(Math.abs(sweep) / ARC_STEP));
131
+ const out: PathCommand[] = [];
132
+ for (let i = 0; i <= steps; i++) {
133
+ const p = point(radius, fromAngle + (sweep * i) / steps);
134
+ out.push({ type: i === 0 ? "M" : "L", x: p.x, y: p.y });
146
135
  }
136
+ return out;
147
137
  }
@@ -1,22 +1,29 @@
1
1
  import {
2
- colorProperty,
3
- node,
4
- property,
5
- strokeProperty,
6
- vector2Property,
7
- type AssetScope,
2
+ component2d,
3
+ Graphics2D,
4
+ Types,
8
5
  type Color,
9
- type NodeConfig,
10
- type NodeHandle,
6
+ type NodeOf,
7
+ type NodeView,
11
8
  type NormalizedColor,
12
9
  type PathCommand,
13
- type RenderContext2D,
14
- type Stroke,
15
10
  type Vector2,
11
+ // Named so the emitted declaration reaches these through the sdk, not core.
12
+ type Node2D as _Node2D,
13
+ type Node2DProps as _Node2DProps,
14
+ type Shadow as _Shadow,
15
+ type ShadowResolved as _ShadowResolved,
16
+ type Stroke as _Stroke,
17
+ type StrokeResolved as _StrokeResolved,
16
18
  } from "@motionscript/sdk";
17
19
  import { braceOutline } from "../kit/brace";
18
- import { add, between, followed, FollowMark, heading, type FollowProps, type Quad } from "../kit/follow";
19
- import { declareFormulas, drawLabels } from "../kit/furniture";
20
+ import { listed, painted } from "../kit/component";
21
+ import {
22
+ add, between, container, FOLLOW_COMMANDS, FOLLOW_STATE, followedQuad, heading, outlineBounds, OWN_FRAME, resting,
23
+ type FollowProps, type FollowValues, type Quad,
24
+ } from "../kit/follow";
25
+ import { drawLabels, loadFormulas } from "../kit/furniture";
26
+ import { strokeShape } from "../kit/mark";
20
27
 
21
28
  export type BraceSide = "below" | "above" | "left" | "right";
22
29
 
@@ -40,87 +47,102 @@ const EDGE: Record<BraceSide, [number, number]> = {
40
47
  right: [1, 2],
41
48
  };
42
49
 
43
- /**
44
- * A curly brace along one side of a node or of a part of a formula, or between
45
- * two points when it names neither, with an optional label at its tip. A label
46
- * written between dollar signs is typeset.
47
- */
48
- @node({
49
- key: "curlyBrace",
50
- parentKey: "shape",
51
- forkable: true,
52
- layout: { children: "freeform", defaultWidthMode: "hug", defaultHeightMode: "hug", acceptsChildren: false },
53
- })
54
- export class CurlyBrace extends FollowMark<CurlyBraceProps> {
55
- @property({ default: "below", options: ["below", "above", "left", "right"], animatable: false }) declare side: BraceSide;
50
+ const state = {
51
+ ...FOLLOW_STATE,
52
+ side: listed(["below", "above", "left", "right"], "below", { animatable: false }),
56
53
  /** The two ends of a brace that follows nothing, from the brace's own position. It bulges to the left of tail to head. */
57
- @vector2Property({ default: { x: -80, y: 0 } }) declare tail: Vector2;
58
- @vector2Property({ default: { x: 80, y: 0 } }) declare head: Vector2;
54
+ tail: Types.vector2({ x: -80, y: 0 }),
55
+ head: Types.vector2({ x: 80, y: 0 }),
59
56
  /** The room between the target and the brace. (`gap` is a layout's.) */
60
- @property({ default: 8, min: 0, unit: "px" }) declare clearance: number;
57
+ clearance: Types.number(8, { min: 0, unit: "px" }),
61
58
  /** How far the brace reaches from its ends to its tip. (`depth` is a node's place in 3D.) */
62
- @property({ default: 18, min: 2, unit: "px" }) declare reach: number;
63
-
64
- @property({ default: "", animatable: false }) declare label: string;
65
- @property({ default: 28, min: 1, unit: "px" }) declare labelSize: number;
66
- @colorProperty({ default: "#e5e7eb" }) declare labelColor: Color;
67
- @property({ default: "", animatable: false }) declare fontFamily: string;
68
-
69
- @strokeProperty({ default: { weight: 3, fill: "#e5e7eb", align: "center", cap: "round", join: "round" } })
70
- declare stroke: Stroke;
71
-
72
- constructor(props?: NodeConfig<CurlyBrace, CurlyBraceProps>) {
73
- super(props ?? {});
74
- }
59
+ reach: Types.number(18, { min: 2, unit: "px" }),
60
+ label: Types.text("", { animatable: false }),
61
+ labelSize: Types.number(28, { min: 1, unit: "px" }),
62
+ labelColor: Types.color("#e5e7eb"),
63
+ fontFamily: Types.text("", { animatable: false }),
64
+ };
75
65
 
76
- override declareAssets(assets: AssetScope): void {
77
- super.declareAssets(assets);
78
- if (this.label !== "" && this.fontFamily !== "") assets.font(this.fontFamily);
79
- declareFormulas(assets, [this.label]);
80
- }
66
+ interface BraceValues extends FollowValues {
67
+ readonly side: BraceSide;
68
+ readonly tail: Vector2;
69
+ readonly head: Vector2;
70
+ readonly clearance: number;
71
+ readonly reach: number;
72
+ }
81
73
 
82
- protected override quad(): Quad | null {
83
- // No target: the span itself, as an edge with nothing above it.
84
- return this._spans() ? [this.head, this.tail, this.tail, this.head] : super.quad();
85
- }
74
+ /** Whether the brace follows nothing, and so spans its own two points. */
75
+ function spans(s: BraceValues, parted: NodeView | null): boolean {
76
+ return !s.retarget && resting(s, parted) === null;
77
+ }
86
78
 
87
- /** Whether the brace follows nothing, and so spans its own two points. */
88
- private _spans(): boolean {
89
- return !this.retarget && followed(this, this.on) === null;
90
- }
79
+ /** No target: the span itself, as an edge with nothing above it. */
80
+ function quadOf(s: BraceValues, parted: NodeView | null): Quad | null {
81
+ return spans(s, parted) ? [s.head, s.tail, s.tail, s.head] : followedQuad(s, parted);
82
+ }
91
83
 
92
- protected outline(quad: Quad): PathCommand[] {
93
- const { from, to, out } = this._span(quad);
94
- return braceOutline(from, to, out, this.reach);
95
- }
84
+ /** The edge the brace runs along, pushed out by the gap, and the way it bulges. */
85
+ function edgeOf(s: BraceValues, parted: NodeView | null, quad: Quad): { from: Vector2; to: Vector2; out: Vector2 } {
86
+ const [i, j] = spans(s, parted) ? [1, 0] : EDGE[s.side];
87
+ const { unit } = heading(quad[i], quad[j]);
88
+ // Left of the edge's own direction, in a y-up frame.
89
+ const out = { x: -unit.y, y: unit.x };
90
+ return { from: add(quad[i], out, s.clearance), to: add(quad[j], out, s.clearance), out };
91
+ }
96
92
 
97
- protected override renderSelf(ctx: RenderContext2D): void {
98
- super.renderSelf(ctx);
99
- if (this.label === "") return;
100
- const quad = this.quad();
101
- if (!quad) return;
102
- const { from, to, out } = this._span(quad);
103
- const reach = Math.min(this.reach, heading(from, to).length / 2) + this.labelSize * 0.75;
104
- const at = add(between(from, to), out, reach);
105
- const align = out.x > 0.5 ? "left" : out.x < -0.5 ? "right" : "center";
106
- const style = { fontSize: this.labelSize, fontFamily: this.fontFamily, color: this.labelColor as NormalizedColor };
107
- for (const part of drawLabels([{ text: this.label, at, align }], style)) ctx.draw(part);
108
- }
93
+ function outline(s: BraceValues, parted: NodeView | null): PathCommand[] {
94
+ const quad = quadOf(s, parted);
95
+ if (!quad) return [];
96
+ const { from, to, out } = edgeOf(s, parted, quad);
97
+ return braceOutline(from, to, out, s.reach);
98
+ }
109
99
 
110
- /** The edge the brace runs along, pushed out by the gap, and the way it bulges. */
111
- private _span(quad: Quad): { from: Vector2; to: Vector2; out: Vector2 } {
112
- const [i, j] = this._spans() ? [1, 0] : EDGE[this.side];
113
- const { unit } = heading(quad[i], quad[j]);
114
- // Left of the edge's own direction, in a y-up frame.
115
- const out = { x: -unit.y, y: unit.x };
116
- return { from: add(quad[i], out, this.clearance), to: add(quad[j], out, this.clearance), out };
117
- }
100
+ /** One of the two ends to drag, while the brace spans them. */
101
+ const end = (which: "tail" | "head") => ({
102
+ at: (s: BraceValues) => (spans(s, container(null, s)) ? s[which] : null),
103
+ value: (at: Vector2) => ({ x: at.x, y: at.y }),
104
+ });
118
105
 
119
- override _handles(): readonly NodeHandle[] {
120
- return this._spans() ? [{ prop: "tail", at: this.tail }, { prop: "head", at: this.head }] : [];
121
- }
106
+ /**
107
+ * A curly brace along one side of a node or of a part of a formula, or between
108
+ * two points when it names neither, with an optional label at its tip. A label
109
+ * written between dollar signs is typeset.
110
+ */
111
+ export const CurlyBrace = component2d({
112
+ key: "curlyBrace",
113
+ state,
114
+ defaults: { stroke: { weight: 3, fill: "#e5e7eb", align: "center", cap: "round", join: "round" }, width: "hug", height: "hug" },
115
+ load: (loader, s) => {
116
+ if (s.label !== "" && s.fontFamily !== "") loader.font(s.fontFamily);
117
+ loadFormulas(loader, [s.label]);
118
+ },
119
+ commands: FOLLOW_COMMANDS,
120
+ handles: { tail: end("tail"), head: end("head") },
121
+ measure: () => ({ width: 0, height: 0 }),
122
+ bounds: (_size, s) => outlineBounds(outline(s, container(null, s))),
123
+ render: (ctx, s) => {
124
+ const parted = container(ctx, s);
125
+ const data = outline(s, parted);
126
+ const shape = data.length > 0 ? () => new Graphics2D().path({ data, centerBounds: OWN_FRAME, start: s.start, end: s.end }) : null;
127
+ painted(
128
+ ctx,
129
+ () => {
130
+ if (shape) ctx.draw(shape().shadow(s.shadow).fill([]));
131
+ if (s.label === "") return;
132
+ const quad = quadOf(s, parted);
133
+ if (!quad) return;
134
+ const { from, to, out } = edgeOf(s, parted, quad);
135
+ const reach = Math.min(s.reach, heading(from, to).length / 2) + s.labelSize * 0.75;
136
+ const at = add(between(from, to), out, reach);
137
+ const align = out.x > 0.5 ? "left" : out.x < -0.5 ? "right" : "center";
138
+ const style = { fontSize: s.labelSize, fontFamily: s.fontFamily, color: s.labelColor as NormalizedColor };
139
+ ctx.draw(drawLabels([{ text: s.label, at, align }], style));
140
+ },
141
+ () => {
142
+ if (shape) strokeShape(ctx, shape, s);
143
+ },
144
+ );
145
+ },
146
+ });
122
147
 
123
- override _handleValue(prop: string, at: Vector2): Vector2 | null {
124
- return this._spans() && (prop === "tail" || prop === "head") ? { x: at.x, y: at.y } : null;
125
- }
126
- }
148
+ export type CurlyBrace = NodeOf<typeof CurlyBrace>;
@@ -1,13 +1,24 @@
1
1
  import {
2
- node,
3
- property,
4
- strokeProperty,
5
- type NodeConfig,
2
+ component2d,
3
+ Graphics2D,
4
+ Types,
5
+ type NodeOf,
6
+ type NodeView,
6
7
  type PathCommand,
7
- type Stroke,
8
8
  type Vector2,
9
+ // Named so the emitted declaration reaches these through the sdk, not core.
10
+ type Node2D as _Node2D,
11
+ type Node2DProps as _Node2DProps,
12
+ type Shadow as _Shadow,
13
+ type ShadowResolved as _ShadowResolved,
14
+ type Stroke as _Stroke,
15
+ type StrokeResolved as _StrokeResolved,
9
16
  } from "@motionscript/sdk";
10
- import { add, between, FollowMark, grown, heading, type FollowProps, type Quad } from "../kit/follow";
17
+ import { listed, painted } from "../kit/component";
18
+ import {
19
+ add, between, container, FOLLOW_COMMANDS, FOLLOW_STATE, followedQuad, grown, heading, outlineBounds, OWN_FRAME, type FollowProps, type FollowValues, type Quad,
20
+ } from "../kit/follow";
21
+ import { strokeShape } from "../kit/mark";
11
22
 
12
23
  export type EmphasisShape = "box" | "circle" | "underline" | "cross";
13
24
 
@@ -61,29 +72,47 @@ const SHAPES: Record<EmphasisShape, (quad: Quad, gap: number) => PathCommand[]>
61
72
  },
62
73
  };
63
74
 
75
+ const state = {
76
+ ...FOLLOW_STATE,
77
+ shape: listed(["box", "circle", "underline", "cross"], "box", { animatable: false }),
78
+ /** The room between the target and the mark. (`gap` is a layout's.) */
79
+ clearance: Types.number(10, { unit: "px" }),
80
+ };
81
+
82
+ interface EmphasisValues extends FollowValues {
83
+ readonly shape: EmphasisShape;
84
+ readonly clearance: number;
85
+ }
86
+
87
+ function outline(s: EmphasisValues, parted: NodeView | null): PathCommand[] {
88
+ const quad = followedQuad(s, parted);
89
+ return quad ? SHAPES[s.shape](quad, s.clearance) : [];
90
+ }
91
+
64
92
  /**
65
93
  * A box, a circle, an underline or a cross drawn on a node, or on one part of
66
94
  * a formula. `progressTo` draws it on, and `retargetTo` slides it to another.
67
95
  */
68
- @node({
96
+ export const EmphasisMark = component2d({
69
97
  key: "emphasisMark",
70
- parentKey: "shape",
71
- forkable: true,
72
- layout: { children: "freeform", defaultWidthMode: "hug", defaultHeightMode: "hug", acceptsChildren: false },
73
- })
74
- export class EmphasisMark extends FollowMark<EmphasisMarkProps> {
75
- @property({ default: "box", options: ["box", "circle", "underline", "cross"], animatable: false }) declare shape: EmphasisShape;
76
- /** The room between the target and the mark. (`gap` is a layout's.) */
77
- @property({ default: 10, unit: "px" }) declare clearance: number;
78
-
79
- @strokeProperty({ default: { weight: 3, fill: "#facc15", align: "center", cap: "round", join: "round" } })
80
- declare stroke: Stroke;
81
-
82
- constructor(props?: NodeConfig<EmphasisMark, EmphasisMarkProps>) {
83
- super(props ?? {});
84
- }
98
+ state,
99
+ defaults: { stroke: { weight: 3, fill: "#facc15", align: "center", cap: "round", join: "round" }, width: "hug", height: "hug" },
100
+ commands: FOLLOW_COMMANDS,
101
+ measure: () => ({ width: 0, height: 0 }),
102
+ bounds: (_size, s) => outlineBounds(outline(s, container(null, s))),
103
+ render: (ctx, s) => {
104
+ const data = outline(s, container(ctx, s));
105
+ const shape = data.length > 0 ? () => new Graphics2D().path({ data, centerBounds: OWN_FRAME, start: s.start, end: s.end }) : null;
106
+ painted(
107
+ ctx,
108
+ () => {
109
+ if (shape) ctx.draw(shape().shadow(s.shadow).fill([]));
110
+ },
111
+ () => {
112
+ if (shape) strokeShape(ctx, shape, s);
113
+ },
114
+ );
115
+ },
116
+ });
85
117
 
86
- protected outline(quad: Quad): PathCommand[] {
87
- return SHAPES[this.shape](quad, this.clearance);
88
- }
89
- }
118
+ export type EmphasisMark = NodeOf<typeof EmphasisMark>;