@beff/aula 0.8.2 → 0.9.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.
- package/dist/generated/aula.core5.wasm +0 -0
- package/dist/index.d.ts +7304 -6
- package/dist/index.js +14864 -11
- package/dist/index.js.map +1 -0
- package/package.json +8 -25
- package/dist/animations.d.ts +0 -635
- package/dist/animations.js +0 -1778
- package/dist/braces.d.ts +0 -84
- package/dist/braces.js +0 -253
- package/dist/card.d.ts +0 -17
- package/dist/card.js +0 -90
- package/dist/colors-palette.d.ts +0 -2242
- package/dist/colors-palette.js +0 -2241
- package/dist/colors.d.ts +0 -120
- package/dist/colors.js +0 -403
- package/dist/curves.d.ts +0 -10
- package/dist/curves.js +0 -128
- package/dist/engine.d.ts +0 -1705
- package/dist/engine.js +0 -3561
- package/dist/errors.d.ts +0 -19
- package/dist/errors.js +0 -25
- package/dist/geometry.d.ts +0 -551
- package/dist/geometry.js +0 -1555
- package/dist/graph.d.ts +0 -117
- package/dist/graph.js +0 -526
- package/dist/graphing.d.ts +0 -597
- package/dist/graphing.js +0 -1504
- package/dist/host.d.ts +0 -19
- package/dist/host.js +0 -85
- package/dist/lexicon.d.ts +0 -10
- package/dist/lexicon.js +0 -23
- package/dist/load.d.ts +0 -21
- package/dist/load.js +0 -109
- package/dist/logo.d.ts +0 -30
- package/dist/logo.js +0 -186
- package/dist/manim.d.ts +0 -22
- package/dist/manim.js +0 -19
- package/dist/media.d.ts +0 -34
- package/dist/media.js +0 -391
- package/dist/mobjects.d.ts +0 -127
- package/dist/mobjects.js +0 -316
- package/dist/picture.d.ts +0 -23
- package/dist/picture.js +0 -291
- package/dist/pointcloud.d.ts +0 -144
- package/dist/pointcloud.js +0 -384
- package/dist/rates.d.ts +0 -19
- package/dist/rates.js +0 -54
- package/dist/run.d.ts +0 -101
- package/dist/run.js +0 -323
- package/dist/tables.d.ts +0 -179
- package/dist/tables.js +0 -403
- package/dist/tex.d.ts +0 -8
- package/dist/tex.js +0 -257
- package/dist/text-layouts.d.ts +0 -64
- package/dist/text-layouts.js +0 -225
- package/dist/text.d.ts +0 -61
- package/dist/text.js +0 -78
- package/dist/theme.d.ts +0 -25
- package/dist/theme.js +0 -12
- package/dist/user-code.d.ts +0 -5
- package/dist/user-code.js +0 -20
- package/dist/utils.d.ts +0 -152
- package/dist/utils.js +0 -517
- package/dist/vector-field.d.ts +0 -132
- package/dist/vector-field.js +0 -361
package/dist/engine.js
DELETED
|
@@ -1,3561 +0,0 @@
|
|
|
1
|
-
// The engine's API in Manim CE's form (class names kept, snake_case names in
|
|
2
|
-
// camelCase, Manim's defaults, keyword arguments in one last options object), over the jco
|
|
3
|
-
// bindings of the WebAssembly component (engine/generated, from crates/aula-wasm/wit).
|
|
4
|
-
// Besides signatures and defaults it holds only what the design puts here: defineProject,
|
|
5
|
-
// defineChapter and the run functions, the `.animate` builder and the
|
|
6
|
-
// callback table (host.ts). index.ts exports it: the Manim names on the namespace `m`
|
|
7
|
-
// (manim.ts), defineProject and defineChapter.
|
|
8
|
-
import { engine } from "./load.js";
|
|
9
|
-
import { fault } from "./errors.js";
|
|
10
|
-
import { getSmoothCubicBezierHandlePoints, shoelaceDirection, } from "./utils.js";
|
|
11
|
-
import { register } from "./host.js";
|
|
12
|
-
import { texGlyphs } from "./tex.js";
|
|
13
|
-
// --- constants -------------------------------------------------------------------------
|
|
14
|
-
// The engine's constants (`engine.constants()`), as values here: an import of @beff/aula does
|
|
15
|
-
// not load the engine. `constants.test.ts` checks that they are the engine's.
|
|
16
|
-
export const ORIGIN = [0, 0];
|
|
17
|
-
export const UP = [0, 1];
|
|
18
|
-
export const DOWN = [0, -1];
|
|
19
|
-
export const LEFT = [-1, 0];
|
|
20
|
-
export const RIGHT = [1, 0];
|
|
21
|
-
export const UL = [-1, 1];
|
|
22
|
-
export const UR = [1, 1];
|
|
23
|
-
export const DL = [-1, -1];
|
|
24
|
-
export const DR = [1, -1];
|
|
25
|
-
export const FRAME_HEIGHT = 8;
|
|
26
|
-
/** The frame is 16:9. */
|
|
27
|
-
export const FRAME_WIDTH = (FRAME_HEIGHT * 16) / 9;
|
|
28
|
-
/** The width of a frame in pixels (854). */
|
|
29
|
-
export const PIXEL_WIDTH = 854;
|
|
30
|
-
export const WHITE = "#FFFFFF";
|
|
31
|
-
export const BLACK = "#000000";
|
|
32
|
-
/** Manim's `YELLOW` (`YELLOW_C`). */
|
|
33
|
-
export const YELLOW = "#F7D96F";
|
|
34
|
-
/** Manim's `PURE_YELLOW`: the default colour of `Indicate`, `Flash`, `Circumscribe` and
|
|
35
|
-
* `SurroundingRectangle`. */
|
|
36
|
-
export const PURE_YELLOW = "#FFFF00";
|
|
37
|
-
export const PI = Math.PI;
|
|
38
|
-
export const TAU = 2 * Math.PI;
|
|
39
|
-
export const DEGREES = Math.PI / 180;
|
|
40
|
-
export const SMALL_BUFF = 0.1;
|
|
41
|
-
export const MED_SMALL_BUFF = 0.25;
|
|
42
|
-
export const MED_LARGE_BUFF = 0.5;
|
|
43
|
-
export const LARGE_BUFF = 1;
|
|
44
|
-
export const BOLD = "BOLD";
|
|
45
|
-
export const NORMAL = "NORMAL";
|
|
46
|
-
/** The built-in rate functions and their engine names: they run in the engine. */
|
|
47
|
-
const rateNames = new WeakMap();
|
|
48
|
-
function builtin(name) {
|
|
49
|
-
const f = (t) => engine.rateAt(name, t);
|
|
50
|
-
rateNames.set(f, name);
|
|
51
|
-
return f;
|
|
52
|
-
}
|
|
53
|
-
/** The engine's form of `f`: a built-in name, or a JS function in the callback table. */
|
|
54
|
-
export function rawRate(f) {
|
|
55
|
-
const name = rateNames.get(f);
|
|
56
|
-
return name === undefined ? { tag: "custom", val: register(f) } : { tag: "named", val: name };
|
|
57
|
-
}
|
|
58
|
-
/** `f` on 0..1; 0 below and 1 above (Manim's `unit_interval`). */
|
|
59
|
-
function unitInterval(t, f) {
|
|
60
|
-
return t < 0 ? 0 : t > 1 ? 1 : f(t);
|
|
61
|
-
}
|
|
62
|
-
/** `f` on 0..1; 0 outside (Manim's `zero`). */
|
|
63
|
-
function zeroOutside(t, f) {
|
|
64
|
-
return t < 0 || t > 1 ? 0 : f(t);
|
|
65
|
-
}
|
|
66
|
-
function sigmoidOf(x) {
|
|
67
|
-
return 1 / (1 + Math.exp(-x));
|
|
68
|
-
}
|
|
69
|
-
/** Manim's `smooth(t, inflection)`, for an inflection that is not the engine's 10. */
|
|
70
|
-
function smoothWith(t, inflection) {
|
|
71
|
-
return unitInterval(t, (u) => {
|
|
72
|
-
const error = sigmoidOf(-inflection / 2);
|
|
73
|
-
return Math.min(Math.max((sigmoidOf(inflection * (u - 0.5)) - error) / (1 - 2 * error), 0), 1);
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
export const linear = builtin("linear");
|
|
77
|
-
/** Slow at the start and the end (Manim `smooth`). The engine runs the default inflection 10. */
|
|
78
|
-
export function smooth(t, inflection = 10) {
|
|
79
|
-
return inflection === 10 ? engine.rateAt("smooth", t) : smoothWith(t, inflection);
|
|
80
|
-
}
|
|
81
|
-
rateNames.set(smooth, "smooth");
|
|
82
|
-
/** Manim `rush_into`: the first half of `smooth`, stretched. */
|
|
83
|
-
export function rushInto(t, inflection = 10) {
|
|
84
|
-
return inflection === 10 ? engine.rateAt("rush-into", t) : unitInterval(t, (u) => 2 * smoothWith(u / 2, inflection));
|
|
85
|
-
}
|
|
86
|
-
rateNames.set(rushInto, "rush-into");
|
|
87
|
-
/** Manim `rush_from`: the second half of `smooth`, stretched. */
|
|
88
|
-
export function rushFrom(t, inflection = 10) {
|
|
89
|
-
return inflection === 10
|
|
90
|
-
? engine.rateAt("rush-from", t)
|
|
91
|
-
: unitInterval(t, (u) => 2 * smoothWith(u / 2 + 0.5, inflection) - 1);
|
|
92
|
-
}
|
|
93
|
-
rateNames.set(rushFrom, "rush-from");
|
|
94
|
-
export const slowInto = builtin("slow-into");
|
|
95
|
-
export const doubleSmooth = builtin("double-smooth");
|
|
96
|
-
/** Manim `there_and_back`: `smooth` to 1 at the middle, then back to 0. */
|
|
97
|
-
export function thereAndBack(t, inflection = 10) {
|
|
98
|
-
return inflection === 10
|
|
99
|
-
? engine.rateAt("there-and-back", t)
|
|
100
|
-
: zeroOutside(t, (u) => smoothWith(u < 0.5 ? 2 * u : 2 * (1 - u), inflection));
|
|
101
|
-
}
|
|
102
|
-
rateNames.set(thereAndBack, "there-and-back");
|
|
103
|
-
/** Manim `there_and_back_with_pause`: there, a pause of `pauseRatio` of the time at 1, back. */
|
|
104
|
-
export function thereAndBackWithPause(t, pauseRatio = 1 / 3) {
|
|
105
|
-
if (pauseRatio === 1 / 3)
|
|
106
|
-
return engine.rateAt("there-and-back-with-pause", t);
|
|
107
|
-
return zeroOutside(t, (u) => {
|
|
108
|
-
const a = 2 / (1 - pauseRatio);
|
|
109
|
-
if (u < 0.5 - pauseRatio / 2)
|
|
110
|
-
return smoothWith(a * u, 10);
|
|
111
|
-
if (u < 0.5 + pauseRatio / 2)
|
|
112
|
-
return 1;
|
|
113
|
-
return smoothWith(a - a * u, 10);
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
rateNames.set(thereAndBackWithPause, "there-and-back-with-pause");
|
|
117
|
-
export const easeInSine = builtin("ease-in-sine");
|
|
118
|
-
export const easeOutSine = builtin("ease-out-sine");
|
|
119
|
-
export const easeInOutSine = builtin("ease-in-out-sine");
|
|
120
|
-
export const easeInQuad = builtin("ease-in-quad");
|
|
121
|
-
export const easeOutQuad = builtin("ease-out-quad");
|
|
122
|
-
export const easeInOutQuad = builtin("ease-in-out-quad");
|
|
123
|
-
export const easeInCubic = builtin("ease-in-cubic");
|
|
124
|
-
export const easeOutCubic = builtin("ease-out-cubic");
|
|
125
|
-
export const easeInOutCubic = builtin("ease-in-out-cubic");
|
|
126
|
-
export const easeInQuart = builtin("ease-in-quart");
|
|
127
|
-
export const easeOutQuart = builtin("ease-out-quart");
|
|
128
|
-
export const easeInOutQuart = builtin("ease-in-out-quart");
|
|
129
|
-
export const easeInQuint = builtin("ease-in-quint");
|
|
130
|
-
export const easeOutQuint = builtin("ease-out-quint");
|
|
131
|
-
export const easeInOutQuint = builtin("ease-in-out-quint");
|
|
132
|
-
export const easeInExpo = builtin("ease-in-expo");
|
|
133
|
-
export const easeOutExpo = builtin("ease-out-expo");
|
|
134
|
-
export const easeInOutExpo = builtin("ease-in-out-expo");
|
|
135
|
-
export const easeInCirc = builtin("ease-in-circ");
|
|
136
|
-
export const easeOutCirc = builtin("ease-out-circ");
|
|
137
|
-
export const easeInOutCirc = builtin("ease-in-out-circ");
|
|
138
|
-
export const easeInBack = builtin("ease-in-back");
|
|
139
|
-
export const easeOutBack = builtin("ease-out-back");
|
|
140
|
-
export const easeInOutBack = builtin("ease-in-out-back");
|
|
141
|
-
export const easeInElastic = builtin("ease-in-elastic");
|
|
142
|
-
export const easeOutElastic = builtin("ease-out-elastic");
|
|
143
|
-
export const easeInOutElastic = builtin("ease-in-out-elastic");
|
|
144
|
-
export const easeInBounce = builtin("ease-in-bounce");
|
|
145
|
-
export const easeOutBounce = builtin("ease-out-bounce");
|
|
146
|
-
export const easeInOutBounce = builtin("ease-in-out-bounce");
|
|
147
|
-
// --- points ----------------------------------------------------------------------------
|
|
148
|
-
export function add(a, b) {
|
|
149
|
-
return [a[0] + b[0], a[1] + b[1]];
|
|
150
|
-
}
|
|
151
|
-
export function sub(a, b) {
|
|
152
|
-
return [a[0] - b[0], a[1] - b[1]];
|
|
153
|
-
}
|
|
154
|
-
export function mul(v, k) {
|
|
155
|
-
return [v[0] * k, v[1] * k];
|
|
156
|
-
}
|
|
157
|
-
/** Euclidean length. */
|
|
158
|
-
export function norm(v) {
|
|
159
|
-
return Math.hypot(v[0], v[1]);
|
|
160
|
-
}
|
|
161
|
-
/** `a + (b - a) * t`. */
|
|
162
|
-
export function lerp(a, b, t) {
|
|
163
|
-
return [a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t];
|
|
164
|
-
}
|
|
165
|
-
// --- Mobject ---------------------------------------------------------------------------
|
|
166
|
-
const RAW = Symbol("aula.mobject");
|
|
167
|
-
const ID = Symbol("aula.mobjectId");
|
|
168
|
-
const ANIMATION = Symbol("aula.animation");
|
|
169
|
-
/** In a move, from its first part: the ids of the objects on the real clock and of their
|
|
170
|
-
* members (`Chapter.closed`). The move code does not read or change them. */
|
|
171
|
-
let closed = new Set();
|
|
172
|
-
/** The engine handle of a Mobject. */
|
|
173
|
-
export function raw(m) {
|
|
174
|
-
return m[RAW];
|
|
175
|
-
}
|
|
176
|
-
function isPoint(v) {
|
|
177
|
-
return Array.isArray(v) && v.length === 2 && typeof v[0] === "number" && typeof v[1] === "number";
|
|
178
|
-
}
|
|
179
|
-
/** An argument error with Manim's name of the function. */
|
|
180
|
-
export function wrong(label, message, hint) {
|
|
181
|
-
return fault(`${label}: ${message}`, hint);
|
|
182
|
-
}
|
|
183
|
-
/** The Mobjects of rest arguments, arrays flattened. */
|
|
184
|
-
function mobjects(items) {
|
|
185
|
-
return items.flatMap((v) => (v instanceof Mobject ? [raw(v)] : mobjects(v)));
|
|
186
|
-
}
|
|
187
|
-
/** The updaters that `addUpdater` gave each node, in order (Manim `updaters`). */
|
|
188
|
-
const updatersOf = new WeakMap();
|
|
189
|
-
/** The children of `m` become `list`, in order (Manim `m.submobjects = list`). */
|
|
190
|
-
function setSubmobjects(m, list) {
|
|
191
|
-
for (const c of raw(m).children())
|
|
192
|
-
raw(m).remove(c);
|
|
193
|
-
for (const c of list)
|
|
194
|
-
raw(m).add(raw(c));
|
|
195
|
-
}
|
|
196
|
-
/** The start value of `seed`. */
|
|
197
|
-
const SEED = 0x6d2b79f5;
|
|
198
|
-
/** The state of `random`. Each chapter run sets it to `SEED` (`freshSeed`). */
|
|
199
|
-
let seed = SEED;
|
|
200
|
-
/** Starts `random` again from `SEED`: a run of a chapter or of the title card calls it before
|
|
201
|
-
* it runs user code, so `shuffle` gives the same order in each run (#406). */
|
|
202
|
-
function freshSeed() {
|
|
203
|
-
seed = SEED;
|
|
204
|
-
}
|
|
205
|
-
/** A number from 0 to 1 that is the same in each run (a fixed seed), for `shuffle`. */
|
|
206
|
-
function random() {
|
|
207
|
-
seed = (seed + 0x6d2b79f5) | 0;
|
|
208
|
-
let t = seed;
|
|
209
|
-
t = Math.imul(t ^ (t >>> 15), t | 1);
|
|
210
|
-
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
|
211
|
-
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
212
|
-
}
|
|
213
|
-
/** `#RRGGBB` to channels 0..255. */
|
|
214
|
-
function rgbOf(c) {
|
|
215
|
-
const n = Number.parseInt(c.slice(1), 16);
|
|
216
|
-
return [(n >> 16) & 255, (n >> 8) & 255, n & 255];
|
|
217
|
-
}
|
|
218
|
-
/** The colour `alpha` of the way from `a` to `b` (Manim `interpolate_color`). */
|
|
219
|
-
function mixColor(a, b, alpha) {
|
|
220
|
-
const [x, y] = [rgbOf(a), rgbOf(b)];
|
|
221
|
-
const hex = x.map((v, i) => Math.round(v + ((y[i] ?? v) - v) * alpha)
|
|
222
|
-
.toString(16)
|
|
223
|
-
.padStart(2, "0"));
|
|
224
|
-
return `#${hex.join("").toUpperCase()}`;
|
|
225
|
-
}
|
|
226
|
-
/** `n` colours evenly along the colours `colors` (Manim `color_gradient`). */
|
|
227
|
-
function colorsAlong(colors, n) {
|
|
228
|
-
if (colors.length === 1 || n === 1)
|
|
229
|
-
return Array.from({ length: n }, () => colors[0] ?? WHITE);
|
|
230
|
-
return Array.from({ length: n }, (_, i) => {
|
|
231
|
-
const t = (i / (n - 1)) * (colors.length - 1);
|
|
232
|
-
const k = Math.min(Math.floor(t), colors.length - 2);
|
|
233
|
-
return mixColor(colors[k] ?? WHITE, colors[k + 1] ?? WHITE, t - k);
|
|
234
|
-
});
|
|
235
|
-
}
|
|
236
|
-
/** The curves of control points, four for each curve. */
|
|
237
|
-
function curvesOf(controls) {
|
|
238
|
-
const out = [];
|
|
239
|
-
for (let i = 0; i + 3 < controls.length; i += 4)
|
|
240
|
-
out.push(controls.slice(i, i + 4));
|
|
241
|
-
return out;
|
|
242
|
-
}
|
|
243
|
-
/** The point of cubic `c` at `t`. */
|
|
244
|
-
function bezierAt(c, t) {
|
|
245
|
-
const [a = ORIGIN, b = ORIGIN, d = ORIGIN, e = ORIGIN] = c;
|
|
246
|
-
const u = 1 - t;
|
|
247
|
-
const k = [u * u * u, 3 * u * u * t, 3 * u * t * t, t * t * t];
|
|
248
|
-
return [
|
|
249
|
-
(k[0] ?? 0) * a[0] + (k[1] ?? 0) * b[0] + (k[2] ?? 0) * d[0] + (k[3] ?? 0) * e[0],
|
|
250
|
-
(k[0] ?? 0) * a[1] + (k[1] ?? 0) * b[1] + (k[2] ?? 0) * d[1] + (k[3] ?? 0) * e[1],
|
|
251
|
-
];
|
|
252
|
-
}
|
|
253
|
-
/** The part of cubic `c` from `t0` to `t1`, as a cubic (Manim `partial_bezier_points`). */
|
|
254
|
-
function bezierPart(c, t0, t1) {
|
|
255
|
-
const p0 = bezierAt(c, t0);
|
|
256
|
-
const p3 = bezierAt(c, t1);
|
|
257
|
-
const h = (t1 - t0) / 3;
|
|
258
|
-
const d = (t) => {
|
|
259
|
-
const [a = ORIGIN, b = ORIGIN, e = ORIGIN, f = ORIGIN] = c;
|
|
260
|
-
const u = 1 - t;
|
|
261
|
-
return [
|
|
262
|
-
3 * u * u * (b[0] - a[0]) + 6 * u * t * (e[0] - b[0]) + 3 * t * t * (f[0] - e[0]),
|
|
263
|
-
3 * u * u * (b[1] - a[1]) + 6 * u * t * (e[1] - b[1]) + 3 * t * t * (f[1] - e[1]),
|
|
264
|
-
];
|
|
265
|
-
};
|
|
266
|
-
return [p0, add(p0, mul(d(t0), h)), sub(p3, mul(d(t1), h)), p3];
|
|
267
|
-
}
|
|
268
|
-
/** The points with `n` more curves: each curve is cut into equal pieces, the first curves
|
|
269
|
-
* into more (Manim `insert_n_curves_to_point_list` with `bezier_remap`). */
|
|
270
|
-
function insertCurves(n, controls) {
|
|
271
|
-
const curves = curvesOf(controls);
|
|
272
|
-
if (curves.length === 0) {
|
|
273
|
-
const p = controls[0] ?? ORIGIN;
|
|
274
|
-
return Array.from({ length: 4 * n }, () => p);
|
|
275
|
-
}
|
|
276
|
-
const target = curves.length + n;
|
|
277
|
-
const splits = curves.map(() => 0);
|
|
278
|
-
for (let i = 0; i < target; i++) {
|
|
279
|
-
const k = Math.floor((i * curves.length) / target);
|
|
280
|
-
splits[k] = (splits[k] ?? 0) + 1;
|
|
281
|
-
}
|
|
282
|
-
return curves.flatMap((c, i) => {
|
|
283
|
-
const k = splits[i] ?? 1;
|
|
284
|
-
return Array.from({ length: k }, (_, j) => bezierPart(c, j / k, (j + 1) / k)).flat();
|
|
285
|
-
});
|
|
286
|
-
}
|
|
287
|
-
/** The subpaths of control points: a curve that does not start where the last one ended
|
|
288
|
-
* starts a new one (Manim `get_subpaths_from_points`). */
|
|
289
|
-
function subpathsOf(controls) {
|
|
290
|
-
const out = [];
|
|
291
|
-
for (const c of curvesOf(controls)) {
|
|
292
|
-
const last = out.at(-1);
|
|
293
|
-
const end = last?.at(-1);
|
|
294
|
-
if (last !== undefined && end !== undefined && c[0] !== undefined && norm(sub(end, c[0])) < 1e-6)
|
|
295
|
-
last.push(...c);
|
|
296
|
-
else
|
|
297
|
-
out.push([...c]);
|
|
298
|
-
}
|
|
299
|
-
return out;
|
|
300
|
-
}
|
|
301
|
-
/** Subpath `n`, or a point curve at the last point when there is no subpath `n` (Manim
|
|
302
|
-
* `align_points`). */
|
|
303
|
-
function nthSubpath(list, n) {
|
|
304
|
-
const last = list.at(-1)?.at(-1) ?? ORIGIN;
|
|
305
|
-
return list[n] ?? [last, last, last, last];
|
|
306
|
-
}
|
|
307
|
-
/** The count of rows or columns of `arrangeInGrid`: given, or from the alignments or sizes. */
|
|
308
|
-
function gridSize(n, alignments, sizes) {
|
|
309
|
-
return n ?? alignments?.length ?? sizes?.length;
|
|
310
|
-
}
|
|
311
|
-
/** The points after the last whole curve of each node: a path that `startNewPath` started
|
|
312
|
-
* (Manim's points that are not a whole curve). A copy does not keep them. */
|
|
313
|
-
const pendingOf = new WeakMap();
|
|
314
|
-
/** `#RRGGBB` to channels 0..1. */
|
|
315
|
-
function rgbUnit(c) {
|
|
316
|
-
const [r, g, b] = rgbOf(c);
|
|
317
|
-
return [r / 255, g / 255, b / 255];
|
|
318
|
-
}
|
|
319
|
-
const CAP_OF = { auto: "AUTO", round: "ROUND", butt: "BUTT", square: "SQUARE" };
|
|
320
|
-
const JOINT_OF = { auto: "AUTO", round: "ROUND", bevel: "BEVEL", miter: "MITER" };
|
|
321
|
-
function capName(c) {
|
|
322
|
-
const names = { AUTO: "auto", ROUND: "round", BUTT: "butt", SQUARE: "square" };
|
|
323
|
-
return names[c];
|
|
324
|
-
}
|
|
325
|
-
function jointName(j) {
|
|
326
|
-
const names = { AUTO: "auto", ROUND: "round", BEVEL: "bevel", MITER: "miter" };
|
|
327
|
-
return names[j];
|
|
328
|
-
}
|
|
329
|
-
/** The VMobject settings that a copy keeps (Manim copies every attribute). */
|
|
330
|
-
function copySettings(from, to) {
|
|
331
|
-
to.preFunctionHandleToAnchorScaleFactor = from.preFunctionHandleToAnchorScaleFactor;
|
|
332
|
-
to.makeSmoothAfterApplyingFunctions = from.makeSmoothAfterApplyingFunctions;
|
|
333
|
-
to.toleranceForPointEquality = from.toleranceForPointEquality;
|
|
334
|
-
to.closeNewPoints = from.closeNewPoints;
|
|
335
|
-
}
|
|
336
|
-
/** An invisible mobject with one point (Manim `VectorizedPoint`). */
|
|
337
|
-
function pointMobject(location) {
|
|
338
|
-
const p = wrap(engine.vGroup([]));
|
|
339
|
-
p.setPoints([location]);
|
|
340
|
-
raw(p).setFill(undefined, 0, true);
|
|
341
|
-
raw(p).setStroke(undefined, 0, 0, true);
|
|
342
|
-
return p;
|
|
343
|
-
}
|
|
344
|
-
/** The point about which `applyPointsFunctionAboutPoint` and its callers work. */
|
|
345
|
-
function aboutOf(m, aboutPoint, aboutEdge) {
|
|
346
|
-
return aboutPoint ?? m.getCriticalPoint(aboutEdge ?? ORIGIN);
|
|
347
|
-
}
|
|
348
|
-
/** The one wrapper of each live node, by `id()`. A live wrapper holds its node, so the id of a
|
|
349
|
-
* live wrapper is not used by another node. */
|
|
350
|
-
const wrappers = new Map();
|
|
351
|
-
const collected = new FinalizationRegistry((id) => {
|
|
352
|
-
if (wrappers.get(id)?.deref() === undefined)
|
|
353
|
-
wrappers.delete(id);
|
|
354
|
-
});
|
|
355
|
-
/** The wrapper of the node of `handle`: the one that exists, or a new Mobject. An unused
|
|
356
|
-
* handle is freed when it is collected (jco's dispose of a handle from a list frees the
|
|
357
|
-
* wrong handle). */
|
|
358
|
-
export function wrap(handle) {
|
|
359
|
-
return wrappers.get(handle.id())?.deref() ?? new Mobject(handle);
|
|
360
|
-
}
|
|
361
|
-
/** An integer property key, as Python indexes (negative from the end). */
|
|
362
|
-
const INDEX = /^-?\d+$/;
|
|
363
|
-
/** `group[i]`: child `i` (negative from the end), or undefined. */
|
|
364
|
-
const indexing = {
|
|
365
|
-
get(target, key, receiver) {
|
|
366
|
-
if (closed.size > 0 && typeof key === "string" && closed.has(target[ID])) {
|
|
367
|
-
fault(`${key}: this object runs on the real clock, so a move cannot read or change it after its first part`, 'read or change it before the first play of the move, or give its dt updater { clock: "move" }');
|
|
368
|
-
}
|
|
369
|
-
if (typeof key === "string" && INDEX.test(key))
|
|
370
|
-
return target.submobjects.at(Number(key));
|
|
371
|
-
const value = Reflect.get(target, key, receiver);
|
|
372
|
-
if (typeof value !== "function" || typeof key !== "string" || !isMethod(target, key))
|
|
373
|
-
return value;
|
|
374
|
-
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- the receiver is the Proxy of this Mobject, and a method takes any arguments
|
|
375
|
-
return bound(receiver, key, value);
|
|
376
|
-
},
|
|
377
|
-
};
|
|
378
|
-
/** The Mobject of a method that `mob.method` gave (Python's `method.__self__`). */
|
|
379
|
-
export const METHOD_SELF = Symbol("aula.methodSelf");
|
|
380
|
-
/** The unbound function of such a method (Python's `method.__func__`). */
|
|
381
|
-
export const METHOD_FUNC = Symbol("aula.methodFunc");
|
|
382
|
-
const boundMethods = new WeakMap();
|
|
383
|
-
/** True when `key` is a method of the class of `target` (not a getter, not the constructor). */
|
|
384
|
-
function isMethod(target, key) {
|
|
385
|
-
if (key === "constructor")
|
|
386
|
-
return false;
|
|
387
|
-
for (let o = Object.getPrototypeOf(target); o !== null; o = Object.getPrototypeOf(o)) {
|
|
388
|
-
const d = Object.getOwnPropertyDescriptor(o, key);
|
|
389
|
-
if (d !== undefined)
|
|
390
|
-
return typeof d.value === "function";
|
|
391
|
-
}
|
|
392
|
-
return false;
|
|
393
|
-
}
|
|
394
|
-
function bound(self, key, f) {
|
|
395
|
-
let methods = boundMethods.get(self);
|
|
396
|
-
if (methods === undefined) {
|
|
397
|
-
methods = new Map();
|
|
398
|
-
boundMethods.set(self, methods);
|
|
399
|
-
}
|
|
400
|
-
const known = methods.get(key);
|
|
401
|
-
if (known !== undefined && known[METHOD_FUNC] === f)
|
|
402
|
-
return known;
|
|
403
|
-
const method = Object.assign((...args) => Reflect.apply(f, self, args), {
|
|
404
|
-
[METHOD_SELF]: self,
|
|
405
|
-
[METHOD_FUNC]: f,
|
|
406
|
-
});
|
|
407
|
-
methods.set(key, method);
|
|
408
|
-
return method;
|
|
409
|
-
}
|
|
410
|
-
/** One node of the picture tree. Each node has one Mobject: `group[0] === circle` when `circle`
|
|
411
|
-
* is the first child of `group`. `group[i]` is child `i` (negative from the end), `length` is
|
|
412
|
-
* the number of children, and `for (const m of group)` goes through the children. Methods
|
|
413
|
-
* that change it return it, so they chain. */
|
|
414
|
-
class MobjectClass {
|
|
415
|
-
[RAW];
|
|
416
|
-
/** The id of the node. */
|
|
417
|
-
[ID];
|
|
418
|
-
/** Manim's `name`: "Mobject" unless the options give one. */
|
|
419
|
-
name = "Mobject";
|
|
420
|
-
/** Manim's VMobject settings: `applyFunction` first moves each handle this fraction of the way
|
|
421
|
-
* to its anchor, and then back. */
|
|
422
|
-
preFunctionHandleToAnchorScaleFactor = 0.01;
|
|
423
|
-
/** True: `applyFunction` ends with `makeSmooth`. */
|
|
424
|
-
makeSmoothAfterApplyingFunctions = false;
|
|
425
|
-
/** The distance at which `considerPointsEquals` takes two points as equal. */
|
|
426
|
-
toleranceForPointEquality = 1e-6;
|
|
427
|
-
/** Manim's `close_new_points`. Only Manim's OpenGL renderer reads it. */
|
|
428
|
-
closeNewPoints = false;
|
|
429
|
-
constructor(handle) {
|
|
430
|
-
this[RAW] = handle;
|
|
431
|
-
this[ID] = handle.id();
|
|
432
|
-
const self = new Proxy(this, indexing);
|
|
433
|
-
wrappers.set(handle.id(), new WeakRef(self));
|
|
434
|
-
collected.register(self, handle.id());
|
|
435
|
-
// oxlint-disable-next-line no-constructor-return -- the Proxy gives `group[i]`; it is the one wrapper of the node
|
|
436
|
-
return self;
|
|
437
|
-
}
|
|
438
|
-
// tree
|
|
439
|
-
/** The children (Manim `submobjects`). */
|
|
440
|
-
get submobjects() {
|
|
441
|
-
return raw(this).children().map(wrap);
|
|
442
|
-
}
|
|
443
|
-
/** The number of children (Manim `len(group)`). */
|
|
444
|
-
get length() {
|
|
445
|
-
return this.submobjects.length;
|
|
446
|
-
}
|
|
447
|
-
/** The children, in order (Manim `for m in group`). */
|
|
448
|
-
*[Symbol.iterator]() {
|
|
449
|
-
yield* this.submobjects;
|
|
450
|
-
}
|
|
451
|
-
add(...items) {
|
|
452
|
-
for (const m of mobjects(items))
|
|
453
|
-
raw(this).add(m);
|
|
454
|
-
return this;
|
|
455
|
-
}
|
|
456
|
-
remove(...items) {
|
|
457
|
-
for (const m of mobjects(items))
|
|
458
|
-
raw(this).remove(m);
|
|
459
|
-
return this;
|
|
460
|
-
}
|
|
461
|
-
/** This node and every descendant, depth first. `recurse` has no effect, as in Manim. */
|
|
462
|
-
// oxlint-disable-next-line no-unused-vars -- Manim's parameter, which has no effect in Manim either
|
|
463
|
-
getFamily(recurse = true) {
|
|
464
|
-
return raw(this).family().map(wrap);
|
|
465
|
-
}
|
|
466
|
-
/** The family members that have points of their own (Manim `family_members_with_points`). */
|
|
467
|
-
familyMembersWithPoints() {
|
|
468
|
-
return this.getFamily().filter((m) => m.hasPoints());
|
|
469
|
-
}
|
|
470
|
-
/** Puts `mobject` at child `index` (Manim `insert`). */
|
|
471
|
-
insert(index, mobject) {
|
|
472
|
-
const list = this.submobjects.filter((c) => c !== mobject);
|
|
473
|
-
list.splice(index, 0, mobject);
|
|
474
|
-
setSubmobjects(this, list);
|
|
475
|
-
return this;
|
|
476
|
-
}
|
|
477
|
-
/** Puts the mobjects first among the children, in their order (Manim `add_to_back`). */
|
|
478
|
-
addToBack(...items) {
|
|
479
|
-
const front = [...new Set(items)];
|
|
480
|
-
setSubmobjects(this, [...front, ...this.submobjects.filter((c) => !front.includes(c))]);
|
|
481
|
-
return this;
|
|
482
|
-
}
|
|
483
|
-
/** Sets attributes: `set({ width: 3, zIndex: 2 })` (Manim `set(**kwargs)`). */
|
|
484
|
-
set(attributes) {
|
|
485
|
-
for (const [key, value] of Object.entries(attributes))
|
|
486
|
-
Reflect.set(this, key, value);
|
|
487
|
-
return this;
|
|
488
|
-
}
|
|
489
|
-
/** The children that have points or children (Manim `nonempty_submobjects`). */
|
|
490
|
-
nonemptySubmobjects() {
|
|
491
|
-
return this.submobjects.filter((c) => c.hasPoints() || c.length > 0);
|
|
492
|
-
}
|
|
493
|
-
/** `[this]` if it has points of its own, then the children (Manim `split`). */
|
|
494
|
-
split() {
|
|
495
|
-
return [...(this.hasPoints() ? [this] : []), ...this.submobjects];
|
|
496
|
-
}
|
|
497
|
-
/** The group class of this mobject: `m.VGroup` (Manim `get_group_class`). */
|
|
498
|
-
getGroupClass() {
|
|
499
|
-
return VGroup;
|
|
500
|
-
}
|
|
501
|
-
/** The base class of this mobject: `m.VMobject` (Manim `get_mobject_type_class`). */
|
|
502
|
-
getMobjectTypeClass() {
|
|
503
|
-
return VMobject;
|
|
504
|
-
}
|
|
505
|
-
/** True when `m` is this node or inside it. */
|
|
506
|
-
contains(m) {
|
|
507
|
-
return raw(this).contains(raw(m));
|
|
508
|
-
}
|
|
509
|
-
/** True when the node has points of its own. */
|
|
510
|
-
hasPoints() {
|
|
511
|
-
return raw(this).hasPoints() || (pendingOf.get(this)?.length ?? 0) > 0;
|
|
512
|
-
}
|
|
513
|
-
/** True when the node has no points of its own (Manim `has_no_points`). */
|
|
514
|
-
hasNoPoints() {
|
|
515
|
-
return !this.hasPoints();
|
|
516
|
-
}
|
|
517
|
-
/** The number of own control points (Manim `get_num_points`). */
|
|
518
|
-
getNumPoints() {
|
|
519
|
-
return this.getPoints().length;
|
|
520
|
-
}
|
|
521
|
-
/** The names of the point arrays: `["points"]` (Manim `get_array_attrs`). */
|
|
522
|
-
getArrayAttrs() {
|
|
523
|
-
return ["points"];
|
|
524
|
-
}
|
|
525
|
-
/** Changes the own points with `func` (Manim `apply_over_attr_arrays`). */
|
|
526
|
-
applyOverAttrArrays(func) {
|
|
527
|
-
return this.setPoints(func(this.getPoints()));
|
|
528
|
-
}
|
|
529
|
-
/** The points of the whole family (Manim `get_merged_array("points")`). */
|
|
530
|
-
getMergedArray(arrayAttr = "points") {
|
|
531
|
-
if (arrayAttr !== "points")
|
|
532
|
-
wrong("getMergedArray", `the only array is "points"; got ${JSON.stringify(arrayAttr)}`);
|
|
533
|
-
return this.getFamily().flatMap((m) => m.getPoints());
|
|
534
|
-
}
|
|
535
|
-
/** The points of the whole family (Manim `get_all_points`). */
|
|
536
|
-
getAllPoints() {
|
|
537
|
-
return this.getMergedArray("points");
|
|
538
|
-
}
|
|
539
|
-
/** The anchors of the family: the points that set the bounds (Manim
|
|
540
|
-
* `get_points_defining_boundary`). */
|
|
541
|
-
getPointsDefiningBoundary() {
|
|
542
|
-
return this.getFamily().flatMap((m) => curvesOf(m.getPoints()).flatMap((c) => [c[0] ?? ORIGIN, c[3] ?? ORIGIN]));
|
|
543
|
-
}
|
|
544
|
-
/** `reduceFunc` of the coordinates `dim` of the boundary points; 0 without points (Manim
|
|
545
|
-
* `reduce_across_dimension`). */
|
|
546
|
-
reduceAcrossDimension(reduceFunc, dim) {
|
|
547
|
-
const boundary = this.getPointsDefiningBoundary();
|
|
548
|
-
if (boundary.length === 0)
|
|
549
|
-
return 0;
|
|
550
|
-
return reduceFunc(boundary.map((p) => p[dim === 0 ? 0 : 1]));
|
|
551
|
-
}
|
|
552
|
-
/** A number for the node while it lives. `a === b` also compares nodes. */
|
|
553
|
-
id() {
|
|
554
|
-
return raw(this).id();
|
|
555
|
-
}
|
|
556
|
-
copy() {
|
|
557
|
-
const c = wrap(raw(this).copy());
|
|
558
|
-
c.name = this.name;
|
|
559
|
-
copySettings(this, c);
|
|
560
|
-
return c;
|
|
561
|
-
}
|
|
562
|
-
// measuring
|
|
563
|
-
/** `[[minX, minY], [maxX, maxY]]`, or undefined without points. */
|
|
564
|
-
bounds() {
|
|
565
|
-
return raw(this).bounds();
|
|
566
|
-
}
|
|
567
|
-
getCriticalPoint(direction) {
|
|
568
|
-
return raw(this).critical(direction);
|
|
569
|
-
}
|
|
570
|
-
getCenter() {
|
|
571
|
-
return raw(this).center();
|
|
572
|
-
}
|
|
573
|
-
getLeft() {
|
|
574
|
-
return raw(this).leftPoint();
|
|
575
|
-
}
|
|
576
|
-
getRight() {
|
|
577
|
-
return raw(this).rightPoint();
|
|
578
|
-
}
|
|
579
|
-
getTop() {
|
|
580
|
-
return raw(this).topPoint();
|
|
581
|
-
}
|
|
582
|
-
getBottom() {
|
|
583
|
-
return raw(this).bottomPoint();
|
|
584
|
-
}
|
|
585
|
-
/** The x of the critical point in `direction` (Manim `get_x`). */
|
|
586
|
-
getX(direction = ORIGIN) {
|
|
587
|
-
return this.getCoord(0, direction);
|
|
588
|
-
}
|
|
589
|
-
/** The y of the critical point in `direction` (Manim `get_y`). */
|
|
590
|
-
getY(direction = ORIGIN) {
|
|
591
|
-
return this.getCoord(1, direction);
|
|
592
|
-
}
|
|
593
|
-
/** Coordinate `dim` (0 x, 1 y) of the critical point in `direction` (Manim `get_coord`). */
|
|
594
|
-
getCoord(dim, direction = ORIGIN) {
|
|
595
|
-
return this.getExtremumAlongDim({ dim, key: direction[dim === 0 ? 0 : 1] });
|
|
596
|
-
}
|
|
597
|
-
/** The min (`key` < 0), the middle (0) or the max (> 0) of coordinate `dim` of `points`
|
|
598
|
-
* (default: the boundary points) (Manim `get_extremum_along_dim`). */
|
|
599
|
-
getExtremumAlongDim(opts = {}) {
|
|
600
|
-
const { points: given, dim = 0, key = 0 } = opts;
|
|
601
|
-
if (given === undefined) {
|
|
602
|
-
const c = this.getCriticalPoint(dim === 0 ? [key, 0] : [0, key]);
|
|
603
|
-
return c[dim === 0 ? 0 : 1];
|
|
604
|
-
}
|
|
605
|
-
const values = given.map((p) => p[dim === 0 ? 0 : 1]);
|
|
606
|
-
const lo = Math.min(...values);
|
|
607
|
-
const hi = Math.max(...values);
|
|
608
|
-
return key < 0 ? lo : key === 0 ? (lo + hi) / 2 : hi;
|
|
609
|
-
}
|
|
610
|
-
/** The size along `dim`: 0 the width, 1 the height (Manim `length_over_dim`). */
|
|
611
|
-
lengthOverDim(dim) {
|
|
612
|
-
return dim === 0 ? this.width : this.height;
|
|
613
|
-
}
|
|
614
|
-
/** The mean of the points of the family (Manim `get_center_of_mass`). */
|
|
615
|
-
getCenterOfMass() {
|
|
616
|
-
const all = this.getAllPoints();
|
|
617
|
-
if (all.length === 0)
|
|
618
|
-
return ORIGIN;
|
|
619
|
-
return mul(all.reduce(add, ORIGIN), 1 / all.length);
|
|
620
|
-
}
|
|
621
|
-
/** The boundary point that is farthest in `direction` (Manim `get_boundary_point`). */
|
|
622
|
-
getBoundaryPoint(direction) {
|
|
623
|
-
let best = ORIGIN;
|
|
624
|
-
let most = -Infinity;
|
|
625
|
-
for (const p of this.getPointsDefiningBoundary()) {
|
|
626
|
-
const d = p[0] * direction[0] + p[1] * direction[1];
|
|
627
|
-
if (d > most)
|
|
628
|
-
[best, most] = [p, d];
|
|
629
|
-
}
|
|
630
|
-
return best;
|
|
631
|
-
}
|
|
632
|
-
/** The point halfway along the path (Manim `get_midpoint`). */
|
|
633
|
-
getMidpoint() {
|
|
634
|
-
return this.pointFromProportion(0.5);
|
|
635
|
-
}
|
|
636
|
-
/** `[getStart(), getEnd()]` (Manim `get_start_and_end`). */
|
|
637
|
-
getStartAndEnd() {
|
|
638
|
-
return [this.getStart(), this.getEnd()];
|
|
639
|
-
}
|
|
640
|
-
/** The proportion of the path's length at `point`, which must be on the path (Manim
|
|
641
|
-
* `proportion_from_point`). */
|
|
642
|
-
proportionFromPoint(point) {
|
|
643
|
-
const curves = this.getCubicBezierTuples();
|
|
644
|
-
const steps = 64;
|
|
645
|
-
const lengths = curves.map((c) => {
|
|
646
|
-
let length = 0;
|
|
647
|
-
for (let i = 0; i < steps; i++)
|
|
648
|
-
length += norm(sub(bezierAt(c, (i + 1) / steps), bezierAt(c, i / steps)));
|
|
649
|
-
return length;
|
|
650
|
-
});
|
|
651
|
-
const total = lengths.reduce((a, b) => a + b, 0);
|
|
652
|
-
let before = 0;
|
|
653
|
-
for (const [i, c] of curves.entries()) {
|
|
654
|
-
for (let k = 0; k <= steps; k++) {
|
|
655
|
-
let t = k / steps;
|
|
656
|
-
// Refine the nearest sample by halving the step.
|
|
657
|
-
let h = 1 / steps;
|
|
658
|
-
for (let r = 0; r < 30; r++) {
|
|
659
|
-
h /= 2;
|
|
660
|
-
const [a, b] = [Math.max(0, t - h), Math.min(1, t + h)];
|
|
661
|
-
const best = [a, t, b].reduce((x, y) => norm(sub(bezierAt(c, y), point)) < norm(sub(bezierAt(c, x), point)) ? y : x);
|
|
662
|
-
t = best;
|
|
663
|
-
}
|
|
664
|
-
if (norm(sub(bezierAt(c, t), point)) < 1e-6) {
|
|
665
|
-
let partial = 0;
|
|
666
|
-
const n = Math.max(1, Math.round(t * steps));
|
|
667
|
-
for (let j = 0; j < n; j++)
|
|
668
|
-
partial += norm(sub(bezierAt(c, ((j + 1) * t) / n), bezierAt(c, (j * t) / n)));
|
|
669
|
-
return total === 0 ? 0 : (before + partial) / total;
|
|
670
|
-
}
|
|
671
|
-
}
|
|
672
|
-
before += lengths[i] ?? 0;
|
|
673
|
-
}
|
|
674
|
-
return wrong("proportionFromPoint", `the point [${point.join(", ")}] is not on the path`);
|
|
675
|
-
}
|
|
676
|
-
/** `nPieces` copies of the own path, each one piece of it, as a VGroup (Manim
|
|
677
|
-
* `get_pieces`). */
|
|
678
|
-
getPieces(nPieces) {
|
|
679
|
-
const template = this.copy();
|
|
680
|
-
setSubmobjects(template, []);
|
|
681
|
-
return VGroup(Array.from({ length: nPieces }, (_, i) => template.copy().pointwiseBecomePartial(this, i / nPieces, (i + 1) / nPieces)));
|
|
682
|
-
}
|
|
683
|
-
/** The own path becomes the part of `mobject`'s own path from proportion `a` to `b`
|
|
684
|
-
* (Manim `pointwise_become_partial`). */
|
|
685
|
-
pointwiseBecomePartial(mobject, a, b) {
|
|
686
|
-
raw(this).pointwiseBecomePartial(raw(mobject), a, b);
|
|
687
|
-
return this;
|
|
688
|
-
}
|
|
689
|
-
/** The point that orders this mobject among equal `zIndex` values: its center (Manim
|
|
690
|
-
* `get_z_index_reference_point`). */
|
|
691
|
-
getZIndexReferencePoint() {
|
|
692
|
-
return this.getCenter();
|
|
693
|
-
}
|
|
694
|
-
/** Manim's `z_index`: the frame draws the members of the stage sorted by it. */
|
|
695
|
-
get zIndex() {
|
|
696
|
-
return raw(this).zIndex();
|
|
697
|
-
}
|
|
698
|
-
set zIndex(value) {
|
|
699
|
-
raw(this).setZIndex(value, false);
|
|
700
|
-
}
|
|
701
|
-
/** Sets `zIndex` of the family, or of this node only (Manim `set_z_index`). A higher value is
|
|
702
|
-
* drawn on top; equal values keep the order of the stage. */
|
|
703
|
-
setZIndex(zIndexValue, opts = {}) {
|
|
704
|
-
raw(this).setZIndex(zIndexValue, opts.family ?? true);
|
|
705
|
-
return this;
|
|
706
|
-
}
|
|
707
|
-
/** True when the mobject is fully outside the frame (Manim `is_off_screen`). */
|
|
708
|
-
isOffScreen() {
|
|
709
|
-
return (this.left() > FRAME_WIDTH / 2 ||
|
|
710
|
-
this.right() < -FRAME_WIDTH / 2 ||
|
|
711
|
-
this.bottom() > FRAME_HEIGHT / 2 ||
|
|
712
|
-
this.top() < -FRAME_HEIGHT / 2);
|
|
713
|
-
}
|
|
714
|
-
get width() {
|
|
715
|
-
return raw(this).width();
|
|
716
|
-
}
|
|
717
|
-
/** Scales the mobject to this width (Manim's `width` setter: `scaleToFitWidth`). */
|
|
718
|
-
set width(value) {
|
|
719
|
-
this.scaleToFitWidth(value);
|
|
720
|
-
}
|
|
721
|
-
get height() {
|
|
722
|
-
return raw(this).height();
|
|
723
|
-
}
|
|
724
|
-
/** Scales the mobject to this height (Manim's `height` setter: `scaleToFitHeight`). */
|
|
725
|
-
set height(value) {
|
|
726
|
-
this.scaleToFitHeight(value);
|
|
727
|
-
}
|
|
728
|
-
/** The x of the left edge. */
|
|
729
|
-
left() {
|
|
730
|
-
return raw(this).left();
|
|
731
|
-
}
|
|
732
|
-
right() {
|
|
733
|
-
return raw(this).right();
|
|
734
|
-
}
|
|
735
|
-
top() {
|
|
736
|
-
return raw(this).top();
|
|
737
|
-
}
|
|
738
|
-
bottom() {
|
|
739
|
-
return raw(this).bottom();
|
|
740
|
-
}
|
|
741
|
-
getStart() {
|
|
742
|
-
return raw(this).start();
|
|
743
|
-
}
|
|
744
|
-
/** The last point (an arrow: its tip). */
|
|
745
|
-
getEnd() {
|
|
746
|
-
return raw(this).end();
|
|
747
|
-
}
|
|
748
|
-
isClosed() {
|
|
749
|
-
return raw(this).isClosed();
|
|
750
|
-
}
|
|
751
|
-
getCubicBezierTuples() {
|
|
752
|
-
return raw(this).bezierTuples();
|
|
753
|
-
}
|
|
754
|
-
pointFromProportion(alpha) {
|
|
755
|
-
return raw(this).pointFromProportion(alpha);
|
|
756
|
-
}
|
|
757
|
-
/** The critical point in a corner direction, such as `m.UR` (Manim `get_corner`). */
|
|
758
|
-
getCorner(direction) {
|
|
759
|
-
return this.getCriticalPoint(direction);
|
|
760
|
-
}
|
|
761
|
-
/** The critical point in an edge direction, such as `m.UP` (Manim `get_edge_center`). */
|
|
762
|
-
getEdgeCenter(direction) {
|
|
763
|
-
return this.getCriticalPoint(direction);
|
|
764
|
-
}
|
|
765
|
-
/** `getEnd() - getStart()` (Manim `Line.get_vector`). */
|
|
766
|
-
getVector() {
|
|
767
|
-
return sub(this.getEnd(), this.getStart());
|
|
768
|
-
}
|
|
769
|
-
/** The distance from `getStart()` to `getEnd()` (Manim `Line.get_length`). */
|
|
770
|
-
getLength() {
|
|
771
|
-
return norm(this.getVector());
|
|
772
|
-
}
|
|
773
|
-
/** The angle of `getVector()`, in radians from the x axis (Manim `Line.get_angle`). */
|
|
774
|
-
getAngle() {
|
|
775
|
-
const [x, y] = this.getVector();
|
|
776
|
-
return Math.atan2(y, x);
|
|
777
|
-
}
|
|
778
|
-
/** `getVector()` with length 1, or `[0, 0]` (Manim `Line.get_unit_vector`). */
|
|
779
|
-
getUnitVector() {
|
|
780
|
-
const v = this.getVector();
|
|
781
|
-
const n = norm(v);
|
|
782
|
-
return n === 0 ? [0, 0] : mul(v, 1 / n);
|
|
783
|
-
}
|
|
784
|
-
// placing
|
|
785
|
-
/** Moves the centre to the origin (Manim `center`). */
|
|
786
|
-
center() {
|
|
787
|
-
return this.moveTo(ORIGIN);
|
|
788
|
-
}
|
|
789
|
-
/** Moves along `dim` (0 x, 1 y) so that the critical point in `direction` has coordinate
|
|
790
|
-
* `value` (Manim `set_coord`). */
|
|
791
|
-
setCoord(value, dim, direction = ORIGIN) {
|
|
792
|
-
const d = value - this.getCoord(dim, direction);
|
|
793
|
-
return this.shift(dim === 0 ? [d, 0] : [0, d]);
|
|
794
|
-
}
|
|
795
|
-
/** Moves along x so that the critical point in `direction` has x `x` (Manim `set_x`). */
|
|
796
|
-
setX(x, direction = ORIGIN) {
|
|
797
|
-
return this.setCoord(x, 0, direction);
|
|
798
|
-
}
|
|
799
|
-
/** Moves along y so that the critical point in `direction` has y `y` (Manim `set_y`). */
|
|
800
|
-
setY(y, direction = ORIGIN) {
|
|
801
|
-
return this.setCoord(y, 1, direction);
|
|
802
|
-
}
|
|
803
|
-
/** Maps every point of the family through `function`, about `aboutPoint` (the origin by
|
|
804
|
-
* default) or the critical point `aboutEdge` (Manim `apply_function`). */
|
|
805
|
-
applyFunction(func, opts = {}) {
|
|
806
|
-
const about = opts.aboutPoint === undefined && opts.aboutEdge === undefined
|
|
807
|
-
? ORIGIN
|
|
808
|
-
: aboutOf(this, opts.aboutPoint, opts.aboutEdge);
|
|
809
|
-
const factor = this.preFunctionHandleToAnchorScaleFactor;
|
|
810
|
-
this.scaleHandleToAnchorDistances(factor);
|
|
811
|
-
const id = register((p) => add(func(sub(p, about)), about));
|
|
812
|
-
raw(this).applyPoints(id);
|
|
813
|
-
this.scaleHandleToAnchorDistances(1 / factor);
|
|
814
|
-
if (this.makeSmoothAfterApplyingFunctions)
|
|
815
|
-
this.makeSmooth();
|
|
816
|
-
return this;
|
|
817
|
-
}
|
|
818
|
-
/** Moves the center to `function(center)` (Manim `apply_functo_position`). */
|
|
819
|
-
applyFunctionToPosition(func) {
|
|
820
|
-
return this.moveTo(func(this.getCenter()));
|
|
821
|
-
}
|
|
822
|
-
/** Moves the center of each child through `function` (Manim
|
|
823
|
-
* `apply_functo_submobject_positions`). */
|
|
824
|
-
applyFunctionToSubmobjectPositions(func) {
|
|
825
|
-
for (const c of this.submobjects)
|
|
826
|
-
c.applyFunctionToPosition(func);
|
|
827
|
-
return this;
|
|
828
|
-
}
|
|
829
|
-
/** Maps the points by a 2 x 2 matrix (or the top-left of a 3 x 3 one), about the origin by
|
|
830
|
-
* default (Manim `apply_matrix`). */
|
|
831
|
-
applyMatrix(matrix, opts = {}) {
|
|
832
|
-
const [a = 1, b = 0] = matrix[0] ?? [];
|
|
833
|
-
const [c = 0, d = 1] = matrix[1] ?? [];
|
|
834
|
-
return this.applyFunction(([x, y]) => [a * x + b * y, c * x + d * y], opts);
|
|
835
|
-
}
|
|
836
|
-
/** Maps the points as complex numbers `m.Complex`, `[re, im]` (Manim `apply_complex_function`). */
|
|
837
|
-
applyComplexFunction(func, opts = {}) {
|
|
838
|
-
return this.applyFunction((p) => {
|
|
839
|
-
const [re, im] = func(p);
|
|
840
|
-
return [re, im];
|
|
841
|
-
}, opts);
|
|
842
|
-
}
|
|
843
|
-
/** Changes the own points of each family member with `func`, about `aboutPoint` or the
|
|
844
|
-
* critical point `aboutEdge` (the center by default) (Manim
|
|
845
|
-
* `apply_points_funcabout_point`). */
|
|
846
|
-
applyPointsFunctionAboutPoint(func, opts = {}) {
|
|
847
|
-
const about = aboutOf(this, opts.aboutPoint, opts.aboutEdge);
|
|
848
|
-
for (const m of this.familyMembersWithPoints()) {
|
|
849
|
-
const moved = func(m.getPoints().map((p) => sub(p, about)));
|
|
850
|
-
m.setPoints(moved.map((p) => add(p, about)));
|
|
851
|
-
}
|
|
852
|
-
return this;
|
|
853
|
-
}
|
|
854
|
-
/** Reverses the order of the points of each family member (Manim `reverse_points`). */
|
|
855
|
-
reversePoints() {
|
|
856
|
-
for (const m of this.familyMembersWithPoints())
|
|
857
|
-
m.setPoints(m.getPoints().toReversed());
|
|
858
|
-
return this;
|
|
859
|
-
}
|
|
860
|
-
/** Repeats the points of each family member `count` times (Manim `repeat`). */
|
|
861
|
-
repeat(count) {
|
|
862
|
-
for (const m of this.familyMembersWithPoints()) {
|
|
863
|
-
const own = m.getPoints();
|
|
864
|
-
m.setPoints(Array.from({ length: count }, () => own).flat());
|
|
865
|
-
}
|
|
866
|
-
return this;
|
|
867
|
-
}
|
|
868
|
-
/** The own points of each family member take those of `mobject`'s family, member by member;
|
|
869
|
-
* the style stays (Manim `match_points`). */
|
|
870
|
-
matchPoints(mobject, _opts = {}) {
|
|
871
|
-
const theirs = mobject.getFamily();
|
|
872
|
-
this.getFamily().forEach((m, i) => {
|
|
873
|
-
const other = theirs[i];
|
|
874
|
-
if (other !== undefined)
|
|
875
|
-
m.setPoints(other.getPoints());
|
|
876
|
-
});
|
|
877
|
-
return this;
|
|
878
|
-
}
|
|
879
|
-
shift(vector) {
|
|
880
|
-
raw(this).shift(vector);
|
|
881
|
-
return this;
|
|
882
|
-
}
|
|
883
|
-
/** Scales about `aboutPoint` or the critical point `aboutEdge` (the center by default). With
|
|
884
|
-
* `scaleStroke`, the stroke widths scale too. */
|
|
885
|
-
scale(scaleFactor, opts = {}) {
|
|
886
|
-
const o = opts;
|
|
887
|
-
if (o.scaleStroke === true) {
|
|
888
|
-
for (const m of this.getFamily())
|
|
889
|
-
m.setStroke({ width: Math.abs(scaleFactor) * m.getStrokeWidth(), family: false });
|
|
890
|
-
}
|
|
891
|
-
if (o.aboutPoint !== undefined)
|
|
892
|
-
raw(this).scaleAbout(scaleFactor, o.aboutPoint ?? ORIGIN);
|
|
893
|
-
else if (o.aboutEdge !== undefined)
|
|
894
|
-
raw(this).scaleAboutEdge(scaleFactor, o.aboutEdge ?? ORIGIN);
|
|
895
|
-
else
|
|
896
|
-
raw(this).scale(scaleFactor);
|
|
897
|
-
return this;
|
|
898
|
-
}
|
|
899
|
-
/** Stretches axis `dim` (0 x, 1 y) by `factor` about `aboutPoint` or the critical point
|
|
900
|
-
* `aboutEdge` (the center by default). */
|
|
901
|
-
stretch(factor, dim, opts = {}) {
|
|
902
|
-
if (opts.aboutPoint !== undefined)
|
|
903
|
-
return this.stretchAboutPoint(factor, dim, opts.aboutPoint);
|
|
904
|
-
raw(this).stretch(factor, dim, opts.aboutEdge ?? ORIGIN);
|
|
905
|
-
return this;
|
|
906
|
-
}
|
|
907
|
-
/** Stretches axis `dim` by `factor` about `point` (Manim `stretch_about_point`). */
|
|
908
|
-
stretchAboutPoint(factor, dim, point) {
|
|
909
|
-
return this.applyPointsFunctionAboutPoint((ps) => ps.map(([x, y]) => (dim === 0 ? [x * factor, y] : [x, y * factor])), { aboutPoint: point });
|
|
910
|
-
}
|
|
911
|
-
/** Scales (or with `stretch`, stretches) so that the size along `dim` is `length` (Manim
|
|
912
|
-
* `rescale_to_fit`). A mobject with no size there does not change. */
|
|
913
|
-
rescaleToFit(length, dim, opts = {}) {
|
|
914
|
-
const { stretch = false, ...about } = opts;
|
|
915
|
-
const old = this.lengthOverDim(dim);
|
|
916
|
-
if (old === 0)
|
|
917
|
-
return this;
|
|
918
|
-
return stretch ? this.stretch(length / old, dim, about) : this.scale(length / old, about);
|
|
919
|
-
}
|
|
920
|
-
/** Scales the group by `factor` and each child by `1 / factor`: the children move apart
|
|
921
|
-
* (Manim `space_out_submobjects`). */
|
|
922
|
-
spaceOutSubmobjects(factor = 1.5, opts = {}) {
|
|
923
|
-
this.scale(factor, opts);
|
|
924
|
-
for (const c of this.submobjects)
|
|
925
|
-
c.scale(1 / factor);
|
|
926
|
-
return this;
|
|
927
|
-
}
|
|
928
|
-
stretchToFitHeight(height, opts = {}) {
|
|
929
|
-
raw(this).stretchToFitHeight(height, opts.aboutEdge ?? ORIGIN);
|
|
930
|
-
return this;
|
|
931
|
-
}
|
|
932
|
-
stretchToFitWidth(width, opts = {}) {
|
|
933
|
-
raw(this).stretchToFitWidth(width, opts.aboutEdge ?? ORIGIN);
|
|
934
|
-
return this;
|
|
935
|
-
}
|
|
936
|
-
scaleToFitWidth(width) {
|
|
937
|
-
raw(this).scaleToFitWidth(width);
|
|
938
|
-
return this;
|
|
939
|
-
}
|
|
940
|
-
scaleToFitHeight(height) {
|
|
941
|
-
raw(this).scaleToFitHeight(height);
|
|
942
|
-
return this;
|
|
943
|
-
}
|
|
944
|
-
/** Shrinks (never grows) to at most `width`. */
|
|
945
|
-
fitWidth(width) {
|
|
946
|
-
raw(this).fitWidth(width);
|
|
947
|
-
return this;
|
|
948
|
-
}
|
|
949
|
-
/** Rotates counter-clockwise by `angle` about `aboutPoint` or the critical point
|
|
950
|
-
* `aboutEdge` (the center by default). */
|
|
951
|
-
rotate(angle, opts = {}) {
|
|
952
|
-
if (opts.aboutPoint === undefined && opts.aboutEdge === undefined)
|
|
953
|
-
raw(this).rotate(angle);
|
|
954
|
-
else
|
|
955
|
-
raw(this).rotateAbout(angle, aboutOf(this, opts.aboutPoint, opts.aboutEdge));
|
|
956
|
-
return this;
|
|
957
|
-
}
|
|
958
|
-
/** Rotates about the origin (Manim `rotate_about_origin`). */
|
|
959
|
-
rotateAboutOrigin(angle) {
|
|
960
|
-
return this.rotate(angle, { aboutPoint: ORIGIN });
|
|
961
|
-
}
|
|
962
|
-
/** Mirrors the mobject across the line through its center (or `aboutPoint`, or the critical
|
|
963
|
-
* point `aboutEdge`) in the direction `axis` (Manim `flip`: a half turn about `axis`). */
|
|
964
|
-
flip(axis = UP, opts = {}) {
|
|
965
|
-
const n = norm(axis);
|
|
966
|
-
if (n === 0)
|
|
967
|
-
return wrong("flip", "axis must not be [0, 0]");
|
|
968
|
-
const [ux, uy] = mul(axis, 1 / n);
|
|
969
|
-
return this.applyPointsFunctionAboutPoint((ps) => ps.map(([x, y]) => [(2 * ux * ux - 1) * x + 2 * ux * uy * y, 2 * ux * uy * x + (2 * uy * uy - 1) * y]), opts);
|
|
970
|
-
}
|
|
971
|
-
/** Moves the critical point `alignedEdge` of the mobject onto the point, or onto the same
|
|
972
|
-
* critical point of the mobject. `coorMask` `[1, 0]` moves along x only. */
|
|
973
|
-
moveTo(pointOrMobject, opts = {}) {
|
|
974
|
-
const { alignedEdge = ORIGIN, coorMask = [1, 1] } = opts;
|
|
975
|
-
const target = pointOrMobject instanceof Mobject ? pointOrMobject.getCriticalPoint(alignedEdge) : pointOrMobject;
|
|
976
|
-
const here = this.getCriticalPoint(alignedEdge);
|
|
977
|
-
return this.shift([(target[0] - here[0]) * coorMask[0], (target[1] - here[1]) * coorMask[1]]);
|
|
978
|
-
}
|
|
979
|
-
/** Puts the mobject `buff` away from `mobjectOrPoint` in `direction`. With `alignedEdge`,
|
|
980
|
-
* the edges of both in that direction line up (Manim `aligned_edge`).
|
|
981
|
-
* `submobjectToAlign` or `indexOfSubmobjectToAlign` lines up a child in place of the whole;
|
|
982
|
-
* `coorMask` `[1, 0]` moves along x only. */
|
|
983
|
-
nextTo(mobjectOrPoint, direction = RIGHT, opts = {}) {
|
|
984
|
-
const { buff = MED_SMALL_BUFF, alignedEdge = ORIGIN, submobjectToAlign, indexOfSubmobjectToAlign, coorMask = [1, 1], } = opts;
|
|
985
|
-
const child = (m, i) => m.submobjects.at(i) ?? wrong("nextTo", `the Mobject has no submobject ${String(i)}`);
|
|
986
|
-
let target;
|
|
987
|
-
if (mobjectOrPoint instanceof Mobject) {
|
|
988
|
-
const aligner = indexOfSubmobjectToAlign === undefined ? mobjectOrPoint : child(mobjectOrPoint, indexOfSubmobjectToAlign);
|
|
989
|
-
target = aligner.getCriticalPoint(add(alignedEdge, direction));
|
|
990
|
-
}
|
|
991
|
-
else
|
|
992
|
-
target = mobjectOrPoint;
|
|
993
|
-
const own = submobjectToAlign ?? (indexOfSubmobjectToAlign === undefined ? this : child(this, indexOfSubmobjectToAlign));
|
|
994
|
-
const here = own.getCriticalPoint(sub(alignedEdge, direction));
|
|
995
|
-
const v = add(sub(target, here), mul(direction, buff));
|
|
996
|
-
return this.shift([v[0] * coorMask[0], v[1] * coorMask[1]]);
|
|
997
|
-
}
|
|
998
|
-
/** Moves the critical point in `direction` to the frame edge, `buff` inside (Manim
|
|
999
|
-
* `align_on_border`). */
|
|
1000
|
-
alignOnBorder(direction, buff = MED_LARGE_BUFF) {
|
|
1001
|
-
const here = this.getCriticalPoint(direction);
|
|
1002
|
-
const v = [0, 1].map((k) => {
|
|
1003
|
-
const d = direction[k] ?? 0;
|
|
1004
|
-
const sign = Math.sign(d);
|
|
1005
|
-
const half = k === 0 ? FRAME_WIDTH / 2 : FRAME_HEIGHT / 2;
|
|
1006
|
-
return (sign * half - (here[k] ?? 0) - buff * d) * Math.abs(sign);
|
|
1007
|
-
});
|
|
1008
|
-
return this.shift([v[0] ?? 0, v[1] ?? 0]);
|
|
1009
|
-
}
|
|
1010
|
-
/** Moves the mobject inside the frame where an edge is past it, `buff` from the edge
|
|
1011
|
-
* (Manim `shift_onto_screen`). */
|
|
1012
|
-
shiftOntoScreen(opts = {}) {
|
|
1013
|
-
const { buff = MED_LARGE_BUFF } = opts;
|
|
1014
|
-
for (const v of [UP, DOWN, LEFT, RIGHT]) {
|
|
1015
|
-
const dim = v[0] === 0 ? 1 : 0;
|
|
1016
|
-
const max = (dim === 0 ? FRAME_WIDTH / 2 : FRAME_HEIGHT / 2) - buff;
|
|
1017
|
-
const edge = this.getEdgeCenter(v);
|
|
1018
|
-
if (edge[0] * v[0] + edge[1] * v[1] > max)
|
|
1019
|
-
this.toEdge(v, { buff });
|
|
1020
|
-
}
|
|
1021
|
-
return this;
|
|
1022
|
-
}
|
|
1023
|
-
toCorner(corner = DL, opts = {}) {
|
|
1024
|
-
raw(this).toCorner(corner, opts.buff ?? MED_LARGE_BUFF);
|
|
1025
|
-
return this;
|
|
1026
|
-
}
|
|
1027
|
-
toEdge(edge = LEFT, opts = {}) {
|
|
1028
|
-
raw(this).toEdge(edge, opts.buff ?? MED_LARGE_BUFF);
|
|
1029
|
-
return this;
|
|
1030
|
-
}
|
|
1031
|
-
/** Moves the mobject so that its edge in `direction` lines up with the same edge of
|
|
1032
|
-
* `mobjectOrPoint` (or with the point), on each axis where `direction` is not 0. */
|
|
1033
|
-
alignTo(mobjectOrPoint, direction = ORIGIN) {
|
|
1034
|
-
if (mobjectOrPoint instanceof Mobject)
|
|
1035
|
-
raw(this).alignTo(raw(mobjectOrPoint), direction);
|
|
1036
|
-
else
|
|
1037
|
-
raw(this).alignToPoint(mobjectOrPoint, direction);
|
|
1038
|
-
return this;
|
|
1039
|
-
}
|
|
1040
|
-
replace(mobject, opts = {}) {
|
|
1041
|
-
raw(this).replace(raw(mobject), opts.dimToMatch ?? 0, opts.stretch ?? false);
|
|
1042
|
-
return this;
|
|
1043
|
-
}
|
|
1044
|
-
/** `replace(mobject)`, then a margin of `buff` along `dimToMatch` (Manim `surround`). */
|
|
1045
|
-
surround(mobject, opts = {}) {
|
|
1046
|
-
const { dimToMatch = 0, stretch = false, buff = MED_SMALL_BUFF } = opts;
|
|
1047
|
-
this.replace(mobject, { dimToMatch, stretch });
|
|
1048
|
-
const length = mobject.lengthOverDim(dimToMatch);
|
|
1049
|
-
return length === 0 ? this : this.scale((length + buff) / length);
|
|
1050
|
-
}
|
|
1051
|
-
/** Scales so that the size along `dim` is that of `mobject` (Manim `match_dim_size`). */
|
|
1052
|
-
matchDimSize(mobject, dim, opts = {}) {
|
|
1053
|
-
return this.rescaleToFit(mobject.lengthOverDim(dim), dim, opts);
|
|
1054
|
-
}
|
|
1055
|
-
/** Scales to the width of `mobject` (Manim `match_width`). */
|
|
1056
|
-
matchWidth(mobject, opts = {}) {
|
|
1057
|
-
return this.matchDimSize(mobject, 0, opts);
|
|
1058
|
-
}
|
|
1059
|
-
/** Scales to the height of `mobject` (Manim `match_height`). */
|
|
1060
|
-
matchHeight(mobject, opts = {}) {
|
|
1061
|
-
return this.matchDimSize(mobject, 1, opts);
|
|
1062
|
-
}
|
|
1063
|
-
/** Moves along `dim` to the coordinate of `mobject`'s critical point `direction` (Manim
|
|
1064
|
-
* `match_coord`). */
|
|
1065
|
-
matchCoord(mobject, dim, direction = ORIGIN) {
|
|
1066
|
-
return this.setCoord(mobject.getCoord(dim, direction), dim, direction);
|
|
1067
|
-
}
|
|
1068
|
-
/** Moves along x to the x of `mobject` (Manim `match_x`). */
|
|
1069
|
-
matchX(mobject, direction = ORIGIN) {
|
|
1070
|
-
return this.matchCoord(mobject, 0, direction);
|
|
1071
|
-
}
|
|
1072
|
-
/** Moves along y to the y of `mobject` (Manim `match_y`). */
|
|
1073
|
-
matchY(mobject, direction = ORIGIN) {
|
|
1074
|
-
return this.matchCoord(mobject, 1, direction);
|
|
1075
|
-
}
|
|
1076
|
-
/** Rotates and scales the own straight path so it runs from `start` to `end`. */
|
|
1077
|
-
putStartAndEndOn(start, end) {
|
|
1078
|
-
raw(this).putStartAndEndOn(start, end);
|
|
1079
|
-
return this;
|
|
1080
|
-
}
|
|
1081
|
-
/** Puts each submobject next to the one before it in `direction`. `center` (true) then
|
|
1082
|
-
* moves the group to the origin; `alignedEdge` lines up the edges of the submobjects. */
|
|
1083
|
-
arrange(direction = RIGHT, opts = {}) {
|
|
1084
|
-
const { buff = MED_SMALL_BUFF, center = true, alignedEdge = ORIGIN } = opts;
|
|
1085
|
-
raw(this).arrange(direction, buff, center, alignedEdge);
|
|
1086
|
-
return this;
|
|
1087
|
-
}
|
|
1088
|
-
/** Puts the submobjects in a grid and keeps the center of the group (Manim
|
|
1089
|
-
* `arrange_in_grid`). With no `rows` and `cols`, the grid is about square; with one of them,
|
|
1090
|
-
* the other fits all the submobjects. */
|
|
1091
|
-
arrangeInGrid(opts = {}) {
|
|
1092
|
-
const { buff = MED_SMALL_BUFF, cellAlignment = ORIGIN, rowAlignments, colAlignments, flowOrder = "rd" } = opts;
|
|
1093
|
-
const mobs = this.submobjects;
|
|
1094
|
-
const start = this.getCenter();
|
|
1095
|
-
let cols = gridSize(opts.cols, colAlignments, opts.colWidths);
|
|
1096
|
-
let rows = gridSize(opts.rows, rowAlignments, opts.rowHeights);
|
|
1097
|
-
if (rows === undefined && cols === undefined)
|
|
1098
|
-
cols = Math.ceil(Math.sqrt(mobs.length));
|
|
1099
|
-
if (rows === undefined)
|
|
1100
|
-
rows = Math.ceil(mobs.length / (cols ?? 1));
|
|
1101
|
-
if (cols === undefined)
|
|
1102
|
-
cols = Math.ceil(mobs.length / rows);
|
|
1103
|
-
const [r, c] = [rows, cols];
|
|
1104
|
-
if (r * c < mobs.length) {
|
|
1105
|
-
return wrong("arrangeInGrid", `${String(r)} rows and ${String(c)} columns have too few cells for ${String(mobs.length)} submobjects`);
|
|
1106
|
-
}
|
|
1107
|
-
const [buffX, buffY] = typeof buff === "number" ? [buff, buff] : buff;
|
|
1108
|
-
const alignments = (a, n, map, name, keep) => {
|
|
1109
|
-
if (a === undefined)
|
|
1110
|
-
return Array.from({ length: n }, () => [cellAlignment[0] * keep[0], cellAlignment[1] * keep[1]]);
|
|
1111
|
-
if (a.length !== n)
|
|
1112
|
-
wrong("arrangeInGrid", `${name}Alignments has ${String(a.length)} characters for ${String(n)} ${name}s`);
|
|
1113
|
-
return a
|
|
1114
|
-
.split("")
|
|
1115
|
-
.map((ch) => map[ch] ?? wrong("arrangeInGrid", `${name}Alignments has an unknown character ${JSON.stringify(ch)}`));
|
|
1116
|
-
};
|
|
1117
|
-
const rowAlign = alignments(rowAlignments, r, { u: UP, c: ORIGIN, d: DOWN }, "row", RIGHT).toReversed();
|
|
1118
|
-
const colAlign = alignments(colAlignments, c, { l: LEFT, c: ORIGIN, r: RIGHT }, "col", UP);
|
|
1119
|
-
const index = {
|
|
1120
|
-
dr: (i, j) => r - i - 1 + j * r,
|
|
1121
|
-
dl: (i, j) => r - i - 1 + (c - j - 1) * r,
|
|
1122
|
-
ur: (i, j) => i + j * r,
|
|
1123
|
-
ul: (i, j) => i + (c - j - 1) * r,
|
|
1124
|
-
rd: (i, j) => (r - i - 1) * c + j,
|
|
1125
|
-
ld: (i, j) => (r - i - 1) * c + (c - j - 1),
|
|
1126
|
-
ru: (i, j) => i * c + j,
|
|
1127
|
-
lu: (i, j) => i * c + (c - j - 1),
|
|
1128
|
-
};
|
|
1129
|
-
const at = index[flowOrder] ??
|
|
1130
|
-
wrong("arrangeInGrid", `flowOrder must be rd, dr, ld, dl, ru, ur, lu or ul; got ${flowOrder}`);
|
|
1131
|
-
const grid = Array.from({ length: r }, (_r, i) => Array.from({ length: c }, (_c, j) => mobs[at(i, j)]));
|
|
1132
|
-
const measuredHeights = grid.map((cells) => Math.max(0, ...cells.map((m) => m?.height ?? 0)));
|
|
1133
|
-
const measuredWidths = Array.from({ length: c }, (_c, j) => Math.max(0, ...grid.map((cells) => cells[j]?.width ?? 0)));
|
|
1134
|
-
const sizes = (given, n, measured, name) => {
|
|
1135
|
-
const list = given === undefined || given.length === 0 ? Array.from({ length: n }, () => undefined) : [...given];
|
|
1136
|
-
if (list.length !== n)
|
|
1137
|
-
wrong("arrangeInGrid", `${name} has ${String(list.length)} values for ${String(n)}`);
|
|
1138
|
-
return list.map((v, k) => v ?? measured[k] ?? 0);
|
|
1139
|
-
};
|
|
1140
|
-
const heights = sizes(opts.rowHeights === undefined ? undefined : opts.rowHeights.toReversed(), r, measuredHeights, "rowHeights");
|
|
1141
|
-
const widths = sizes(opts.colWidths, c, measuredWidths, "colWidths");
|
|
1142
|
-
let y = 0;
|
|
1143
|
-
for (let i = 0; i < r; i++) {
|
|
1144
|
-
let x = 0;
|
|
1145
|
-
for (let j = 0; j < c; j++) {
|
|
1146
|
-
const mob = grid[i]?.[j];
|
|
1147
|
-
const w = widths[j] ?? 0;
|
|
1148
|
-
const h = heights[i] ?? 0;
|
|
1149
|
-
if (mob !== undefined) {
|
|
1150
|
-
const align = add(rowAlign[i] ?? ORIGIN, colAlign[j] ?? ORIGIN);
|
|
1151
|
-
const cell = [x + ((align[0] + 1) * w) / 2, y + ((align[1] + 1) * h) / 2];
|
|
1152
|
-
mob.moveTo(cell, { alignedEdge: align });
|
|
1153
|
-
}
|
|
1154
|
-
x += w + buffX;
|
|
1155
|
-
}
|
|
1156
|
-
y += (heights[i] ?? 0) + buffY;
|
|
1157
|
-
}
|
|
1158
|
-
return this.moveTo(start);
|
|
1159
|
-
}
|
|
1160
|
-
/** Sorts the children by `pointToNumFunc` of their centers, or by `submobFunc` (Manim `sort`). */
|
|
1161
|
-
sort(pointToNumFunc = (p) => p[0], opts = {}) {
|
|
1162
|
-
const key = opts.submobFunc ?? ((m) => pointToNumFunc(m.getCenter()));
|
|
1163
|
-
const keyed = this.submobjects.map((m) => ({ m, k: key(m) }));
|
|
1164
|
-
keyed.sort((a, b) => a.k - b.k);
|
|
1165
|
-
setSubmobjects(this, keyed.map((e) => e.m));
|
|
1166
|
-
return this;
|
|
1167
|
-
}
|
|
1168
|
-
/** Puts the children in a random order, the same in each run (Manim `shuffle`). */
|
|
1169
|
-
shuffle(opts = {}) {
|
|
1170
|
-
if (opts.recursive === true)
|
|
1171
|
-
for (const c of this.submobjects)
|
|
1172
|
-
c.shuffle({ recursive: true });
|
|
1173
|
-
const list = this.submobjects;
|
|
1174
|
-
for (let i = list.length - 1; i > 0; i--) {
|
|
1175
|
-
const j = Math.floor(random() * (i + 1));
|
|
1176
|
-
[list[i], list[j]] = [list[j] ?? this, list[i] ?? this];
|
|
1177
|
-
}
|
|
1178
|
-
setSubmobjects(this, list);
|
|
1179
|
-
return this;
|
|
1180
|
-
}
|
|
1181
|
-
/** Reverses the order of the children (Manim `invert`). */
|
|
1182
|
-
invert(opts = {}) {
|
|
1183
|
-
if (opts.recursive === true)
|
|
1184
|
-
for (const c of this.submobjects)
|
|
1185
|
-
c.invert({ recursive: true });
|
|
1186
|
-
setSubmobjects(this, this.submobjects.toReversed());
|
|
1187
|
-
return this;
|
|
1188
|
-
}
|
|
1189
|
-
/** `arrange` (Manim's older name `arrange_submobjects`). */
|
|
1190
|
-
arrangeSubmobjects(...args) {
|
|
1191
|
-
return this.arrange(...args);
|
|
1192
|
-
}
|
|
1193
|
-
/** `sort` (Manim's older name `sort_submobjects`). */
|
|
1194
|
-
sortSubmobjects(...args) {
|
|
1195
|
-
return this.sort(...args);
|
|
1196
|
-
}
|
|
1197
|
-
/** `shuffle` (Manim's older name `shuffle_submobjects`). */
|
|
1198
|
-
shuffleSubmobjects(...args) {
|
|
1199
|
-
return this.shuffle(...args);
|
|
1200
|
-
}
|
|
1201
|
-
// style
|
|
1202
|
-
/** `setFill(color, { opacity })`; `setFill({ opacity })` keeps the colour. */
|
|
1203
|
-
setFill(color, opts = {}) {
|
|
1204
|
-
const c = typeof color === "string" ? color : undefined;
|
|
1205
|
-
const o = typeof color === "object" ? color : opts;
|
|
1206
|
-
raw(this).setFill(c, o.opacity, o.family ?? true);
|
|
1207
|
-
return this;
|
|
1208
|
-
}
|
|
1209
|
-
/** `setStroke(color, { width, opacity })`; `setStroke({ width })` keeps the colour. */
|
|
1210
|
-
setStroke(color, opts = {}) {
|
|
1211
|
-
const c = typeof color === "string" ? color : undefined;
|
|
1212
|
-
const o = typeof color === "object" ? color : opts;
|
|
1213
|
-
raw(this).setStroke(c, o.width, o.opacity, o.family ?? true);
|
|
1214
|
-
return this;
|
|
1215
|
-
}
|
|
1216
|
-
/** Sets the fill and stroke colour of the family, or of this node only with `family: false`.
|
|
1217
|
-
* `alpha` has no effect, as in Manim. */
|
|
1218
|
-
setColor(color = PURE_YELLOW, opts = {}) {
|
|
1219
|
-
raw(this).setColor(color, opts.family ?? true);
|
|
1220
|
-
return this;
|
|
1221
|
-
}
|
|
1222
|
-
/** Sets both opacities of the family, or of this node only (Manim `set_opacity`). */
|
|
1223
|
-
setOpacity(opacity, opts = {}) {
|
|
1224
|
-
raw(this).setOpacity(opacity, opts.family ?? true);
|
|
1225
|
-
return this;
|
|
1226
|
-
}
|
|
1227
|
-
/** Multiplies both opacities by `1 - darkness`, of the family or of this node only. */
|
|
1228
|
-
fade(darkness = 0.5, opts = {}) {
|
|
1229
|
-
raw(this).fade(darkness, opts.family ?? true);
|
|
1230
|
-
return this;
|
|
1231
|
-
}
|
|
1232
|
-
/** Mixes the colour of each member with points `alpha` of the way to `color` (Manim
|
|
1233
|
-
* `fade_to`). */
|
|
1234
|
-
fadeTo(color, alpha, opts = {}) {
|
|
1235
|
-
if (this.hasPoints())
|
|
1236
|
-
this.setColor(mixColor(this.getColor(), color, alpha), { family: false });
|
|
1237
|
-
if (opts.family ?? true)
|
|
1238
|
-
for (const c of this.submobjects)
|
|
1239
|
-
c.fadeTo(color, alpha);
|
|
1240
|
-
return this;
|
|
1241
|
-
}
|
|
1242
|
-
/** The stroke colour, or the fill colour when there is no stroke (Manim `get_color`). */
|
|
1243
|
-
getColor() {
|
|
1244
|
-
return this.getStrokeWidth() === 0 || this.getStrokeOpacity() === 0 ? this.getFillColor() : this.getStrokeColor();
|
|
1245
|
-
}
|
|
1246
|
-
/** Gives the family the colour that `getColor` gives (Manim `to_original_color`). */
|
|
1247
|
-
toOriginalColor() {
|
|
1248
|
-
return this.setColor(this.getColor());
|
|
1249
|
-
}
|
|
1250
|
-
/** Takes the colour of `mobject` (Manim `match_color`). */
|
|
1251
|
-
matchColor(mobject) {
|
|
1252
|
-
return this.setColor(mobject.getColor());
|
|
1253
|
-
}
|
|
1254
|
-
/** Colours the members with points along the colours, in order (Manim
|
|
1255
|
-
* `set_color_by_gradient`). */
|
|
1256
|
-
setColorByGradient(...colors) {
|
|
1257
|
-
return this.setSubmobjectColorsByGradient(...colors);
|
|
1258
|
-
}
|
|
1259
|
-
/** Colours the members with points along the colours, in order (Manim
|
|
1260
|
-
* `set_submobject_colors_by_gradient`). */
|
|
1261
|
-
setSubmobjectColorsByGradient(...colors) {
|
|
1262
|
-
if (colors.length === 0)
|
|
1263
|
-
return wrong("setColorByGradient", "give at least one colour");
|
|
1264
|
-
if (colors.length === 1)
|
|
1265
|
-
return this.setColor(colors[0]);
|
|
1266
|
-
const mobs = this.familyMembersWithPoints();
|
|
1267
|
-
const list = colorsAlong(colors, mobs.length);
|
|
1268
|
-
mobs.forEach((m, i) => m.setColor(list[i], { family: false }));
|
|
1269
|
-
return this;
|
|
1270
|
-
}
|
|
1271
|
-
/** Colours the members with points by the distance of their centers from `center` (the
|
|
1272
|
-
* center by default): `innerColor` at the center, `outerColor` from `radius` (Manim
|
|
1273
|
-
* `set_colors_by_radial_gradient`). */
|
|
1274
|
-
setColorsByRadialGradient(opts = {}) {
|
|
1275
|
-
return this.setSubmobjectColorsByRadialGradient(opts);
|
|
1276
|
-
}
|
|
1277
|
-
/** As `setColorsByRadialGradient` (Manim `set_submobject_colors_by_radial_gradient`). */
|
|
1278
|
-
setSubmobjectColorsByRadialGradient(opts = {}) {
|
|
1279
|
-
const { center = this.getCenter(), radius = 1, innerColor = WHITE, outerColor = BLACK } = opts;
|
|
1280
|
-
for (const m of this.familyMembersWithPoints()) {
|
|
1281
|
-
const t = Math.min(norm(sub(m.getCenter(), center)) / radius, 1);
|
|
1282
|
-
m.setColor(mixColor(innerColor, outerColor, t), { family: false });
|
|
1283
|
-
}
|
|
1284
|
-
return this;
|
|
1285
|
-
}
|
|
1286
|
-
/** Adds a `BackgroundRectangle` as the first child (Manim `add_background_rectangle`). */
|
|
1287
|
-
addBackgroundRectangle(opts = {}) {
|
|
1288
|
-
const { color = BLACK, opacity = 0.75, buff = 0 } = opts;
|
|
1289
|
-
// m.BackgroundRectangle(this, { color, fillOpacity: opacity, buff }); geometry.ts imports this file.
|
|
1290
|
-
const rect = styled(wrap(engine.backgroundRectangle(raw(this), color, opacity, buff)), {
|
|
1291
|
-
strokeWidth: 0,
|
|
1292
|
-
strokeOpacity: 0,
|
|
1293
|
-
});
|
|
1294
|
-
return this.addToBack(rect);
|
|
1295
|
-
}
|
|
1296
|
-
/** Adds a background rectangle to each child (Manim
|
|
1297
|
-
* `add_background_rectangle_to_submobjects`). */
|
|
1298
|
-
addBackgroundRectangleToSubmobjects(opts = {}) {
|
|
1299
|
-
for (const c of this.submobjects)
|
|
1300
|
-
c.addBackgroundRectangle(opts);
|
|
1301
|
-
return this;
|
|
1302
|
-
}
|
|
1303
|
-
/** Adds a background rectangle to each member with points (Manim
|
|
1304
|
-
* `add_background_rectangle_to_family_members_with_points`). */
|
|
1305
|
-
addBackgroundRectangleToFamilyMembersWithPoints(opts = {}) {
|
|
1306
|
-
for (const m of this.familyMembersWithPoints())
|
|
1307
|
-
m.addBackgroundRectangle(opts);
|
|
1308
|
-
return this;
|
|
1309
|
-
}
|
|
1310
|
-
/** The own style becomes the mix of the styles of `mobject1` and `mobject2` at `alpha`
|
|
1311
|
-
* (Manim `interpolate_color`). */
|
|
1312
|
-
interpolateColor(mobject1, mobject2, alpha) {
|
|
1313
|
-
const mix = (a, b) => a + (b - a) * alpha;
|
|
1314
|
-
raw(this).setFill(mixColor(mobject1.getFillColor(), mobject2.getFillColor(), alpha), mix(mobject1.getFillOpacity(), mobject2.getFillOpacity()), false);
|
|
1315
|
-
raw(this).setStroke(mixColor(mobject1.getStrokeColor(), mobject2.getStrokeColor(), alpha), mix(mobject1.getStrokeWidth(), mobject2.getStrokeWidth()), mix(mobject1.getStrokeOpacity(), mobject2.getStrokeOpacity()), false);
|
|
1316
|
-
return this;
|
|
1317
|
-
}
|
|
1318
|
-
/** The own points become `pathFunc` of the points of `mobject1` and `mobject2` at `alpha`
|
|
1319
|
-
* (a straight line by default), and the own style their mix (Manim `interpolate`). Both must
|
|
1320
|
-
* have the same number of points: call `alignData` first. */
|
|
1321
|
-
interpolate(mobject1, mobject2, alpha, pathFunc = (a, b, t) => a.map((p, i) => lerp(p, b[i] ?? p, t))) {
|
|
1322
|
-
const [a, b] = [mobject1.getPoints(), mobject2.getPoints()];
|
|
1323
|
-
if (a.length !== b.length) {
|
|
1324
|
-
wrong("interpolate", `the mobjects have ${String(a.length)} and ${String(b.length)} points`, "call mobject1.alignData(mobject2) first");
|
|
1325
|
-
}
|
|
1326
|
-
this.setPoints(pathFunc(a, b, alpha));
|
|
1327
|
-
return this.interpolateColor(mobject1, mobject2, alpha);
|
|
1328
|
-
}
|
|
1329
|
-
/** Makes this family and `mobject`'s family the same shape, with the same number of points
|
|
1330
|
-
* in each member, for `interpolate` (Manim `align_data`). */
|
|
1331
|
-
alignData(mobject, opts = {}) {
|
|
1332
|
-
this.nullPointAlign(mobject);
|
|
1333
|
-
this.alignSubmobjects(mobject);
|
|
1334
|
-
if (opts.skipPointAlignment !== true)
|
|
1335
|
-
this.alignPoints(mobject);
|
|
1336
|
-
const theirs = mobject.submobjects;
|
|
1337
|
-
this.submobjects.forEach((m, i) => {
|
|
1338
|
-
const other = theirs[i];
|
|
1339
|
-
if (other !== undefined)
|
|
1340
|
-
m.alignData(other, opts);
|
|
1341
|
-
});
|
|
1342
|
-
return this;
|
|
1343
|
-
}
|
|
1344
|
-
/** An invisible mobject with one point at `center` (the center by default) (Manim
|
|
1345
|
-
* `get_point_mobject`). */
|
|
1346
|
-
getPointMobject(center) {
|
|
1347
|
-
return pointMobject(center ?? this.getCenter());
|
|
1348
|
-
}
|
|
1349
|
-
/** Gives both own paths the same number of curves, subpath by subpath (Manim
|
|
1350
|
-
* `align_points`). */
|
|
1351
|
-
alignPoints(mobject) {
|
|
1352
|
-
if (this.getNumPoints() === mobject.getNumPoints())
|
|
1353
|
-
return this;
|
|
1354
|
-
for (const m of [this, mobject])
|
|
1355
|
-
if (m.hasNoPoints())
|
|
1356
|
-
m.setPoints(pointMobject(m.getCenter()).getPoints());
|
|
1357
|
-
const [s1, s2] = [subpathsOf(this.getPoints()), subpathsOf(mobject.getPoints())];
|
|
1358
|
-
const [p1, p2] = [[], []];
|
|
1359
|
-
for (let n = 0; n < Math.max(s1.length, s2.length); n++) {
|
|
1360
|
-
const [a, b] = [nthSubpath(s1, n), nthSubpath(s2, n)];
|
|
1361
|
-
p1.push(...insertCurves(Math.max(0, (b.length - a.length) / 4), a));
|
|
1362
|
-
p2.push(...insertCurves(Math.max(0, (a.length - b.length) / 4), b));
|
|
1363
|
-
}
|
|
1364
|
-
this.setPoints(p1);
|
|
1365
|
-
mobject.setPoints(p2);
|
|
1366
|
-
return this;
|
|
1367
|
-
}
|
|
1368
|
-
/** Adds curves to the own path until it has as many points as `largerMobject` (Manim
|
|
1369
|
-
* `align_points_with_larger`). */
|
|
1370
|
-
alignPointsWithLarger(largerMobject) {
|
|
1371
|
-
const n = (largerMobject.getNumPoints() - this.getNumPoints()) / 4;
|
|
1372
|
-
return n > 0 ? this.setPoints(insertCurves(n, this.getPoints())) : this;
|
|
1373
|
-
}
|
|
1374
|
-
/** Gives both mobjects the same number of children, with faded copies (Manim
|
|
1375
|
-
* `align_submobjects`). */
|
|
1376
|
-
alignSubmobjects(mobject) {
|
|
1377
|
-
const [n1, n2] = [this.length, mobject.length];
|
|
1378
|
-
this.addNMoreSubmobjects(Math.max(0, n2 - n1));
|
|
1379
|
-
mobject.addNMoreSubmobjects(Math.max(0, n1 - n2));
|
|
1380
|
-
return this;
|
|
1381
|
-
}
|
|
1382
|
-
/** When one of the two has no own points and the other has, the other moves its own path
|
|
1383
|
-
* into a child (Manim `null_point_align`). */
|
|
1384
|
-
nullPointAlign(mobject) {
|
|
1385
|
-
for (const [a, b] of [
|
|
1386
|
-
[this, mobject],
|
|
1387
|
-
[mobject, this],
|
|
1388
|
-
]) {
|
|
1389
|
-
if (a.hasNoPoints() && b.hasPoints())
|
|
1390
|
-
b.pushSelfIntoSubmobjects();
|
|
1391
|
-
}
|
|
1392
|
-
return this;
|
|
1393
|
-
}
|
|
1394
|
-
/** Moves the own path into a new first child (Manim `push_self_into_submobjects`). */
|
|
1395
|
-
pushSelfIntoSubmobjects() {
|
|
1396
|
-
const copy = this.copy();
|
|
1397
|
-
setSubmobjects(copy, []);
|
|
1398
|
-
this.resetPoints();
|
|
1399
|
-
return this.add(copy);
|
|
1400
|
-
}
|
|
1401
|
-
/** Adds `n` children: faded copies of the children, spread evenly (Manim
|
|
1402
|
-
* `add_n_more_submobjects`). */
|
|
1403
|
-
addNMoreSubmobjects(n) {
|
|
1404
|
-
if (n === 0)
|
|
1405
|
-
return this;
|
|
1406
|
-
const current = this.submobjects;
|
|
1407
|
-
if (current.length === 0) {
|
|
1408
|
-
return this.add(...Array.from({ length: n }, () => this.getPointMobject()));
|
|
1409
|
-
}
|
|
1410
|
-
const target = current.length + n;
|
|
1411
|
-
const counts = current.map(() => 0);
|
|
1412
|
-
for (let i = 0; i < target; i++) {
|
|
1413
|
-
const k = Math.floor((i * current.length) / target);
|
|
1414
|
-
counts[k] = (counts[k] ?? 0) + 1;
|
|
1415
|
-
}
|
|
1416
|
-
const list = current.flatMap((m, i) => [
|
|
1417
|
-
m,
|
|
1418
|
-
...Array.from({ length: Math.max(0, (counts[i] ?? 1) - 1) }, () => this.repeatSubmobject(m).fade(1)),
|
|
1419
|
-
]);
|
|
1420
|
-
for (const c of raw(this).children())
|
|
1421
|
-
raw(this).remove(c);
|
|
1422
|
-
for (const c of list)
|
|
1423
|
-
raw(this).add(raw(c));
|
|
1424
|
-
return this;
|
|
1425
|
-
}
|
|
1426
|
-
/** A copy of `submob` (Manim `repeat_submobject`). */
|
|
1427
|
-
repeatSubmobject(submob) {
|
|
1428
|
-
return submob.copy();
|
|
1429
|
-
}
|
|
1430
|
-
/** Removes the own points (Manim `reset_points`). */
|
|
1431
|
-
resetPoints() {
|
|
1432
|
-
return this.setPoints([]);
|
|
1433
|
-
}
|
|
1434
|
-
/** Runs `func` on each member with points (Manim `apply_to_family`). */
|
|
1435
|
-
applyToFamily(func) {
|
|
1436
|
-
for (const m of this.familyMembersWithPoints())
|
|
1437
|
-
func(m);
|
|
1438
|
-
return this;
|
|
1439
|
-
}
|
|
1440
|
-
getFillOpacity() {
|
|
1441
|
-
return raw(this).fillOpacity();
|
|
1442
|
-
}
|
|
1443
|
-
getStrokeOpacity() {
|
|
1444
|
-
return raw(this).strokeOpacity();
|
|
1445
|
-
}
|
|
1446
|
-
getStrokeWidth() {
|
|
1447
|
-
return raw(this).strokeWidth();
|
|
1448
|
-
}
|
|
1449
|
-
getFillColor() {
|
|
1450
|
-
return raw(this).fillColor();
|
|
1451
|
-
}
|
|
1452
|
-
getStrokeColor() {
|
|
1453
|
-
return raw(this).strokeColor();
|
|
1454
|
-
}
|
|
1455
|
-
// copies
|
|
1456
|
-
/** Takes the points and styles of `other`'s family, member by member (same tree shape). */
|
|
1457
|
-
becomePointsOf(other) {
|
|
1458
|
-
raw(this).becomePointsOf(raw(other));
|
|
1459
|
-
return this;
|
|
1460
|
-
}
|
|
1461
|
-
/** Becomes a copy of `mobject`: points, styles, children, kind and flags. `matchHeight`,
|
|
1462
|
-
* `matchWidth` and `stretch` first fit the copy to this size, and `matchCenter` moves it
|
|
1463
|
-
* here (Manim `become`). */
|
|
1464
|
-
become(mobject, opts = {}) {
|
|
1465
|
-
const { matchHeight = false, matchWidth = false, matchCenter = false, stretch = false } = opts;
|
|
1466
|
-
let source = mobject;
|
|
1467
|
-
if (stretch || matchHeight || matchWidth || matchCenter) {
|
|
1468
|
-
source = mobject.copy();
|
|
1469
|
-
if (stretch)
|
|
1470
|
-
source.stretchToFitHeight(this.height).stretchToFitWidth(this.width);
|
|
1471
|
-
else {
|
|
1472
|
-
if (matchHeight)
|
|
1473
|
-
source.matchHeight(this);
|
|
1474
|
-
if (matchWidth)
|
|
1475
|
-
source.matchWidth(this);
|
|
1476
|
-
}
|
|
1477
|
-
if (matchCenter)
|
|
1478
|
-
source.moveTo(this.getCenter());
|
|
1479
|
-
}
|
|
1480
|
-
raw(this).becomeCopyOf(raw(source));
|
|
1481
|
-
return this;
|
|
1482
|
-
}
|
|
1483
|
-
/** Keeps a copy as `target` and returns it (Manim `generate_target`). Change the target,
|
|
1484
|
-
* then play `m.MoveToTarget(mob)`. A copy is always deep, so `useDeepcopy` has no effect. */
|
|
1485
|
-
// oxlint-disable-next-line no-unused-vars -- Manim's parameter: a copy is always deep
|
|
1486
|
-
generateTarget(useDeepcopy = false) {
|
|
1487
|
-
return wrap(raw(this).generateTarget());
|
|
1488
|
-
}
|
|
1489
|
-
/** The target of `generateTarget` (Manim `target`). Reading it without a target is an error. */
|
|
1490
|
-
get target() {
|
|
1491
|
-
const t = raw(this).target();
|
|
1492
|
-
if (t === undefined) {
|
|
1493
|
-
return wrong("target", "the Mobject has no target", "call mob.generateTarget() first");
|
|
1494
|
-
}
|
|
1495
|
-
return wrap(t);
|
|
1496
|
-
}
|
|
1497
|
-
set target(target) {
|
|
1498
|
-
raw(this).setTarget(raw(target));
|
|
1499
|
-
}
|
|
1500
|
-
/** Keeps a copy of the current state (Manim `save_state`). `restore()` and `m.Restore(mob)`
|
|
1501
|
-
* go back to it. */
|
|
1502
|
-
saveState() {
|
|
1503
|
-
raw(this).saveState();
|
|
1504
|
-
return this;
|
|
1505
|
-
}
|
|
1506
|
-
/** Becomes the state that `saveState` kept (Manim `restore`). */
|
|
1507
|
-
restore() {
|
|
1508
|
-
raw(this).restore();
|
|
1509
|
-
return this;
|
|
1510
|
-
}
|
|
1511
|
-
/** The own control points, four for each curve: `[anchor, handle, handle, anchor]`
|
|
1512
|
-
* (Manim `points`). The children are not included. */
|
|
1513
|
-
getPoints() {
|
|
1514
|
-
return [...raw(this).getPoints(), ...(pendingOf.get(this) ?? [])];
|
|
1515
|
-
}
|
|
1516
|
-
/** Sets the own path from control points, four for each curve, as `getPoints` gives
|
|
1517
|
-
* them (Manim `set_points`). One point is a path of that point alone (Manim's
|
|
1518
|
-
* `VectorizedPoint`). Points after the last whole curve start a path that is not drawn
|
|
1519
|
-
* until a curve ends it. The style stays. */
|
|
1520
|
-
setPoints(points) {
|
|
1521
|
-
const whole = points.length === 1 ? 1 : points.length - (points.length % 4);
|
|
1522
|
-
raw(this).setPoints(points.slice(0, whole));
|
|
1523
|
-
if (whole === points.length)
|
|
1524
|
-
pendingOf.delete(this);
|
|
1525
|
-
else
|
|
1526
|
-
pendingOf.set(this, points.slice(whole));
|
|
1527
|
-
return this;
|
|
1528
|
-
}
|
|
1529
|
-
/** Sets the own path to a smooth curve through `points` (Manim `set_points_smoothly`).
|
|
1530
|
-
* The style stays. */
|
|
1531
|
-
setPointsSmoothly(points) {
|
|
1532
|
-
return this.setPointsAsCorners(points).makeSmooth();
|
|
1533
|
-
}
|
|
1534
|
-
// VMobject: the path
|
|
1535
|
-
/** The number of points of a curve: 4 (Manim `n_points_per_curve`). */
|
|
1536
|
-
get nPointsPerCurve() {
|
|
1537
|
-
return 4;
|
|
1538
|
-
}
|
|
1539
|
-
/** Removes the own points (Manim `clear_points`). */
|
|
1540
|
-
clearPoints() {
|
|
1541
|
-
return this.setPoints([]);
|
|
1542
|
-
}
|
|
1543
|
-
/** Adds points after the own points (Manim `append_points`). */
|
|
1544
|
-
appendPoints(newPoints) {
|
|
1545
|
-
return this.setPoints([...this.getPoints(), ...newPoints]);
|
|
1546
|
-
}
|
|
1547
|
-
/** Gives the own points `newLength` points: cut, or repeated from the start (Manim
|
|
1548
|
-
* `resize_points`). */
|
|
1549
|
-
resizePoints(newLength, resizeFunc = (ps, n) => ps.length === 0 ? [] : Array.from({ length: n }, (_v, i) => ps[i % ps.length] ?? ORIGIN)) {
|
|
1550
|
-
const own = this.getPoints();
|
|
1551
|
-
return own.length === newLength ? this : this.setPoints(resizeFunc(own, newLength));
|
|
1552
|
-
}
|
|
1553
|
-
/** Sets the own curves from four lists: start anchors, first handles, second handles and end
|
|
1554
|
-
* anchors (Manim `set_anchors_and_handles`). */
|
|
1555
|
-
setAnchorsAndHandles(anchors1, handles1, handles2, anchors2) {
|
|
1556
|
-
const n = anchors1.length;
|
|
1557
|
-
if (handles1.length !== n || handles2.length !== n || anchors2.length !== n) {
|
|
1558
|
-
wrong("setAnchorsAndHandles", "the four lists must have the same length");
|
|
1559
|
-
}
|
|
1560
|
-
return this.setPoints(anchors1.flatMap((a, i) => [a, handles1[i] ?? a, handles2[i] ?? a, anchors2[i] ?? a]));
|
|
1561
|
-
}
|
|
1562
|
-
/** Starts a new subpath at `point` (Manim `start_new_path`). */
|
|
1563
|
-
startNewPath(point) {
|
|
1564
|
-
const own = this.getPoints();
|
|
1565
|
-
const rest = own.length % 4;
|
|
1566
|
-
if (rest === 0)
|
|
1567
|
-
return this.setPoints([...own, point]);
|
|
1568
|
-
// Close the open curve with its start anchor, as Manim does.
|
|
1569
|
-
const last = curvesOf(own).at(-1)?.[0] ?? own[own.length - rest] ?? point;
|
|
1570
|
-
return this.setPoints([...own, ...Array.from({ length: 4 - rest }, () => last), point]);
|
|
1571
|
-
}
|
|
1572
|
-
/** Adds one curve (Manim `add_cubic_bezier_curve`). */
|
|
1573
|
-
addCubicBezierCurve(anchor1, handle1, handle2, anchor2) {
|
|
1574
|
-
return this.appendPoints([anchor1, handle1, handle2, anchor2]);
|
|
1575
|
-
}
|
|
1576
|
-
/** Adds curves, each `[anchor, handle, handle, anchor]` (Manim `add_cubic_bezier_curves`). */
|
|
1577
|
-
addCubicBezierCurves(curves) {
|
|
1578
|
-
return this.appendPoints(curves.flat());
|
|
1579
|
-
}
|
|
1580
|
-
/** Adds a curve from the last point (Manim `add_cubic_bezier_curve_to`). A path with no
|
|
1581
|
-
* points is an error: call `startNewPath` first. */
|
|
1582
|
-
addCubicBezierCurveTo(handle1, handle2, anchor) {
|
|
1583
|
-
if (!this.hasPoints()) {
|
|
1584
|
-
return wrong("addCubicBezierCurveTo", "the Mobject has no points", "call startNewPath(point) first");
|
|
1585
|
-
}
|
|
1586
|
-
if (this.hasNewPathStarted())
|
|
1587
|
-
return this.appendPoints([handle1, handle2, anchor]);
|
|
1588
|
-
return this.appendPoints([this.getLastPoint(), handle1, handle2, anchor]);
|
|
1589
|
-
}
|
|
1590
|
-
/** Adds a quadratic curve from the last point, as a cubic (Manim
|
|
1591
|
-
* `add_quadratic_bezier_curve_to`). */
|
|
1592
|
-
addQuadraticBezierCurveTo(handle, anchor) {
|
|
1593
|
-
const last = this.getLastPoint();
|
|
1594
|
-
return this.addCubicBezierCurveTo(add(mul(handle, 2 / 3), mul(last, 1 / 3)), add(mul(handle, 2 / 3), mul(anchor, 1 / 3)), anchor);
|
|
1595
|
-
}
|
|
1596
|
-
/** Adds a straight line from the last point (Manim `add_line_to`). */
|
|
1597
|
-
addLineTo(point) {
|
|
1598
|
-
const last = this.getLastPoint();
|
|
1599
|
-
return this.addCubicBezierCurveTo(lerp(last, point, 1 / 3), lerp(last, point, 2 / 3), point);
|
|
1600
|
-
}
|
|
1601
|
-
/** Adds a curve that goes on smoothly from the last curve: `addSmoothCurveTo(anchor)` or
|
|
1602
|
-
* `addSmoothCurveTo(handle2, anchor)` (Manim `add_smooth_curve_to`). */
|
|
1603
|
-
addSmoothCurveTo(...points) {
|
|
1604
|
-
if (points.length !== 1 && points.length !== 2) {
|
|
1605
|
-
return wrong("addSmoothCurveTo", `give 1 or 2 points; got ${String(points.length)}`);
|
|
1606
|
-
}
|
|
1607
|
-
const anchor = points.at(-1) ?? ORIGIN;
|
|
1608
|
-
if (this.hasNewPathStarted())
|
|
1609
|
-
return this.addLineTo(anchor);
|
|
1610
|
-
const own = this.getPoints();
|
|
1611
|
-
const [lastH2, lastA2] = [own.at(-2), own.at(-1)];
|
|
1612
|
-
if (lastH2 === undefined || lastA2 === undefined) {
|
|
1613
|
-
return wrong("addSmoothCurveTo", "the Mobject has no points", "call startNewPath(point) first");
|
|
1614
|
-
}
|
|
1615
|
-
const tangent = sub(lastA2, lastH2);
|
|
1616
|
-
const handle1 = add(lastA2, tangent);
|
|
1617
|
-
let handle2 = points.length === 2 ? points[0] : undefined;
|
|
1618
|
-
if (handle2 === undefined) {
|
|
1619
|
-
// A half turn of the tangent about the line to the anchor: its mirror image.
|
|
1620
|
-
const to = sub(anchor, lastA2);
|
|
1621
|
-
const n = norm(to);
|
|
1622
|
-
const u = n === 0 ? [1, 0] : mul(to, 1 / n);
|
|
1623
|
-
const along = tangent[0] * u[0] + tangent[1] * u[1];
|
|
1624
|
-
const mirrored = sub(mul(u, 2 * along), tangent);
|
|
1625
|
-
handle2 = sub(anchor, mirrored);
|
|
1626
|
-
}
|
|
1627
|
-
return this.appendPoints([lastA2, handle1, handle2, anchor]);
|
|
1628
|
-
}
|
|
1629
|
-
/** True when the last point starts a path that has no curve yet (Manim
|
|
1630
|
-
* `has_new_path_started`). */
|
|
1631
|
-
hasNewPathStarted() {
|
|
1632
|
-
return this.getPoints().length % 4 === 1;
|
|
1633
|
-
}
|
|
1634
|
-
/** The last own point (Manim `get_last_point`). */
|
|
1635
|
-
getLastPoint() {
|
|
1636
|
-
return this.getPoints().at(-1) ?? wrong("getLastPoint", "the Mobject has no points");
|
|
1637
|
-
}
|
|
1638
|
-
/** Adds a line back to the start of the last subpath, if the path is not closed (Manim
|
|
1639
|
-
* `close_path`). */
|
|
1640
|
-
closePath() {
|
|
1641
|
-
const own = this.getPoints();
|
|
1642
|
-
const first = own[0];
|
|
1643
|
-
const last = own.at(-1);
|
|
1644
|
-
if (first !== undefined && last !== undefined && this.considerPointsEquals(first, last))
|
|
1645
|
-
return this;
|
|
1646
|
-
const start = this.getSubpaths().at(-1)?.[0];
|
|
1647
|
-
return start === undefined ? this : this.addLineTo(start);
|
|
1648
|
-
}
|
|
1649
|
-
/** Adds straight lines from the last point through `points` (Manim
|
|
1650
|
-
* `add_points_as_corners`). */
|
|
1651
|
-
addPointsAsCorners(points) {
|
|
1652
|
-
if (points.length === 0)
|
|
1653
|
-
return this;
|
|
1654
|
-
let own = this.getPoints();
|
|
1655
|
-
const start = own.at(-1) ?? wrong("addPointsAsCorners", "the Mobject has no points", "call startNewPath(point) first");
|
|
1656
|
-
if (own.length % 4 === 1)
|
|
1657
|
-
own = own.slice(0, -1);
|
|
1658
|
-
const corners = [start, ...points];
|
|
1659
|
-
const added = points.flatMap((end, i) => {
|
|
1660
|
-
const from = corners[i] ?? start;
|
|
1661
|
-
return [from, lerp(from, end, 1 / 3), lerp(from, end, 2 / 3), end];
|
|
1662
|
-
});
|
|
1663
|
-
return this.setPoints([...own, ...added]);
|
|
1664
|
-
}
|
|
1665
|
-
/** The own path becomes straight lines through `points` (Manim `set_points_as_corners`). */
|
|
1666
|
-
setPointsAsCorners(points) {
|
|
1667
|
-
const a = points.slice(0, -1);
|
|
1668
|
-
const b = points.slice(1);
|
|
1669
|
-
return this.setAnchorsAndHandles(a, a.map((p, i) => lerp(p, b[i] ?? p, 1 / 3)), a.map((p, i) => lerp(p, b[i] ?? p, 2 / 3)), b);
|
|
1670
|
-
}
|
|
1671
|
-
/** Moves the handles of each member with points: `"smooth"` makes each subpath a smooth
|
|
1672
|
-
* curve through its anchors, `"jagged"` makes straight lines (Manim `change_anchor_mode`). */
|
|
1673
|
-
changeAnchorMode(mode) {
|
|
1674
|
-
for (const m of this.familyMembersWithPoints()) {
|
|
1675
|
-
const out = [];
|
|
1676
|
-
for (const subpath of m.getSubpaths()) {
|
|
1677
|
-
const curves = curvesOf(subpath);
|
|
1678
|
-
const anchors = [...curves.map((c) => c[0] ?? ORIGIN), subpath.at(-1) ?? ORIGIN];
|
|
1679
|
-
const [h1, h2] = mode === "smooth"
|
|
1680
|
-
? getSmoothCubicBezierHandlePoints(anchors)
|
|
1681
|
-
: [
|
|
1682
|
-
anchors.slice(0, -1).map((p, i) => lerp(p, anchors[i + 1] ?? p, 1 / 3)),
|
|
1683
|
-
anchors.slice(0, -1).map((p, i) => lerp(p, anchors[i + 1] ?? p, 2 / 3)),
|
|
1684
|
-
];
|
|
1685
|
-
curves.forEach((c, i) => {
|
|
1686
|
-
out.push(c[0] ?? ORIGIN, h1[i] ?? ORIGIN, h2[i] ?? ORIGIN, c[3] ?? ORIGIN);
|
|
1687
|
-
});
|
|
1688
|
-
}
|
|
1689
|
-
m.setPoints(out);
|
|
1690
|
-
}
|
|
1691
|
-
return this;
|
|
1692
|
-
}
|
|
1693
|
-
/** `changeAnchorMode("smooth")` (Manim `make_smooth`). */
|
|
1694
|
-
makeSmooth() {
|
|
1695
|
-
return this.changeAnchorMode("smooth");
|
|
1696
|
-
}
|
|
1697
|
-
/** `changeAnchorMode("jagged")` (Manim `make_jagged`). */
|
|
1698
|
-
makeJagged() {
|
|
1699
|
-
return this.changeAnchorMode("jagged");
|
|
1700
|
-
}
|
|
1701
|
-
/** Adds a subpath of whole curves (Manim `add_subpath`). */
|
|
1702
|
-
addSubpath(points) {
|
|
1703
|
-
if (points.length % 4 !== 0) {
|
|
1704
|
-
wrong("addSubpath", `points must have four points for each curve; got ${String(points.length)}`);
|
|
1705
|
-
}
|
|
1706
|
-
return this.appendPoints(points);
|
|
1707
|
-
}
|
|
1708
|
-
/** Adds the own points of `vectorizedMobject` (Manim `append_vectorized_mobject`). */
|
|
1709
|
-
appendVectorizedMobject(vectorizedMobject) {
|
|
1710
|
-
if (this.hasNewPathStarted())
|
|
1711
|
-
this.setPoints(this.getPoints().slice(0, -1));
|
|
1712
|
-
return this.appendPoints(vectorizedMobject.getPoints());
|
|
1713
|
-
}
|
|
1714
|
-
/** Moves each handle `factor` of the way from its anchor (Manim
|
|
1715
|
-
* `scale_handle_to_anchor_distances`). */
|
|
1716
|
-
scaleHandleToAnchorDistances(factor) {
|
|
1717
|
-
for (const m of this.familyMembersWithPoints()) {
|
|
1718
|
-
if (m.getPoints().length < 4)
|
|
1719
|
-
continue;
|
|
1720
|
-
const [a1, h1, h2, a2] = m.getAnchorsAndHandles();
|
|
1721
|
-
m.setAnchorsAndHandles(a1, a1.map((a, i) => add(a, mul(sub(h1[i] ?? a, a), factor))), a2.map((a, i) => add(a, mul(sub(h2[i] ?? a, a), factor))), a2);
|
|
1722
|
-
}
|
|
1723
|
-
return this;
|
|
1724
|
-
}
|
|
1725
|
-
/** True when the points are equal up to `toleranceForPointEquality` (Manim
|
|
1726
|
-
* `consider_points_equals`). */
|
|
1727
|
-
considerPointsEquals(p0, p1) {
|
|
1728
|
-
const atol = this.toleranceForPointEquality;
|
|
1729
|
-
return [0, 1].every((k) => Math.abs((p0[k] ?? 0) - (p1[k] ?? 0)) <= atol + 1e-5 * Math.abs(p1[k] ?? 0));
|
|
1730
|
-
}
|
|
1731
|
-
/** `considerPointsEquals` (Manim `consider_points_equals_2d`: points have two numbers here). */
|
|
1732
|
-
considerPointsEquals2d(p0, p1) {
|
|
1733
|
-
return this.considerPointsEquals(p0, p1);
|
|
1734
|
-
}
|
|
1735
|
-
/** The curves of `points`, four points each (Manim `get_cubic_bezier_tuples_from_points`). */
|
|
1736
|
-
getCubicBezierTuplesFromPoints(points) {
|
|
1737
|
-
return curvesOf(points);
|
|
1738
|
-
}
|
|
1739
|
-
/** As `getCubicBezierTuplesFromPoints` (Manim `gen_cubic_bezier_tuples_from_points`). */
|
|
1740
|
-
genCubicBezierTuplesFromPoints(points) {
|
|
1741
|
-
return curvesOf(points);
|
|
1742
|
-
}
|
|
1743
|
-
/** The subpaths of `points`: a curve that does not start where the last one ended starts a
|
|
1744
|
-
* new subpath (Manim `get_subpaths_from_points`). */
|
|
1745
|
-
getSubpathsFromPoints(points) {
|
|
1746
|
-
return this.getSubpathSplitIndicesFromPoints(points).map(([a, b]) => points.slice(a, b));
|
|
1747
|
-
}
|
|
1748
|
-
/** As `getSubpathsFromPoints` (Manim `gen_subpaths_from_points_2d`). */
|
|
1749
|
-
genSubpathsFromPoints2d(points) {
|
|
1750
|
-
return this.getSubpathsFromPoints(points);
|
|
1751
|
-
}
|
|
1752
|
-
/** `[start, end)` point indices of each subpath of `points` (Manim
|
|
1753
|
-
* `get_subpath_split_indices_from_points`). `nDims` has no effect: points have two numbers. */
|
|
1754
|
-
// oxlint-disable-next-line no-unused-vars -- Manim's parameter: points have two numbers
|
|
1755
|
-
getSubpathSplitIndicesFromPoints(points, nDims = 2) {
|
|
1756
|
-
const out = [];
|
|
1757
|
-
const curves = Math.floor(points.length / 4);
|
|
1758
|
-
for (let i = 0; i < curves; i++) {
|
|
1759
|
-
const start = points[4 * i] ?? ORIGIN;
|
|
1760
|
-
const previous = out.at(-1);
|
|
1761
|
-
const end = points[4 * i - 1];
|
|
1762
|
-
if (previous !== undefined && end !== undefined && this.considerPointsEquals(end, start)) {
|
|
1763
|
-
previous[1] = 4 * i + 4;
|
|
1764
|
-
}
|
|
1765
|
-
else
|
|
1766
|
-
out.push([4 * i, 4 * i + 4]);
|
|
1767
|
-
}
|
|
1768
|
-
return out;
|
|
1769
|
-
}
|
|
1770
|
-
/** The subpaths of the own points (Manim `get_subpaths`). */
|
|
1771
|
-
getSubpaths() {
|
|
1772
|
-
return this.getSubpathsFromPoints(this.getPoints());
|
|
1773
|
-
}
|
|
1774
|
-
/** The four points of curve `n` (Manim `get_nth_curve_points`). */
|
|
1775
|
-
getNthCurvePoints(n) {
|
|
1776
|
-
const c = curvesOf(this.getPoints())[n];
|
|
1777
|
-
return c ?? wrong("getNthCurvePoints", `the Mobject has no curve ${String(n)}`);
|
|
1778
|
-
}
|
|
1779
|
-
/** Curve `n` as a function of `t` from 0 to 1 (Manim `get_nth_curve_function`). */
|
|
1780
|
-
getNthCurveFunction(n) {
|
|
1781
|
-
const c = this.getNthCurvePoints(n);
|
|
1782
|
-
return (t) => bezierAt(c, t);
|
|
1783
|
-
}
|
|
1784
|
-
/** The lengths of the `samplePoints - 1` straight pieces of curve `n` (default 10 samples)
|
|
1785
|
-
* (Manim `get_nth_curve_length_pieces`). */
|
|
1786
|
-
getNthCurveLengthPieces(n, samplePoints = 10) {
|
|
1787
|
-
const f = this.getNthCurveFunction(n);
|
|
1788
|
-
const ps = Array.from({ length: samplePoints }, (_v, i) => f(samplePoints === 1 ? 0 : i / (samplePoints - 1)));
|
|
1789
|
-
return ps.slice(1).map((p, i) => norm(sub(p, ps[i] ?? p)));
|
|
1790
|
-
}
|
|
1791
|
-
/** The length of curve `n` (Manim `get_nth_curve_length`). */
|
|
1792
|
-
getNthCurveLength(n, samplePoints = 10) {
|
|
1793
|
-
return this.getNthCurveLengthPieces(n, samplePoints).reduce((a, b) => a + b, 0);
|
|
1794
|
-
}
|
|
1795
|
-
/** `[function, length]` of curve `n` (Manim `get_nth_curve_function_with_length`). */
|
|
1796
|
-
getNthCurveFunctionWithLength(n, samplePoints = 10) {
|
|
1797
|
-
return [this.getNthCurveFunction(n), this.getNthCurveLength(n, samplePoints)];
|
|
1798
|
-
}
|
|
1799
|
-
/** The number of own curves (Manim `get_num_curves`). */
|
|
1800
|
-
getNumCurves() {
|
|
1801
|
-
return Math.floor(this.getPoints().length / 4);
|
|
1802
|
-
}
|
|
1803
|
-
/** Each own curve as a function of `t` (Manim `get_curve_functions`). */
|
|
1804
|
-
getCurveFunctions() {
|
|
1805
|
-
return Array.from({ length: this.getNumCurves() }, (_v, i) => this.getNthCurveFunction(i));
|
|
1806
|
-
}
|
|
1807
|
-
/** `[function, length]` of each own curve (Manim `get_curve_functions_with_lengths`). */
|
|
1808
|
-
getCurveFunctionsWithLengths(opts = {}) {
|
|
1809
|
-
return Array.from({ length: this.getNumCurves() }, (_v, i) => this.getNthCurveFunctionWithLength(i, opts.samplePoints));
|
|
1810
|
-
}
|
|
1811
|
-
/** `[startAnchors, firstHandles, secondHandles, endAnchors]` (Manim
|
|
1812
|
-
* `get_anchors_and_handles`). */
|
|
1813
|
-
getAnchorsAndHandles() {
|
|
1814
|
-
const curves = curvesOf(this.getPoints());
|
|
1815
|
-
const at = (k) => curves.map((c) => c[k] ?? ORIGIN);
|
|
1816
|
-
return [at(0), at(1), at(2), at(3)];
|
|
1817
|
-
}
|
|
1818
|
-
/** The start anchor of each own curve (Manim `get_start_anchors`). */
|
|
1819
|
-
getStartAnchors() {
|
|
1820
|
-
return this.getAnchorsAndHandles()[0];
|
|
1821
|
-
}
|
|
1822
|
-
/** The end anchor of each own curve (Manim `get_end_anchors`). */
|
|
1823
|
-
getEndAnchors() {
|
|
1824
|
-
return this.getAnchorsAndHandles()[3];
|
|
1825
|
-
}
|
|
1826
|
-
/** The start and end anchor of each own curve, in turn (Manim `get_anchors`). */
|
|
1827
|
-
getAnchors() {
|
|
1828
|
-
const [a1, , , a2] = this.getAnchorsAndHandles();
|
|
1829
|
-
return a1.flatMap((a, i) => [a, a2[i] ?? a]);
|
|
1830
|
-
}
|
|
1831
|
-
/** The length of the own path (Manim `get_arc_length`). */
|
|
1832
|
-
getArcLength(samplePointsPerCurve) {
|
|
1833
|
-
return Array.from({ length: this.getNumCurves() }, (_v, i) => this.getNthCurveLength(i, samplePointsPerCurve)).reduce((a, b) => a + b, 0);
|
|
1834
|
-
}
|
|
1835
|
-
/** Adds `n` curves to the own path: the curves are cut into pieces (Manim
|
|
1836
|
-
* `insert_n_curves`). */
|
|
1837
|
-
insertNCurves(n) {
|
|
1838
|
-
const own = this.getPoints();
|
|
1839
|
-
const started = own.length % 4 === 1 ? own.at(-1) : undefined;
|
|
1840
|
-
this.setPoints(this.insertNCurvesToPointList(n, started === undefined ? own : own.slice(0, -1)));
|
|
1841
|
-
return started === undefined ? this : this.appendPoints([started]);
|
|
1842
|
-
}
|
|
1843
|
-
/** `points` with `n` more curves (Manim `insert_n_curves_to_point_list`). */
|
|
1844
|
-
insertNCurvesToPointList(n, points) {
|
|
1845
|
-
return insertCurves(n, points);
|
|
1846
|
-
}
|
|
1847
|
-
/** Each mobject has one fill colour and one stroke colour: nothing to align (Manim
|
|
1848
|
-
* `align_rgbas`). */
|
|
1849
|
-
// oxlint-disable-next-line no-unused-vars -- Manim's parameter: there is nothing to align
|
|
1850
|
-
alignRgbas(vmobject) {
|
|
1851
|
-
return this;
|
|
1852
|
-
}
|
|
1853
|
-
/** A copy whose own path is the part of this path from proportion `a` to `b` (Manim
|
|
1854
|
-
* `get_subcurve`). */
|
|
1855
|
-
getSubcurve(a, b) {
|
|
1856
|
-
return this.copy().pointwiseBecomePartial(this, a, b);
|
|
1857
|
-
}
|
|
1858
|
-
/** `"CW"` or `"CCW"`: the direction of the anchors (Manim `get_direction`). A brace gives its
|
|
1859
|
-
* direction as a unit vector, as in Manim. */
|
|
1860
|
-
getDirection() {
|
|
1861
|
-
return shoelaceDirection(this.getStartAnchors());
|
|
1862
|
-
}
|
|
1863
|
-
/** Reverses the own points (Manim `reverse_direction`). */
|
|
1864
|
-
reverseDirection() {
|
|
1865
|
-
return this.setPoints(this.getPoints().toReversed());
|
|
1866
|
-
}
|
|
1867
|
-
/** Reverses the own points when the direction is not `targetDirection` (Manim
|
|
1868
|
-
* `force_direction`). */
|
|
1869
|
-
forceDirection(targetDirection) {
|
|
1870
|
-
return this.getDirection() === targetDirection ? this : this.reverseDirection();
|
|
1871
|
-
}
|
|
1872
|
-
// VMobject: the style
|
|
1873
|
-
/** Sets the fill and the stroke, of the family or of this node only (Manim `set_style`). */
|
|
1874
|
-
setStyle(opts = {}) {
|
|
1875
|
-
const { fillColor, fillOpacity, strokeColor, strokeWidth, strokeOpacity, family = true } = opts;
|
|
1876
|
-
raw(this).setFill(fillColor, fillOpacity, family);
|
|
1877
|
-
raw(this).setStroke(strokeColor, strokeWidth, strokeOpacity, family);
|
|
1878
|
-
return this;
|
|
1879
|
-
}
|
|
1880
|
-
/** The fill and the stroke (Manim `get_style`). `simple` has no effect: a mobject has one
|
|
1881
|
-
* colour and one opacity each. */
|
|
1882
|
-
getStyle(_opts = {}) {
|
|
1883
|
-
return {
|
|
1884
|
-
fillColor: this.getFillColor(),
|
|
1885
|
-
fillOpacity: this.getFillOpacity(),
|
|
1886
|
-
strokeColor: this.getStrokeColor(),
|
|
1887
|
-
strokeWidth: this.getStrokeWidth(),
|
|
1888
|
-
strokeOpacity: this.getStrokeOpacity(),
|
|
1889
|
-
};
|
|
1890
|
-
}
|
|
1891
|
-
/** Takes the style of `vmobject`, and with `family`, the children take the styles of its
|
|
1892
|
-
* children, paired evenly (Manim `match_style`). */
|
|
1893
|
-
matchStyle(vmobject, opts = {}) {
|
|
1894
|
-
this.setStyle({ ...vmobject.getStyle({ simple: true }), family: false });
|
|
1895
|
-
raw(this).setCapStyle(raw(vmobject).capStyle(), false);
|
|
1896
|
-
raw(this).setJointType(raw(vmobject).jointType(), false);
|
|
1897
|
-
if (opts.family === false)
|
|
1898
|
-
return this;
|
|
1899
|
-
const own = this.submobjects;
|
|
1900
|
-
if (own.length === 0)
|
|
1901
|
-
return this;
|
|
1902
|
-
const theirs = vmobject.submobjects.length === 0 ? [vmobject] : vmobject.submobjects;
|
|
1903
|
-
const length = Math.max(own.length, theirs.length);
|
|
1904
|
-
for (let i = 0; i < length; i++) {
|
|
1905
|
-
const a = own[Math.floor((i * own.length) / length)];
|
|
1906
|
-
const b = theirs[Math.floor((i * theirs.length) / length)];
|
|
1907
|
-
if (a !== undefined && b !== undefined)
|
|
1908
|
-
a.matchStyle(b);
|
|
1909
|
-
}
|
|
1910
|
-
return this;
|
|
1911
|
-
}
|
|
1912
|
-
/** The ends of the stroke of the family (Manim `set_cap_style`). */
|
|
1913
|
-
setCapStyle(capStyle) {
|
|
1914
|
-
raw(this).setCapStyle(capName(capStyle), true);
|
|
1915
|
-
return this;
|
|
1916
|
-
}
|
|
1917
|
-
/** The ends of the stroke (Manim `cap_style`). */
|
|
1918
|
-
get capStyle() {
|
|
1919
|
-
return CAP_OF[raw(this).capStyle()];
|
|
1920
|
-
}
|
|
1921
|
-
/** The corners of the stroke (Manim `joint_type`). Setting it changes the family. */
|
|
1922
|
-
get jointType() {
|
|
1923
|
-
return JOINT_OF[raw(this).jointType()];
|
|
1924
|
-
}
|
|
1925
|
-
set jointType(value) {
|
|
1926
|
-
raw(this).setJointType(jointName(value), true);
|
|
1927
|
-
}
|
|
1928
|
-
/** `[[r, g, b, opacity]]`, each from 0 to 1 (Manim `get_fill_rgbas`). */
|
|
1929
|
-
getFillRgbas() {
|
|
1930
|
-
return [[...rgbUnit(this.getFillColor()), this.getFillOpacity()]];
|
|
1931
|
-
}
|
|
1932
|
-
/** `[[r, g, b, opacity]]` of the stroke (Manim `get_stroke_rgbas`). */
|
|
1933
|
-
getStrokeRgbas() {
|
|
1934
|
-
return [[...rgbUnit(this.getStrokeColor()), this.getStrokeOpacity()]];
|
|
1935
|
-
}
|
|
1936
|
-
/** `[[r, g, b, opacity]]` of `color` (Manim `generate_rgbas_array`). */
|
|
1937
|
-
generateRgbasArray(color, opacity) {
|
|
1938
|
-
return [[...rgbUnit(color), opacity]];
|
|
1939
|
-
}
|
|
1940
|
-
/** Sets the fill (`"fill_rgbas"`) or the stroke (`"stroke_rgbas"`) of the family (Manim
|
|
1941
|
-
* `update_rgbas_array`). */
|
|
1942
|
-
updateRgbasArray(arrayName, color, opacity) {
|
|
1943
|
-
if (arrayName === "fill_rgbas")
|
|
1944
|
-
raw(this).setFill(color, opacity, true);
|
|
1945
|
-
else
|
|
1946
|
-
raw(this).setStroke(color, undefined, opacity, true);
|
|
1947
|
-
return this;
|
|
1948
|
-
}
|
|
1949
|
-
/** `[fill colour]` (Manim `get_fill_colors`: one colour each here). */
|
|
1950
|
-
getFillColors() {
|
|
1951
|
-
return [this.getFillColor()];
|
|
1952
|
-
}
|
|
1953
|
-
/** `[fill opacity]` (Manim `get_fill_opacities`). */
|
|
1954
|
-
getFillOpacities() {
|
|
1955
|
-
return [this.getFillOpacity()];
|
|
1956
|
-
}
|
|
1957
|
-
/** `[stroke colour]` (Manim `get_stroke_colors`). */
|
|
1958
|
-
getStrokeColors() {
|
|
1959
|
-
return [this.getStrokeColor()];
|
|
1960
|
-
}
|
|
1961
|
-
/** `[stroke opacity]` (Manim `get_stroke_opacities`). */
|
|
1962
|
-
getStrokeOpacities() {
|
|
1963
|
-
return [this.getStrokeOpacity()];
|
|
1964
|
-
}
|
|
1965
|
-
/** Adds a filled triangle tip in the stroke colour (Manim `add_tip`). The path is
|
|
1966
|
-
* shortened to end at the base of the tip. A tip at the end makes the Mobject an arrow
|
|
1967
|
-
* for the arrow checks. */
|
|
1968
|
-
addTip(opts = {}) {
|
|
1969
|
-
const tipLength = opts.tipLength ?? 0.35;
|
|
1970
|
-
raw(this).addTip(tipLength, opts.tipWidth ?? tipLength, opts.atStart ?? false);
|
|
1971
|
-
return this;
|
|
1972
|
-
}
|
|
1973
|
-
/** The text without whitespace ("" for a non-text). */
|
|
1974
|
-
text() {
|
|
1975
|
-
return raw(this).text();
|
|
1976
|
-
}
|
|
1977
|
-
get fontSize() {
|
|
1978
|
-
return raw(this).fontSize();
|
|
1979
|
-
}
|
|
1980
|
-
// updaters
|
|
1981
|
-
/** Runs `updateFunction(m, dt)` on every frame while on stage; with `callUpdater`, once now
|
|
1982
|
-
* with dt 0. `index` puts it at that place among the updaters of this node.
|
|
1983
|
-
*
|
|
1984
|
-
* An updater that takes `dt` runs on the real clock: in a move, it runs after the move with
|
|
1985
|
-
* real seconds, so its speed is the same in the move and after it. Its object is then closed
|
|
1986
|
-
* in the move: from the first part of the move, the move code does not read or change it.
|
|
1987
|
-
* With `clock: "move"`, it runs on the clock of the move: with the Manim seconds of each
|
|
1988
|
-
* part, together with the animations. An updater with no `dt` runs on the clock of the move. */
|
|
1989
|
-
addUpdater(updateFunction, opts = {}) {
|
|
1990
|
-
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- the updater is called with this node
|
|
1991
|
-
const f = updateFunction;
|
|
1992
|
-
const list = updatersOf.get(this) ?? [];
|
|
1993
|
-
const entry = {
|
|
1994
|
-
f,
|
|
1995
|
-
id: register((dt) => {
|
|
1996
|
-
f(this, dt);
|
|
1997
|
-
}),
|
|
1998
|
-
real: f.length >= 2 && opts.clock !== "move",
|
|
1999
|
-
};
|
|
2000
|
-
if (opts.index === undefined || opts.index >= list.length) {
|
|
2001
|
-
raw(this).addUpdater(entry.id, false, entry.real);
|
|
2002
|
-
list.push(entry);
|
|
2003
|
-
}
|
|
2004
|
-
else {
|
|
2005
|
-
// The engine appends: add the updaters again in their new order, with new callback ids
|
|
2006
|
-
// (removing an updater frees its id).
|
|
2007
|
-
for (const e of list)
|
|
2008
|
-
raw(this).removeUpdater(e.id);
|
|
2009
|
-
list.splice(opts.index, 0, entry);
|
|
2010
|
-
for (const e of list) {
|
|
2011
|
-
if (e !== entry) {
|
|
2012
|
-
const g = e.f;
|
|
2013
|
-
e.id = register((dt) => {
|
|
2014
|
-
g(this, dt);
|
|
2015
|
-
});
|
|
2016
|
-
}
|
|
2017
|
-
raw(this).addUpdater(e.id, false, e.real);
|
|
2018
|
-
}
|
|
2019
|
-
}
|
|
2020
|
-
updatersOf.set(this, list);
|
|
2021
|
-
if (opts.callUpdater === true)
|
|
2022
|
-
f(this, 0);
|
|
2023
|
-
return this;
|
|
2024
|
-
}
|
|
2025
|
-
/** Removes `updateFunction` from the updaters of this node (Manim `remove_updater`). */
|
|
2026
|
-
removeUpdater(updateFunction) {
|
|
2027
|
-
const list = updatersOf.get(this) ?? [];
|
|
2028
|
-
for (const e of list)
|
|
2029
|
-
if (e.f === updateFunction)
|
|
2030
|
-
raw(this).removeUpdater(e.id);
|
|
2031
|
-
updatersOf.set(this, list.filter((e) => e.f !== updateFunction));
|
|
2032
|
-
return this;
|
|
2033
|
-
}
|
|
2034
|
-
/** Removes the updaters of the family, or of this node only (Manim `clear_updaters`). */
|
|
2035
|
-
clearUpdaters(opts = {}) {
|
|
2036
|
-
const recursive = opts.recursive ?? true;
|
|
2037
|
-
raw(this).clearUpdaters(recursive);
|
|
2038
|
-
for (const m of recursive ? this.getFamily() : [this])
|
|
2039
|
-
updatersOf.delete(m);
|
|
2040
|
-
return this;
|
|
2041
|
-
}
|
|
2042
|
-
hasUpdaters() {
|
|
2043
|
-
return raw(this).hasUpdaters();
|
|
2044
|
-
}
|
|
2045
|
-
/** The updaters of this node, in order (Manim `get_updaters`). */
|
|
2046
|
-
getUpdaters() {
|
|
2047
|
-
return (updatersOf.get(this) ?? []).map((e) => e.f);
|
|
2048
|
-
}
|
|
2049
|
-
/** The updaters of the family (Manim `get_family_updaters`). */
|
|
2050
|
-
getFamilyUpdaters() {
|
|
2051
|
-
return this.getFamily().flatMap((m) => m.getUpdaters());
|
|
2052
|
-
}
|
|
2053
|
-
/** The updaters of this node that take `dt`: functions of two parameters (Manim
|
|
2054
|
-
* `get_time_based_updaters`). */
|
|
2055
|
-
getTimeBasedUpdaters() {
|
|
2056
|
-
return this.getUpdaters().filter((f) => f.length >= 2);
|
|
2057
|
-
}
|
|
2058
|
-
/** True when an updater of this node takes `dt` (Manim `has_time_based_updater`). */
|
|
2059
|
-
hasTimeBasedUpdater() {
|
|
2060
|
-
return this.getTimeBasedUpdaters().length > 0;
|
|
2061
|
-
}
|
|
2062
|
-
/** The updaters of this node become those of `mobject` (Manim `match_updaters`). */
|
|
2063
|
-
matchUpdaters(mobject) {
|
|
2064
|
-
this.clearUpdaters();
|
|
2065
|
-
for (const f of mobject.getUpdaters())
|
|
2066
|
-
this.addUpdater(f);
|
|
2067
|
-
return this;
|
|
2068
|
-
}
|
|
2069
|
-
/** Runs the updaters of the family, or of this node only (Manim `update`). */
|
|
2070
|
-
update(dt = 0, opts = {}) {
|
|
2071
|
-
raw(this).update(dt, opts.recursive ?? true);
|
|
2072
|
-
return this;
|
|
2073
|
-
}
|
|
2074
|
-
/** Pauses the updaters of the family, or of this node only (Manim `suspend_updating`). */
|
|
2075
|
-
suspendUpdating(opts = {}) {
|
|
2076
|
-
raw(this).suspendUpdating(opts.recursive ?? true);
|
|
2077
|
-
return this;
|
|
2078
|
-
}
|
|
2079
|
-
/** Resumes the updaters and runs them once (Manim `resume_updating`). */
|
|
2080
|
-
resumeUpdating(opts = {}) {
|
|
2081
|
-
raw(this).resumeUpdating(opts.recursive ?? true);
|
|
2082
|
-
return this;
|
|
2083
|
-
}
|
|
2084
|
-
/** `mob.always.nextTo(other, m.UP)` adds an updater that calls the method on every frame,
|
|
2085
|
-
* and calls it now (Manim `always`). The methods chain: each adds its own updater. */
|
|
2086
|
-
get always() {
|
|
2087
|
-
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- a Proxy has the type of its target; the trap makes it a builder
|
|
2088
|
-
const self = new Proxy({}, {
|
|
2089
|
-
get: (_t, key) => {
|
|
2090
|
-
if (typeof key === "symbol")
|
|
2091
|
-
return undefined;
|
|
2092
|
-
return (...args) => {
|
|
2093
|
-
this.addUpdater((m) => {
|
|
2094
|
-
const method = Reflect.get(m, key);
|
|
2095
|
-
if (typeof method !== "function")
|
|
2096
|
-
wrong("always", `the Mobject has no method ${key}`);
|
|
2097
|
-
else
|
|
2098
|
-
Reflect.apply(method, m, args);
|
|
2099
|
-
}, { callUpdater: true });
|
|
2100
|
-
return self;
|
|
2101
|
-
};
|
|
2102
|
-
},
|
|
2103
|
-
});
|
|
2104
|
-
return self;
|
|
2105
|
-
}
|
|
2106
|
-
/** Manim's `.animate`: the Mobject methods change a copy, played as one animation. */
|
|
2107
|
-
get animate() {
|
|
2108
|
-
return builder(this);
|
|
2109
|
-
}
|
|
2110
|
-
}
|
|
2111
|
-
/** The animations that run in JavaScript (`begin`, `interpolate` and `finish` of the class). */
|
|
2112
|
-
const drivenAnimations = new WeakSet();
|
|
2113
|
-
/** True when `a` runs in JavaScript: a subclass of `m.Animation` that its class drives, not an
|
|
2114
|
-
* engine animation. */
|
|
2115
|
-
export function isDriven(a) {
|
|
2116
|
-
return drivenAnimations.has(a);
|
|
2117
|
-
}
|
|
2118
|
-
/** The engine animation of the next Animation that is made (see `engineAnimation`). */
|
|
2119
|
-
let made;
|
|
2120
|
-
/** The families with points of `mobjects`, member by member (Manim
|
|
2121
|
-
* `get_all_families_zipped`): the zip stops at the shortest family. */
|
|
2122
|
-
function familiesZipped(list) {
|
|
2123
|
-
const families = list.map((m) => m.getFamily().filter((f) => f.hasPoints()));
|
|
2124
|
-
const out = [];
|
|
2125
|
-
for (let i = 0; families.length > 0 && families.every((f) => i < f.length); i++) {
|
|
2126
|
-
out.push(families.map((f) => f[i]).filter((x) => x !== undefined));
|
|
2127
|
-
}
|
|
2128
|
-
return out;
|
|
2129
|
-
}
|
|
2130
|
-
/** One animation: it plays once. Make your own animation as in Manim: extend `m.Animation` and
|
|
2131
|
-
* override `interpolateMobject(alpha)` or `interpolateSubmobject(submobject, start, alpha)`.
|
|
2132
|
-
* Such an animation runs in JavaScript, with the engine's clock:
|
|
2133
|
-
*
|
|
2134
|
-
* class Spin extends m.Animation {
|
|
2135
|
-
* interpolateMobject(alpha: number) {
|
|
2136
|
-
* this.mobject?.becomePointsOf(this.startingMobject!).rotate(this.rateFunc(alpha) * m.TAU);
|
|
2137
|
-
* }
|
|
2138
|
-
* }
|
|
2139
|
-
* s.play(new Spin(square, { runTime: 2 }));
|
|
2140
|
-
*/
|
|
2141
|
-
export class Animation {
|
|
2142
|
-
[ANIMATION];
|
|
2143
|
-
/** The animated mobject (Manim `mobject`), or undefined (`Wait`, a group). */
|
|
2144
|
-
mobject;
|
|
2145
|
-
/** The copy that `begin` keeps (Manim `starting_mobject`). */
|
|
2146
|
-
startingMobject;
|
|
2147
|
-
name;
|
|
2148
|
-
lagRatio;
|
|
2149
|
-
reverseRateFunction;
|
|
2150
|
-
#suspend;
|
|
2151
|
-
/** The rate function (Manim `rate_func`). */
|
|
2152
|
-
rateFunc;
|
|
2153
|
-
#remover;
|
|
2154
|
-
#introducer;
|
|
2155
|
-
/** True for an animation that runs in JavaScript (`begin`, `interpolate`, `finish` here). */
|
|
2156
|
-
#driven;
|
|
2157
|
-
constructor(mobject, opts = {}) {
|
|
2158
|
-
const engineMade = made;
|
|
2159
|
-
made = undefined;
|
|
2160
|
-
this.mobject = mobject;
|
|
2161
|
-
this.startingMobject = undefined;
|
|
2162
|
-
this.name = opts.name;
|
|
2163
|
-
this.reverseRateFunction = opts.reverseRateFunction ?? false;
|
|
2164
|
-
this.#suspend = opts.suspendMobjectUpdating ?? true;
|
|
2165
|
-
this.#remover = opts.remover ?? engineMade?.remover ?? false;
|
|
2166
|
-
this.#introducer = opts.introducer ?? engineMade?.introducer ?? false;
|
|
2167
|
-
this.lagRatio = opts.lagRatio ?? engineMade?.lagRatio ?? 0;
|
|
2168
|
-
this.rateFunc = opts.rateFunc ?? engineMade?.rate ?? smooth;
|
|
2169
|
-
if (engineMade === undefined) {
|
|
2170
|
-
this.#driven = true;
|
|
2171
|
-
drivenAnimations.add(this);
|
|
2172
|
-
this[ANIMATION] = engine.driven(mobject === undefined ? undefined : raw(mobject), register(() => {
|
|
2173
|
-
this.begin();
|
|
2174
|
-
}), register((alpha) => {
|
|
2175
|
-
this.interpolate(alpha);
|
|
2176
|
-
}), register(() => {
|
|
2177
|
-
this.finish();
|
|
2178
|
-
}), opts.runTime ?? 1);
|
|
2179
|
-
if (this.#remover)
|
|
2180
|
-
this[ANIMATION].setRemover();
|
|
2181
|
-
return;
|
|
2182
|
-
}
|
|
2183
|
-
this.#driven = false;
|
|
2184
|
-
this[ANIMATION] = engineMade.handle;
|
|
2185
|
-
const h = this[ANIMATION];
|
|
2186
|
-
if (opts.rateFunc !== undefined || this.reverseRateFunction)
|
|
2187
|
-
h.setRateFunc(rawRate(this.#engineRate()));
|
|
2188
|
-
if (opts.lagRatio !== undefined)
|
|
2189
|
-
h.setLagRatio(opts.lagRatio);
|
|
2190
|
-
if (opts.remover === true)
|
|
2191
|
-
h.setRemover();
|
|
2192
|
-
if (opts.runTime !== undefined)
|
|
2193
|
-
h.setRunTime(opts.runTime);
|
|
2194
|
-
if (opts.suspendMobjectUpdating !== undefined)
|
|
2195
|
-
h.setSuspendUpdating(opts.suspendMobjectUpdating);
|
|
2196
|
-
}
|
|
2197
|
-
/** The rate function that the engine runs: `rateFunc`, backwards with `reverseRateFunction`. */
|
|
2198
|
-
#engineRate() {
|
|
2199
|
-
const f = this.rateFunc;
|
|
2200
|
-
return this.reverseRateFunction ? (t) => f(1 - t) : f;
|
|
2201
|
-
}
|
|
2202
|
-
/** Pauses the updaters of the mobject while the animation plays (Manim
|
|
2203
|
-
* `suspend_mobject_updating`). */
|
|
2204
|
-
get suspendMobjectUpdating() {
|
|
2205
|
-
return this.#suspend;
|
|
2206
|
-
}
|
|
2207
|
-
set suspendMobjectUpdating(suspend) {
|
|
2208
|
-
this.#suspend = suspend;
|
|
2209
|
-
if (!this.#driven)
|
|
2210
|
-
this[ANIMATION].setSuspendUpdating(suspend);
|
|
2211
|
-
}
|
|
2212
|
-
/** The run time in seconds (Manim `run_time`). */
|
|
2213
|
-
get runTime() {
|
|
2214
|
-
return this.getRunTime();
|
|
2215
|
-
}
|
|
2216
|
-
set runTime(seconds) {
|
|
2217
|
-
this.setRunTime(seconds);
|
|
2218
|
-
}
|
|
2219
|
-
/** Prepares the animation and sets the start (Manim `begin`). The engine calls it when the
|
|
2220
|
-
* animation starts; call it yourself only to drive an animation in an updater. */
|
|
2221
|
-
begin() {
|
|
2222
|
-
if (!this.#driven) {
|
|
2223
|
-
this[ANIMATION].begin();
|
|
2224
|
-
return;
|
|
2225
|
-
}
|
|
2226
|
-
this.startingMobject = this.createStartingMobject();
|
|
2227
|
-
if (this.suspendMobjectUpdating)
|
|
2228
|
-
this.mobject?.suspendUpdating();
|
|
2229
|
-
this.interpolate(0);
|
|
2230
|
-
}
|
|
2231
|
-
/** Sets the end (Manim `finish`). */
|
|
2232
|
-
finish() {
|
|
2233
|
-
if (!this.#driven) {
|
|
2234
|
-
this[ANIMATION].finish();
|
|
2235
|
-
return;
|
|
2236
|
-
}
|
|
2237
|
-
this.interpolate(1);
|
|
2238
|
-
if (this.suspendMobjectUpdating)
|
|
2239
|
-
this.mobject?.resumeUpdating();
|
|
2240
|
-
}
|
|
2241
|
-
/** Removes the mobject from `scene` when the animation is a remover (Manim
|
|
2242
|
-
* `clean_up_from_scene`). The engine does this at the end of a play. */
|
|
2243
|
-
cleanUpFromScene(scene) {
|
|
2244
|
-
if (this.isRemover() && this.mobject !== undefined)
|
|
2245
|
-
scene.remove(this.mobject);
|
|
2246
|
-
}
|
|
2247
|
-
/** A copy of the mobject, which `begin` keeps (Manim `create_starting_mobject`). */
|
|
2248
|
-
createStartingMobject() {
|
|
2249
|
-
return this.mobject?.copy();
|
|
2250
|
-
}
|
|
2251
|
-
/** The mobjects of the animation (Manim `get_all_mobjects`). */
|
|
2252
|
-
getAllMobjects() {
|
|
2253
|
-
return [this.mobject, this.startingMobject].filter((x) => x !== undefined);
|
|
2254
|
-
}
|
|
2255
|
-
/** The family members with points of each of `getAllMobjects()`, zipped (Manim
|
|
2256
|
-
* `get_all_families_zipped`). */
|
|
2257
|
-
getAllFamiliesZipped() {
|
|
2258
|
-
return familiesZipped(this.getAllMobjects());
|
|
2259
|
-
}
|
|
2260
|
-
/** Runs the updaters of the other mobjects of the animation (Manim `update_mobjects`). */
|
|
2261
|
-
updateMobjects(dt) {
|
|
2262
|
-
for (const m of this.getAllMobjectsToUpdate())
|
|
2263
|
-
m.update(dt);
|
|
2264
|
-
}
|
|
2265
|
-
/** `getAllMobjects()` without the animated mobject (Manim `get_all_mobjects_to_update`). */
|
|
2266
|
-
getAllMobjectsToUpdate() {
|
|
2267
|
-
return this.getAllMobjects().filter((m) => m !== this.mobject);
|
|
2268
|
-
}
|
|
2269
|
-
/** Sets the animation at linear progress `alpha`, 0 to 1 (Manim `interpolate`). */
|
|
2270
|
-
interpolate(alpha) {
|
|
2271
|
-
if (this.#driven)
|
|
2272
|
-
this.interpolateMobject(alpha);
|
|
2273
|
-
else
|
|
2274
|
-
this[ANIMATION].interpolate(alpha);
|
|
2275
|
-
}
|
|
2276
|
-
/** Sets the mobject at linear progress `alpha` (Manim `interpolate_mobject`): calls
|
|
2277
|
-
* `interpolateSubmobject` for each family member, with its own progress. */
|
|
2278
|
-
interpolateMobject(alpha) {
|
|
2279
|
-
if (!this.#driven) {
|
|
2280
|
-
this[ANIMATION].interpolate(alpha);
|
|
2281
|
-
return;
|
|
2282
|
-
}
|
|
2283
|
-
const families = this.getAllFamiliesZipped();
|
|
2284
|
-
families.forEach((members, i) => {
|
|
2285
|
-
const [submobject, starting, ...rest] = members;
|
|
2286
|
-
if (submobject === undefined || starting === undefined)
|
|
2287
|
-
return;
|
|
2288
|
-
const subAlpha = this.getSubAlpha(alpha, i, families.length);
|
|
2289
|
-
// Manim passes the extra members (an outline, a target copy) before alpha.
|
|
2290
|
-
const step = Reflect.get(this, "interpolateSubmobject");
|
|
2291
|
-
if (typeof step === "function")
|
|
2292
|
-
Reflect.apply(step, this, [submobject, starting, ...rest, subAlpha]);
|
|
2293
|
-
});
|
|
2294
|
-
}
|
|
2295
|
-
/** Sets one family member at its progress (Manim `interpolate_submobject`). It does nothing
|
|
2296
|
-
* here: an animation that you write overrides it. */
|
|
2297
|
-
// oxlint-disable-next-line no-unused-vars -- Manim's signature: an animation that you write uses them
|
|
2298
|
-
interpolateSubmobject(submobject, startingSubmobject, alpha) {
|
|
2299
|
-
return this;
|
|
2300
|
-
}
|
|
2301
|
-
/** The progress of member `index` of `numSubmobjects`, with `lagRatio` and the rate function
|
|
2302
|
-
* (Manim `get_sub_alpha`). */
|
|
2303
|
-
getSubAlpha(alpha, index, numSubmobjects) {
|
|
2304
|
-
const full = (numSubmobjects - 1) * this.lagRatio + 1;
|
|
2305
|
-
const t = Math.min(1, Math.max(0, alpha * full - index * this.lagRatio));
|
|
2306
|
-
return this.reverseRateFunction ? this.rateFunc(1 - t) : this.rateFunc(t);
|
|
2307
|
-
}
|
|
2308
|
-
setRunTime(runTime) {
|
|
2309
|
-
this[ANIMATION].setRunTime(runTime);
|
|
2310
|
-
return this;
|
|
2311
|
-
}
|
|
2312
|
-
/** The run time in seconds: the one that was set, or the default of the animation. */
|
|
2313
|
-
getRunTime() {
|
|
2314
|
-
return this[ANIMATION].runTime();
|
|
2315
|
-
}
|
|
2316
|
-
/** Manim `set_rate_func`: a built-in rate function such as `m.smooth`, or any `(t) => number`. */
|
|
2317
|
-
setRateFunc(rateFunc) {
|
|
2318
|
-
this.rateFunc = rateFunc;
|
|
2319
|
-
if (!this.#driven)
|
|
2320
|
-
this[ANIMATION].setRateFunc(rawRate(this.#engineRate()));
|
|
2321
|
-
return this;
|
|
2322
|
-
}
|
|
2323
|
-
getRateFunc() {
|
|
2324
|
-
return this.rateFunc;
|
|
2325
|
-
}
|
|
2326
|
-
/** Sets `lagRatio`, as the `lagRatio` option does. */
|
|
2327
|
-
setLagRatio(lagRatio) {
|
|
2328
|
-
this.lagRatio = lagRatio;
|
|
2329
|
-
if (!this.#driven)
|
|
2330
|
-
this[ANIMATION].setLagRatio(lagRatio);
|
|
2331
|
-
return this;
|
|
2332
|
-
}
|
|
2333
|
-
setName(name) {
|
|
2334
|
-
this.name = name;
|
|
2335
|
-
return this;
|
|
2336
|
-
}
|
|
2337
|
-
/** True when the animation removes its mobject at the end. */
|
|
2338
|
-
isRemover() {
|
|
2339
|
-
return this.#remover;
|
|
2340
|
-
}
|
|
2341
|
-
/** True when the animation brings its mobject onto the stage. */
|
|
2342
|
-
isIntroducer() {
|
|
2343
|
-
return this.#introducer;
|
|
2344
|
-
}
|
|
2345
|
-
}
|
|
2346
|
-
/** `m.Mobject(opts)` makes an empty Mobject, as Manim's `Mobject()`. `instanceof m.Mobject`
|
|
2347
|
-
* is true for every node. */
|
|
2348
|
-
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- the Proxy adds the call of Manim's `Mobject()` to the class
|
|
2349
|
-
export const Mobject = new Proxy(MobjectClass, {
|
|
2350
|
-
apply(target, _this, args) {
|
|
2351
|
-
const o = args[0] ?? {};
|
|
2352
|
-
const mob = new target(engine.vGroup([]));
|
|
2353
|
-
if (o.color !== undefined)
|
|
2354
|
-
mob.setColor(o.color);
|
|
2355
|
-
if (o.name !== undefined)
|
|
2356
|
-
mob.name = o.name;
|
|
2357
|
-
if (o.target !== undefined)
|
|
2358
|
-
mob.target = o.target;
|
|
2359
|
-
if (o.zIndex !== undefined)
|
|
2360
|
-
mob.setZIndex(o.zIndex);
|
|
2361
|
-
return mob;
|
|
2362
|
-
},
|
|
2363
|
-
});
|
|
2364
|
-
// Manim's class name, for `printFamily` and the messages.
|
|
2365
|
-
Object.defineProperty(MobjectClass, "name", { value: "Mobject" });
|
|
2366
|
-
/** An engine animation as an Animation (or a subclass `Cls`): `opts` are the caller's options,
|
|
2367
|
-
* `defaults` the defaults of the engine animation. */
|
|
2368
|
-
export function engineAnimation(Cls, handle, mobject, opts = {}, defaults = {}) {
|
|
2369
|
-
made = {
|
|
2370
|
-
handle,
|
|
2371
|
-
rate: defaults.rate ?? smooth,
|
|
2372
|
-
lagRatio: defaults.lagRatio ?? 0,
|
|
2373
|
-
introducer: defaults.introducer ?? false,
|
|
2374
|
-
remover: defaults.remover ?? false,
|
|
2375
|
-
};
|
|
2376
|
-
const a = new Cls(mobject, opts);
|
|
2377
|
-
if (opts.shift !== undefined)
|
|
2378
|
-
handle.setShift(opts.shift);
|
|
2379
|
-
if (opts.scale !== undefined)
|
|
2380
|
-
handle.setScale(opts.scale);
|
|
2381
|
-
if (opts.pathArc !== undefined)
|
|
2382
|
-
handle.setPathArc(opts.pathArc);
|
|
2383
|
-
if (opts.pathFunc !== undefined) {
|
|
2384
|
-
const f = opts.pathFunc;
|
|
2385
|
-
handle.setPathFunc(register((starts, ends, t) => f(starts, ends, t)));
|
|
2386
|
-
}
|
|
2387
|
-
return a;
|
|
2388
|
-
}
|
|
2389
|
-
/** Sets the options of a made animation, as the options of its call would. */
|
|
2390
|
-
function configure(a, o) {
|
|
2391
|
-
const h = a[ANIMATION];
|
|
2392
|
-
if (o.reverseRateFunction !== undefined)
|
|
2393
|
-
a.reverseRateFunction = o.reverseRateFunction;
|
|
2394
|
-
if (o.rateFunc !== undefined || o.reverseRateFunction === true)
|
|
2395
|
-
a.setRateFunc(o.rateFunc ?? a.rateFunc);
|
|
2396
|
-
if (o.lagRatio !== undefined)
|
|
2397
|
-
a.setLagRatio(o.lagRatio);
|
|
2398
|
-
if (o.remover === true)
|
|
2399
|
-
h.setRemover();
|
|
2400
|
-
if (o.runTime !== undefined)
|
|
2401
|
-
h.setRunTime(o.runTime);
|
|
2402
|
-
if (o.pathArc !== undefined)
|
|
2403
|
-
h.setPathArc(o.pathArc);
|
|
2404
|
-
if (o.suspendMobjectUpdating !== undefined)
|
|
2405
|
-
h.setSuspendUpdating(o.suspendMobjectUpdating);
|
|
2406
|
-
if (o.name !== undefined)
|
|
2407
|
-
a.name = o.name;
|
|
2408
|
-
}
|
|
2409
|
-
function builder(m) {
|
|
2410
|
-
const target = m.copy();
|
|
2411
|
-
const animation = engineAnimation(Animation, engine.animateTo(raw(m), raw(target)), m);
|
|
2412
|
-
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- a Proxy has the type of its target; the traps make it a builder
|
|
2413
|
-
const self = new Proxy(() => undefined, {
|
|
2414
|
-
apply(_t, _this, args) {
|
|
2415
|
-
configure(animation, isOptions(args[0]) ? args[0] : {});
|
|
2416
|
-
return self;
|
|
2417
|
-
},
|
|
2418
|
-
get(_t, key) {
|
|
2419
|
-
if (typeof key === "symbol" || key in animation) {
|
|
2420
|
-
const value = Reflect.get(animation, key);
|
|
2421
|
-
return typeof value === "function" ? value.bind(animation) : value;
|
|
2422
|
-
}
|
|
2423
|
-
const method = Reflect.get(target, key);
|
|
2424
|
-
if (typeof method !== "function")
|
|
2425
|
-
return undefined;
|
|
2426
|
-
return (...args) => {
|
|
2427
|
-
Reflect.apply(method, target, args);
|
|
2428
|
-
return self;
|
|
2429
|
-
};
|
|
2430
|
-
},
|
|
2431
|
-
set(_t, key, value) {
|
|
2432
|
-
return Reflect.set(animation, key, value);
|
|
2433
|
-
},
|
|
2434
|
-
getPrototypeOf() {
|
|
2435
|
-
return Animation.prototype;
|
|
2436
|
-
},
|
|
2437
|
-
});
|
|
2438
|
-
return self;
|
|
2439
|
-
}
|
|
2440
|
-
/** The Animations of rest arguments, arrays flattened. */
|
|
2441
|
-
export function animationList(items) {
|
|
2442
|
-
return items.flatMap((v) => (v instanceof Animation ? [v] : animationList(v)));
|
|
2443
|
-
}
|
|
2444
|
-
function animationOf(label, v, at) {
|
|
2445
|
-
if (v[ANIMATION].used()) {
|
|
2446
|
-
fault(`${label}: ${at} was already played or put in another animation`, "an Animation plays once: make a new one for each play");
|
|
2447
|
-
}
|
|
2448
|
-
return v[ANIMATION];
|
|
2449
|
-
}
|
|
2450
|
-
function isOptions(v) {
|
|
2451
|
-
return (typeof v === "object" && v !== null && !Array.isArray(v) && !(v instanceof Mobject) && !(v instanceof Animation));
|
|
2452
|
-
}
|
|
2453
|
-
/** Rest arguments `T` with an optional options object `O` last. */
|
|
2454
|
-
export function split(args) {
|
|
2455
|
-
const last = args.at(-1);
|
|
2456
|
-
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- an options object is the last argument only
|
|
2457
|
-
return isOptions(last) ? [args.slice(0, -1), last] : [[...args], undefined];
|
|
2458
|
-
}
|
|
2459
|
-
/** The engine animations of rest arguments (arrays flatten), each checked. */
|
|
2460
|
-
export function animations(label, items) {
|
|
2461
|
-
const out = [];
|
|
2462
|
-
const walk = (v, at) => {
|
|
2463
|
-
if (v instanceof Animation)
|
|
2464
|
-
out.push(animationOf(label, v, at));
|
|
2465
|
-
else
|
|
2466
|
-
v.forEach((x, i) => {
|
|
2467
|
-
walk(x, `${at}[${String(i)}]`);
|
|
2468
|
-
});
|
|
2469
|
-
};
|
|
2470
|
-
items.forEach((v, i) => {
|
|
2471
|
-
walk(v, `animations[${String(i)}]`);
|
|
2472
|
-
});
|
|
2473
|
-
return out;
|
|
2474
|
-
}
|
|
2475
|
-
/** The shape functions that Circumscribe draws as a circle: geometry.ts adds `m.Circle`. */
|
|
2476
|
-
export const circleFrames = new WeakSet();
|
|
2477
|
-
/** A number that animations change and updaters read. */
|
|
2478
|
-
class Tracker {
|
|
2479
|
-
#raw;
|
|
2480
|
-
constructor(value = 0) {
|
|
2481
|
-
this.#raw = new engine.Tracker(value);
|
|
2482
|
-
}
|
|
2483
|
-
getValue() {
|
|
2484
|
-
return this.#raw.getValue();
|
|
2485
|
-
}
|
|
2486
|
-
setValue(value) {
|
|
2487
|
-
this.#raw.setValue(value);
|
|
2488
|
-
return this;
|
|
2489
|
-
}
|
|
2490
|
-
incrementValue(dValue) {
|
|
2491
|
-
return this.setValue(this.getValue() + dValue);
|
|
2492
|
-
}
|
|
2493
|
-
/** Sets the value between the values of two trackers at `alpha` (Manim `interpolate`).
|
|
2494
|
-
* `pathFunc` maps `[a, 0]`, `[b, 0]` and `alpha` to the point whose x is the value. */
|
|
2495
|
-
interpolate(mobject1, mobject2, alpha, pathFunc = lerp) {
|
|
2496
|
-
return this.setValue(pathFunc([mobject1.getValue(), 0], [mobject2.getValue(), 0], alpha)[0]);
|
|
2497
|
-
}
|
|
2498
|
-
/** `tracker.animate.setValue(v)`, `tracker.animate.incrementValue(d)`, or with the
|
|
2499
|
-
* animation options first: `tracker.animate({ rateFunc }).setValue(v)`. */
|
|
2500
|
-
get animate() {
|
|
2501
|
-
const t = this.#raw;
|
|
2502
|
-
let options = {};
|
|
2503
|
-
const methods = {
|
|
2504
|
-
setValue: (value) => engineAnimation(Animation, t.animateTo(value), undefined, options),
|
|
2505
|
-
incrementValue: (delta) => engineAnimation(Animation, t.animateBy(delta), undefined, options),
|
|
2506
|
-
};
|
|
2507
|
-
const call = (opts) => {
|
|
2508
|
-
options = opts;
|
|
2509
|
-
return methods;
|
|
2510
|
-
};
|
|
2511
|
-
return Object.assign(call, methods);
|
|
2512
|
-
}
|
|
2513
|
-
}
|
|
2514
|
-
/** `ValueTracker(value)` (Manim; called without `new`). */
|
|
2515
|
-
export function ValueTracker(value = 0) {
|
|
2516
|
-
return new Tracker(value);
|
|
2517
|
-
}
|
|
2518
|
-
/** `m` with the style options applied, in Manim's order. */
|
|
2519
|
-
export function styled(m, style = {}) {
|
|
2520
|
-
if (style.color !== undefined)
|
|
2521
|
-
raw(m).setColor(style.color, true);
|
|
2522
|
-
raw(m).setFill(style.fillColor, style.fillOpacity, true);
|
|
2523
|
-
raw(m).setStroke(style.strokeColor, style.strokeWidth, style.strokeOpacity, true);
|
|
2524
|
-
return m;
|
|
2525
|
-
}
|
|
2526
|
-
/** A new shape with the style options applied, in Manim's order. */
|
|
2527
|
-
function shape(make, style = {}) {
|
|
2528
|
-
return styled(wrap(make()), style);
|
|
2529
|
-
}
|
|
2530
|
-
const HEAVY_WEIGHTS = new Set(["BOLD", "SEMIBOLD", "ULTRABOLD", "HEAVY", "ULTRAHEAVY"]);
|
|
2531
|
-
const isBold = (w) => HEAVY_WEIGHTS.has(w);
|
|
2532
|
-
const isItalic = (s) => s === "ITALIC" || s === "OBLIQUE";
|
|
2533
|
-
/** Manim's `_find_indexes`: the [start, end) character ranges of `key` in `text`. */
|
|
2534
|
-
function textRanges(key, text) {
|
|
2535
|
-
const chars = Array.from(text);
|
|
2536
|
-
const slice = /^\[(-?\d*):(-?\d*)\]$/.exec(key);
|
|
2537
|
-
if (slice !== null) {
|
|
2538
|
-
const n = chars.length;
|
|
2539
|
-
const at = (x, d) => {
|
|
2540
|
-
const v = x === undefined || x === "" ? d : Number(x);
|
|
2541
|
-
return Math.min(Math.max(v < 0 ? n + v : v, 0), n);
|
|
2542
|
-
};
|
|
2543
|
-
const [a, b] = [at(slice[1], 0), at(slice[2], n)];
|
|
2544
|
-
return a < b ? [[a, b]] : [];
|
|
2545
|
-
}
|
|
2546
|
-
if (key === "")
|
|
2547
|
-
return [];
|
|
2548
|
-
const out = [];
|
|
2549
|
-
const k = Array.from(key).length;
|
|
2550
|
-
for (let i = 0; i + k <= chars.length; i++) {
|
|
2551
|
-
if (chars.slice(i, i + k).join("") === key)
|
|
2552
|
-
out.push([i, i + k]);
|
|
2553
|
-
}
|
|
2554
|
-
return out;
|
|
2555
|
-
}
|
|
2556
|
-
function hexOf(c) {
|
|
2557
|
-
const n = Number.parseInt(c.slice(1, 7), 16);
|
|
2558
|
-
return [(n >> 16) & 255, (n >> 8) & 255, n & 255];
|
|
2559
|
-
}
|
|
2560
|
-
/** Manim's `color_gradient`: `n` colours spread evenly over `colors`. */
|
|
2561
|
-
export function gradientColors(colors, n) {
|
|
2562
|
-
if (colors.length === 0)
|
|
2563
|
-
return [];
|
|
2564
|
-
if (colors.length === 1 || n <= 1)
|
|
2565
|
-
return Array.from({ length: n }, () => colors[0] ?? WHITE);
|
|
2566
|
-
return Array.from({ length: n }, (_, i) => {
|
|
2567
|
-
const x = (i / (n - 1)) * (colors.length - 1);
|
|
2568
|
-
const j = Math.min(Math.floor(x), colors.length - 2);
|
|
2569
|
-
const a = hexOf(colors[j] ?? WHITE);
|
|
2570
|
-
const b = hexOf(colors[j + 1] ?? WHITE);
|
|
2571
|
-
const t = x - j;
|
|
2572
|
-
const rgb = a.map((v, k) => Math.round(v + ((b[k] ?? 0) - v) * t));
|
|
2573
|
-
return `#${rgb.map((v) => v.toString(16).padStart(2, "0").toUpperCase()).join("")}`;
|
|
2574
|
-
});
|
|
2575
|
-
}
|
|
2576
|
-
/** The glyph index of each character that has a glyph (not whitespace), else -1. */
|
|
2577
|
-
function glyphIndexes(text) {
|
|
2578
|
-
let next = 0;
|
|
2579
|
-
return Array.from(text).map((c) => (/\s/.test(c) ? -1 : next++));
|
|
2580
|
-
}
|
|
2581
|
-
/** Colours the glyphs of `mob` (the text `text`) with `gradient` and each `t2g` key. */
|
|
2582
|
-
function textGradients(mob, text, gradient, t2g) {
|
|
2583
|
-
const glyphs = mob.submobjects;
|
|
2584
|
-
const index = glyphIndexes(text);
|
|
2585
|
-
const paint = (from, to, colors) => {
|
|
2586
|
-
const ids = index.slice(from, to).filter((i) => i >= 0);
|
|
2587
|
-
gradientColors(colors, ids.length).forEach((c, k) => glyphs[ids[k] ?? -1]?.setColor(c));
|
|
2588
|
-
};
|
|
2589
|
-
if (gradient !== undefined)
|
|
2590
|
-
paint(0, index.length, gradient);
|
|
2591
|
-
for (const [key, colors] of Object.entries(t2g)) {
|
|
2592
|
-
for (const [a, b] of textRanges(key, text))
|
|
2593
|
-
paint(a, b, colors);
|
|
2594
|
-
}
|
|
2595
|
-
}
|
|
2596
|
-
/** House-font text (Manim `Text`). All on-screen words use Text. Its submobjects are the
|
|
2597
|
-
* glyphs, one for each character that is not whitespace. */
|
|
2598
|
-
export function Text(text, opts = {}) {
|
|
2599
|
-
const { fontSize = 48, color = WHITE, fillOpacity = 1, strokeWidth = 0, lineSpacing = -1, slant = NORMAL, weight = NORMAL, t2c = {}, t2s = {}, t2w = {}, t2g = {}, gradient, tabWidth = 4, height, width, } = opts;
|
|
2600
|
-
const content = text.replaceAll("\t", " ".repeat(tabWidth));
|
|
2601
|
-
const mob = wrap(engine.text(content, {
|
|
2602
|
-
size: fontSize,
|
|
2603
|
-
color,
|
|
2604
|
-
bold: isBold(weight),
|
|
2605
|
-
italic: isItalic(slant),
|
|
2606
|
-
...(lineSpacing === -1 ? {} : { lineSpacing: (1 + lineSpacing) / 1.3 }),
|
|
2607
|
-
t2c: Object.entries(t2c),
|
|
2608
|
-
t2w: Object.entries(t2w).map(([k, w]) => [k, isBold(w)]),
|
|
2609
|
-
t2s: Object.entries(t2s).map(([k, v]) => [k, isItalic(v)]),
|
|
2610
|
-
}));
|
|
2611
|
-
textGradients(mob, content, gradient, t2g);
|
|
2612
|
-
if (fillOpacity !== 1)
|
|
2613
|
-
mob.setFill({ opacity: fillOpacity });
|
|
2614
|
-
if (strokeWidth !== 0)
|
|
2615
|
-
mob.setStroke({ width: strokeWidth });
|
|
2616
|
-
if (height !== undefined)
|
|
2617
|
-
mob.scaleToFitHeight(height);
|
|
2618
|
-
if (width !== undefined)
|
|
2619
|
-
mob.scaleToFitWidth(width);
|
|
2620
|
-
return mob;
|
|
2621
|
-
}
|
|
2622
|
-
/** A picture from a file of the project (`m.Image("shot.png")`), an object of the scene like a
|
|
2623
|
-
* shape: it moves, scales, rotates and fades (`fadeIn`, `setOpacity`), and the frame crops it.
|
|
2624
|
-
* The path is from the chapter file and stays inside the project folder (no URLs). PNG, JPEG and
|
|
2625
|
-
* WebP are shown as they are, a GIF shows its first frame, and an SVG is a picture. It is 2 units
|
|
2626
|
-
* high, as wide as the file's ratio gives. `aula check` reports a missing file, an unknown
|
|
2627
|
-
* format and a file that is too large, at this call. A picture has no stroke. */
|
|
2628
|
-
export function Image(path, opts = {}) {
|
|
2629
|
-
const { height, width } = opts;
|
|
2630
|
-
if (typeof path !== "string")
|
|
2631
|
-
fault("m.Image: the path must be a string", 'm.Image("pictures/shot.png")');
|
|
2632
|
-
if (placing === undefined) {
|
|
2633
|
-
fault(`m.Image(${JSON.stringify(path)}): called outside a chapter`, "make the picture in the rows or in a move of a chapter, which know the project folder");
|
|
2634
|
-
}
|
|
2635
|
-
const found = placing.picture(path);
|
|
2636
|
-
if (typeof found === "string")
|
|
2637
|
-
fault(found);
|
|
2638
|
-
const mob = wrap(engine.image(found.path, found.mime, found.width, found.height, found.bytes));
|
|
2639
|
-
if (height !== undefined)
|
|
2640
|
-
mob.scaleToFitHeight(height);
|
|
2641
|
-
if (width !== undefined)
|
|
2642
|
-
mob.scaleToFitWidth(width);
|
|
2643
|
-
return mob;
|
|
2644
|
-
}
|
|
2645
|
-
/** Source code with syntax highlighting: Manim's `Code`. Its submobjects are the background,
|
|
2646
|
-
* the line numbers (with `addLineNumbers`) and the code lines. */
|
|
2647
|
-
export class CodeMobject extends Mobject {
|
|
2648
|
-
/** The code lines: line `i` is `codeLines.submobjects[i]`, and its submobjects are its
|
|
2649
|
-
* characters that are not whitespace. */
|
|
2650
|
-
get codeLines() {
|
|
2651
|
-
return this.#part(-1);
|
|
2652
|
-
}
|
|
2653
|
-
/** The line numbers, one submobject for each line, or undefined without them. */
|
|
2654
|
-
get lineNumbers() {
|
|
2655
|
-
return this.submobjects.length === 3 ? this.#part(1) : undefined;
|
|
2656
|
-
}
|
|
2657
|
-
/** The panel (with the window buttons as its submobjects). */
|
|
2658
|
-
get background() {
|
|
2659
|
-
return this.#part(0);
|
|
2660
|
-
}
|
|
2661
|
-
#part(i) {
|
|
2662
|
-
const parts = this.submobjects;
|
|
2663
|
-
const part = parts.at(i);
|
|
2664
|
-
return part ?? wrong("Code", "the code block has lost its parts");
|
|
2665
|
-
}
|
|
2666
|
-
copy() {
|
|
2667
|
-
return new CodeMobject(raw(this).copy());
|
|
2668
|
-
}
|
|
2669
|
-
}
|
|
2670
|
-
/** Source code with syntax highlighting (Manim `Code`): `Code({ codeString, language })`. The
|
|
2671
|
-
* engine has the grammars of bat. A language with no grammar is an error at the call; no
|
|
2672
|
-
* language is plain text. */
|
|
2673
|
-
export function Code(opts) {
|
|
2674
|
-
const style = opts.formatterStyle ?? "dark";
|
|
2675
|
-
const bg = opts.backgroundConfig ?? {};
|
|
2676
|
-
const text = opts.paragraphConfig ?? {};
|
|
2677
|
-
const from = opts.lineNumbersFrom ?? 1;
|
|
2678
|
-
if (!Number.isSafeInteger(from))
|
|
2679
|
-
wrong("Code", `lineNumbersFrom must be an integer; got ${String(from)}`);
|
|
2680
|
-
const block = engine.code(opts.codeString, {
|
|
2681
|
-
...(opts.language === undefined ? {} : { language: opts.language }),
|
|
2682
|
-
formatterStyle: style,
|
|
2683
|
-
tabWidth: opts.tabWidth ?? 4,
|
|
2684
|
-
addLineNumbers: opts.addLineNumbers ?? true,
|
|
2685
|
-
lineNumbersFrom: BigInt(from),
|
|
2686
|
-
background: opts.background ?? "rectangle",
|
|
2687
|
-
buff: bg.buff ?? 0.3,
|
|
2688
|
-
...(bg.fillColor === undefined ? {} : { fillColor: bg.fillColor }),
|
|
2689
|
-
fillOpacity: bg.fillOpacity ?? 1,
|
|
2690
|
-
...(bg.strokeColor === undefined ? {} : { strokeColor: bg.strokeColor }),
|
|
2691
|
-
strokeWidth: bg.strokeWidth ?? 1,
|
|
2692
|
-
cornerRadius: bg.cornerRadius ?? 0.2,
|
|
2693
|
-
fontSize: text.fontSize ?? 24,
|
|
2694
|
-
lineSpacing: text.lineSpacing ?? 0.5,
|
|
2695
|
-
});
|
|
2696
|
-
return new CodeMobject(block);
|
|
2697
|
-
}
|
|
2698
|
-
function escapeRegExp(s) {
|
|
2699
|
-
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
2700
|
-
}
|
|
2701
|
-
/** Manim's `_break_up_tex_strings`: `{{ … }}` groups and the isolated substrings become parts. */
|
|
2702
|
-
function texParts(strings, isolate) {
|
|
2703
|
-
let pieces = strings.flatMap((s) => s.split(/{{(.*?)}}/));
|
|
2704
|
-
if (pieces.length > strings.length)
|
|
2705
|
-
pieces = pieces.map((p) => p.trim());
|
|
2706
|
-
const patterns = isolate.filter((x) => x !== "").map((x) => `(${escapeRegExp(x)})`);
|
|
2707
|
-
if (patterns.length > 0) {
|
|
2708
|
-
const pattern = new RegExp(patterns.join("|"));
|
|
2709
|
-
pieces = pieces.flatMap((p) => p.split(pattern));
|
|
2710
|
-
}
|
|
2711
|
-
return pieces.filter((p) => typeof p === "string" && p !== "");
|
|
2712
|
-
}
|
|
2713
|
-
/** A formula: Manim's `MathTex` or `Tex`. Its submobjects are the parts, one for each
|
|
2714
|
-
* argument, `{{ … }}` group and isolated substring; each part's submobjects are its glyphs. */
|
|
2715
|
-
export class MathTexMobject extends Mobject {
|
|
2716
|
-
#texStrings;
|
|
2717
|
-
/** The whole LaTeX (Manim `tex_string`). */
|
|
2718
|
-
texString;
|
|
2719
|
-
constructor(handle, texStrings, texString) {
|
|
2720
|
-
super(handle);
|
|
2721
|
-
this.#texStrings = texStrings;
|
|
2722
|
-
this.texString = texString;
|
|
2723
|
-
}
|
|
2724
|
-
/** The LaTeX of each part (Manim `tex_strings`). */
|
|
2725
|
-
get texStrings() {
|
|
2726
|
-
return this.#texStrings;
|
|
2727
|
-
}
|
|
2728
|
-
/** The whole LaTeX (Manim `get_tex_string`). */
|
|
2729
|
-
getTexString() {
|
|
2730
|
-
return this.texString;
|
|
2731
|
-
}
|
|
2732
|
-
/** Sets the opacity of the parts that match `tex`; with `remainingOpacity`, the other parts
|
|
2733
|
-
* take that opacity first (Manim `set_opacity_by_tex`). */
|
|
2734
|
-
setOpacityByTex(tex, opacity = 0.5, remainingOpacity, opts = {}) {
|
|
2735
|
-
if (remainingOpacity !== undefined)
|
|
2736
|
-
this.setOpacity(remainingOpacity);
|
|
2737
|
-
for (const part of this.getPartsByTex(tex, opts).submobjects)
|
|
2738
|
-
part.setOpacity(opacity);
|
|
2739
|
-
return this;
|
|
2740
|
-
}
|
|
2741
|
-
/** Sorts the parts by their LaTeX (Manim `sort_alphabetically`). */
|
|
2742
|
-
sortAlphabetically() {
|
|
2743
|
-
const parts = this.submobjects.map((part, i) => ({ part, tex: this.#texStrings[i] ?? "" }));
|
|
2744
|
-
parts.sort((a, b) => (a.tex < b.tex ? -1 : a.tex > b.tex ? 1 : 0));
|
|
2745
|
-
this.remove(...parts.map((p) => p.part));
|
|
2746
|
-
this.add(...parts.map((p) => p.part));
|
|
2747
|
-
this.#texStrings = parts.map((p) => p.tex);
|
|
2748
|
-
return this;
|
|
2749
|
-
}
|
|
2750
|
-
copy() {
|
|
2751
|
-
return new MathTexMobject(raw(this).copy(), this.texStrings, this.texString);
|
|
2752
|
-
}
|
|
2753
|
-
/** The parts whose LaTeX contains `tex` (or is `tex`), as a VGroup. */
|
|
2754
|
-
getPartsByTex(tex, opts = {}) {
|
|
2755
|
-
const { substring = true, caseSensitive = true } = opts;
|
|
2756
|
-
const fold = (s) => (caseSensitive ? s : s.toLowerCase());
|
|
2757
|
-
const parts = this.submobjects.filter((_, i) => {
|
|
2758
|
-
const own = fold(this.texStrings[i] ?? "");
|
|
2759
|
-
return substring ? own.includes(fold(tex)) : own === fold(tex);
|
|
2760
|
-
});
|
|
2761
|
-
return VGroup(parts);
|
|
2762
|
-
}
|
|
2763
|
-
/** The first part whose LaTeX contains `tex`, or undefined (Manim `None`). */
|
|
2764
|
-
getPartByTex(tex, opts = {}) {
|
|
2765
|
-
return this.getPartsByTex(tex, opts).submobjects[0];
|
|
2766
|
-
}
|
|
2767
|
-
setColorByTex(tex, color, opts = {}) {
|
|
2768
|
-
for (const part of this.getPartsByTex(tex, opts).submobjects)
|
|
2769
|
-
part.setColor(color);
|
|
2770
|
-
return this;
|
|
2771
|
-
}
|
|
2772
|
-
setColorByTexToColorMap(texsToColorMap, opts = {}) {
|
|
2773
|
-
for (const [tex, color] of Object.entries(texsToColorMap))
|
|
2774
|
-
this.setColorByTex(tex, color, opts);
|
|
2775
|
-
return this;
|
|
2776
|
-
}
|
|
2777
|
-
/** The index of `part` among the parts. */
|
|
2778
|
-
indexOfPart(part) {
|
|
2779
|
-
const i = this.submobjects.indexOf(part);
|
|
2780
|
-
if (i < 0)
|
|
2781
|
-
wrong("indexOfPart", "the Mobject is not a part of this formula");
|
|
2782
|
-
return i;
|
|
2783
|
-
}
|
|
2784
|
-
indexOfPartByTex(tex, opts = {}) {
|
|
2785
|
-
const part = this.getPartByTex(tex, opts);
|
|
2786
|
-
if (part === undefined) {
|
|
2787
|
-
return wrong("indexOfPartByTex", `no part has ${JSON.stringify(tex)}`, `the parts are: ${this.texStrings.map((t) => JSON.stringify(t)).join(", ")}`);
|
|
2788
|
-
}
|
|
2789
|
-
return this.indexOfPart(part);
|
|
2790
|
-
}
|
|
2791
|
-
}
|
|
2792
|
-
function formula(name, strings, opts) {
|
|
2793
|
-
const text = name === "Tex";
|
|
2794
|
-
const { fontSize = 48, color = WHITE, argSeparator = text ? "" : " ", substringsToIsolate = [], texToColorMap = {}, } = opts;
|
|
2795
|
-
if (!Number.isFinite(fontSize))
|
|
2796
|
-
wrong(name, `fontSize must be a finite number; got ${String(fontSize)}`);
|
|
2797
|
-
const texStrings = texParts(strings, [...substringsToIsolate, ...Object.keys(texToColorMap)]);
|
|
2798
|
-
const glyphs = texGlyphs(name, texStrings, argSeparator, fontSize);
|
|
2799
|
-
const handle = engine.tex(name, glyphs, color);
|
|
2800
|
-
const f = new MathTexMobject(handle, texStrings, texStrings.join(argSeparator));
|
|
2801
|
-
return f.setColorByTexToColorMap(texToColorMap);
|
|
2802
|
-
}
|
|
2803
|
-
function texArguments(args) {
|
|
2804
|
-
const [strings, o] = split(args);
|
|
2805
|
-
return [strings, o ?? {}];
|
|
2806
|
-
}
|
|
2807
|
-
/** Math-mode LaTeX (Manim `MathTex`), drawn by MathJax: `MathTex("a^2", "+", "b^2", { fontSize })`. */
|
|
2808
|
-
export function MathTex(...args) {
|
|
2809
|
-
const [strings, o] = texArguments(args);
|
|
2810
|
-
return formula("MathTex", strings, o);
|
|
2811
|
-
}
|
|
2812
|
-
/** Text-mode LaTeX (Manim `Tex`): math goes between `$` signs. */
|
|
2813
|
-
export function Tex(...args) {
|
|
2814
|
-
const [strings, o] = texArguments(args);
|
|
2815
|
-
return formula("Tex", strings, o);
|
|
2816
|
-
}
|
|
2817
|
-
/** A Mobject with no points: build its path with `startNewPath`, `addLineTo`,
|
|
2818
|
-
* `setPointsAsCorners` and the other path methods (Manim `VMobject`). */
|
|
2819
|
-
export function VMobject(opts = {}) {
|
|
2820
|
-
const { jointType, capStyle, closeNewPoints = false, preFunctionHandleToAnchorScaleFactor = 0.01, makeSmoothAfterApplyingFunctions = false, toleranceForPointEquality = 1e-6, nPointsPerCubicCurve = 4, ...style } = opts;
|
|
2821
|
-
if (nPointsPerCubicCurve !== 4) {
|
|
2822
|
-
wrong("VMobject", `nPointsPerCubicCurve must be 4; got ${String(nPointsPerCubicCurve)}`, "aula draws cubic curves");
|
|
2823
|
-
}
|
|
2824
|
-
const mob = shape(() => engine.vGroup([]), style);
|
|
2825
|
-
if (jointType !== undefined)
|
|
2826
|
-
mob.jointType = jointType;
|
|
2827
|
-
if (capStyle !== undefined)
|
|
2828
|
-
mob.setCapStyle(capStyle);
|
|
2829
|
-
mob.closeNewPoints = closeNewPoints;
|
|
2830
|
-
mob.preFunctionHandleToAnchorScaleFactor = preFunctionHandleToAnchorScaleFactor;
|
|
2831
|
-
mob.makeSmoothAfterApplyingFunctions = makeSmoothAfterApplyingFunctions;
|
|
2832
|
-
mob.toleranceForPointEquality = toleranceForPointEquality;
|
|
2833
|
-
return mob;
|
|
2834
|
-
}
|
|
2835
|
-
export function VGroup(...items) {
|
|
2836
|
-
return wrap(engine.vGroup(mobjects(items)));
|
|
2837
|
-
}
|
|
2838
|
-
export function flatPoints(items) {
|
|
2839
|
-
const out = [];
|
|
2840
|
-
const walk = (v) => {
|
|
2841
|
-
if (isPoint(v))
|
|
2842
|
-
out.push(v);
|
|
2843
|
-
else
|
|
2844
|
-
for (const x of v)
|
|
2845
|
-
walk(x);
|
|
2846
|
-
};
|
|
2847
|
-
items.forEach(walk);
|
|
2848
|
-
return out;
|
|
2849
|
-
}
|
|
2850
|
-
/** An open polyline through `points` (not in Manim): `Polyline(...points, opts)`. */
|
|
2851
|
-
export function Polyline(...args) {
|
|
2852
|
-
const [items, style] = split(args);
|
|
2853
|
-
return shape(() => engine.polyline(flatPoints(items)), style);
|
|
2854
|
-
}
|
|
2855
|
-
export function CubicBezier(startAnchor, startHandle, endHandle, endAnchor, opts = {}) {
|
|
2856
|
-
return shape(() => engine.cubicBezier(startAnchor, startHandle, endHandle, endAnchor), opts);
|
|
2857
|
-
}
|
|
2858
|
-
/** `vmobject` as dashes (Manim `DashedVMobject`), in the style of `vmobject`. */
|
|
2859
|
-
export function DashedVMobject(vmobject, opts = {}) {
|
|
2860
|
-
const { numDashes = 15, dashedRatio = 0.5, dashOffset = 0, equalLengths = true } = opts;
|
|
2861
|
-
if (!Number.isSafeInteger(numDashes) || numDashes < 0) {
|
|
2862
|
-
wrong("DashedVMobject", `numDashes must be a whole number; got ${String(numDashes)}`);
|
|
2863
|
-
}
|
|
2864
|
-
return wrap(engine.dashed(raw(vmobject), numDashes, dashedRatio, dashOffset, equalLengths));
|
|
2865
|
-
}
|
|
2866
|
-
/** A number that `setValue` changes: Manim's `DecimalNumber`. Its submobjects are the glyphs. */
|
|
2867
|
-
export class DecimalNumberMobject extends Mobject {
|
|
2868
|
-
copy() {
|
|
2869
|
-
return new DecimalNumberMobject(raw(this).copy());
|
|
2870
|
-
}
|
|
2871
|
-
getValue() {
|
|
2872
|
-
return raw(this).getValue();
|
|
2873
|
-
}
|
|
2874
|
-
/** Draws `number` in place of the value, at the same size, with the edge `edgeToFix` in place. */
|
|
2875
|
-
setValue(number) {
|
|
2876
|
-
raw(this).setValue(number);
|
|
2877
|
-
return this;
|
|
2878
|
-
}
|
|
2879
|
-
incrementValue(deltaT = 1) {
|
|
2880
|
-
return this.setValue(this.getValue() + deltaT);
|
|
2881
|
-
}
|
|
2882
|
-
}
|
|
2883
|
-
/** A whole number: Manim's `Integer`. `getValue` rounds (half to even). */
|
|
2884
|
-
export class IntegerMobject extends DecimalNumberMobject {
|
|
2885
|
-
copy() {
|
|
2886
|
-
return new IntegerMobject(raw(this).copy());
|
|
2887
|
-
}
|
|
2888
|
-
getValue() {
|
|
2889
|
-
const v = super.getValue();
|
|
2890
|
-
const r = Math.round(v);
|
|
2891
|
-
return Math.abs(v % 1) === 0.5 ? 2 * Math.round(v / 2) : r;
|
|
2892
|
-
}
|
|
2893
|
-
}
|
|
2894
|
-
function numberSettings(label, o, places) {
|
|
2895
|
-
const p = o.numDecimalPlaces ?? places;
|
|
2896
|
-
if (!Number.isSafeInteger(p) || p < 0 || p > 100) {
|
|
2897
|
-
wrong(label, `numDecimalPlaces must be a whole number from 0 to 100; got ${String(p)}`);
|
|
2898
|
-
}
|
|
2899
|
-
return {
|
|
2900
|
-
numDecimalPlaces: p,
|
|
2901
|
-
includeSign: o.includeSign ?? false,
|
|
2902
|
-
groupWithCommas: o.groupWithCommas ?? true,
|
|
2903
|
-
...(o.unit === undefined ? {} : { unit: o.unit }),
|
|
2904
|
-
edgeToFix: o.edgeToFix ?? LEFT,
|
|
2905
|
-
fontSize: o.fontSize ?? 48,
|
|
2906
|
-
color: o.color ?? WHITE,
|
|
2907
|
-
};
|
|
2908
|
-
}
|
|
2909
|
-
/** A number in the house font (Manim `DecimalNumber`): `DecimalNumber(3.14159, { numDecimalPlaces: 3 })`. */
|
|
2910
|
-
export function DecimalNumber(number = 0, opts = {}) {
|
|
2911
|
-
const n = new DecimalNumberMobject(engine.decimalNumber(number, numberSettings("DecimalNumber", opts, 2)));
|
|
2912
|
-
return numberStyle(n, opts);
|
|
2913
|
-
}
|
|
2914
|
-
/** The stroke width and fill opacity of a number's glyphs; `setValue` keeps them. */
|
|
2915
|
-
function numberStyle(n, o) {
|
|
2916
|
-
if (o.strokeWidth !== undefined && o.strokeWidth !== 0)
|
|
2917
|
-
n.setStroke({ width: o.strokeWidth });
|
|
2918
|
-
if (o.fillOpacity !== undefined && o.fillOpacity !== 1)
|
|
2919
|
-
n.setFill({ opacity: o.fillOpacity });
|
|
2920
|
-
return n;
|
|
2921
|
-
}
|
|
2922
|
-
/** A whole number (Manim `Integer`): `DecimalNumber` with no decimal places. */
|
|
2923
|
-
export function Integer(number = 0, opts = {}) {
|
|
2924
|
-
return numberStyle(new IntegerMobject(engine.decimalNumber(number, numberSettings("Integer", opts, 0))), opts);
|
|
2925
|
-
}
|
|
2926
|
-
/** Rebuilt by `func` on every frame (Manim `always_redraw`). */
|
|
2927
|
-
export function alwaysRedraw(func) {
|
|
2928
|
-
const id = register(() => {
|
|
2929
|
-
engine.redrawn(raw(func()));
|
|
2930
|
-
});
|
|
2931
|
-
return shape(() => engine.alwaysRedraw(id));
|
|
2932
|
-
}
|
|
2933
|
-
// --- chapters --------------------------------------------------------------------------
|
|
2934
|
-
const ROW = Symbol("aula.row");
|
|
2935
|
-
/** `export default defineChapter({ heading, metaphor, rows: () => [ … ] })`: one chapter.
|
|
2936
|
-
* Makes no engine call. */
|
|
2937
|
-
export function defineChapter(fields) {
|
|
2938
|
-
const call = new Error("defineChapter");
|
|
2939
|
-
const heading = spokenField("defineChapter", "heading", fields.heading, call);
|
|
2940
|
-
return { ...fields, heading: heading.text, headingParts: heading.parts, headingCall: heading.call, call };
|
|
2941
|
-
}
|
|
2942
|
-
/** The written text, the parts and the call of a `title` or a `heading` (`field`): a plain
|
|
2943
|
-
* string (one part with no reading, at `call`), or a `say` (its parts, at its own call). Any
|
|
2944
|
-
* other value is a fault. */
|
|
2945
|
-
function spokenField(name, field, value, call) {
|
|
2946
|
-
if (typeof value === "string")
|
|
2947
|
-
return { text: value, parts: [{ text: value }], call };
|
|
2948
|
-
if (!isSay(value)) {
|
|
2949
|
-
fault(`${name}: the ${field} must be a string or a say`, `${field}: "text" or ${field}: say\`text \${pronounce("word", { say: "words" })}\``);
|
|
2950
|
-
}
|
|
2951
|
-
const data = value[ROW];
|
|
2952
|
-
const parts = data.kind === "say" ? data.parts : [];
|
|
2953
|
-
return { text: parts.map((p) => p.text).join(""), parts, call: data.call };
|
|
2954
|
-
}
|
|
2955
|
-
const PROJECT = Symbol("aula.project");
|
|
2956
|
-
/** `export default defineProject({ title, storyboard, theme, lexicon, chapters })`: one project,
|
|
2957
|
-
* in the `index.ts` of its folder. Makes no engine call. */
|
|
2958
|
-
export function defineProject(fields) {
|
|
2959
|
-
const call = new Error("defineProject");
|
|
2960
|
-
const title = spokenField("defineProject", "title", fields.title, call);
|
|
2961
|
-
return {
|
|
2962
|
-
[PROJECT]: true,
|
|
2963
|
-
storyboard: "",
|
|
2964
|
-
lexicon: {},
|
|
2965
|
-
...fields,
|
|
2966
|
-
title: title.text,
|
|
2967
|
-
titleParts: title.parts,
|
|
2968
|
-
titleCall: title.call,
|
|
2969
|
-
call,
|
|
2970
|
-
};
|
|
2971
|
-
}
|
|
2972
|
-
/** True for a value that defineProject made. */
|
|
2973
|
-
export function isProjectDefinition(v) {
|
|
2974
|
-
return typeof v === "object" && v !== null && PROJECT in v;
|
|
2975
|
-
}
|
|
2976
|
-
/** True while a move runs: a row made then is an error. */
|
|
2977
|
-
let moving = false;
|
|
2978
|
-
function row(name, data) {
|
|
2979
|
-
if (moving) {
|
|
2980
|
-
fault(`${name}: called while a move runs`, "rows are the items of the list that rows returns; a move only changes the picture");
|
|
2981
|
-
}
|
|
2982
|
-
return { [ROW]: data(new Error(name)) };
|
|
2983
|
-
}
|
|
2984
|
-
/** Starts a paragraph: one narration cue. `notes` is plain data that `aula timeline` prints
|
|
2985
|
-
* under the paragraph. `check` is for kits: aula calls it one time, after the chapter is drawn,
|
|
2986
|
-
* and reports its findings. */
|
|
2987
|
-
export function paragraph(notes = {}, check) {
|
|
2988
|
-
return row("paragraph", (call) => ({ kind: "paragraph", call, notes, check }));
|
|
2989
|
-
}
|
|
2990
|
-
const PRONOUNCE = Symbol("aula.pronounce");
|
|
2991
|
-
/** ``say`The file is 4 ${pronounce("GB", { say: "gigabytes" })}.` ``: one word that the
|
|
2992
|
-
* screen, the word times and `aula script` show as `written`, and that the narrator reads as
|
|
2993
|
-
* `reading`. The written part is one word, and its word time covers all of the reading. Use it
|
|
2994
|
-
* only for a word whose reading changes with the context. It goes in a `say`: a row, the
|
|
2995
|
-
* `title` of `defineProject` or the `heading` of `defineChapter`. `aula check` gives an error when the
|
|
2996
|
-
* written part is not one word, or when a phoneme is not in Kokoro's token table. */
|
|
2997
|
-
export function pronounce(written, reading) {
|
|
2998
|
-
const usage = 'pronounce("word", { say: "words" }) or pronounce("word", { phonemes: "fˈQnimz" })';
|
|
2999
|
-
if (typeof written !== "string")
|
|
3000
|
-
fault("pronounce: the written word must be a string", usage);
|
|
3001
|
-
const r = reading;
|
|
3002
|
-
const one = typeof r === "object" &&
|
|
3003
|
-
r !== null &&
|
|
3004
|
-
Object.keys(r).length === 1 &&
|
|
3005
|
-
(("say" in r && typeof r.say === "string") || ("phonemes" in r && typeof r.phonemes === "string"));
|
|
3006
|
-
if (!one)
|
|
3007
|
-
fault("pronounce: give the reading as { say: string } or { phonemes: string }", usage);
|
|
3008
|
-
return { [PRONOUNCE]: true, written, reading };
|
|
3009
|
-
}
|
|
3010
|
-
/** True for a value that `pronounce` made. */
|
|
3011
|
-
function isPronounce(v) {
|
|
3012
|
-
return typeof v === "object" && v !== null && PRONOUNCE in v;
|
|
3013
|
-
}
|
|
3014
|
-
/** The parts of a row tag's text: each piece of the template, and the written word and the
|
|
3015
|
-
* reading of each `pronounce` between them. Every other `${…}` is an error. */
|
|
3016
|
-
function tagParts(name, strings, values) {
|
|
3017
|
-
if (strings.length !== values.length + 1) {
|
|
3018
|
-
fault(`${name}: write the text as it is, with no \${…}`, "the text in the source is the text that is spoken");
|
|
3019
|
-
}
|
|
3020
|
-
const parts = [];
|
|
3021
|
-
strings.forEach((text, i) => {
|
|
3022
|
-
if (text === undefined) {
|
|
3023
|
-
fault(`${name}: the text has an invalid escape`, "escape only a backtick, a backslash and ${ with a backslash");
|
|
3024
|
-
}
|
|
3025
|
-
if (/[\r\n]/.test(text)) {
|
|
3026
|
-
fault(`${name}: write the text on one line`, "a row's text stays on the line of its call");
|
|
3027
|
-
}
|
|
3028
|
-
if (text !== "")
|
|
3029
|
-
parts.push({ text });
|
|
3030
|
-
if (i === values.length)
|
|
3031
|
-
return;
|
|
3032
|
-
const value = values[i];
|
|
3033
|
-
if (!isPronounce(value)) {
|
|
3034
|
-
fault(`${name}: write the text as it is; a \${…} can only be pronounce(written, reading)`, "the text in the source is the text that is spoken");
|
|
3035
|
-
}
|
|
3036
|
-
const reading = "say" in value.reading
|
|
3037
|
-
? { tag: "words", val: value.reading.say }
|
|
3038
|
-
: { tag: "phonemes", val: value.reading.phonemes };
|
|
3039
|
-
parts.push({ text: value.written, reading });
|
|
3040
|
-
});
|
|
3041
|
-
return parts;
|
|
3042
|
-
}
|
|
3043
|
-
/** The text of a row tag (`name` is the tag), as written: one template with no invalid escape
|
|
3044
|
-
* and no line break. A `${…}` can only be the value of `pronounce`, and its written word is in
|
|
3045
|
-
* the text. Throws a AulaError at the row. A kit's own row tag calls it with the values of its
|
|
3046
|
-
* template. */
|
|
3047
|
-
export function tagText(name, strings, ...values) {
|
|
3048
|
-
return tagParts(name, strings, values)
|
|
3049
|
-
.map((p) => p.text)
|
|
3050
|
-
.join("");
|
|
3051
|
-
}
|
|
3052
|
-
const SAY = Symbol("aula.say");
|
|
3053
|
-
/** True for a value that `say` made. */
|
|
3054
|
-
function isSay(v) {
|
|
3055
|
-
return typeof v === "object" && v !== null && SAY in v;
|
|
3056
|
-
}
|
|
3057
|
-
/** ``say`text` ``: the next words of the narration. A `${…}` can only be
|
|
3058
|
-
* `pronounce(written, reading)`: ``say`The file is 4 ${pronounce("GB", { say: "gigabytes" })}.` ``.
|
|
3059
|
-
* The written text is on the screen where aula shows the text (the cards, for a title or a
|
|
3060
|
-
* heading), in the word times and in `aula script`, and the narrator speaks the reading. A
|
|
3061
|
-
* `say` is a row, the `title` of `defineProject` or the `heading` of `defineChapter`. */
|
|
3062
|
-
export function say(strings, ...words) {
|
|
3063
|
-
const parts = tagParts("say", strings, words);
|
|
3064
|
-
return { ...row("say", (call) => ({ kind: "say", call, parts })), [SAY]: true };
|
|
3065
|
-
}
|
|
3066
|
-
/** ``show`note``timing` ``: a move. The note says what changes on screen. The timing, a
|
|
3067
|
-
* second template right after the note, gives the length of the move: ``25%`` is a share of the
|
|
3068
|
-
* `say` above it (0 < N <= 100) and scales with the narration, and ``3s`` is seconds (N > 0).
|
|
3069
|
-
* ``25%`` is the usual timing. ``show`note``25%` `` is an open move, ``show`note``25%`((s) =>
|
|
3070
|
-
* { … })`` a move, and ``show`note``25%`(() => {})`` a hold. The move starts on the first word
|
|
3071
|
-
* of its `say`, and its parts (each `s.play`, `s.fadeAll` and `s.wait`) split its length in
|
|
3072
|
-
* proportion to their Manim lengths, each at least 0.5 seconds. The longer one wins: the picture holds until the `say`
|
|
3073
|
-
* ends, or the narration waits until the move ends. */
|
|
3074
|
-
export function show(strings) {
|
|
3075
|
-
const note = tagText("show", strings);
|
|
3076
|
-
const { call } = row("show", (c) => ({ kind: "show", call: c, note, move: undefined, timing: undefined }))[ROW];
|
|
3077
|
-
/** The row with `timing`: none when the second template is missing (the engine stops there). */
|
|
3078
|
-
const timed = (timing) => {
|
|
3079
|
-
const close = (move) => {
|
|
3080
|
-
if (typeof move !== "function")
|
|
3081
|
-
fault("show: give the move function after the timing", "show`note``25%`((s) => { … })");
|
|
3082
|
-
return { [ROW]: { kind: "show", call, note, move, timing } };
|
|
3083
|
-
};
|
|
3084
|
-
return Object.assign(close, { [ROW]: { kind: "show", call, note, move: undefined, timing } });
|
|
3085
|
-
};
|
|
3086
|
-
function untimed(timing) {
|
|
3087
|
-
return typeof timing === "function" ? timed(undefined)(timing) : timed(timingText(timing));
|
|
3088
|
-
}
|
|
3089
|
-
// `show`note`` in the rows, with no timing: the engine stops at its row.
|
|
3090
|
-
return Object.assign(untimed, { [ROW]: { kind: "show", call, note, move: undefined, timing: undefined } });
|
|
3091
|
-
}
|
|
3092
|
-
/** The text of a timing template, as written: a `${…}` stays in it, so the engine names it. */
|
|
3093
|
-
function timingText(strings) {
|
|
3094
|
-
return strings.raw.join("${…}");
|
|
3095
|
-
}
|
|
3096
|
-
/** ``embed`note`(path)``: a prepared clip that plays between two `say` rows, while the
|
|
3097
|
-
* narration is silent. The note says what the clip shows. The `path` is a file of the project
|
|
3098
|
-
* folder: an MP4 (H.264, AAC) or WebM (VP8 or VP9, Opus or Vorbis) video, which fills the
|
|
3099
|
-
* picture in 16:9 on the theme background, or a WAV (PCM) or MP3 sound, which plays over the
|
|
3100
|
-
* picture that stays (or over `{ image }`, a PNG or JPEG file). The clip plays after the `say`
|
|
3101
|
-
* above it, and the next `say` starts 0.25 s after it ends: the narration waits for it, and no
|
|
3102
|
-
* move runs. Write it after a `say` that ends a sentence. `aula check` gives an error at the row
|
|
3103
|
-
* for a file that is missing or too big (50 MB), with a format that aula does not play or a
|
|
3104
|
-
* header with no length. */
|
|
3105
|
-
export function embed(strings) {
|
|
3106
|
-
const note = tagText("embed", strings);
|
|
3107
|
-
const usage = 'embed`note`("samples/a.wav", { image: "samples/a.png" })';
|
|
3108
|
-
return (path, options) => {
|
|
3109
|
-
if (typeof path !== "string")
|
|
3110
|
-
fault("embed: give the path of the file after the note", 'embed`note`("clips/demo.mp4")');
|
|
3111
|
-
const given = options;
|
|
3112
|
-
if (given !== undefined && (typeof given !== "object" || given === null)) {
|
|
3113
|
-
fault("embed: the options are an object", usage);
|
|
3114
|
-
}
|
|
3115
|
-
const unknown = given === undefined ? undefined : Object.keys(given).find((k) => k !== "image");
|
|
3116
|
-
if (unknown !== undefined)
|
|
3117
|
-
fault(`embed: the options have ${JSON.stringify(unknown)}; the only option is image`, usage);
|
|
3118
|
-
const image = options?.image;
|
|
3119
|
-
if (image !== undefined && typeof image !== "string")
|
|
3120
|
-
fault("embed: image is the path of a PNG or JPEG file", usage);
|
|
3121
|
-
return row("embed", (call) => ({ kind: "embed", call, note, path, image }));
|
|
3122
|
-
};
|
|
3123
|
-
}
|
|
3124
|
-
/** The scene of a move, as `self` in a Manim `construct`: the picture calls. */
|
|
3125
|
-
export class Scene {
|
|
3126
|
-
#raw;
|
|
3127
|
-
#slot;
|
|
3128
|
-
/** The place of every call, when the scene is not a chapter's (the card). */
|
|
3129
|
-
#at;
|
|
3130
|
-
#warnings = [];
|
|
3131
|
-
/** The scene updaters and their callback ids (Manim `Scene.updaters`). */
|
|
3132
|
-
#updaters = [];
|
|
3133
|
-
constructor(handle, slot, at) {
|
|
3134
|
-
this.#raw = handle;
|
|
3135
|
-
this.#slot = slot;
|
|
3136
|
-
this.#at = at;
|
|
3137
|
-
}
|
|
3138
|
-
/** The place of the calling line: the first frame in the chapter file. */
|
|
3139
|
-
#place() {
|
|
3140
|
-
return this.#at ?? placeIn(new Error("place"), this.#slot);
|
|
3141
|
-
}
|
|
3142
|
-
/** A part of a move starts: from now on, the move code does not read or change the objects
|
|
3143
|
-
* on the real clock. */
|
|
3144
|
-
#part() {
|
|
3145
|
-
closed = new Set(this.#raw.closed());
|
|
3146
|
-
}
|
|
3147
|
-
/** Plays animations together (Manim `self.play`) until the longest ends. `rateFunc` and
|
|
3148
|
-
* `lagRatio` replace the values of every animation, as in Manim. In a move, the play is one
|
|
3149
|
-
* part of the move, and `runTime` (or its longest animation) is its Manim length: the parts
|
|
3150
|
-
* split the length of the move in proportion to their Manim lengths. `runTime` replaces the
|
|
3151
|
-
* run time of every animation; outside a move (the card), it is seconds. */
|
|
3152
|
-
play(...args) {
|
|
3153
|
-
const at = this.#place();
|
|
3154
|
-
const [items, o = {}] = split(args);
|
|
3155
|
-
for (const a of animationList(items)) {
|
|
3156
|
-
if (o.rateFunc !== undefined)
|
|
3157
|
-
a.setRateFunc(o.rateFunc);
|
|
3158
|
-
if (o.lagRatio !== undefined)
|
|
3159
|
-
a.setLagRatio(o.lagRatio);
|
|
3160
|
-
}
|
|
3161
|
-
const list = animations("s.play", items);
|
|
3162
|
-
this.#part();
|
|
3163
|
-
this.#raw.play(at, list, o.runTime);
|
|
3164
|
-
}
|
|
3165
|
-
add(...items) {
|
|
3166
|
-
for (const r of mobjects(items)) {
|
|
3167
|
-
this.#raw.add(r);
|
|
3168
|
-
}
|
|
3169
|
-
}
|
|
3170
|
-
remove(...items) {
|
|
3171
|
-
for (const r of mobjects(items)) {
|
|
3172
|
-
this.#raw.remove(r);
|
|
3173
|
-
}
|
|
3174
|
-
}
|
|
3175
|
-
/** Moves the mobjects to the front, in order (Manim `self.bring_to_front`, the same as
|
|
3176
|
-
* `add`). A mobject that is not on the stage is added. */
|
|
3177
|
-
bringToFront(...items) {
|
|
3178
|
-
this.add(...items);
|
|
3179
|
-
}
|
|
3180
|
-
/** Moves the mobjects to the back, in order (Manim `self.bring_to_back`). A mobject that is
|
|
3181
|
-
* not on the stage is added at the back. */
|
|
3182
|
-
bringToBack(...items) {
|
|
3183
|
-
this.#raw.bringToBack(mobjects(items));
|
|
3184
|
-
}
|
|
3185
|
-
/** Holds the picture (Manim `self.wait`) for `duration` seconds, 1 by default. In a move, it is
|
|
3186
|
-
* one part of the move, and `duration` is its Manim length. `frozenFrame` has no effect: the
|
|
3187
|
-
* updaters run on every frame. */
|
|
3188
|
-
wait(duration, _opts = {}) {
|
|
3189
|
-
const at = this.#place();
|
|
3190
|
-
this.#part();
|
|
3191
|
-
this.#raw.wait(at, duration);
|
|
3192
|
-
}
|
|
3193
|
-
/** `wait(duration)` (Manim `pause`). */
|
|
3194
|
-
pause(duration) {
|
|
3195
|
-
const at = this.#place();
|
|
3196
|
-
this.#part();
|
|
3197
|
-
this.#raw.wait(at, duration);
|
|
3198
|
-
}
|
|
3199
|
-
/** The objects on the stage, without the docked heading, that no other object on the stage
|
|
3200
|
-
* holds (Manim `get_top_level_mobjects`). */
|
|
3201
|
-
getTopLevelMobjects() {
|
|
3202
|
-
const all = this.#raw.mobjects().map(wrap);
|
|
3203
|
-
const families = all.map((m) => m.getFamily());
|
|
3204
|
-
return all.filter((m) => families.filter((f) => f.includes(m)).length === 1);
|
|
3205
|
-
}
|
|
3206
|
-
/** Every object on the stage and all their members (Manim `get_mobject_family_members`). */
|
|
3207
|
-
getMobjectFamilyMembers() {
|
|
3208
|
-
return [...new Set(this.#raw.mobjects().flatMap((m) => wrap(m).getFamily()))];
|
|
3209
|
-
}
|
|
3210
|
-
/** The values of the attributes `keys` (Manim `get_attrs`). */
|
|
3211
|
-
getAttrs(...keys) {
|
|
3212
|
-
return keys.map((k) => Reflect.get(this, k));
|
|
3213
|
-
}
|
|
3214
|
-
/** Runs the updaters of the objects on the stage now (Manim `update_mobjects`). */
|
|
3215
|
-
updateMobjects(dt) {
|
|
3216
|
-
for (const m of this.#raw.mobjects())
|
|
3217
|
-
wrap(m).update(dt);
|
|
3218
|
-
}
|
|
3219
|
-
/** Runs the scene updaters now (Manim `update_self`). */
|
|
3220
|
-
updateSelf(dt) {
|
|
3221
|
-
for (const u of this.#updaters.slice())
|
|
3222
|
-
u.func(dt);
|
|
3223
|
-
}
|
|
3224
|
-
/** True when a scene updater or an updater that takes `dt` is on the stage (Manim
|
|
3225
|
-
* `should_update_mobjects`). */
|
|
3226
|
-
shouldUpdateMobjects() {
|
|
3227
|
-
return this.#updaters.length > 0 || this.getMobjectFamilyMembers().some((m) => m.hasTimeBasedUpdater());
|
|
3228
|
-
}
|
|
3229
|
-
/** Runs `func(dt)` on every frame, after the updaters of the objects (Manim
|
|
3230
|
-
* `Scene.add_updater`). */
|
|
3231
|
-
addUpdater(func) {
|
|
3232
|
-
const id = register((dt) => {
|
|
3233
|
-
func(dt);
|
|
3234
|
-
});
|
|
3235
|
-
this.#updaters.push({ func, id });
|
|
3236
|
-
this.#raw.addUpdater(id);
|
|
3237
|
-
}
|
|
3238
|
-
/** Removes the scene updater `func` (Manim `Scene.remove_updater`). */
|
|
3239
|
-
removeUpdater(func) {
|
|
3240
|
-
for (const u of this.#updaters.filter((x) => x.func === func))
|
|
3241
|
-
this.#raw.removeUpdater(u.id);
|
|
3242
|
-
this.#updaters.splice(0, this.#updaters.length, ...this.#updaters.filter((x) => x.func !== func));
|
|
3243
|
-
}
|
|
3244
|
-
/** `newMobject` takes the place of `oldMobject` on the stage, or inside the object that holds
|
|
3245
|
-
* it (Manim `replace`). `oldMobject` that is not on the stage is an error. */
|
|
3246
|
-
replace(oldMobject, newMobject) {
|
|
3247
|
-
this.#raw.replace(raw(oldMobject), raw(newMobject));
|
|
3248
|
-
}
|
|
3249
|
-
/** Removes every object except the docked heading now, with no animation (Manim `clear`). */
|
|
3250
|
-
clear() {
|
|
3251
|
-
this.#raw.clear();
|
|
3252
|
-
}
|
|
3253
|
-
/** Adds the mobjects and keeps them in front: each later `add` moves them back to the front
|
|
3254
|
-
* (Manim `add_foreground_mobjects`). */
|
|
3255
|
-
addForegroundMobjects(...items) {
|
|
3256
|
-
this.#raw.addForeground(mobjects(items));
|
|
3257
|
-
}
|
|
3258
|
-
/** `addForegroundMobjects(mobject)` (Manim `add_foreground_mobject`). */
|
|
3259
|
-
addForegroundMobject(mobject) {
|
|
3260
|
-
this.addForegroundMobjects(mobject);
|
|
3261
|
-
}
|
|
3262
|
-
/** The mobjects are no longer kept in front; they stay on the stage (Manim
|
|
3263
|
-
* `remove_foreground_mobjects`). */
|
|
3264
|
-
removeForegroundMobjects(...items) {
|
|
3265
|
-
this.#raw.removeForeground(mobjects(items));
|
|
3266
|
-
}
|
|
3267
|
-
/** `removeForegroundMobjects(mobject)` (Manim `remove_foreground_mobject`). */
|
|
3268
|
-
removeForegroundMobject(mobject) {
|
|
3269
|
-
this.removeForegroundMobjects(mobject);
|
|
3270
|
-
}
|
|
3271
|
-
/** Fades everything except the docked heading and `keep` (a new canvas). In a move, the fade
|
|
3272
|
-
* is one part of the move. */
|
|
3273
|
-
fadeAll(opts = {}) {
|
|
3274
|
-
const at = this.#place();
|
|
3275
|
-
const keep = mobjects([opts.keep ?? []]);
|
|
3276
|
-
this.#part();
|
|
3277
|
-
this.#raw.fadeAll(at, keep);
|
|
3278
|
-
}
|
|
3279
|
-
/** Everything on screen except the docked heading, as one group. */
|
|
3280
|
-
canvas() {
|
|
3281
|
-
return wrap(this.#raw.canvas());
|
|
3282
|
-
}
|
|
3283
|
-
/** A kit finding on the picture: a warning with `message`, at the calling line or at `at`.
|
|
3284
|
-
* `aula check` reports it. */
|
|
3285
|
-
warn(message, at) {
|
|
3286
|
-
const location = at ?? this.#place();
|
|
3287
|
-
this.#warnings.push({ severity: "warning", message, location, stack: [], related: [] });
|
|
3288
|
-
}
|
|
3289
|
-
/** The kit findings so far. */
|
|
3290
|
-
get warnings() {
|
|
3291
|
-
return this.#warnings;
|
|
3292
|
-
}
|
|
3293
|
-
/** Seconds since the chapter start. */
|
|
3294
|
-
get time() {
|
|
3295
|
-
return this.#raw.time();
|
|
3296
|
-
}
|
|
3297
|
-
}
|
|
3298
|
-
/** The place of `call` in the chapter file: its first frame in that file. */
|
|
3299
|
-
function placeIn(call, slot) {
|
|
3300
|
-
const frame = slot.frames(call).find((f) => f.file === slot.source);
|
|
3301
|
-
return { file: slot.file, line: frame?.line ?? 0, col: frame?.col ?? 0 };
|
|
3302
|
-
}
|
|
3303
|
-
const err = (val) => ({ tag: "err", val });
|
|
3304
|
-
/** What the engine gets for an `embed`: the kind and the length of the clip, or the problem of
|
|
3305
|
-
* its file. */
|
|
3306
|
-
function clipFile(slot, path, image) {
|
|
3307
|
-
const main = slot.clip(path);
|
|
3308
|
-
if (typeof main === "string")
|
|
3309
|
-
return err(main);
|
|
3310
|
-
if (main.kind === "image") {
|
|
3311
|
-
return err(`embed: ${JSON.stringify(path)} is a picture, and a picture goes with a sound: embed\`note\`("sound.wav", { image: ${JSON.stringify(path)} })`);
|
|
3312
|
-
}
|
|
3313
|
-
if (image !== undefined) {
|
|
3314
|
-
if (main.kind === "video")
|
|
3315
|
-
return err(`embed: ${JSON.stringify(path)} is a video, which has its own picture: leave out { image }`);
|
|
3316
|
-
const picture = slot.clip(image);
|
|
3317
|
-
if (typeof picture === "string")
|
|
3318
|
-
return err(picture);
|
|
3319
|
-
if (picture.kind !== "image") {
|
|
3320
|
-
return err(`embed: the image ${JSON.stringify(image)} is not a PNG or JPEG file`);
|
|
3321
|
-
}
|
|
3322
|
-
}
|
|
3323
|
-
return { tag: "ok", val: { kind: main.kind, seconds: main.seconds } };
|
|
3324
|
-
}
|
|
3325
|
-
/** The chapter that runs: where `m.Image` looks for its file. */
|
|
3326
|
-
let placing;
|
|
3327
|
-
/** Runs `f` with `slot` as the chapter that runs. */
|
|
3328
|
-
function inSlot(slot, f) {
|
|
3329
|
-
const before = placing;
|
|
3330
|
-
placing = slot;
|
|
3331
|
-
try {
|
|
3332
|
-
return f();
|
|
3333
|
-
}
|
|
3334
|
-
finally {
|
|
3335
|
-
placing = before;
|
|
3336
|
-
}
|
|
3337
|
-
}
|
|
3338
|
-
/** The rows of a chapter: a new call of its `rows`, with the seed of `shuffle` at its start. */
|
|
3339
|
-
function rowsOf(chapter, slot) {
|
|
3340
|
-
freshSeed();
|
|
3341
|
-
return inSlot(slot, () => chapter.rows().map((r) => r[ROW]));
|
|
3342
|
-
}
|
|
3343
|
-
/** The notes in the engine's form: each key with its values, in the record's order. */
|
|
3344
|
-
function noteList(notes) {
|
|
3345
|
-
return Object.entries(notes).map(([key, v]) => ({ key, values: typeof v === "string" ? [v] : [...v] }));
|
|
3346
|
-
}
|
|
3347
|
-
/** Gives the rows to the engine in order; with a scene, it runs each move after its `show`. */
|
|
3348
|
-
function write(handle, chapter, rows, slot, scene, card) {
|
|
3349
|
-
inSlot(slot, () => writeRows(handle, chapter, rows, slot, scene, card));
|
|
3350
|
-
}
|
|
3351
|
-
function writeRows(handle, chapter, rows, slot, scene, card) {
|
|
3352
|
-
handle.lexicon([...slot.lexicon]);
|
|
3353
|
-
handle.header(placeIn(chapter.headingCall, slot), [...chapter.headingParts], chapter.metaphor);
|
|
3354
|
-
card?.();
|
|
3355
|
-
for (const r of rows) {
|
|
3356
|
-
const at = placeIn(r.call, slot);
|
|
3357
|
-
if (r.kind === "paragraph") {
|
|
3358
|
-
handle.paragraph(at, noteList(r.notes));
|
|
3359
|
-
}
|
|
3360
|
-
else if (r.kind === "say") {
|
|
3361
|
-
handle.say(at, [...r.parts]);
|
|
3362
|
-
}
|
|
3363
|
-
else if (r.kind === "embed") {
|
|
3364
|
-
handle.embed(at, r.note, r.path, r.image, clipFile(slot, r.path, r.image));
|
|
3365
|
-
}
|
|
3366
|
-
else if (r.move === undefined) {
|
|
3367
|
-
handle.open(at, r.note, r.timing);
|
|
3368
|
-
}
|
|
3369
|
-
else {
|
|
3370
|
-
handle.show(at, r.note, r.timing);
|
|
3371
|
-
if (scene !== undefined) {
|
|
3372
|
-
moving = true;
|
|
3373
|
-
try {
|
|
3374
|
-
r.move(scene);
|
|
3375
|
-
}
|
|
3376
|
-
finally {
|
|
3377
|
-
moving = false;
|
|
3378
|
-
closed = new Set();
|
|
3379
|
-
}
|
|
3380
|
-
}
|
|
3381
|
-
}
|
|
3382
|
-
}
|
|
3383
|
-
}
|
|
3384
|
-
/** The findings of the paragraph checks on the complete text and on what was on screen
|
|
3385
|
-
* (`screens`, one for each paragraph). A finding that repeats one before it (the same message
|
|
3386
|
-
* at the same place) is dropped. */
|
|
3387
|
-
function findings(rows, text, slot, screens, exists) {
|
|
3388
|
-
const chapter = { ...text, number: slot.number, count: slot.count, exists };
|
|
3389
|
-
const out = [];
|
|
3390
|
-
const seen = new Set();
|
|
3391
|
-
let index = -1;
|
|
3392
|
-
for (const r of rows) {
|
|
3393
|
-
if (r.kind !== "paragraph")
|
|
3394
|
-
continue;
|
|
3395
|
-
index += 1;
|
|
3396
|
-
const p = text.paragraphs[index];
|
|
3397
|
-
if (r.check === undefined || p === undefined)
|
|
3398
|
-
continue;
|
|
3399
|
-
const screen = screens[index] ?? { instants: [] };
|
|
3400
|
-
for (const f of r.check({ ...p, index, notes: r.notes, screen }, chapter)) {
|
|
3401
|
-
const location = f.at ?? p.place;
|
|
3402
|
-
const key = JSON.stringify([f.message, location]);
|
|
3403
|
-
if (seen.has(key))
|
|
3404
|
-
continue;
|
|
3405
|
-
seen.add(key);
|
|
3406
|
-
out.push({
|
|
3407
|
-
severity: "warning",
|
|
3408
|
-
message: f.message,
|
|
3409
|
-
...(location === undefined ? {} : { location }),
|
|
3410
|
-
stack: [],
|
|
3411
|
-
related: [],
|
|
3412
|
-
});
|
|
3413
|
-
}
|
|
3414
|
-
}
|
|
3415
|
-
return out;
|
|
3416
|
-
}
|
|
3417
|
-
/** Plays `card` for the heading cue of `handle` on its own scene, at place `at`. Gives the
|
|
3418
|
-
* scene: its warnings are the card's findings. */
|
|
3419
|
-
function playCard(handle, slot, at, card, heading) {
|
|
3420
|
-
const scene = new Scene(handle, slot, at);
|
|
3421
|
-
const [start, end] = handle.headingCue();
|
|
3422
|
-
const chrome = (...items) => {
|
|
3423
|
-
handle.chrome(mobjects(items));
|
|
3424
|
-
};
|
|
3425
|
-
moving = true;
|
|
3426
|
-
try {
|
|
3427
|
-
inSlot(slot, () => {
|
|
3428
|
-
card(scene, { ...heading, start, end, chrome });
|
|
3429
|
-
});
|
|
3430
|
-
}
|
|
3431
|
-
finally {
|
|
3432
|
-
moving = false;
|
|
3433
|
-
}
|
|
3434
|
-
return scene;
|
|
3435
|
-
}
|
|
3436
|
-
/** The text of `rows`: a text-only engine chapter, finished. */
|
|
3437
|
-
function textOf(chapter, rows, slot) {
|
|
3438
|
-
const handle = engine.Chapter.textPass(slot.number, slot.count + 1);
|
|
3439
|
-
write(handle, chapter, rows, slot);
|
|
3440
|
-
handle.finish();
|
|
3441
|
-
return { text: handle.text(), problems: handle.problems() };
|
|
3442
|
-
}
|
|
3443
|
-
/** Reads a chapter: calls `rows` and no move. It gives the text and the text problems; the
|
|
3444
|
-
* paragraph checks run only in `drawRows`. A chapter error is thrown (a AulaError, a WIT
|
|
3445
|
-
* `result` error or the JS error). */
|
|
3446
|
-
export function readRows(chapter, slot) {
|
|
3447
|
-
const rows = rowsOf(chapter, slot);
|
|
3448
|
-
const { text, problems } = textOf(chapter, rows, slot);
|
|
3449
|
-
return { text, diagnostics: problems };
|
|
3450
|
-
}
|
|
3451
|
-
/** Checks a chapter (`aula check`): calls `rows`, then each move in list order, with no clock
|
|
3452
|
-
* and no narration. Each animation jumps to its end, and the engine's checks, the kit findings
|
|
3453
|
-
* and the paragraph checks (with `exists`, `Host.exists`) look at the pictures in order. A
|
|
3454
|
-
* chapter error is thrown (a AulaError, a WIT `result` error or the JS error). */
|
|
3455
|
-
export function checkRows(chapter, theme, card, slot, exists) {
|
|
3456
|
-
const rows = rowsOf(chapter, slot);
|
|
3457
|
-
const { text } = textOf(chapter, rows, slot);
|
|
3458
|
-
const handle = engine.Chapter.orderPass(slot.number, slot.count + 1);
|
|
3459
|
-
const scene = new Scene(handle, slot);
|
|
3460
|
-
const heading = { title: chapter.heading, chapter: slot.number, count: slot.count, theme };
|
|
3461
|
-
let cardScene;
|
|
3462
|
-
write(handle, chapter, rows, slot, scene, () => {
|
|
3463
|
-
cardScene = playCard(handle, slot, placeIn(chapter.call, slot), card, heading);
|
|
3464
|
-
});
|
|
3465
|
-
handle.finish();
|
|
3466
|
-
return {
|
|
3467
|
-
diagnostics: [
|
|
3468
|
-
...handle.problems(),
|
|
3469
|
-
...handle.report(),
|
|
3470
|
-
...(cardScene?.warnings ?? []),
|
|
3471
|
-
...scene.warnings,
|
|
3472
|
-
...findings(rows, text, slot, handle.screens(), exists),
|
|
3473
|
-
],
|
|
3474
|
-
};
|
|
3475
|
-
}
|
|
3476
|
-
/** Checks chapter 0: the card of the project `title`, with no clock. */
|
|
3477
|
-
export function checkTitleCard(title, theme, card, count) {
|
|
3478
|
-
freshSeed();
|
|
3479
|
-
const handle = engine.Chapter.orderPass(0, count + 1);
|
|
3480
|
-
const slot = {
|
|
3481
|
-
number: 0,
|
|
3482
|
-
count,
|
|
3483
|
-
source: "",
|
|
3484
|
-
file: "",
|
|
3485
|
-
frames: () => [],
|
|
3486
|
-
lexicon: [],
|
|
3487
|
-
clip: () => "no files",
|
|
3488
|
-
picture: () => "a picture needs a project folder",
|
|
3489
|
-
};
|
|
3490
|
-
const scene = playCard(handle, slot, { file: "", line: 0, col: 0 }, card, { title, chapter: 0, count, theme });
|
|
3491
|
-
handle.finish();
|
|
3492
|
-
return { diagnostics: [...handle.report(), ...scene.warnings] };
|
|
3493
|
-
}
|
|
3494
|
-
function finished(handle) {
|
|
3495
|
-
handle.finish();
|
|
3496
|
-
return {
|
|
3497
|
-
diagnostics: [...handle.problems(), ...handle.report()],
|
|
3498
|
-
lottie: handle.lottie(),
|
|
3499
|
-
frames: handle.frames(),
|
|
3500
|
-
chapterSeconds: handle.chapterSeconds(),
|
|
3501
|
-
waits: handle.waits(),
|
|
3502
|
-
embeds: handle.played(),
|
|
3503
|
-
narration: handle.narration(),
|
|
3504
|
-
};
|
|
3505
|
-
}
|
|
3506
|
-
/** Draws a chapter on the narration clock: calls `rows`, then each move once, in list order,
|
|
3507
|
-
* from the first word of its `say` to its end (or for its own length), with its parts sharing
|
|
3508
|
-
* that time. An open move holds the picture. When a move is longer than its `say`, the
|
|
3509
|
-
* narration waits. A chapter error is thrown (a AulaError, a WIT `result` error or the JS
|
|
3510
|
-
* error). */
|
|
3511
|
-
export function drawRows(chapter, theme, card, slot, drawing) {
|
|
3512
|
-
const rows = rowsOf(chapter, slot);
|
|
3513
|
-
const handle = engine.Chapter.render(drawing.narration, theme.background, slot.number, drawing.count, drawing.name, drawing.lint, drawing.prune);
|
|
3514
|
-
const scene = new Scene(handle, slot);
|
|
3515
|
-
const heading = { title: chapter.heading, chapter: slot.number, count: slot.count, theme };
|
|
3516
|
-
write(handle, chapter, rows, slot, scene, () => {
|
|
3517
|
-
playCard(handle, slot, placeIn(chapter.call, slot), card, heading);
|
|
3518
|
-
});
|
|
3519
|
-
return finished(handle);
|
|
3520
|
-
}
|
|
3521
|
-
/** Draws chapter 0: the card of the project `title` while the title is spoken. */
|
|
3522
|
-
export function drawTitle(title, theme, card, drawing) {
|
|
3523
|
-
freshSeed();
|
|
3524
|
-
const handle = engine.Chapter.render(drawing.narration, theme.background, 0, drawing.count, drawing.name, drawing.lint, drawing.prune);
|
|
3525
|
-
const count = drawing.count - 1;
|
|
3526
|
-
const slot = {
|
|
3527
|
-
number: 0,
|
|
3528
|
-
count,
|
|
3529
|
-
source: "",
|
|
3530
|
-
file: "",
|
|
3531
|
-
frames: () => [],
|
|
3532
|
-
lexicon: [],
|
|
3533
|
-
clip: () => "no files",
|
|
3534
|
-
picture: () => "a picture needs a project folder",
|
|
3535
|
-
};
|
|
3536
|
-
playCard(handle, slot, { file: "", line: 0, col: 0 }, card, { title, chapter: 0, count, theme });
|
|
3537
|
-
return finished(handle);
|
|
3538
|
-
}
|
|
3539
|
-
/** Draws `draw` as a Manim construct. `s.play` and `s.wait` run from t = 0 at 60 frames a
|
|
3540
|
-
* second, on a black background. There is no project, no narration and no heading. */
|
|
3541
|
-
export function renderScene(draw) {
|
|
3542
|
-
const handle = engine.Chapter.scene();
|
|
3543
|
-
const slot = {
|
|
3544
|
-
number: 0,
|
|
3545
|
-
count: 1,
|
|
3546
|
-
source: "scene",
|
|
3547
|
-
file: "scene",
|
|
3548
|
-
frames: () => [],
|
|
3549
|
-
lexicon: [],
|
|
3550
|
-
clip: () => "no files",
|
|
3551
|
-
picture: () => "a picture needs a project folder",
|
|
3552
|
-
};
|
|
3553
|
-
draw(new Scene(handle, slot));
|
|
3554
|
-
handle.finish();
|
|
3555
|
-
const trace = JSON.parse(handle.trace());
|
|
3556
|
-
if (!Array.isArray(trace))
|
|
3557
|
-
fault("renderScene: the trace is not a list of frames");
|
|
3558
|
-
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- aula wrote this trace in this run
|
|
3559
|
-
const leaves = trace;
|
|
3560
|
-
return { lottie: handle.lottie(), frames: handle.frames(), trace: leaves };
|
|
3561
|
-
}
|