@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
@@ -1,137 +1,137 @@
1
- import {
2
- component2d,
3
- Graphics2D,
4
- Types,
5
- type Color,
6
- type NodeOf,
7
- type NormalizedColor,
8
- type PaintProps,
9
- type PathCommand,
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,
17
- } from "@motionscript/sdk";
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";
22
-
23
- export interface AngleMarkProps extends PaintProps {
24
- fromAngle?: number;
25
- toAngle?: number;
26
- radius?: number;
27
- corner?: "arc" | "square" | "auto";
28
- label?: string;
29
- labelSize?: number;
30
- labelColor?: Color;
31
- fontFamily?: string;
32
- }
33
-
34
- /** Degrees of arc per drawn segment. */
35
- const ARC_STEP = 4;
36
- /** How close to a right angle `auto` draws the square. */
37
- const RIGHT_SLOP = 0.5;
38
-
39
- const point = (radius: number, degrees: number) => {
40
- const turn = (degrees * Math.PI) / 180;
41
- return { x: radius * Math.cos(turn), y: radius * Math.sin(turn) };
42
- };
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
-
68
- /**
69
- * The mark of an angle at the node's own position: an arc from one direction
70
- * to another, or the small square of a right angle, with an optional label.
71
- * Directions are degrees counter-clockwise from the right, so binding one to a
72
- * variable opens and closes the angle. A label of `°` shows the angle's size.
73
- */
74
- export const AngleMark = component2d({
75
- key: "angleMark",
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
- });
108
-
109
- export type AngleMark = NodeOf<typeof AngleMark>;
110
-
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
- }
115
-
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
- ];
129
- }
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 });
135
- }
136
- return out;
137
- }
1
+ import {
2
+ component2d,
3
+ Graphics2D,
4
+ Types,
5
+ type Color,
6
+ type NodeOf,
7
+ type NormalizedColor,
8
+ type PaintProps,
9
+ type PathCommand,
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,
17
+ } from "@motionscript/sdk";
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";
22
+
23
+ export interface AngleMarkProps extends PaintProps {
24
+ fromAngle?: number;
25
+ toAngle?: number;
26
+ radius?: number;
27
+ corner?: "arc" | "square" | "auto";
28
+ label?: string;
29
+ labelSize?: number;
30
+ labelColor?: Color;
31
+ fontFamily?: string;
32
+ }
33
+
34
+ /** Degrees of arc per drawn segment. */
35
+ const ARC_STEP = 4;
36
+ /** How close to a right angle `auto` draws the square. */
37
+ const RIGHT_SLOP = 0.5;
38
+
39
+ const point = (radius: number, degrees: number) => {
40
+ const turn = (degrees * Math.PI) / 180;
41
+ return { x: radius * Math.cos(turn), y: radius * Math.sin(turn) };
42
+ };
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
+
68
+ /**
69
+ * The mark of an angle at the node's own position: an arc from one direction
70
+ * to another, or the small square of a right angle, with an optional label.
71
+ * Directions are degrees counter-clockwise from the right, so binding one to a
72
+ * variable opens and closes the angle. A label of `°` shows the angle's size.
73
+ */
74
+ export const AngleMark = component2d({
75
+ key: "angleMark",
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
+ });
108
+
109
+ export type AngleMark = NodeOf<typeof AngleMark>;
110
+
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
+ }
115
+
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
+ ];
129
+ }
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 });
135
+ }
136
+ return out;
137
+ }
@@ -1,148 +1,148 @@
1
- import {
2
- component2d,
3
- Graphics2D,
4
- Types,
5
- type Color,
6
- type NodeOf,
7
- type NodeView,
8
- type NormalizedColor,
9
- type PathCommand,
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,
18
- } from "@motionscript/sdk";
19
- import { braceOutline } from "../kit/brace";
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";
27
-
28
- export type BraceSide = "below" | "above" | "left" | "right";
29
-
30
- export interface CurlyBraceProps extends FollowProps {
31
- side?: BraceSide;
32
- tail?: Vector2;
33
- head?: Vector2;
34
- clearance?: number;
35
- reach?: number;
36
- label?: string;
37
- labelSize?: number;
38
- labelColor?: Color;
39
- fontFamily?: string;
40
- }
41
-
42
- /** The edge of a target a brace runs along, as two corner indexes of its quad, read with the brace's bulge to the left. */
43
- const EDGE: Record<BraceSide, [number, number]> = {
44
- below: [2, 3],
45
- above: [0, 1],
46
- left: [3, 0],
47
- right: [1, 2],
48
- };
49
-
50
- const state = {
51
- ...FOLLOW_STATE,
52
- side: listed(["below", "above", "left", "right"], "below", { animatable: false }),
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. */
54
- tail: Types.vector2({ x: -80, y: 0 }),
55
- head: Types.vector2({ x: 80, y: 0 }),
56
- /** The room between the target and the brace. (`gap` is a layout's.) */
57
- clearance: Types.number(8, { min: 0, unit: "px" }),
58
- /** How far the brace reaches from its ends to its tip. (`depth` is a node's place in 3D.) */
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
- };
65
-
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
- }
73
-
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
- }
78
-
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
- }
83
-
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
- }
92
-
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
- }
99
-
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
- });
105
-
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
- });
147
-
148
- export type CurlyBrace = NodeOf<typeof CurlyBrace>;
1
+ import {
2
+ component2d,
3
+ Graphics2D,
4
+ Types,
5
+ type Color,
6
+ type NodeOf,
7
+ type NodeView,
8
+ type NormalizedColor,
9
+ type PathCommand,
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,
18
+ } from "@motionscript/sdk";
19
+ import { braceOutline } from "../kit/brace";
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";
27
+
28
+ export type BraceSide = "below" | "above" | "left" | "right";
29
+
30
+ export interface CurlyBraceProps extends FollowProps {
31
+ side?: BraceSide;
32
+ tail?: Vector2;
33
+ head?: Vector2;
34
+ clearance?: number;
35
+ reach?: number;
36
+ label?: string;
37
+ labelSize?: number;
38
+ labelColor?: Color;
39
+ fontFamily?: string;
40
+ }
41
+
42
+ /** The edge of a target a brace runs along, as two corner indexes of its quad, read with the brace's bulge to the left. */
43
+ const EDGE: Record<BraceSide, [number, number]> = {
44
+ below: [2, 3],
45
+ above: [0, 1],
46
+ left: [3, 0],
47
+ right: [1, 2],
48
+ };
49
+
50
+ const state = {
51
+ ...FOLLOW_STATE,
52
+ side: listed(["below", "above", "left", "right"], "below", { animatable: false }),
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. */
54
+ tail: Types.vector2({ x: -80, y: 0 }),
55
+ head: Types.vector2({ x: 80, y: 0 }),
56
+ /** The room between the target and the brace. (`gap` is a layout's.) */
57
+ clearance: Types.number(8, { min: 0, unit: "px" }),
58
+ /** How far the brace reaches from its ends to its tip. (`depth` is a node's place in 3D.) */
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
+ };
65
+
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
+ }
73
+
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
+ }
78
+
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
+ }
83
+
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
+ }
92
+
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
+ }
99
+
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
+ });
105
+
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
+ });
147
+
148
+ export type CurlyBrace = NodeOf<typeof CurlyBrace>;