@motionscript/math 0.1.0-alpha.8 → 0.1.0

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 (158) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/LICENSE +201 -201
  3. package/NOTICE +49 -49
  4. package/README.md +390 -391
  5. package/dist/annotate/angle-mark.js.map +1 -1
  6. package/dist/annotate/curly-brace.js.map +1 -1
  7. package/dist/annotate/emphasis-mark.js.map +1 -1
  8. package/dist/browser/chunks/{chunk-FETQZEN2.js → chunk-IRZCG6PM.js} +2 -2
  9. package/dist/browser/chunks/chunk-IRZCG6PM.js.map +7 -0
  10. package/dist/browser/chunks/{chunk-K77KLQX4.js → chunk-WMVW6ZIV.js} +2 -2
  11. package/dist/browser/chunks/chunk-WMVW6ZIV.js.map +7 -0
  12. package/dist/browser/chunks/{mathjax-DNP3YRES.js → mathjax-WV55AC2P.js} +1 -1
  13. package/dist/browser/chunks/{mathjax-DNP3YRES.js.map → mathjax-WV55AC2P.js.map} +1 -1
  14. package/dist/browser/index.js +1 -1
  15. package/dist/browser/index.js.map +1 -1
  16. package/dist/browser/kit.js +1 -1
  17. package/dist/browser/manifest.json +1 -1
  18. package/dist/browser/tex.js +1 -1
  19. package/dist/browser/tex.js.map +1 -1
  20. package/dist/engine.d.ts +1 -1
  21. package/dist/engine.js +1 -1
  22. package/dist/engine.js.map +1 -1
  23. package/dist/index.js.map +1 -1
  24. package/dist/kit/brace.js.map +1 -1
  25. package/dist/kit/cache.js.map +1 -1
  26. package/dist/kit/component.d.ts.map +1 -1
  27. package/dist/kit/component.js.map +1 -1
  28. package/dist/kit/contour.js.map +1 -1
  29. package/dist/kit/coords.js.map +1 -1
  30. package/dist/kit/curve.js.map +1 -1
  31. package/dist/kit/equation.js.map +1 -1
  32. package/dist/kit/equation3d.js.map +1 -1
  33. package/dist/kit/expression.js.map +1 -1
  34. package/dist/kit/field-mark.d.ts.map +1 -1
  35. package/dist/kit/field-mark.js.map +1 -1
  36. package/dist/kit/field.js.map +1 -1
  37. package/dist/kit/follow.d.ts.map +1 -1
  38. package/dist/kit/follow.js.map +1 -1
  39. package/dist/kit/furniture.js.map +1 -1
  40. package/dist/kit/geometry.js.map +1 -1
  41. package/dist/kit/graph.js.map +1 -1
  42. package/dist/kit/mark.js.map +1 -1
  43. package/dist/kit/part-fills.js.map +1 -1
  44. package/dist/kit/place.js.map +1 -1
  45. package/dist/kit/sampler.js.map +1 -1
  46. package/dist/kit/scale.js.map +1 -1
  47. package/dist/kit/source.js.map +1 -1
  48. package/dist/kit/system.js.map +1 -1
  49. package/dist/kit/ticks.js.map +1 -1
  50. package/dist/kit/withdrawn.js.map +1 -1
  51. package/dist/marks/curve-area.js.map +1 -1
  52. package/dist/marks/drop-lines.js.map +1 -1
  53. package/dist/marks/plot-point.js.map +1 -1
  54. package/dist/marks/riemann-sum.js.map +1 -1
  55. package/dist/marks/tangent-line.js.map +1 -1
  56. package/dist/marks/vector-arrow.js.map +1 -1
  57. package/dist/nodes.js.map +1 -1
  58. package/dist/plots/function-plot.d.ts.map +1 -1
  59. package/dist/plots/function-plot.js.map +1 -1
  60. package/dist/plots/points-plot.js.map +1 -1
  61. package/dist/plots/stream-lines.js.map +1 -1
  62. package/dist/plots/traced-path.js.map +1 -1
  63. package/dist/plots/vector-field.js.map +1 -1
  64. package/dist/structures/graph-layout.js.map +1 -1
  65. package/dist/structures/network-graph.js.map +1 -1
  66. package/dist/structures/sample-space.js.map +1 -1
  67. package/dist/systems/axes2d.d.ts.map +1 -1
  68. package/dist/systems/axes2d.js.map +1 -1
  69. package/dist/systems/number-line.js.map +1 -1
  70. package/dist/systems/number-plane.js.map +1 -1
  71. package/dist/systems/polar-plane.js.map +1 -1
  72. package/dist/tex/geometry.js.map +1 -1
  73. package/dist/tex/index.js.map +1 -1
  74. package/dist/tex/mathjax.js.map +1 -1
  75. package/dist/tex/matrix.js.map +1 -1
  76. package/dist/tex/node.js.map +1 -1
  77. package/dist/tex/tween.js.map +1 -1
  78. package/dist/tex/typeset.d.ts.map +1 -1
  79. package/dist/tex/typeset.js.map +1 -1
  80. package/dist/three/axes3d.js.map +1 -1
  81. package/dist/three/curve-plot3d.js.map +1 -1
  82. package/dist/three/entries.js.map +1 -1
  83. package/dist/three/label3d.js.map +1 -1
  84. package/dist/three/plot-point3d.js.map +1 -1
  85. package/dist/three/space.js.map +1 -1
  86. package/dist/three/surface-plot3d.js.map +1 -1
  87. package/dist/three/vector-arrow3d.js.map +1 -1
  88. package/package.json +4 -4
  89. package/registry.json +251 -251
  90. package/src/annotate/angle-mark.ts +137 -137
  91. package/src/annotate/curly-brace.ts +148 -148
  92. package/src/annotate/emphasis-mark.ts +118 -118
  93. package/src/engine.ts +86 -86
  94. package/src/index.ts +29 -29
  95. package/src/kit/brace.ts +37 -37
  96. package/src/kit/cache.ts +61 -61
  97. package/src/kit/component.ts +57 -57
  98. package/src/kit/contour.ts +94 -94
  99. package/src/kit/coords.ts +278 -278
  100. package/src/kit/curve.ts +47 -47
  101. package/src/kit/equation.ts +181 -181
  102. package/src/kit/equation3d.ts +137 -137
  103. package/src/kit/expression.ts +159 -159
  104. package/src/kit/field-mark.ts +171 -171
  105. package/src/kit/field.ts +50 -50
  106. package/src/kit/follow.ts +229 -229
  107. package/src/kit/furniture.ts +113 -113
  108. package/src/kit/geometry.ts +49 -49
  109. package/src/kit/graph.ts +116 -116
  110. package/src/kit/mark.ts +152 -152
  111. package/src/kit/part-fills.ts +61 -61
  112. package/src/kit/place.ts +77 -77
  113. package/src/kit/sampler.ts +281 -281
  114. package/src/kit/scale.ts +39 -39
  115. package/src/kit/source.ts +57 -57
  116. package/src/kit/system.ts +105 -105
  117. package/src/kit/ticks.ts +85 -85
  118. package/src/kit/withdrawn.ts +8 -8
  119. package/src/marks/curve-area.ts +89 -89
  120. package/src/marks/drop-lines.ts +71 -71
  121. package/src/marks/plot-point.ts +102 -102
  122. package/src/marks/riemann-sum.ts +99 -99
  123. package/src/marks/tangent-line.ts +119 -119
  124. package/src/marks/vector-arrow.ts +98 -98
  125. package/src/nodes.ts +99 -99
  126. package/src/plots/function-plot.ts +220 -220
  127. package/src/plots/points-plot.ts +103 -103
  128. package/src/plots/stream-lines.ts +160 -160
  129. package/src/plots/traced-path.ts +70 -70
  130. package/src/plots/vector-field.ts +107 -107
  131. package/src/structures/graph-layout.ts +145 -145
  132. package/src/structures/network-graph.ts +291 -291
  133. package/src/structures/sample-space.ts +210 -210
  134. package/src/systems/axes2d.ts +226 -226
  135. package/src/systems/number-line.ts +130 -130
  136. package/src/systems/number-plane.ts +232 -232
  137. package/src/systems/polar-plane.ts +177 -177
  138. package/src/tex/geometry.ts +703 -703
  139. package/src/tex/index.ts +15 -15
  140. package/src/tex/mathjax.ts +82 -82
  141. package/src/tex/matrix.ts +97 -97
  142. package/src/tex/node.ts +74 -74
  143. package/src/tex/tween.ts +308 -308
  144. package/src/tex/typeset.ts +461 -461
  145. package/src/three/axes3d.ts +267 -267
  146. package/src/three/curve-plot3d.ts +146 -146
  147. package/src/three/entries.ts +42 -42
  148. package/src/three/label3d.ts +52 -52
  149. package/src/three/plot-point3d.ts +63 -63
  150. package/src/three/space.ts +92 -92
  151. package/src/three/surface-plot3d.ts +318 -318
  152. package/src/three/vector-arrow3d.ts +88 -88
  153. package/dist/browser/chunks/chunk-FETQZEN2.js.map +0 -7
  154. package/dist/browser/chunks/chunk-K77KLQX4.js.map +0 -7
  155. package/dist/kit/placement.d.ts +0 -8
  156. package/dist/kit/placement.d.ts.map +0 -1
  157. package/dist/kit/placement.js +0 -8
  158. package/dist/kit/placement.js.map +0 -1
@@ -1,267 +1,267 @@
1
- import {
2
- component2d,
3
- Flow,
4
- Geo,
5
- Graphics3D,
6
- Mat,
7
- Paint,
8
- provides,
9
- Shapes,
10
- Types,
11
- View3D,
12
- type Canvas3DProps,
13
- type Color,
14
- type NodeOf,
15
- type Scene3D,
16
- type Transform3D,
17
- type Values,
18
- type Vector3,
19
- type Vector3Input,
20
- // Named so the emitted declaration reaches these through the sdk, not core.
21
- type Alignment as _Alignment,
22
- type CornerRadiusResolved as _CornerRadiusResolved,
23
- type CornerStyleResolved as _CornerStyleResolved,
24
- type Fill as _Fill,
25
- type FillResolved as _FillResolved,
26
- type FlowMode as _FlowMode,
27
- type FlowResolved as _FlowResolved,
28
- type GapSize as _GapSize,
29
- type Node2D as _Node2D,
30
- type Node2DProps as _Node2DProps,
31
- type NormalizedColor as _NormalizedColor,
32
- type PostEffect3D as _PostEffect3D,
33
- type RectCornerRadius as _RectCornerRadius,
34
- type RectCornerStyle as _RectCornerStyle,
35
- type Shadow as _Shadow,
36
- type ShadowResolved as _ShadowResolved,
37
- type Shadows3D as _Shadows3D,
38
- type Stroke as _Stroke,
39
- type StrokeResolved as _StrokeResolved,
40
- type ToneMapping3D as _ToneMapping3D,
41
- type Vector2 as _Vector2,
42
- } from "@motionscript/sdk";
43
- import type { Interval } from "../kit/coords";
44
- import { AXIS_COLOR, lerpInterval, readInterval } from "../kit/furniture";
45
- import { clamp } from "../kit/geometry";
46
- import { formatTick, ticksFor } from "../kit/ticks";
47
- import { drawLabel3D, type LabelStyle3D } from "./label3d";
48
- import { GRAPH_3D, graphSpace, type Graph3D, type GraphSpace } from "./space";
49
-
50
- export interface Axes3dProps extends Canvas3DProps {
51
- xRange?: Interval;
52
- yRange?: Interval;
53
- zRange?: Interval;
54
- xLength?: number;
55
- yLength?: number;
56
- zLength?: number;
57
- xStep?: number;
58
- yStep?: number;
59
- zStep?: number;
60
- color?: Color;
61
- thickness?: number;
62
- tips?: boolean;
63
- ticks?: boolean;
64
- tickSize?: number;
65
- grid?: boolean;
66
- gridLineColor?: Color;
67
- labels?: boolean;
68
- labelSize?: number;
69
- labelColor?: Color;
70
- fontFamily?: string;
71
- xLabel?: string;
72
- yLabel?: string;
73
- zLabel?: string;
74
- }
75
-
76
- /** How many pixels of a 2D axis one scene unit counts as, when an axis picks its own tick spacing. */
77
- const UNIT_AS_PIXELS = 64;
78
-
79
- type AxisName = "x" | "y" | "z";
80
-
81
- /** The turn that lays a shape built along the scene's +y along each graph axis. */
82
- const ALONG: Record<AxisName, Vector3Input> = { x: [90, 0, 0], y: [0, 0, -90], z: [0, 0, 0] };
83
-
84
- /** The graph axis each axis's tick marks lie along. */
85
- const TICK_ALONG: Record<AxisName, AxisName> = { x: "y", y: "x", z: "y" };
86
-
87
- function range(initial: Interval) {
88
- return Types.custom<Interval>(initial, { resolve: readInterval, lerp: lerpInterval });
89
- }
90
-
91
- /** The axes' own entries, beside a 3D canvas's. */
92
- const own = {
93
- xRange: range([-3, 3]),
94
- yRange: range([-3, 3]),
95
- zRange: range([-2, 2]),
96
- /** How long each axis is drawn, in scene units. */
97
- xLength: Types.number(6, { min: 0.01, step: 0.1 }),
98
- yLength: Types.number(6, { min: 0.01, step: 0.1 }),
99
- zLength: Types.number(4, { min: 0.01, step: 0.1 }),
100
- /** The spacing of each axis's ticks; zero picks a tidy one for the axis's length. */
101
- xStep: Types.number(0, { min: 0 }),
102
- yStep: Types.number(0, { min: 0 }),
103
- zStep: Types.number(0, { min: 0 }),
104
- color: Types.color(AXIS_COLOR),
105
- /** The radius of an axis line, in scene units. */
106
- thickness: Types.number(0.02, { min: 0, step: 0.005 }),
107
- tips: Types.boolean(true, { animatable: false }),
108
- ticks: Types.boolean(true, { animatable: false }),
109
- tickSize: Types.number(0.16, { min: 0, step: 0.01 }),
110
- /** Lines across the plane the x and y axes lie in, at each of their ticks. */
111
- grid: Types.boolean(false, { animatable: false }),
112
- /** (`gridColor` is the viewport's own reference grid.) */
113
- gridLineColor: Types.color("#374151"),
114
- /** A number at each tick. */
115
- labels: Types.boolean(false, { animatable: false }),
116
- /** The height of a label's text, in scene units. */
117
- labelSize: Types.number(0.26, { min: 0.01, step: 0.01 }),
118
- labelColor: Types.color(AXIS_COLOR),
119
- fontFamily: Types.text("", { animatable: false }),
120
- /** A title at the positive end of each axis. */
121
- xLabel: Types.text("x", { animatable: false }),
122
- yLabel: Types.text("y", { animatable: false }),
123
- zLabel: Types.text("z", { animatable: false }),
124
- };
125
-
126
- type AxesValues = Values<typeof own>;
127
-
128
- /**
129
- * A 3D graph: three axes through a box of graph coordinates, z up, seen through
130
- * a camera of its own, so it stands on a 2D stage as it is, the way `axes2d`
131
- * does. Its 3D children sit in its graph coordinates: a surface, a space curve,
132
- * a point, an arrow, or any 3D node. `orbitTo` swings the camera round it.
133
- */
134
- export const Axes3d = component2d({
135
- key: "axes3d",
136
- state: { ...Paint.state, ...Flow.state, ...Shapes.corners, ...View3D.state, ...own },
137
- defaults: { width: "fill", height: "fill" },
138
- commands: {
139
- ...Paint.commands,
140
- ...View3D.commands,
141
- /** Animate the window of values the axes show; everything drawn in them follows. */
142
- rangeTo: { args: ["xRange", "yRange", "zRange"] },
143
- },
144
- allowedChildren: ["2d", "3d"],
145
- load: (loader, s) => {
146
- View3D.load(loader, furnish(s));
147
- if (s.fontFamily !== "") loader.font(s.fontFamily);
148
- },
149
- provide: (s) => [provides(GRAPH_3D, graphOfAxes(s))],
150
- space: (s) => axesSpace(s),
151
- measure: (measurer, s, children) => Flow.measure(measurer, s, children, Paint.inset(s)),
152
- layout: (box, s, children) => Flow.layout(box, s, children, Paint.inset(s)),
153
- render: (ctx, s) => View3D.draw(ctx, s, furnish(s)),
154
- });
155
-
156
- export type Axes3d = NodeOf<typeof Axes3d>;
157
-
158
- /** The axes' own drawing, added to the scene: what `render` draws and `load` declares. */
159
- function furnish(s: AxesValues): (scene: Scene3D) => void {
160
- return (scene) => scene.draw(furniture(s));
161
- }
162
-
163
- /** The last space worked out for each axes, kept while its ranges and lengths are unchanged. */
164
- const spaces = new WeakMap<object, { readonly key: string; readonly space: GraphSpace }>();
165
-
166
- /** The coordinates of a 3D graph: what its children's `x`, `y` and `z` are in. */
167
- function axesSpace(s: AxesValues): GraphSpace {
168
- const { xRange, yRange, zRange, xLength, yLength, zLength } = s;
169
- const key = [xRange, yRange, zRange, xLength, yLength, zLength].join("|");
170
- let kept = spaces.get(s);
171
- if (kept?.key !== key) spaces.set(s, (kept = { key, space: graphSpace(xRange, yRange, zRange, { x: xLength, y: yLength, z: zLength }) }));
172
- return kept.space;
173
- }
174
-
175
- // One per axes, so the context its subtree holds is the same from frame to frame.
176
- const graphs = new WeakMap<object, Graph3D>();
177
-
178
- function graphOfAxes(s: AxesValues): Graph3D {
179
- let graph = graphs.get(s);
180
- if (!graph) graphs.set(s, (graph = { space: () => axesSpace(s) }));
181
- return graph;
182
- }
183
-
184
- /** The axes, their ticks, numbers and titles, and the grid, in the scene's units. */
185
- function furniture(s: AxesValues): Graphics3D {
186
- const space = axesSpace(s);
187
- const g3 = new Graphics3D();
188
- const ranges: Record<AxisName, Interval> = { x: space.xRange, y: space.yRange, z: space.zRange };
189
- const lengths: Record<AxisName, number> = { x: s.xLength, y: s.yLength, z: s.zLength };
190
- const steps: Record<AxisName, number> = { x: s.xStep, y: s.yStep, z: s.zStep };
191
- const titles: Record<AxisName, string> = { x: s.xLabel, y: s.yLabel, z: s.zLabel };
192
- // Where each axis is crossed by the other two: their zero, or the nearest end of a range without one.
193
- const cross: Record<AxisName, number> = {
194
- x: clamp(0, ranges.x[0], ranges.x[1]),
195
- y: clamp(0, ranges.y[0], ranges.y[1]),
196
- z: clamp(0, ranges.z[0], ranges.z[1]),
197
- };
198
- const at = (axis: AxisName, value: number, shift: Partial<Record<AxisName, number>> = {}): Vector3 => {
199
- const point = { ...cross, [axis]: value };
200
- const local = space.toLocal(point.x, point.y, point.z);
201
- const moved = space.offset(0, 0, 0);
202
- for (const key of ["x", "y", "z"] as const) {
203
- const by = shift[key];
204
- if (by === undefined) continue;
205
- // A shift is in scene units, along the graph axis it names.
206
- if (key === "x") moved.z += by;
207
- else if (key === "y") moved.x += by;
208
- else moved.y += by;
209
- }
210
- return { x: local.x + moved.x, y: local.y + moved.y, z: local.z + moved.z };
211
- };
212
-
213
- const { thickness, tickSize } = s;
214
- const paint = Mat.basic({ color: s.color });
215
- const style: LabelStyle3D = { size: s.labelSize, color: s.labelColor, fontFamily: s.fontFamily };
216
- const tipLength = s.tips ? thickness * 9 : 0;
217
- const marks: Transform3D[] = [];
218
- const plane: Vector3[] = [];
219
-
220
- for (const axis of ["x", "y", "z"] as const) {
221
- const [min, max] = ranges[axis];
222
- const length = lengths[axis];
223
- if (!(max > min) || !(length > 0)) continue;
224
-
225
- if (thickness > 0) {
226
- const from = at(axis, min);
227
- const to = at(axis, max);
228
- g3.mesh(Geo.cylinder({ radius: thickness, height: length, segments: [12, 1] }), paint, {
229
- position: { x: (from.x + to.x) / 2, y: (from.y + to.y) / 2, z: (from.z + to.z) / 2 },
230
- rotation: ALONG[axis],
231
- });
232
- if (s.tips) {
233
- g3.mesh(Geo.cone({ radius: thickness * 3.2, height: tipLength, segments: [16, 1] }), paint, {
234
- position: at(axis, max, { [axis]: tipLength / 2 }),
235
- rotation: ALONG[axis],
236
- });
237
- }
238
- }
239
-
240
- const ticks = ticksFor(ranges[axis], steps[axis], length * UNIT_AS_PIXELS);
241
- const side = TICK_ALONG[axis];
242
- for (const value of ticks.values) {
243
- // The crossing is marked by the other axes, and the far end by the tip.
244
- if (Math.abs(value - cross[axis]) < 1e-9 || (s.tips && Math.abs(value - max) < 1e-9)) continue;
245
- if (s.ticks && tickSize > 0) marks.push({ position: at(axis, value), rotation: ALONG[side] });
246
- if (s.labels) {
247
- drawLabel3D(g3, formatTick(value, ticks.step), at(axis, value, { [side]: -(tickSize / 2 + style.size * 0.8) }), style);
248
- }
249
- }
250
- drawLabel3D(g3, titles[axis], at(axis, max, { [axis]: tipLength + style.size * 0.9 }), style);
251
-
252
- if (s.grid && axis !== "z") {
253
- const other = axis === "x" ? "y" : "x";
254
- for (const value of ticks.values) {
255
- const a = { x: 0, y: 0, z: cross.z, [axis]: value, [other]: ranges[other][0] };
256
- const b = { ...a, [other]: ranges[other][1] };
257
- plane.push(space.toLocal(a.x, a.y, a.z), space.toLocal(b.x, b.y, b.z));
258
- }
259
- }
260
- }
261
-
262
- if (marks.length > 0) {
263
- g3.instances(Geo.cylinder({ radius: Math.max(thickness * 0.7, 0.008), height: tickSize, segments: [8, 1] }), paint, marks);
264
- }
265
- if (plane.length > 0) g3.line({ points: plane, segments: true, stroke: { fill: s.gridLineColor } });
266
- return g3;
267
- }
1
+ import {
2
+ component2d,
3
+ Flow,
4
+ Geo,
5
+ Graphics3D,
6
+ Mat,
7
+ Paint,
8
+ provides,
9
+ Shapes,
10
+ Types,
11
+ View3D,
12
+ type Canvas3DProps,
13
+ type Color,
14
+ type NodeOf,
15
+ type Scene3D,
16
+ type Transform3D,
17
+ type Values,
18
+ type Vector3,
19
+ type Vector3Input,
20
+ // Named so the emitted declaration reaches these through the sdk, not core.
21
+ type Alignment as _Alignment,
22
+ type CornerRadiusResolved as _CornerRadiusResolved,
23
+ type CornerStyleResolved as _CornerStyleResolved,
24
+ type Fill as _Fill,
25
+ type FillResolved as _FillResolved,
26
+ type FlowMode as _FlowMode,
27
+ type FlowResolved as _FlowResolved,
28
+ type GapSize as _GapSize,
29
+ type Node2D as _Node2D,
30
+ type Node2DProps as _Node2DProps,
31
+ type NormalizedColor as _NormalizedColor,
32
+ type PostEffect3D as _PostEffect3D,
33
+ type RectCornerRadius as _RectCornerRadius,
34
+ type RectCornerStyle as _RectCornerStyle,
35
+ type Shadow as _Shadow,
36
+ type ShadowResolved as _ShadowResolved,
37
+ type Shadows3D as _Shadows3D,
38
+ type Stroke as _Stroke,
39
+ type StrokeResolved as _StrokeResolved,
40
+ type ToneMapping3D as _ToneMapping3D,
41
+ type Vector2 as _Vector2,
42
+ } from "@motionscript/sdk";
43
+ import type { Interval } from "../kit/coords";
44
+ import { AXIS_COLOR, lerpInterval, readInterval } from "../kit/furniture";
45
+ import { clamp } from "../kit/geometry";
46
+ import { formatTick, ticksFor } from "../kit/ticks";
47
+ import { drawLabel3D, type LabelStyle3D } from "./label3d";
48
+ import { GRAPH_3D, graphSpace, type Graph3D, type GraphSpace } from "./space";
49
+
50
+ export interface Axes3dProps extends Canvas3DProps {
51
+ xRange?: Interval;
52
+ yRange?: Interval;
53
+ zRange?: Interval;
54
+ xLength?: number;
55
+ yLength?: number;
56
+ zLength?: number;
57
+ xStep?: number;
58
+ yStep?: number;
59
+ zStep?: number;
60
+ color?: Color;
61
+ thickness?: number;
62
+ tips?: boolean;
63
+ ticks?: boolean;
64
+ tickSize?: number;
65
+ grid?: boolean;
66
+ gridLineColor?: Color;
67
+ labels?: boolean;
68
+ labelSize?: number;
69
+ labelColor?: Color;
70
+ fontFamily?: string;
71
+ xLabel?: string;
72
+ yLabel?: string;
73
+ zLabel?: string;
74
+ }
75
+
76
+ /** How many pixels of a 2D axis one scene unit counts as, when an axis picks its own tick spacing. */
77
+ const UNIT_AS_PIXELS = 64;
78
+
79
+ type AxisName = "x" | "y" | "z";
80
+
81
+ /** The turn that lays a shape built along the scene's +y along each graph axis. */
82
+ const ALONG: Record<AxisName, Vector3Input> = { x: [90, 0, 0], y: [0, 0, -90], z: [0, 0, 0] };
83
+
84
+ /** The graph axis each axis's tick marks lie along. */
85
+ const TICK_ALONG: Record<AxisName, AxisName> = { x: "y", y: "x", z: "y" };
86
+
87
+ function range(initial: Interval) {
88
+ return Types.custom<Interval>(initial, { resolve: readInterval, lerp: lerpInterval });
89
+ }
90
+
91
+ /** The axes' own entries, beside a 3D canvas's. */
92
+ const own = {
93
+ xRange: range([-3, 3]),
94
+ yRange: range([-3, 3]),
95
+ zRange: range([-2, 2]),
96
+ /** How long each axis is drawn, in scene units. */
97
+ xLength: Types.number(6, { min: 0.01, step: 0.1 }),
98
+ yLength: Types.number(6, { min: 0.01, step: 0.1 }),
99
+ zLength: Types.number(4, { min: 0.01, step: 0.1 }),
100
+ /** The spacing of each axis's ticks; zero picks a tidy one for the axis's length. */
101
+ xStep: Types.number(0, { min: 0 }),
102
+ yStep: Types.number(0, { min: 0 }),
103
+ zStep: Types.number(0, { min: 0 }),
104
+ color: Types.color(AXIS_COLOR),
105
+ /** The radius of an axis line, in scene units. */
106
+ thickness: Types.number(0.02, { min: 0, step: 0.005 }),
107
+ tips: Types.boolean(true, { animatable: false }),
108
+ ticks: Types.boolean(true, { animatable: false }),
109
+ tickSize: Types.number(0.16, { min: 0, step: 0.01 }),
110
+ /** Lines across the plane the x and y axes lie in, at each of their ticks. */
111
+ grid: Types.boolean(false, { animatable: false }),
112
+ /** (`gridColor` is the viewport's own reference grid.) */
113
+ gridLineColor: Types.color("#374151"),
114
+ /** A number at each tick. */
115
+ labels: Types.boolean(false, { animatable: false }),
116
+ /** The height of a label's text, in scene units. */
117
+ labelSize: Types.number(0.26, { min: 0.01, step: 0.01 }),
118
+ labelColor: Types.color(AXIS_COLOR),
119
+ fontFamily: Types.text("", { animatable: false }),
120
+ /** A title at the positive end of each axis. */
121
+ xLabel: Types.text("x", { animatable: false }),
122
+ yLabel: Types.text("y", { animatable: false }),
123
+ zLabel: Types.text("z", { animatable: false }),
124
+ };
125
+
126
+ type AxesValues = Values<typeof own>;
127
+
128
+ /**
129
+ * A 3D graph: three axes through a box of graph coordinates, z up, seen through
130
+ * a camera of its own, so it stands on a 2D stage as it is, the way `axes2d`
131
+ * does. Its 3D children sit in its graph coordinates: a surface, a space curve,
132
+ * a point, an arrow, or any 3D node. `orbitTo` swings the camera round it.
133
+ */
134
+ export const Axes3d = component2d({
135
+ key: "axes3d",
136
+ state: { ...Paint.state, ...Flow.state, ...Shapes.corners, ...View3D.state, ...own },
137
+ defaults: { width: "fill", height: "fill" },
138
+ commands: {
139
+ ...Paint.commands,
140
+ ...View3D.commands,
141
+ /** Animate the window of values the axes show; everything drawn in them follows. */
142
+ rangeTo: { args: ["xRange", "yRange", "zRange"] },
143
+ },
144
+ allowedChildren: ["2d", "3d"],
145
+ load: (loader, s) => {
146
+ View3D.load(loader, furnish(s));
147
+ if (s.fontFamily !== "") loader.font(s.fontFamily);
148
+ },
149
+ provide: (s) => [provides(GRAPH_3D, graphOfAxes(s))],
150
+ space: (s) => axesSpace(s),
151
+ measure: (measurer, s, children) => Flow.measure(measurer, s, children, Paint.inset(s)),
152
+ layout: (box, s, children) => Flow.layout(box, s, children, Paint.inset(s)),
153
+ render: (ctx, s) => View3D.draw(ctx, s, furnish(s)),
154
+ });
155
+
156
+ export type Axes3d = NodeOf<typeof Axes3d>;
157
+
158
+ /** The axes' own drawing, added to the scene: what `render` draws and `load` declares. */
159
+ function furnish(s: AxesValues): (scene: Scene3D) => void {
160
+ return (scene) => scene.draw(furniture(s));
161
+ }
162
+
163
+ /** The last space worked out for each axes, kept while its ranges and lengths are unchanged. */
164
+ const spaces = new WeakMap<object, { readonly key: string; readonly space: GraphSpace }>();
165
+
166
+ /** The coordinates of a 3D graph: what its children's `x`, `y` and `z` are in. */
167
+ function axesSpace(s: AxesValues): GraphSpace {
168
+ const { xRange, yRange, zRange, xLength, yLength, zLength } = s;
169
+ const key = [xRange, yRange, zRange, xLength, yLength, zLength].join("|");
170
+ let kept = spaces.get(s);
171
+ if (kept?.key !== key) spaces.set(s, (kept = { key, space: graphSpace(xRange, yRange, zRange, { x: xLength, y: yLength, z: zLength }) }));
172
+ return kept.space;
173
+ }
174
+
175
+ // One per axes, so the context its subtree holds is the same from frame to frame.
176
+ const graphs = new WeakMap<object, Graph3D>();
177
+
178
+ function graphOfAxes(s: AxesValues): Graph3D {
179
+ let graph = graphs.get(s);
180
+ if (!graph) graphs.set(s, (graph = { space: () => axesSpace(s) }));
181
+ return graph;
182
+ }
183
+
184
+ /** The axes, their ticks, numbers and titles, and the grid, in the scene's units. */
185
+ function furniture(s: AxesValues): Graphics3D {
186
+ const space = axesSpace(s);
187
+ const g3 = new Graphics3D();
188
+ const ranges: Record<AxisName, Interval> = { x: space.xRange, y: space.yRange, z: space.zRange };
189
+ const lengths: Record<AxisName, number> = { x: s.xLength, y: s.yLength, z: s.zLength };
190
+ const steps: Record<AxisName, number> = { x: s.xStep, y: s.yStep, z: s.zStep };
191
+ const titles: Record<AxisName, string> = { x: s.xLabel, y: s.yLabel, z: s.zLabel };
192
+ // Where each axis is crossed by the other two: their zero, or the nearest end of a range without one.
193
+ const cross: Record<AxisName, number> = {
194
+ x: clamp(0, ranges.x[0], ranges.x[1]),
195
+ y: clamp(0, ranges.y[0], ranges.y[1]),
196
+ z: clamp(0, ranges.z[0], ranges.z[1]),
197
+ };
198
+ const at = (axis: AxisName, value: number, shift: Partial<Record<AxisName, number>> = {}): Vector3 => {
199
+ const point = { ...cross, [axis]: value };
200
+ const local = space.toLocal(point.x, point.y, point.z);
201
+ const moved = space.offset(0, 0, 0);
202
+ for (const key of ["x", "y", "z"] as const) {
203
+ const by = shift[key];
204
+ if (by === undefined) continue;
205
+ // A shift is in scene units, along the graph axis it names.
206
+ if (key === "x") moved.z += by;
207
+ else if (key === "y") moved.x += by;
208
+ else moved.y += by;
209
+ }
210
+ return { x: local.x + moved.x, y: local.y + moved.y, z: local.z + moved.z };
211
+ };
212
+
213
+ const { thickness, tickSize } = s;
214
+ const paint = Mat.basic({ color: s.color });
215
+ const style: LabelStyle3D = { size: s.labelSize, color: s.labelColor, fontFamily: s.fontFamily };
216
+ const tipLength = s.tips ? thickness * 9 : 0;
217
+ const marks: Transform3D[] = [];
218
+ const plane: Vector3[] = [];
219
+
220
+ for (const axis of ["x", "y", "z"] as const) {
221
+ const [min, max] = ranges[axis];
222
+ const length = lengths[axis];
223
+ if (!(max > min) || !(length > 0)) continue;
224
+
225
+ if (thickness > 0) {
226
+ const from = at(axis, min);
227
+ const to = at(axis, max);
228
+ g3.mesh(Geo.cylinder({ radius: thickness, height: length, segments: [12, 1] }), paint, {
229
+ position: { x: (from.x + to.x) / 2, y: (from.y + to.y) / 2, z: (from.z + to.z) / 2 },
230
+ rotation: ALONG[axis],
231
+ });
232
+ if (s.tips) {
233
+ g3.mesh(Geo.cone({ radius: thickness * 3.2, height: tipLength, segments: [16, 1] }), paint, {
234
+ position: at(axis, max, { [axis]: tipLength / 2 }),
235
+ rotation: ALONG[axis],
236
+ });
237
+ }
238
+ }
239
+
240
+ const ticks = ticksFor(ranges[axis], steps[axis], length * UNIT_AS_PIXELS);
241
+ const side = TICK_ALONG[axis];
242
+ for (const value of ticks.values) {
243
+ // The crossing is marked by the other axes, and the far end by the tip.
244
+ if (Math.abs(value - cross[axis]) < 1e-9 || (s.tips && Math.abs(value - max) < 1e-9)) continue;
245
+ if (s.ticks && tickSize > 0) marks.push({ position: at(axis, value), rotation: ALONG[side] });
246
+ if (s.labels) {
247
+ drawLabel3D(g3, formatTick(value, ticks.step), at(axis, value, { [side]: -(tickSize / 2 + style.size * 0.8) }), style);
248
+ }
249
+ }
250
+ drawLabel3D(g3, titles[axis], at(axis, max, { [axis]: tipLength + style.size * 0.9 }), style);
251
+
252
+ if (s.grid && axis !== "z") {
253
+ const other = axis === "x" ? "y" : "x";
254
+ for (const value of ticks.values) {
255
+ const a = { x: 0, y: 0, z: cross.z, [axis]: value, [other]: ranges[other][0] };
256
+ const b = { ...a, [other]: ranges[other][1] };
257
+ plane.push(space.toLocal(a.x, a.y, a.z), space.toLocal(b.x, b.y, b.z));
258
+ }
259
+ }
260
+ }
261
+
262
+ if (marks.length > 0) {
263
+ g3.instances(Geo.cylinder({ radius: Math.max(thickness * 0.7, 0.008), height: tickSize, segments: [8, 1] }), paint, marks);
264
+ }
265
+ if (plane.length > 0) g3.line({ points: plane, segments: true, stroke: { fill: s.gridLineColor } });
266
+ return g3;
267
+ }