@beff/aula 0.8.1 → 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.
Files changed (65) hide show
  1. package/dist/generated/aula.core5.wasm +0 -0
  2. package/dist/index.d.ts +7304 -6
  3. package/dist/index.js +14864 -11
  4. package/dist/index.js.map +1 -0
  5. package/package.json +8 -25
  6. package/dist/animations.d.ts +0 -635
  7. package/dist/animations.js +0 -1778
  8. package/dist/braces.d.ts +0 -84
  9. package/dist/braces.js +0 -253
  10. package/dist/card.d.ts +0 -17
  11. package/dist/card.js +0 -90
  12. package/dist/colors-palette.d.ts +0 -2242
  13. package/dist/colors-palette.js +0 -2241
  14. package/dist/colors.d.ts +0 -120
  15. package/dist/colors.js +0 -403
  16. package/dist/curves.d.ts +0 -10
  17. package/dist/curves.js +0 -128
  18. package/dist/engine.d.ts +0 -1705
  19. package/dist/engine.js +0 -3561
  20. package/dist/errors.d.ts +0 -19
  21. package/dist/errors.js +0 -25
  22. package/dist/geometry.d.ts +0 -551
  23. package/dist/geometry.js +0 -1555
  24. package/dist/graph.d.ts +0 -117
  25. package/dist/graph.js +0 -526
  26. package/dist/graphing.d.ts +0 -597
  27. package/dist/graphing.js +0 -1504
  28. package/dist/host.d.ts +0 -19
  29. package/dist/host.js +0 -85
  30. package/dist/lexicon.d.ts +0 -10
  31. package/dist/lexicon.js +0 -23
  32. package/dist/load.d.ts +0 -21
  33. package/dist/load.js +0 -109
  34. package/dist/logo.d.ts +0 -30
  35. package/dist/logo.js +0 -186
  36. package/dist/manim.d.ts +0 -22
  37. package/dist/manim.js +0 -19
  38. package/dist/media.d.ts +0 -34
  39. package/dist/media.js +0 -391
  40. package/dist/mobjects.d.ts +0 -127
  41. package/dist/mobjects.js +0 -316
  42. package/dist/picture.d.ts +0 -23
  43. package/dist/picture.js +0 -291
  44. package/dist/pointcloud.d.ts +0 -144
  45. package/dist/pointcloud.js +0 -384
  46. package/dist/rates.d.ts +0 -19
  47. package/dist/rates.js +0 -54
  48. package/dist/run.d.ts +0 -101
  49. package/dist/run.js +0 -323
  50. package/dist/tables.d.ts +0 -179
  51. package/dist/tables.js +0 -403
  52. package/dist/tex.d.ts +0 -8
  53. package/dist/tex.js +0 -257
  54. package/dist/text-layouts.d.ts +0 -64
  55. package/dist/text-layouts.js +0 -225
  56. package/dist/text.d.ts +0 -61
  57. package/dist/text.js +0 -78
  58. package/dist/theme.d.ts +0 -25
  59. package/dist/theme.js +0 -12
  60. package/dist/user-code.d.ts +0 -5
  61. package/dist/user-code.js +0 -20
  62. package/dist/utils.d.ts +0 -152
  63. package/dist/utils.js +0 -517
  64. package/dist/vector-field.d.ts +0 -132
  65. package/dist/vector-field.js +0 -361
@@ -1,361 +0,0 @@
1
- // Manim CE's vector fields: VectorField, ArrowVectorField and StreamLines. A vector field is a
2
- // function from a point to a vector. StreamLines traces agents that move with the field, and
3
- // its flow animation runs on an updater, as in Manim.
4
- import { FRAME_HEIGHT, FRAME_WIDTH, Mobject, ORIGIN, Polyline, add, easeOutSine, linear, mul, norm, raw, wrong, } from "./engine.js";
5
- import { AnimationGroup, Create, Succession, UpdateFromAlphaFunc } from "./animations.js";
6
- import { BLUE_E, GREEN, RED } from "./colors-palette.js";
7
- import { colorToRgb, colorToRgba, interpolateColor, rgbToColor } from "./colors.js";
8
- import { partialPoints } from "./curves.js";
9
- import { engine } from "./load.js";
10
- import { Vector } from "./geometry.js";
11
- import { YELLOW } from "./engine.js";
12
- import { sigmoid } from "./utils.js";
13
- /** Manim's `DEFAULT_SCALAR_FIELD_COLORS`. */
14
- const DEFAULT_COLORS = [BLUE_E, GREEN, YELLOW, RED];
15
- /** Manim's `VectorField.shift_func`: the field moved by `shiftVector`. */
16
- function shiftFunc(func, shiftVector) {
17
- return (p) => func([p[0] - shiftVector[0], p[1] - shiftVector[1]]);
18
- }
19
- /** Manim's `VectorField.scale_func`: the field with its points scaled by `scalar`. */
20
- function scaleFunc(func, scalar) {
21
- return (p) => func(mul(p, scalar));
22
- }
23
- /** A small seeded generator (mulberry32), so a field looks the same in each build. */
24
- function seeded(seed) {
25
- let a = seed >>> 0;
26
- return () => {
27
- a = (a + 0x6d2b79f5) >>> 0;
28
- let t = a;
29
- t = Math.imul(t ^ (t >>> 15), t | 1);
30
- t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
31
- return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
32
- };
33
- }
34
- /** A vector field: Manim's `VectorField`. It has no picture of its own; it moves mobjects. */
35
- export class VectorFieldMobject extends Mobject {
36
- #c = { func: () => ORIGIN, single: undefined, scheme: norm, min: 0, max: 2, colors: [...DEFAULT_COLORS] };
37
- #movement;
38
- /** The field (Manim `func`). */
39
- get func() {
40
- return this.#c.func;
41
- }
42
- /** True when `color` was given (Manim `single_color`). */
43
- get singleColor() {
44
- return this.#c.single !== undefined;
45
- }
46
- copy() {
47
- return this.copyField(new VectorFieldMobject(raw(this).copy()));
48
- }
49
- /** Gives `to`, a copy of the node, the field and the colours of this one. */
50
- copyField(to) {
51
- to.#c = this.#c;
52
- return to;
53
- }
54
- /** Sets the field and its colours (the constructors call it). */
55
- setUp(func, opts) {
56
- const { color, colorScheme = norm, minColorSchemeValue = 0, maxColorSchemeValue = 2, colors = DEFAULT_COLORS, } = opts;
57
- this.#c = {
58
- func,
59
- single: color === undefined ? undefined : rgbToColor(colorToRgb(color)),
60
- scheme: colorScheme,
61
- min: minColorSchemeValue,
62
- max: maxColorSchemeValue,
63
- colors: colors.map((c) => rgbToColor(colorToRgb(c))),
64
- };
65
- return this;
66
- }
67
- /** The colour of the field at `pos` (Manim `pos_to_color`). */
68
- posToColor(pos) {
69
- const { single, scheme, min, max, colors } = this.#c;
70
- if (single !== undefined)
71
- return single;
72
- const value = Math.min(Math.max(scheme(this.#c.func(pos)), min), max);
73
- let alpha = max === min ? 0 : ((value - min) / (max - min)) * (colors.length - 1);
74
- const c1 = colors[Math.floor(alpha)] ?? YELLOW;
75
- const c2 = colors[Math.min(Math.floor(alpha + 1), colors.length - 1)] ?? c1;
76
- alpha %= 1;
77
- return interpolateColor(c1, c2, alpha);
78
- }
79
- /** `func` moved by `shiftVector` (Manim `shift_func`). Also `m.VectorField.shiftFunc`. */
80
- shiftFunc(func, shiftVector) {
81
- return shiftFunc(func, shiftVector);
82
- }
83
- /** `func` with its points scaled by `scalar` (Manim `scale_func`). Also `m.VectorField.scaleFunc`. */
84
- scaleFunc(func, scalar) {
85
- return scaleFunc(func, scalar);
86
- }
87
- /** Maps each point of the field through `coordinateSystem.coordsToPoint` (Manim
88
- * `fit_to_coordinate_system`). */
89
- fitToCoordinateSystem(coordinateSystem) {
90
- return this.applyFunction((p) => coordinateSystem.coordsToPoint(p[0], p[1]));
91
- }
92
- /** Moves `mob` along the field for time `dt`, in `substeps` Runge-Kutta steps (Manim
93
- * `nudge`). With `pointwise`, each point of `mob` moves; else its centre. */
94
- nudge(mob, dt = 1, substeps = 1, pointwise = false) {
95
- const f = this.#c.func;
96
- const h = dt / substeps;
97
- const rk = (p) => {
98
- const k1 = f(p);
99
- const k2 = f(add(p, mul(k1, h / 2)));
100
- const k3 = f(add(p, mul(k2, h / 2)));
101
- const k4 = f(add(p, mul(k3, h)));
102
- return mul(add(add(k1, mul(k2, 2)), add(mul(k3, 2), k4)), h / 6);
103
- };
104
- for (let i = 0; i < substeps; i++) {
105
- if (pointwise)
106
- mob.applyFunction((p) => add(p, rk(p)));
107
- else
108
- mob.shift(rk(mob.getCenter()));
109
- }
110
- return this;
111
- }
112
- /** Nudges each submobject (Manim `nudge_submobjects`). */
113
- nudgeSubmobjects(dt = 1, substeps = 1, pointwise = false) {
114
- for (const mob of this.submobjects)
115
- this.nudge(mob, dt, substeps, pointwise);
116
- return this;
117
- }
118
- /** An updater that moves a mobject along the field at `speed` (Manim `get_nudge_updater`). */
119
- getNudgeUpdater(speed = 1, pointwise = false) {
120
- return (mob, dt) => {
121
- this.nudge(mob, dt * speed, 1, pointwise);
122
- };
123
- }
124
- /** Moves the submobjects along the field on every frame (Manim `start_submobject_movement`). */
125
- startSubmobjectMovement(speed = 1, pointwise = false) {
126
- this.stopSubmobjectMovement();
127
- const updater = (_m, dt) => {
128
- this.nudgeSubmobjects(dt * speed, 1, pointwise);
129
- };
130
- this.#movement = updater;
131
- return this.addUpdater(updater);
132
- }
133
- /** Stops the movement of `startSubmobjectMovement` (Manim `stop_submobject_movement`). */
134
- stopSubmobjectMovement() {
135
- if (this.#movement !== undefined)
136
- this.removeUpdater(this.#movement);
137
- this.#movement = undefined;
138
- return this;
139
- }
140
- /** A function from values to `[r, g, b, a]` colours of the gradient of `colors` from `start`
141
- * to `end` (Manim `get_vectorized_rgba_gradient_function`). */
142
- getVectorizedRgbaGradientFunction(start, end, colors) {
143
- const list = colors.map((c) => rgbToColor(colorToRgb(c)));
144
- return (values, opacity = 1) => values.map((v) => {
145
- const alpha = Math.min(Math.max(end === start ? 0 : (v - start) / (end - start), 0), 1);
146
- const scaled = alpha * (list.length - 1);
147
- const i = Math.floor(scaled);
148
- const c = interpolateColor(list[i] ?? YELLOW, list[Math.min(i + 1, list.length - 1)] ?? YELLOW, scaled % 1);
149
- const [r, g, b] = colorToRgba(c);
150
- return [r, g, b, opacity];
151
- });
152
- }
153
- }
154
- /** A vector field with no picture (Manim `VectorField`): `VectorField(func, opts)`. */
155
- export const VectorField = Object.assign(function VectorField(func, opts = {}) {
156
- return new VectorFieldMobject(engine.vGroup([])).setUp(func, opts);
157
- }, { shiftFunc, scaleFunc });
158
- /** Manim's field ranges: the frame by default, the step 0.5, and the end included. */
159
- function fieldRanges(xRange, yRange) {
160
- const full = (r, half) => {
161
- const [lo, hi, d = 0.5] = r ?? [Math.floor(-half), Math.ceil(half)];
162
- const n = Math.max(0, Math.ceil((hi + d - lo) / d));
163
- return Array.from({ length: n }, (_, i) => lo + i * d);
164
- };
165
- return [full(xRange, FRAME_WIDTH / 2), full(yRange, FRAME_HEIGHT / 2)];
166
- }
167
- /** A field of arrows: Manim's `ArrowVectorField`. */
168
- export class ArrowVectorFieldMobject extends VectorFieldMobject {
169
- lengthFunc = (n) => 0.45 * sigmoid(n);
170
- vectorConfig = {};
171
- copy() {
172
- const c = this.copyField(new ArrowVectorFieldMobject(raw(this).copy()));
173
- c.lengthFunc = this.lengthFunc;
174
- c.vectorConfig = this.vectorConfig;
175
- return c;
176
- }
177
- getVector(point) {
178
- if (point === undefined)
179
- return super.getVector();
180
- let output = this.func(point);
181
- const n = norm(output);
182
- const defined = Number.isFinite(n);
183
- if (!defined)
184
- output = [0, 0];
185
- else if (n !== 0)
186
- output = mul(output, this.lengthFunc(n) / n);
187
- const v = Vector(output, this.vectorConfig).shift(point);
188
- if (defined)
189
- v.setColor(this.posToColor(point));
190
- return v;
191
- }
192
- }
193
- /** Arrows of `func` on a grid (Manim `ArrowVectorField`): `ArrowVectorField(func, { xRange, yRange })`. */
194
- export function ArrowVectorField(func, opts = {}) {
195
- const { xRange, yRange, lengthFunc, opacity = 1, vectorConfig = {}, ...field } = opts;
196
- const f = new ArrowVectorFieldMobject(engine.vGroup([])).setUp(func, field);
197
- if (lengthFunc !== undefined)
198
- f.lengthFunc = lengthFunc;
199
- f.vectorConfig = vectorConfig;
200
- const [xs, ys] = fieldRanges(xRange, yRange);
201
- for (const x of xs)
202
- for (const y of ys)
203
- f.add(f.getVector([x, y]));
204
- return f.setOpacity(opacity);
205
- }
206
- /** The step of a field range (default 0.5). */
207
- function step(r) {
208
- return r?.[2] ?? 0.5;
209
- }
210
- /** The part of a passing flash at `alpha` (Manim `ShowPassingFlash`). */
211
- function flash(alpha, timeWidth) {
212
- const upper = alpha * (1 + timeWidth);
213
- return [Math.max(upper - timeWidth, 0), Math.min(upper, 1)];
214
- }
215
- /** The traces of agents in a field: Manim's `StreamLines`. Its submobjects are the lines. */
216
- export class StreamLinesMobject extends VectorFieldMobject {
217
- virtualTime = 3;
218
- #lines = [];
219
- #flow;
220
- #random = seeded(0);
221
- /** The lines (Manim `stream_lines`). */
222
- get streamLines() {
223
- return this.#lines.map((l) => l.mob);
224
- }
225
- /** The speed of the flow of `startAnimation` (Manim `flow_speed`). */
226
- get flowSpeed() {
227
- return this.#flow?.flowSpeed;
228
- }
229
- copy() {
230
- const c = this.copyField(new StreamLinesMobject(raw(this).copy()));
231
- c.virtualTime = this.virtualTime;
232
- c.#lines = c.submobjects.map((mob, i) => {
233
- const own = this.#lines[i];
234
- return { mob, points: mob.getPoints(), duration: own?.duration ?? 0, time: 0 };
235
- });
236
- return c;
237
- }
238
- /** Adds a line, for the constructor. */
239
- addLine(mob, duration) {
240
- this.#lines.push({ mob, points: mob.getPoints(), duration, time: 0 });
241
- this.add(mob);
242
- }
243
- /** Each line is drawn, in random order (Manim `create`). `runTime` is the time of each line
244
- * (default `virtualTime`, or a function of it); `lagRatio` makes the whole 1.5 times as long. */
245
- create(lagRatio, runTime, opts = {}) {
246
- const time = runTime === undefined ? this.virtualTime : typeof runTime === "function" ? runTime(this.virtualTime) : runTime;
247
- const lag = lagRatio ?? time / 2 / Math.max(1, this.submobjects.length);
248
- const order = this.#lines.map((_, i) => i);
249
- for (let i = order.length - 1; i > 0; i--) {
250
- const j = Math.floor(this.#random() * (i + 1));
251
- [order[i], order[j]] = [order[j] ?? j, order[i] ?? i];
252
- }
253
- const animations = order.map((i) => Create(this.#lines[i]?.mob ?? this, { ...opts, runTime: time }));
254
- return AnimationGroup(...animations, { lagRatio: lag });
255
- }
256
- /** Makes the lines flow on every frame, with a passing flash of `timeWidth` along each line
257
- * (Manim `start_animation`). With `warmUp` the lines start one by one. */
258
- startAnimation(opts = {}) {
259
- const { warmUp = true, flowSpeed = 1, timeWidth = 0.3, rateFunc = linear } = opts;
260
- if (this.#flow !== undefined)
261
- this.removeUpdater(this.#flow.updater);
262
- for (const line of this.#lines) {
263
- line.time = this.#random() * this.virtualTime * (warmUp ? -1 : 1);
264
- }
265
- const show = (line, time) => {
266
- const runTime = line.duration / flowSpeed;
267
- const alpha = rateFunc(Math.min(Math.max(time / runTime, 0), 1));
268
- const [lo, hi] = flash(alpha, timeWidth);
269
- line.mob.setPoints(partialPoints(line.points, lo, hi));
270
- };
271
- const updater = (_m, dt) => {
272
- for (const line of this.#lines) {
273
- line.time += dt * flowSpeed;
274
- if (line.time >= this.virtualTime)
275
- line.time -= this.virtualTime;
276
- show(line, line.time);
277
- }
278
- };
279
- for (const line of this.#lines)
280
- show(line, line.time);
281
- this.#flow = { updater, flowSpeed, timeWidth, rateFunc };
282
- return this.addUpdater(updater);
283
- }
284
- /** Ends the flow smoothly: each line finishes its flash, then is drawn whole (Manim
285
- * `end_animation`). Call `startAnimation` first. */
286
- endAnimation() {
287
- const flow = this.#flow;
288
- if (flow === undefined) {
289
- return wrong("endAnimation", "the stream lines do not flow", "call startAnimation() first");
290
- }
291
- this.removeUpdater(flow.updater);
292
- this.#flow = undefined;
293
- const maxRunTime = this.virtualTime / flow.flowSpeed;
294
- const creationRunTime = (maxRunTime / (1 + flow.timeWidth)) * easeOutSine(0.001) * 1000;
295
- const animations = this.#lines.map((line) => {
296
- const draw = UpdateFromAlphaFunc(line.mob, (mob, alpha) => {
297
- mob.setPoints(partialPoints(line.points, 0, alpha)).setStroke({ opacity: 1 });
298
- }, { runTime: creationRunTime, rateFunc: easeOutSine });
299
- const t0 = line.time;
300
- const first = t0 <= 0
301
- ? UpdateFromAlphaFunc(line.mob, (mob, alpha) => {
302
- if (alpha === 0)
303
- mob.setStroke({ opacity: 0 });
304
- }, { runTime: Math.max(-t0 / flow.flowSpeed, 1e-3), rateFunc: linear })
305
- : UpdateFromAlphaFunc(line.mob, (mob, alpha) => {
306
- const time = t0 + alpha * (this.virtualTime - t0);
307
- const flashAlpha = flow.rateFunc(Math.min(time / (line.duration / flow.flowSpeed), 1));
308
- const [lo, hi] = flash(flashAlpha, flow.timeWidth);
309
- mob.setPoints(partialPoints(line.points, lo, hi));
310
- }, { runTime: Math.max(maxRunTime - t0 / flow.flowSpeed, 1e-3), rateFunc: linear });
311
- return Succession(first, draw);
312
- });
313
- return AnimationGroup(...animations);
314
- }
315
- }
316
- /** Stream lines of `func` (Manim `StreamLines`): each agent starts near a grid point and moves
317
- * with the field for `virtualTime`. */
318
- export function StreamLines(func, opts = {}) {
319
- const { xRange, yRange, noiseFactor, nRepeats = 1, dt = 0.05, virtualTime = 3, maxAnchorsPerLine = 100, padding = 3, strokeWidth = 1, opacity = 1, ...field } = opts;
320
- const s = new StreamLinesMobject(engine.vGroup([])).setUp(func, field);
321
- s.virtualTime = virtualTime;
322
- const [xs, ys] = fieldRanges(xRange, yRange);
323
- const noise = noiseFactor ?? step(yRange) / 2;
324
- const random = seeded(0);
325
- const [xMin, xMax] = [xs[0] ?? 0, (xs.at(-1) ?? 0) + step(xRange)];
326
- const [yMin, yMax] = [ys[0] ?? 0, (ys.at(-1) ?? 0) + step(yRange)];
327
- const outside = (p) => p[0] < xMin - padding ||
328
- p[0] > xMax + padding - step(xRange) ||
329
- p[1] < yMin - padding ||
330
- p[1] > yMax + padding - step(yRange);
331
- const maxSteps = Math.ceil(virtualTime / dt) + 1;
332
- for (let n = 0; n < nRepeats; n++) {
333
- for (const x of xs) {
334
- for (const y of ys) {
335
- const start = [x - noise / 2 + noise * random(), y - noise / 2 + noise * random()];
336
- const points = [start];
337
- let steps = maxSteps;
338
- for (let k = 0; k < maxSteps; k++) {
339
- const last = points.at(-1) ?? start;
340
- const next = add(last, mul(func(last), dt));
341
- if (outside(next)) {
342
- steps = k;
343
- break;
344
- }
345
- points.push(next);
346
- }
347
- if (steps === 0)
348
- continue;
349
- const every = Math.max(1, Math.floor(points.length / maxAnchorsPerLine));
350
- const anchors = points.filter((_, i) => i % every === 0);
351
- if (anchors.length < 2)
352
- continue;
353
- const line = Polyline(anchors).setPointsSmoothly(anchors);
354
- const middle = anchors[Math.floor(anchors.length / 2)] ?? start;
355
- line.setStroke(s.posToColor(middle), { width: strokeWidth, opacity });
356
- s.addLine(line, steps * dt);
357
- }
358
- }
359
- }
360
- return s;
361
- }