@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/animations.js
DELETED
|
@@ -1,1778 +0,0 @@
|
|
|
1
|
-
// Manim CE's animations (the modules `manim.animation.*`) with CE's names, signatures and
|
|
2
|
-
// defaults. Most run in the engine (`engineAnimation`). The others run in JavaScript, as
|
|
3
|
-
// subclasses of `Animation` that override `interpolateMobject` or `interpolateSubmobject`,
|
|
4
|
-
// exactly as in CE: the engine plays them with its clock (WIT `driven`).
|
|
5
|
-
import { engine } from "./load.js";
|
|
6
|
-
import { Animation, DecimalNumberMobject, DEGREES, FRAME_HEIGHT, FRAME_WIDTH, METHOD_FUNC, METHOD_SELF, Mobject, ORIGIN, PI, PURE_YELLOW, RIGHT, SMALL_BUFF, TAU, UP, VGroup, WHITE, animationList, circleFrames, animations, doubleSmooth, engineAnimation, isDriven, linear, norm, raw, rawRate, smooth, split, sub, thereAndBack, wrong, } from "./engine.js";
|
|
7
|
-
import { BLUE_B, BLUE_D, BLUE_E, GREY, GREY_BROWN } from "./colors-palette.js";
|
|
8
|
-
import { Circle, Dot, Line, Rectangle } from "./geometry.js";
|
|
9
|
-
import { squishRateFunc, wiggle } from "./rates.js";
|
|
10
|
-
import { register } from "./host.js";
|
|
11
|
-
// --- helpers ----------------------------------------------------------------------------
|
|
12
|
-
/** Manim's `interpolate`. */
|
|
13
|
-
function mix(a, b, t) {
|
|
14
|
-
return a + (b - a) * t;
|
|
15
|
-
}
|
|
16
|
-
/** Manim's `inverse_interpolate`. */
|
|
17
|
-
function unmix(a, b, v) {
|
|
18
|
-
return (v - a) / (b - a);
|
|
19
|
-
}
|
|
20
|
-
/** The family members with points (Manim `family_members_with_points`). */
|
|
21
|
-
function withPoints(m) {
|
|
22
|
-
return m.getFamily().filter((f) => f.hasPoints());
|
|
23
|
-
}
|
|
24
|
-
function lerpPoint(p, r, t) {
|
|
25
|
-
return [p[0] + (r[0] - p[0]) * t, p[1] + (r[1] - p[1]) * t];
|
|
26
|
-
}
|
|
27
|
-
/** De Casteljau: cubic `c` split at `t`. */
|
|
28
|
-
function splitCubic(c, t) {
|
|
29
|
-
const [p0, p1, p2, p3] = c;
|
|
30
|
-
const p01 = lerpPoint(p0, p1, t);
|
|
31
|
-
const p12 = lerpPoint(p1, p2, t);
|
|
32
|
-
const p23 = lerpPoint(p2, p3, t);
|
|
33
|
-
const p012 = lerpPoint(p01, p12, t);
|
|
34
|
-
const p123 = lerpPoint(p12, p23, t);
|
|
35
|
-
const mid = lerpPoint(p012, p123, t);
|
|
36
|
-
return [
|
|
37
|
-
[p0, p01, p012, mid],
|
|
38
|
-
[mid, p123, p23, p3],
|
|
39
|
-
];
|
|
40
|
-
}
|
|
41
|
-
/** The part of cubic `q` from `a` to `b` (Manim `partial_bezier_points`). */
|
|
42
|
-
function partialCubic(q, a, b) {
|
|
43
|
-
if (a >= 1)
|
|
44
|
-
return [q[3], q[3], q[3], q[3]];
|
|
45
|
-
const right = splitCubic(q, a)[1];
|
|
46
|
-
return splitCubic(right, Math.min(1, Math.max(0, (b - a) / (1 - a))))[0];
|
|
47
|
-
}
|
|
48
|
-
/** Sets the own points of `mob` to the part of the points of `start` from proportion `a` to
|
|
49
|
-
* `b`, by curves, as Manim's `pointwise_become_partial`. The number of curves stays. */
|
|
50
|
-
function becomePartial(mob, start, a, b) {
|
|
51
|
-
const points = start.getPoints();
|
|
52
|
-
const n = Math.floor(points.length / 4);
|
|
53
|
-
if (n === 0)
|
|
54
|
-
return;
|
|
55
|
-
if (a <= 0 && b >= 1) {
|
|
56
|
-
mob.setPoints(points);
|
|
57
|
-
return;
|
|
58
|
-
}
|
|
59
|
-
const curve = (i) => {
|
|
60
|
-
const at = (k) => points[4 * i + k] ?? ORIGIN;
|
|
61
|
-
return [at(0), at(1), at(2), at(3)];
|
|
62
|
-
};
|
|
63
|
-
const at = (x) => {
|
|
64
|
-
if (x >= 1)
|
|
65
|
-
return [n - 1, 1];
|
|
66
|
-
if (x <= 0)
|
|
67
|
-
return [0, 0];
|
|
68
|
-
const i = Math.floor(x * n);
|
|
69
|
-
return [i, x * n - i];
|
|
70
|
-
};
|
|
71
|
-
const [li, lr] = at(a);
|
|
72
|
-
const [ui, ur] = at(b);
|
|
73
|
-
const curves = [];
|
|
74
|
-
if (li === ui) {
|
|
75
|
-
curves.push(partialCubic(curve(li), lr, ur));
|
|
76
|
-
}
|
|
77
|
-
else {
|
|
78
|
-
curves.push(partialCubic(curve(li), lr, 1));
|
|
79
|
-
for (let i = li + 1; i < ui; i++)
|
|
80
|
-
curves.push(curve(i));
|
|
81
|
-
curves.push(partialCubic(curve(ui), 0, ur));
|
|
82
|
-
}
|
|
83
|
-
const first = curves[0]?.[0] ?? ORIGIN;
|
|
84
|
-
const last = curves.at(-1)?.[3] ?? ORIGIN;
|
|
85
|
-
const before = Array.from({ length: li }, () => [first, first, first, first]);
|
|
86
|
-
const after = Array.from({ length: n - li - curves.length }, () => [last, last, last, last]);
|
|
87
|
-
mob.setPoints([...before, ...curves, ...after].flat());
|
|
88
|
-
}
|
|
89
|
-
/** Reverses the order of the submobjects, in the whole family (Manim `invert(recursive=True)`). */
|
|
90
|
-
function invert(m) {
|
|
91
|
-
const children = m.submobjects;
|
|
92
|
-
if (children.length === 0)
|
|
93
|
-
return;
|
|
94
|
-
m.remove(...children);
|
|
95
|
-
m.add(...children.toReversed());
|
|
96
|
-
for (const c of children)
|
|
97
|
-
invert(c);
|
|
98
|
-
}
|
|
99
|
-
/** The mean of the points of the family (Manim `get_center_of_mass`). */
|
|
100
|
-
function centerOfMass(m) {
|
|
101
|
-
const points = m.getFamily().flatMap((f) => f.getPoints());
|
|
102
|
-
if (points.length === 0)
|
|
103
|
-
return m.getCenter();
|
|
104
|
-
const sum = points.reduce((acc, p) => [acc[0] + p[0], acc[1] + p[1]], [0, 0]);
|
|
105
|
-
return [sum[0] / points.length, sum[1] / points.length];
|
|
106
|
-
}
|
|
107
|
-
/** The center of a point or of a mobject. */
|
|
108
|
-
function centerOf(pointOrMobject) {
|
|
109
|
-
return pointOrMobject instanceof Mobject ? pointOrMobject.getCenter() : pointOrMobject;
|
|
110
|
-
}
|
|
111
|
-
/** Sets the own points and style of `m` between those of `a` and `b` at `t` (Manim
|
|
112
|
-
* `Mobject.interpolate`). The points blend when `a` and `b` have the same number of points. */
|
|
113
|
-
function blend(m, a, b, t) {
|
|
114
|
-
const pa = a.getPoints();
|
|
115
|
-
const pb = b.getPoints();
|
|
116
|
-
if (pa.length === pb.length)
|
|
117
|
-
m.setPoints(pa.map((p, i) => lerpPoint(p, pb[i] ?? p, t)));
|
|
118
|
-
m.setFill({ opacity: mix(a.getFillOpacity(), b.getFillOpacity(), t) });
|
|
119
|
-
m.setStroke({
|
|
120
|
-
width: mix(a.getStrokeWidth(), b.getStrokeWidth(), t),
|
|
121
|
-
opacity: mix(a.getStrokeOpacity(), b.getStrokeOpacity(), t),
|
|
122
|
-
});
|
|
123
|
-
}
|
|
124
|
-
const transformHooks = new WeakMap();
|
|
125
|
-
/** Manim's `path_along_circles(arc, center)`: each point turns by `arc` about `center` while it
|
|
126
|
-
* moves to its end. */
|
|
127
|
-
function pathAlongCircles(arc, center) {
|
|
128
|
-
const turn = (p, angle) => {
|
|
129
|
-
const [s, c] = [Math.sin(angle), Math.cos(angle)];
|
|
130
|
-
const d = sub(p, center);
|
|
131
|
-
return [center[0] + d[0] * c - d[1] * s, center[1] + d[0] * s + d[1] * c];
|
|
132
|
-
};
|
|
133
|
-
return (starts, ends, alpha) => starts.map((p, i) => {
|
|
134
|
-
const back = turn(ends[i] ?? p, -arc);
|
|
135
|
-
return turn([mix(p[0], back[0], alpha), mix(p[1], back[1], alpha)], alpha * arc);
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
/** Manim `Transform` and its subclasses: the mobject morphs into a target. Extend it as in
|
|
139
|
-
* Manim: a subclass that overrides `createTarget` morphs into the mobject that it returns,
|
|
140
|
-
* made when the animation begins.
|
|
141
|
-
*
|
|
142
|
-
* class Grow extends m.TransformAnimation {
|
|
143
|
-
* override createTarget() {
|
|
144
|
-
* return this.mobject?.copy().scale(2);
|
|
145
|
-
* }
|
|
146
|
-
* }
|
|
147
|
-
* s.play(new Grow(box)); */
|
|
148
|
-
export class TransformAnimation extends Animation {
|
|
149
|
-
/** The mobject to morph into (Manim `target_mobject`). */
|
|
150
|
-
targetMobject;
|
|
151
|
-
/** The copy of the target that the morph uses (Manim `target_copy`). */
|
|
152
|
-
targetCopy;
|
|
153
|
-
/** The angle of the arcs that points move along (Manim `path_arc`). */
|
|
154
|
-
pathArc = 0;
|
|
155
|
-
/** The function that places the points (Manim `path_func`), when one was given. */
|
|
156
|
-
pathFunc;
|
|
157
|
-
/** True when the target takes the place of the mobject at the end. */
|
|
158
|
-
replaceMobjectWithTargetInScene = false;
|
|
159
|
-
#morph;
|
|
160
|
-
begin() {
|
|
161
|
-
if (!isDriven(this)) {
|
|
162
|
-
super.begin();
|
|
163
|
-
return;
|
|
164
|
-
}
|
|
165
|
-
const mobject = this.mobject;
|
|
166
|
-
const target = this.createTarget();
|
|
167
|
-
if (mobject === undefined || target === undefined)
|
|
168
|
-
return;
|
|
169
|
-
this.targetMobject = target;
|
|
170
|
-
this.targetCopy = target.copy();
|
|
171
|
-
this.startingMobject = mobject.copy();
|
|
172
|
-
const h = engine.transform(raw(mobject), raw(this.targetCopy));
|
|
173
|
-
const f = this.rateFunc;
|
|
174
|
-
h.setRateFunc(rawRate(this.reverseRateFunction ? (t) => f(1 - t) : f));
|
|
175
|
-
h.setLagRatio(this.lagRatio);
|
|
176
|
-
if (this.pathArc !== 0)
|
|
177
|
-
h.setPathArc(this.pathArc);
|
|
178
|
-
const path = this.pathFunc;
|
|
179
|
-
if (path !== undefined)
|
|
180
|
-
h.setPathFunc(register((s, e, t) => path(s, e, t)));
|
|
181
|
-
h.setSuspendUpdating(this.suspendMobjectUpdating);
|
|
182
|
-
h.begin();
|
|
183
|
-
this.#morph = h;
|
|
184
|
-
}
|
|
185
|
-
interpolate(alpha) {
|
|
186
|
-
if (this.#morph === undefined)
|
|
187
|
-
super.interpolate(alpha);
|
|
188
|
-
else
|
|
189
|
-
this.#morph.interpolate(alpha);
|
|
190
|
-
}
|
|
191
|
-
finish() {
|
|
192
|
-
if (this.#morph === undefined)
|
|
193
|
-
super.finish();
|
|
194
|
-
else
|
|
195
|
-
this.#morph.finish();
|
|
196
|
-
}
|
|
197
|
-
/** The target (Manim `create_target`). */
|
|
198
|
-
createTarget() {
|
|
199
|
-
const make = transformHooks.get(this)?.target;
|
|
200
|
-
return make === undefined ? this.targetMobject : make();
|
|
201
|
-
}
|
|
202
|
-
createStartingMobject() {
|
|
203
|
-
const make = transformHooks.get(this)?.start;
|
|
204
|
-
return make === undefined ? super.createStartingMobject() : make();
|
|
205
|
-
}
|
|
206
|
-
getAllMobjects() {
|
|
207
|
-
return [this.mobject, this.startingMobject, this.targetMobject, this.targetCopy].filter((x) => x !== undefined);
|
|
208
|
-
}
|
|
209
|
-
cleanUpFromScene(scene) {
|
|
210
|
-
super.cleanUpFromScene(scene);
|
|
211
|
-
if (this.replaceMobjectWithTargetInScene && this.mobject !== undefined && this.targetMobject !== undefined) {
|
|
212
|
-
scene.remove(this.mobject);
|
|
213
|
-
scene.add(this.targetMobject);
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
checkValidityOfInput(input) {
|
|
217
|
-
transformHooks.get(this)?.check?.(input);
|
|
218
|
-
}
|
|
219
|
-
/** Sets `submobject` between `startingSubmobject` and `targetCopy` at `alpha` (Manim
|
|
220
|
-
* `interpolate_submobject`): the points and the style. */
|
|
221
|
-
interpolateSubmobject(submobject, startingSubmobject, targetCopy, alpha = 0) {
|
|
222
|
-
if (targetCopy instanceof Mobject)
|
|
223
|
-
blend(submobject, startingSubmobject, targetCopy, alpha);
|
|
224
|
-
return this;
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
/** An engine morph as a TransformAnimation, with its CE fields and hooks. */
|
|
228
|
-
function transformOf(handle, mobject, target, opts, defaults = {}, hooks = {}) {
|
|
229
|
-
const { pathArcCenters, replaceMobjectWithTargetInScene = false, ...rest } = opts;
|
|
230
|
-
const o = { ...rest };
|
|
231
|
-
if (pathArcCenters !== undefined)
|
|
232
|
-
o.pathFunc = pathAlongCircles(o.pathArc ?? 0, pathArcCenters);
|
|
233
|
-
const a = engineAnimation(TransformAnimation, handle, mobject, o, defaults);
|
|
234
|
-
a.targetMobject = target;
|
|
235
|
-
a.pathArc = opts.pathArc ?? 0;
|
|
236
|
-
a.pathFunc = o.pathFunc;
|
|
237
|
-
a.replaceMobjectWithTargetInScene = replaceMobjectWithTargetInScene;
|
|
238
|
-
transformHooks.set(a, hooks);
|
|
239
|
-
return a;
|
|
240
|
-
}
|
|
241
|
-
/** `mobject` morphs into `targetMobject` (Manim `Transform`). With
|
|
242
|
-
* `replaceMobjectWithTargetInScene`, the target takes its place at the end. */
|
|
243
|
-
export function Transform(mobject, targetMobject, opts = {}) {
|
|
244
|
-
if (targetMobject === undefined) {
|
|
245
|
-
return wrong("Transform", "targetMobject is missing", "give the mobject to morph into: Transform(a, b)");
|
|
246
|
-
}
|
|
247
|
-
const make = opts.replaceMobjectWithTargetInScene === true ? engine.replacementTransform : engine.transform;
|
|
248
|
-
return transformOf(make(raw(mobject), raw(targetMobject)), mobject, targetMobject, opts);
|
|
249
|
-
}
|
|
250
|
-
/** `mobject` morphs into `targetMobject`, which then takes its place (Manim
|
|
251
|
-
* `ReplacementTransform`). */
|
|
252
|
-
export function ReplacementTransform(mobject, targetMobject, opts = {}) {
|
|
253
|
-
return Transform(mobject, targetMobject, { ...opts, replaceMobjectWithTargetInScene: true });
|
|
254
|
-
}
|
|
255
|
-
/** A copy of `mobject` morphs into `targetMobject`, which is added; `mobject` stays (Manim
|
|
256
|
-
* `TransformFromCopy`). */
|
|
257
|
-
export function TransformFromCopy(mobject, targetMobject, opts = {}) {
|
|
258
|
-
return transformOf(engine.transformFromCopy(raw(mobject), raw(targetMobject)), mobject, targetMobject, opts);
|
|
259
|
-
}
|
|
260
|
-
/** `Transform` along a clockwise half circle (Manim `ClockwiseTransform`, `pathArc` -π). */
|
|
261
|
-
export function ClockwiseTransform(mobject, targetMobject, opts = {}) {
|
|
262
|
-
return Transform(mobject, targetMobject, { pathArc: -PI, ...opts });
|
|
263
|
-
}
|
|
264
|
-
/** `Transform` along a counter-clockwise half circle (Manim `CounterclockwiseTransform`,
|
|
265
|
-
* `pathArc` π). */
|
|
266
|
-
export function CounterclockwiseTransform(mobject, targetMobject, opts = {}) {
|
|
267
|
-
return Transform(mobject, targetMobject, { pathArc: PI, ...opts });
|
|
268
|
-
}
|
|
269
|
-
function checkTarget(input) {
|
|
270
|
-
if (!(input instanceof Mobject))
|
|
271
|
-
wrong("MoveToTarget", "the input must be a Mobject");
|
|
272
|
-
else if (!(input.target instanceof Mobject))
|
|
273
|
-
wrong("MoveToTarget", "the Mobject has no target");
|
|
274
|
-
}
|
|
275
|
-
/** Morphs `mobject` into its `target` (Manim `MoveToTarget`): call `generateTarget()` first. */
|
|
276
|
-
export function MoveToTarget(mobject, opts = {}) {
|
|
277
|
-
const handle = engine.moveToTarget(raw(mobject));
|
|
278
|
-
return transformOf(handle, mobject, mobject.target, opts, {}, { check: checkTarget });
|
|
279
|
-
}
|
|
280
|
-
/** Morphs `mobject` back into the state that `saveState()` kept (Manim `Restore`). */
|
|
281
|
-
export function Restore(mobject, opts = {}) {
|
|
282
|
-
return transformOf(engine.restore(raw(mobject)), mobject, undefined, opts, {}, {
|
|
283
|
-
target: () => mobject.copy().restore(),
|
|
284
|
-
});
|
|
285
|
-
}
|
|
286
|
-
/** A Transform of `mobject` that runs in JavaScript, whose target `target()` makes when it
|
|
287
|
-
* begins (Manim's `ApplyMethod` family). */
|
|
288
|
-
function lazy(mobject, target, opts, hooks = {},
|
|
289
|
-
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- T is TransformAnimation unless Cls is given
|
|
290
|
-
Cls = TransformAnimation) {
|
|
291
|
-
const { pathArcCenters, replaceMobjectWithTargetInScene: _replace, pathArc, pathFunc, ...rest } = opts;
|
|
292
|
-
const a = new Cls(mobject, rest);
|
|
293
|
-
a.pathArc = pathArc ?? 0;
|
|
294
|
-
a.pathFunc = pathArcCenters === undefined ? pathFunc : pathAlongCircles(pathArc ?? 0, pathArcCenters);
|
|
295
|
-
transformHooks.set(a, { ...hooks, target });
|
|
296
|
-
return a;
|
|
297
|
-
}
|
|
298
|
-
function checkMethod(label, method) {
|
|
299
|
-
if (typeof method !== "function" || !(METHOD_SELF in method)) {
|
|
300
|
-
wrong(label, "method must be a method of a Mobject, such as mob.shift", "write ApplyMethod(mob.shift, m.UP)");
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
/** Calls method `method` (a method of a Mobject, such as `mob.shift`) on a copy of its Mobject
|
|
304
|
-
* with `args`, and morphs the Mobject into the copy (Manim `ApplyMethod`). The copy is made when
|
|
305
|
-
* the animation begins. */
|
|
306
|
-
export function ApplyMethod(method, ...args) {
|
|
307
|
-
checkMethod("ApplyMethod", method);
|
|
308
|
-
const self = method[METHOD_SELF];
|
|
309
|
-
const f = method[METHOD_FUNC];
|
|
310
|
-
return lazy(self, () => {
|
|
311
|
-
const target = self.copy();
|
|
312
|
-
Reflect.apply(f, target, args);
|
|
313
|
-
return target;
|
|
314
|
-
}, {}, { check: (m) => checkMethod("ApplyMethod", m) });
|
|
315
|
-
}
|
|
316
|
-
/** Moves every point through `function` (Manim `ApplyPointwiseFunction`; 3 seconds by default). */
|
|
317
|
-
export function ApplyPointwiseFunction(func, mobject, opts = {}) {
|
|
318
|
-
return lazy(mobject, () => mobject.copy().applyFunction(func), { runTime: 3, ...opts });
|
|
319
|
-
}
|
|
320
|
-
/** Moves the center of the mobject through `function` (Manim `ApplyPointwiseFunctionToCenter`). */
|
|
321
|
-
export function ApplyPointwiseFunctionToCenter(func, mobject, opts = {}) {
|
|
322
|
-
return lazy(mobject, () => mobject.copy().moveTo(func(mobject.getCenter())), { runTime: 3, ...opts });
|
|
323
|
-
}
|
|
324
|
-
/** Changes the colour of the mobject (Manim `FadeToColor`). */
|
|
325
|
-
export function FadeToColor(mobject, color, opts = {}) {
|
|
326
|
-
return lazy(mobject, () => mobject.copy().setColor(color), opts);
|
|
327
|
-
}
|
|
328
|
-
/** Scales the mobject about its center (Manim `ScaleInPlace`). */
|
|
329
|
-
export function ScaleInPlace(mobject, scaleFactor, opts = {}) {
|
|
330
|
-
return lazy(mobject, () => mobject.copy().scale(scaleFactor), opts);
|
|
331
|
-
}
|
|
332
|
-
/** Shrinks the mobject to its center (Manim `ShrinkToCenter`). */
|
|
333
|
-
export function ShrinkToCenter(mobject, opts = {}) {
|
|
334
|
-
return ScaleInPlace(mobject, 0, opts);
|
|
335
|
-
}
|
|
336
|
-
/** Morphs the mobject into `function(copy)` (Manim `ApplyFunction`): the function changes the
|
|
337
|
-
* copy, or returns a new Mobject. */
|
|
338
|
-
export function ApplyFunction(func, mobject, opts = {}) {
|
|
339
|
-
return lazy(mobject, () => {
|
|
340
|
-
const target = func(mobject.copy());
|
|
341
|
-
if (!(target instanceof Mobject)) {
|
|
342
|
-
return wrong("ApplyFunction", "the function must return a Mobject", "return the mobject that it changes");
|
|
343
|
-
}
|
|
344
|
-
return target;
|
|
345
|
-
}, opts);
|
|
346
|
-
}
|
|
347
|
-
/** The 2x2 matrix of a 2x2 or 3x3 matrix (Manim `initialize_matrix`). */
|
|
348
|
-
function matrix2(matrix) {
|
|
349
|
-
const n = matrix.length;
|
|
350
|
-
const ok = (n === 2 || n === 3) && matrix.every((row) => row.length === n);
|
|
351
|
-
if (!ok)
|
|
352
|
-
wrong("ApplyMatrix", "the matrix must be 2x2 or 3x3", "write [[a, b], [c, d]]");
|
|
353
|
-
const at = (i, j) => matrix[i]?.[j] ?? 0;
|
|
354
|
-
return [
|
|
355
|
-
[at(0, 0), at(0, 1)],
|
|
356
|
-
[at(1, 0), at(1, 1)],
|
|
357
|
-
];
|
|
358
|
-
}
|
|
359
|
-
/** Manim `ApplyMatrix`. */
|
|
360
|
-
export class ApplyMatrixAnimation extends TransformAnimation {
|
|
361
|
-
/** The matrix as 3x3: a 2x2 matrix in the top left of the identity (Manim
|
|
362
|
-
* `initialize_matrix`). An error for other sizes. */
|
|
363
|
-
initializeMatrix(matrix) {
|
|
364
|
-
const [[a, b], [c, d]] = matrix2(matrix);
|
|
365
|
-
const at = (i, j) => matrix[i]?.[j] ?? (i === j ? 1 : 0);
|
|
366
|
-
const three = matrix.length === 3;
|
|
367
|
-
return [
|
|
368
|
-
[a, b, three ? at(0, 2) : 0],
|
|
369
|
-
[c, d, three ? at(1, 2) : 0],
|
|
370
|
-
three ? [at(2, 0), at(2, 1), at(2, 2)] : [0, 0, 1],
|
|
371
|
-
];
|
|
372
|
-
}
|
|
373
|
-
}
|
|
374
|
-
/** Moves every point `p` to `matrix · (p - aboutPoint) + aboutPoint` (Manim `ApplyMatrix`; 3
|
|
375
|
-
* seconds). A 3x3 matrix uses its 2x2 top left part. */
|
|
376
|
-
export function ApplyMatrix(matrix, mobject, opts = {}) {
|
|
377
|
-
const { aboutPoint = ORIGIN, ...rest } = opts;
|
|
378
|
-
const [[a, b], [c, d]] = matrix2(matrix);
|
|
379
|
-
const f = (p) => {
|
|
380
|
-
const [x, y] = sub(p, aboutPoint);
|
|
381
|
-
return [a * x + b * y + aboutPoint[0], c * x + d * y + aboutPoint[1]];
|
|
382
|
-
};
|
|
383
|
-
return lazy(mobject, () => mobject.copy().applyFunction(f), { runTime: 3, ...rest }, {}, ApplyMatrixAnimation);
|
|
384
|
-
}
|
|
385
|
-
/** Moves every point `z` (a complex number `[re, im]`) to `function(z)` (Manim
|
|
386
|
-
* `ApplyComplexFunction`). The points move in straight lines, as in Manim CE 0.22.1: its
|
|
387
|
-
* `_init_path_func` would set the path arc from `function(1)`, but nothing calls it. */
|
|
388
|
-
export function ApplyComplexFunction(func, mobject, opts = {}) {
|
|
389
|
-
return lazy(mobject, () => mobject.copy().applyFunction(func), opts);
|
|
390
|
-
}
|
|
391
|
-
/** Each mobject moves to the place of the next one, the last to the first (Manim
|
|
392
|
-
* `CyclicReplace`), along arcs of `pathArc` (90°). */
|
|
393
|
-
export function CyclicReplace(...args) {
|
|
394
|
-
const [items, opts = {}] = split(args);
|
|
395
|
-
const first = items[0];
|
|
396
|
-
const group = items.length === 1 && first !== undefined ? first : VGroup(items);
|
|
397
|
-
return lazy(group, () => {
|
|
398
|
-
const target = group.copy();
|
|
399
|
-
const parts = target.submobjects;
|
|
400
|
-
const cycled = [parts.at(-1), ...parts.slice(0, -1)];
|
|
401
|
-
cycled.forEach((m1, i) => {
|
|
402
|
-
const m2 = group.submobjects[i];
|
|
403
|
-
if (m1 !== undefined && m2 !== undefined)
|
|
404
|
-
m1.moveTo(m2);
|
|
405
|
-
});
|
|
406
|
-
return target;
|
|
407
|
-
}, { pathArc: 90 * DEGREES, ...opts });
|
|
408
|
-
}
|
|
409
|
-
/** Two mobjects change places (Manim `Swap`: `CyclicReplace` of two). */
|
|
410
|
-
export function Swap(...args) {
|
|
411
|
-
return CyclicReplace(...args);
|
|
412
|
-
}
|
|
413
|
-
/** Plays two animations, and morphs the mobject of the first into the mobject of the second
|
|
414
|
-
* (Manim `TransformAnimations`). Both run for the longer run time. */
|
|
415
|
-
export function TransformAnimations(startAnim, endAnim, opts = {}) {
|
|
416
|
-
const runTime = opts.runTime ?? Math.max(startAnim.getRunTime(), endAnim.getRunTime());
|
|
417
|
-
startAnim.setRunTime(runTime);
|
|
418
|
-
endAnim.setRunTime(runTime);
|
|
419
|
-
const from = startAnim.mobject;
|
|
420
|
-
const to = endAnim.mobject;
|
|
421
|
-
if (from === undefined || to === undefined) {
|
|
422
|
-
return wrong("TransformAnimations", "both animations need a mobject");
|
|
423
|
-
}
|
|
424
|
-
const a = new TransformBetween(from, { rateFunc: squishRateFunc(smooth), ...opts, runTime });
|
|
425
|
-
a.targetMobject = to;
|
|
426
|
-
a.parts = [startAnim, endAnim];
|
|
427
|
-
return a;
|
|
428
|
-
}
|
|
429
|
-
/** `TransformAnimations`: plays its two animations, then morphs between their mobjects. */
|
|
430
|
-
class TransformBetween extends TransformAnimation {
|
|
431
|
-
parts = [];
|
|
432
|
-
begin() {
|
|
433
|
-
for (const p of this.parts)
|
|
434
|
-
p.begin();
|
|
435
|
-
this.startingMobject = this.mobject?.copy();
|
|
436
|
-
}
|
|
437
|
-
interpolate(alpha) {
|
|
438
|
-
for (const p of this.parts)
|
|
439
|
-
p.interpolate(alpha);
|
|
440
|
-
const [from, to] = [this.mobject, this.targetMobject];
|
|
441
|
-
if (from === undefined || to === undefined)
|
|
442
|
-
return;
|
|
443
|
-
const h = engine.transform(raw(from), raw(to.copy()));
|
|
444
|
-
h.setRateFunc(rawRate(this.rateFunc));
|
|
445
|
-
h.begin();
|
|
446
|
-
h.interpolate(alpha);
|
|
447
|
-
}
|
|
448
|
-
finish() {
|
|
449
|
-
for (const p of this.parts)
|
|
450
|
-
p.finish();
|
|
451
|
-
if (this.mobject !== undefined && this.targetMobject !== undefined)
|
|
452
|
-
this.mobject.become(this.targetMobject);
|
|
453
|
-
}
|
|
454
|
-
}
|
|
455
|
-
/** The mobject that a fade plays: the one mobject, or a group of them (Manim `Group`). */
|
|
456
|
-
function faded(first, others) {
|
|
457
|
-
return others.length === 0 ? first : VGroup(first, ...others);
|
|
458
|
-
}
|
|
459
|
-
/** Manim's `_Fade._create_faded_mobject`. */
|
|
460
|
-
function fadedCopy(mob, fadeIn, shift, scale, pointTarget) {
|
|
461
|
-
const k = fadeIn && !pointTarget ? -1 : 1;
|
|
462
|
-
return mob
|
|
463
|
-
.copy()
|
|
464
|
-
.fade(1)
|
|
465
|
-
.shift([shift[0] * k, shift[1] * k])
|
|
466
|
-
.scale(scale);
|
|
467
|
-
}
|
|
468
|
-
/** Fades in one or more mobjects: `FadeIn(a, b, { shift })`. Several fade in as one group. */
|
|
469
|
-
export function FadeIn(...args) {
|
|
470
|
-
const [items, o = {}] = split(args);
|
|
471
|
-
const [first, ...others] = items;
|
|
472
|
-
if (first === undefined)
|
|
473
|
-
return wrong("FadeIn", "no mobject to fade in", "write FadeIn(mob)");
|
|
474
|
-
const mob = faded(first, others);
|
|
475
|
-
const { targetPosition, shift: given, scale = 1, ...rest } = o;
|
|
476
|
-
const shift = targetPosition === undefined ? (given ?? ORIGIN) : sub(centerOf(targetPosition), mob.getCenter());
|
|
477
|
-
const pointTarget = targetPosition !== undefined;
|
|
478
|
-
const fadeShift = pointTarget ? mul(shift, -1) : shift;
|
|
479
|
-
const a = transformOf(engine.fadeIn(raw(mob)), mob, mob, { ...rest, shift: fadeShift, scale }, {
|
|
480
|
-
introducer: true,
|
|
481
|
-
}, {
|
|
482
|
-
target: () => mob,
|
|
483
|
-
start: () => fadedCopy(mob, true, shift, scale, pointTarget),
|
|
484
|
-
});
|
|
485
|
-
return a;
|
|
486
|
-
}
|
|
487
|
-
/** Fades out one or more mobjects, then removes them: `FadeOut(a, b, { shift })`. Several fade
|
|
488
|
-
* out as one group. */
|
|
489
|
-
export function FadeOut(...args) {
|
|
490
|
-
const [items, o = {}] = split(args);
|
|
491
|
-
const [first, ...others] = items;
|
|
492
|
-
if (first === undefined)
|
|
493
|
-
return wrong("FadeOut", "no mobject to fade out", "write FadeOut(mob)");
|
|
494
|
-
const mob = faded(first, others);
|
|
495
|
-
const { targetPosition, shift: given = ORIGIN, scale = 1, ...rest } = o;
|
|
496
|
-
const shift = targetPosition === undefined ? given : sub(centerOf(targetPosition), mob.getCenter());
|
|
497
|
-
const hooks = { target: () => fadedCopy(mob, false, shift, scale, targetPosition !== undefined) };
|
|
498
|
-
if (targetPosition !== undefined) {
|
|
499
|
-
const handle = engine.fadeOutTo(raw(mob), centerOf(targetPosition));
|
|
500
|
-
return transformOf(handle, mob, undefined, { ...rest, scale }, { remover: true }, hooks);
|
|
501
|
-
}
|
|
502
|
-
return transformOf(engine.fadeOut(raw(mob)), mob, undefined, { ...rest, shift, scale }, {
|
|
503
|
-
remover: true,
|
|
504
|
-
}, hooks);
|
|
505
|
-
}
|
|
506
|
-
function mul(v, k) {
|
|
507
|
-
return [v[0] * k, v[1] * k];
|
|
508
|
-
}
|
|
509
|
-
/** Manim `FadeTransform`: the members of `ghostTo`. */
|
|
510
|
-
export class FadeTransformAnimation extends TransformAnimation {
|
|
511
|
-
stretch = true;
|
|
512
|
-
dimToMatch = 1;
|
|
513
|
-
/** `source` takes the box of `target`, then becomes invisible (Manim `ghost_to`). */
|
|
514
|
-
ghostTo(source, target) {
|
|
515
|
-
if (target.hasPoints() || target.submobjects.length > 0) {
|
|
516
|
-
source.replace(target, { stretch: this.stretch, dimToMatch: this.dimToMatch });
|
|
517
|
-
}
|
|
518
|
-
source.setOpacity(0);
|
|
519
|
-
}
|
|
520
|
-
}
|
|
521
|
-
/** `mobject` stretches onto `targetMobject` while it fades out, and `targetMobject` fades in
|
|
522
|
-
* from `mobject`'s box (Manim `FadeTransform`). At the end the target takes its place. */
|
|
523
|
-
export function FadeTransform(mobject, targetMobject, opts = {}) {
|
|
524
|
-
const { stretch = true, dimToMatch = 1, ...rest } = opts;
|
|
525
|
-
const a = engineAnimation(FadeTransformAnimation, engine.fadeTransform(raw(mobject), raw(targetMobject), stretch, dimToMatch), mobject, rest);
|
|
526
|
-
a.targetMobject = targetMobject;
|
|
527
|
-
a.stretch = stretch;
|
|
528
|
-
a.dimToMatch = dimToMatch;
|
|
529
|
-
a.replaceMobjectWithTargetInScene = true;
|
|
530
|
-
return a;
|
|
531
|
-
}
|
|
532
|
-
/** Manim `FadeTransformPieces`: `ghostTo` goes member by member. */
|
|
533
|
-
class FadeTransformPiecesAnimation extends FadeTransformAnimation {
|
|
534
|
-
ghostTo(source, target) {
|
|
535
|
-
const targets = target.getFamily();
|
|
536
|
-
source.getFamily().forEach((m, i) => {
|
|
537
|
-
const t = targets[i];
|
|
538
|
-
if (t !== undefined)
|
|
539
|
-
super.ghostTo(m, t);
|
|
540
|
-
});
|
|
541
|
-
}
|
|
542
|
-
}
|
|
543
|
-
/** `FadeTransform` part by part: each submobject of `mobject` goes to the submobject of
|
|
544
|
-
* `targetMobject` at the same place in the list (Manim `FadeTransformPieces`). It is one
|
|
545
|
-
* transform, as in Manim: the rate and the lag ratio apply to all the parts together. */
|
|
546
|
-
export function FadeTransformPieces(mobject, targetMobject, opts = {}) {
|
|
547
|
-
const { stretch = true, dimToMatch = 1, ...rest } = opts;
|
|
548
|
-
const a = engineAnimation(FadeTransformPiecesAnimation, engine.fadeTransformPieces(raw(mobject), raw(targetMobject), stretch, dimToMatch), mobject, rest);
|
|
549
|
-
a.targetMobject = targetMobject;
|
|
550
|
-
a.stretch = stretch;
|
|
551
|
-
a.dimToMatch = dimToMatch;
|
|
552
|
-
a.replaceMobjectWithTargetInScene = true;
|
|
553
|
-
return a;
|
|
554
|
-
}
|
|
555
|
-
// --- creation -------------------------------------------------------------------------
|
|
556
|
-
/** Draws the strokes of the mobject from their start (Manim `Create`; `lagRatio` 1: one part
|
|
557
|
-
* after another). */
|
|
558
|
-
export function Create(mobject, opts = {}) {
|
|
559
|
-
return engineAnimation(Animation, engine.create(raw(mobject)), mobject, opts, { lagRatio: 1, introducer: true });
|
|
560
|
-
}
|
|
561
|
-
/** Undraws the strokes, then removes the mobject (Manim `Uncreate`: `Create` with
|
|
562
|
-
* `reverseRateFunction` and `remover`). */
|
|
563
|
-
export function Uncreate(mobject, opts = {}) {
|
|
564
|
-
const { reverseRateFunction = true, ...rest } = opts;
|
|
565
|
-
if (!reverseRateFunction) {
|
|
566
|
-
return engineAnimation(Animation, engine.create(raw(mobject)), mobject, { ...rest, remover: rest.remover ?? true }, {
|
|
567
|
-
lagRatio: 1,
|
|
568
|
-
});
|
|
569
|
-
}
|
|
570
|
-
const a = engineAnimation(Animation, engine.uncreate(raw(mobject)), mobject, rest, { lagRatio: 1, remover: true });
|
|
571
|
-
a.reverseRateFunction = true;
|
|
572
|
-
return a;
|
|
573
|
-
}
|
|
574
|
-
/** Manim `DrawBorderThenFill`, `Write` and `Unwrite`. */
|
|
575
|
-
export class DrawBorderThenFillAnimation extends Animation {
|
|
576
|
-
strokeWidth = 2;
|
|
577
|
-
strokeColor;
|
|
578
|
-
/** The last part goes first (Manim `Write(reverse=True)`). */
|
|
579
|
-
reverse = false;
|
|
580
|
-
/** The outlines: a copy with no fill (Manim `get_outline`). */
|
|
581
|
-
getOutline() {
|
|
582
|
-
const outline = this.mobject?.copy();
|
|
583
|
-
if (outline === undefined)
|
|
584
|
-
return undefined;
|
|
585
|
-
outline.setFill({ opacity: 0 });
|
|
586
|
-
for (const m of withPoints(outline))
|
|
587
|
-
m.setStroke(this.getStrokeColor(m), { width: this.strokeWidth });
|
|
588
|
-
return outline;
|
|
589
|
-
}
|
|
590
|
-
/** The colour of the outline of `vmobject` (Manim `get_stroke_color`). */
|
|
591
|
-
getStrokeColor(vmobject) {
|
|
592
|
-
if (this.strokeColor !== undefined)
|
|
593
|
-
return this.strokeColor;
|
|
594
|
-
return vmobject.getStrokeWidth() > 0 ? vmobject.getStrokeColor() : vmobject.getFillColor();
|
|
595
|
-
}
|
|
596
|
-
getAllMobjects() {
|
|
597
|
-
const outline = this.getOutline();
|
|
598
|
-
return [...super.getAllMobjects(), ...(outline === undefined ? [] : [outline])];
|
|
599
|
-
}
|
|
600
|
-
/** Sets one part at `alpha` (Manim `interpolate_submobject`): the first half draws the
|
|
601
|
-
* outline, the second half blends from the outline to the part. */
|
|
602
|
-
interpolateSubmobject(submobject, startingSubmobject, outline, alpha = 0) {
|
|
603
|
-
if (!(outline instanceof Mobject))
|
|
604
|
-
return this;
|
|
605
|
-
if (alpha < 0.5) {
|
|
606
|
-
becomePartial(submobject, outline, 0, 2 * alpha);
|
|
607
|
-
submobject.setFill({ opacity: outline.getFillOpacity() });
|
|
608
|
-
submobject.setStroke(outline.getStrokeColor(), { width: outline.getStrokeWidth() });
|
|
609
|
-
}
|
|
610
|
-
else
|
|
611
|
-
blend(submobject, outline, startingSubmobject, 2 * alpha - 1);
|
|
612
|
-
return this;
|
|
613
|
-
}
|
|
614
|
-
/** Reverses the order of the parts (Manim `reverse_submobjects`). */
|
|
615
|
-
reverseSubmobjects() {
|
|
616
|
-
if (this.mobject !== undefined)
|
|
617
|
-
invert(this.mobject);
|
|
618
|
-
}
|
|
619
|
-
}
|
|
620
|
-
/** Draws the outlines, then fills them (Manim `DrawBorderThenFill`; 2 seconds, rate
|
|
621
|
-
* `doubleSmooth`). */
|
|
622
|
-
export function DrawBorderThenFill(vmobject, opts = {}) {
|
|
623
|
-
const { strokeWidth, strokeColor, ...rest } = opts;
|
|
624
|
-
const a = engineAnimation(DrawBorderThenFillAnimation, engine.drawBorderThenFill(raw(vmobject), strokeWidth, strokeColor), vmobject, rest, { rate: doubleSmooth, introducer: true });
|
|
625
|
-
a.strokeWidth = strokeWidth ?? 2;
|
|
626
|
-
a.strokeColor = strokeColor;
|
|
627
|
-
return a;
|
|
628
|
-
}
|
|
629
|
-
/** Draws the mobject part by part, outline then fill (Manim `Write`): 1 second, or 2 seconds
|
|
630
|
-
* from 15 parts with points; rate `linear`. With `reverse`, the last part goes first and the
|
|
631
|
-
* mobject is removed at the end, as in Manim. */
|
|
632
|
-
export function Write(vmobject, opts = {}) {
|
|
633
|
-
const { reverse = false, ...rest } = opts;
|
|
634
|
-
const parts = withPoints(vmobject).length;
|
|
635
|
-
const a = engineAnimation(DrawBorderThenFillAnimation, engine.write(raw(vmobject), reverse), vmobject, rest, {
|
|
636
|
-
rate: linear,
|
|
637
|
-
lagRatio: Math.min(4 / Math.max(1, parts), 0.2),
|
|
638
|
-
introducer: !reverse,
|
|
639
|
-
remover: reverse,
|
|
640
|
-
});
|
|
641
|
-
a.strokeWidth = 1;
|
|
642
|
-
a.reverse = reverse;
|
|
643
|
-
return a;
|
|
644
|
-
}
|
|
645
|
-
/** `Write` backwards, then removed (Manim `Unwrite`). With `reverse` (the default) the last
|
|
646
|
-
* part is erased first. */
|
|
647
|
-
export function Unwrite(vmobject, opts = {}) {
|
|
648
|
-
const { reverse = true, ...rest } = opts;
|
|
649
|
-
const parts = withPoints(vmobject).length;
|
|
650
|
-
const a = engineAnimation(DrawBorderThenFillAnimation, engine.unwrite(raw(vmobject), reverse), vmobject, rest, {
|
|
651
|
-
rate: linear,
|
|
652
|
-
lagRatio: Math.min(4 / Math.max(1, parts), 0.2),
|
|
653
|
-
remover: true,
|
|
654
|
-
});
|
|
655
|
-
a.strokeWidth = 1;
|
|
656
|
-
a.reverse = reverse;
|
|
657
|
-
a.reverseRateFunction = true;
|
|
658
|
-
return a;
|
|
659
|
-
}
|
|
660
|
-
/** Manim `ShowPartial`: each member shows the part of its path that `getBounds(alpha)` gives.
|
|
661
|
-
* `ShowPartial` itself has no bounds: use `Create` or `ShowPassingFlash`, or extend it. */
|
|
662
|
-
export class ShowPartialAnimation extends Animation {
|
|
663
|
-
/** The proportions `[lower, upper]` of the path to show at `alpha` (Manim `_get_bounds`). */
|
|
664
|
-
getBounds(_alpha) {
|
|
665
|
-
return wrong("ShowPartial", "ShowPartial has no bounds", "use m.Create or m.ShowPassingFlash");
|
|
666
|
-
}
|
|
667
|
-
interpolateSubmobject(submobject, startingSubmobject, alpha) {
|
|
668
|
-
const [a, b] = this.getBounds(alpha);
|
|
669
|
-
becomePartial(submobject, startingSubmobject, a, b);
|
|
670
|
-
return this;
|
|
671
|
-
}
|
|
672
|
-
}
|
|
673
|
-
/** Shows a part of each path (Manim `ShowPartial`); extend it with `getBounds`. */
|
|
674
|
-
export function ShowPartial(mobject, opts = {}) {
|
|
675
|
-
return new ShowPartialAnimation(mobject, opts);
|
|
676
|
-
}
|
|
677
|
-
/** Manim `ShowIncreasingSubsets` and the text animations built on it. */
|
|
678
|
-
export class ShowIncreasingSubsetsAnimation extends Animation {
|
|
679
|
-
allSubmobs = [];
|
|
680
|
-
intFunc = Math.floor;
|
|
681
|
-
interpolateMobject(alpha) {
|
|
682
|
-
const n = this.allSubmobs.length;
|
|
683
|
-
const value = this.reverseRateFunction ? 1 - this.rateFunc(alpha) : this.rateFunc(alpha);
|
|
684
|
-
this.updateSubmobjectList(Math.trunc(this.intFunc(value * n)));
|
|
685
|
-
}
|
|
686
|
-
/** Shows the first `index` submobjects and hides the others (Manim `update_submobject_list`). */
|
|
687
|
-
updateSubmobjectList(index) {
|
|
688
|
-
this.allSubmobs.forEach((m, i) => m.setOpacity(i < index ? 1 : 0));
|
|
689
|
-
}
|
|
690
|
-
}
|
|
691
|
-
function subsets(Cls, group, opts, intFunc) {
|
|
692
|
-
const { intFunc: given, ...rest } = opts;
|
|
693
|
-
const all = group.submobjects;
|
|
694
|
-
for (const m of all)
|
|
695
|
-
m.setOpacity(0);
|
|
696
|
-
const a = new Cls(group, { suspendMobjectUpdating: false, ...rest });
|
|
697
|
-
a.allSubmobs = all;
|
|
698
|
-
a.intFunc = given ?? intFunc;
|
|
699
|
-
return a;
|
|
700
|
-
}
|
|
701
|
-
/** Shows the submobjects one more at a time (Manim `ShowIncreasingSubsets`). */
|
|
702
|
-
export function ShowIncreasingSubsets(group, opts = {}) {
|
|
703
|
-
return subsets(ShowIncreasingSubsetsAnimation, group, opts, Math.floor);
|
|
704
|
-
}
|
|
705
|
-
class OneByOne extends ShowIncreasingSubsetsAnimation {
|
|
706
|
-
updateSubmobjectList(index) {
|
|
707
|
-
this.allSubmobs.forEach((m, i) => m.setOpacity(i === index - 1 ? 1 : 0));
|
|
708
|
-
}
|
|
709
|
-
}
|
|
710
|
-
/** Shows one submobject at a time (Manim `ShowSubmobjectsOneByOne`). */
|
|
711
|
-
export function ShowSubmobjectsOneByOne(group, opts = {}) {
|
|
712
|
-
const g = group instanceof Mobject ? group : VGroup([...group]);
|
|
713
|
-
return subsets(OneByOne, g, opts, Math.ceil);
|
|
714
|
-
}
|
|
715
|
-
/** Manim's run time of a letter-by-letter animation: `max(1 / frame_rate, time_per_char)` for
|
|
716
|
-
* each letter, at Manim's 60 frames a second. */
|
|
717
|
-
function letterTime(text, timePerChar) {
|
|
718
|
-
if (withPoints(text).length === 0) {
|
|
719
|
-
return wrong("AddTextLetterByLetter", "the text has no letters", "give a Text with characters");
|
|
720
|
-
}
|
|
721
|
-
return Math.max(1 / 60, timePerChar) * text.submobjects.length;
|
|
722
|
-
}
|
|
723
|
-
/** Shows the letters of a text one at a time (Manim `AddTextLetterByLetter`; rate `linear`,
|
|
724
|
-
* `timePerChar` 0.1). */
|
|
725
|
-
export function AddTextLetterByLetter(text, opts = {}) {
|
|
726
|
-
const { timePerChar = 0.1, runTime, ...rest } = opts;
|
|
727
|
-
const time = runTime ?? letterTime(text, timePerChar);
|
|
728
|
-
return subsets(ShowIncreasingSubsetsAnimation, text, { rateFunc: linear, introducer: true, ...rest, runTime: time }, Math.ceil);
|
|
729
|
-
}
|
|
730
|
-
/** Hides the letters of a text one at a time, from the last, then removes it (Manim
|
|
731
|
-
* `RemoveTextLetterByLetter`). */
|
|
732
|
-
export function RemoveTextLetterByLetter(text, opts = {}) {
|
|
733
|
-
return AddTextLetterByLetter(text, {
|
|
734
|
-
reverseRateFunction: true,
|
|
735
|
-
introducer: false,
|
|
736
|
-
remover: true,
|
|
737
|
-
...opts,
|
|
738
|
-
});
|
|
739
|
-
}
|
|
740
|
-
/** Shows a text one part at a time, with a short pause after each (Manim `AddTextWordByWord`).
|
|
741
|
-
* The parts are the submobjects of the text. */
|
|
742
|
-
export function AddTextWordByWord(textMobject, opts = {}) {
|
|
743
|
-
const { timePerChar = 0.06, runTime, ...rest } = opts;
|
|
744
|
-
const steps = textMobject.submobjects.flatMap((word) => {
|
|
745
|
-
const letters = Math.max(1, word.submobjects.length);
|
|
746
|
-
const group = word.submobjects.length > 0 ? word : VGroup(word);
|
|
747
|
-
return [ShowIncreasingSubsets(group, { runTime: timePerChar * letters }), Wait(0.005 * letters ** 1.5)];
|
|
748
|
-
});
|
|
749
|
-
return Succession(...steps, { ...rest, ...(runTime === undefined ? {} : { runTime }) });
|
|
750
|
-
}
|
|
751
|
-
/** Manim `TypeWithCursor` and `UntypeWithCursor`. */
|
|
752
|
-
export class TypeWithCursorAnimation extends ShowIncreasingSubsetsAnimation {
|
|
753
|
-
cursor;
|
|
754
|
-
buff = 0.1;
|
|
755
|
-
keepCursorY = true;
|
|
756
|
-
leaveCursorOn = true;
|
|
757
|
-
#cursorY = 0;
|
|
758
|
-
#textY = 0;
|
|
759
|
-
begin() {
|
|
760
|
-
const cursor = this.cursor;
|
|
761
|
-
if (cursor !== undefined && this.mobject !== undefined) {
|
|
762
|
-
this.#cursorY = cursor.getY();
|
|
763
|
-
this.#textY = this.mobject.getY();
|
|
764
|
-
if (this.keepCursorY)
|
|
765
|
-
cursor.setY(this.#cursorY);
|
|
766
|
-
cursor.setOpacity(0);
|
|
767
|
-
this.mobject.add(cursor);
|
|
768
|
-
}
|
|
769
|
-
super.begin();
|
|
770
|
-
}
|
|
771
|
-
finish() {
|
|
772
|
-
const cursor = this.cursor;
|
|
773
|
-
if (cursor !== undefined) {
|
|
774
|
-
if (this.leaveCursorOn)
|
|
775
|
-
cursor.setOpacity(1);
|
|
776
|
-
else {
|
|
777
|
-
cursor.setOpacity(0);
|
|
778
|
-
this.mobject?.remove(cursor);
|
|
779
|
-
}
|
|
780
|
-
}
|
|
781
|
-
super.finish();
|
|
782
|
-
}
|
|
783
|
-
cleanUpFromScene(scene) {
|
|
784
|
-
if (!this.leaveCursorOn && this.cursor !== undefined)
|
|
785
|
-
scene.remove(this.cursor);
|
|
786
|
-
super.cleanUpFromScene(scene);
|
|
787
|
-
}
|
|
788
|
-
updateSubmobjectList(index) {
|
|
789
|
-
super.updateSubmobjectList(index);
|
|
790
|
-
const cursor = this.cursor;
|
|
791
|
-
if (cursor === undefined)
|
|
792
|
-
return;
|
|
793
|
-
const last = this.allSubmobs[index - 1];
|
|
794
|
-
const first = this.allSubmobs[0];
|
|
795
|
-
if (index !== 0 && last !== undefined)
|
|
796
|
-
cursor.nextTo(last, RIGHT, { buff: this.buff }).setY(this.#textY);
|
|
797
|
-
else if (first !== undefined)
|
|
798
|
-
cursor.moveTo(first).setY(this.#textY);
|
|
799
|
-
if (this.keepCursorY)
|
|
800
|
-
cursor.setY(this.#cursorY);
|
|
801
|
-
cursor.setOpacity(1);
|
|
802
|
-
}
|
|
803
|
-
}
|
|
804
|
-
function typing(text, cursor, opts) {
|
|
805
|
-
const { buff = 0.1, keepCursorY = true, leaveCursorOn = true, timePerChar = 0.1, runTime, ...rest } = opts;
|
|
806
|
-
const time = runTime ?? letterTime(text, timePerChar);
|
|
807
|
-
const a = subsets(TypeWithCursorAnimation, text, { rateFunc: linear, ...rest, runTime: time }, Math.ceil);
|
|
808
|
-
a.cursor = cursor;
|
|
809
|
-
a.buff = buff;
|
|
810
|
-
a.keepCursorY = keepCursorY;
|
|
811
|
-
a.leaveCursorOn = leaveCursorOn;
|
|
812
|
-
return a;
|
|
813
|
-
}
|
|
814
|
-
/** Types a text letter by letter, with `cursor` after the last letter (Manim `TypeWithCursor`). */
|
|
815
|
-
export function TypeWithCursor(text, cursor, opts = {}) {
|
|
816
|
-
return typing(text, cursor, { introducer: true, ...opts });
|
|
817
|
-
}
|
|
818
|
-
/** Deletes a text letter by letter from the end, with `cursor` after the last letter, then
|
|
819
|
-
* removes it (Manim `UntypeWithCursor`). */
|
|
820
|
-
export function UntypeWithCursor(text, cursor, opts = {}) {
|
|
821
|
-
return typing(text, cursor, { reverseRateFunction: true, introducer: false, remover: true, ...opts });
|
|
822
|
-
}
|
|
823
|
-
/** Manim `SpiralIn`: the shapes fly in along a spiral and fade in. */
|
|
824
|
-
class SpiralInAnimation extends Animation {
|
|
825
|
-
shapes = [];
|
|
826
|
-
scaleFactor = 8;
|
|
827
|
-
fadeInFraction = 0.3;
|
|
828
|
-
shapeCenter = ORIGIN;
|
|
829
|
-
moves = [];
|
|
830
|
-
interpolateMobject(alpha) {
|
|
831
|
-
const t = this.rateFunc(alpha);
|
|
832
|
-
const parts = this.mobject?.submobjects ?? [];
|
|
833
|
-
parts.forEach((shape, i) => {
|
|
834
|
-
const original = this.shapes[i];
|
|
835
|
-
const move = this.moves[i];
|
|
836
|
-
if (original === undefined || move === undefined)
|
|
837
|
-
return;
|
|
838
|
-
shape.restore();
|
|
839
|
-
const fill = original.getFillOpacity();
|
|
840
|
-
const stroke = original.getStrokeOpacity();
|
|
841
|
-
shape.shift(mul(sub(move.final, move.initial), t));
|
|
842
|
-
shape.rotate(TAU * t, { aboutPoint: this.shapeCenter });
|
|
843
|
-
shape.rotate(-TAU * t, { aboutPoint: centerOfMass(shape) });
|
|
844
|
-
shape.setFill({ opacity: Math.min(fill, (t * fill) / this.fadeInFraction) });
|
|
845
|
-
shape.setStroke({ opacity: Math.min(stroke, (t * stroke) / this.fadeInFraction) });
|
|
846
|
-
});
|
|
847
|
-
}
|
|
848
|
-
}
|
|
849
|
-
/** The submobjects of `shapes` fly in from `scaleFactor` times their distance to the center,
|
|
850
|
-
* along a spiral, and fade in (Manim `SpiralIn`). */
|
|
851
|
-
export function SpiralIn(shapes, opts = {}) {
|
|
852
|
-
const { scaleFactor = 8, fadeInFraction = 0.3, ...rest } = opts;
|
|
853
|
-
const originals = shapes.copy().submobjects;
|
|
854
|
-
const center = shapes.getCenter();
|
|
855
|
-
const moves = shapes.submobjects.map((shape) => {
|
|
856
|
-
const final = shape.getCenter();
|
|
857
|
-
const initial = [
|
|
858
|
-
final[0] + (final[0] - center[0]) * scaleFactor,
|
|
859
|
-
final[1] + (final[1] - center[1]) * scaleFactor,
|
|
860
|
-
];
|
|
861
|
-
shape.moveTo(initial);
|
|
862
|
-
shape.saveState();
|
|
863
|
-
return { initial, final };
|
|
864
|
-
});
|
|
865
|
-
const a = new SpiralInAnimation(shapes, { introducer: true, ...rest });
|
|
866
|
-
a.shapes = originals;
|
|
867
|
-
a.scaleFactor = scaleFactor;
|
|
868
|
-
a.fadeInFraction = fadeInFraction;
|
|
869
|
-
a.shapeCenter = center;
|
|
870
|
-
a.moves = moves;
|
|
871
|
-
return a;
|
|
872
|
-
}
|
|
873
|
-
function grow(handle, mobject, point, opts, start) {
|
|
874
|
-
const { pointColor, ...rest } = opts;
|
|
875
|
-
const startCopy = () => {
|
|
876
|
-
const s = mobject.copy().scale(0).moveTo(point);
|
|
877
|
-
return pointColor === undefined ? s : s.setColor(pointColor);
|
|
878
|
-
};
|
|
879
|
-
return transformOf(handle, mobject, mobject, rest, { introducer: true }, {
|
|
880
|
-
target: () => mobject,
|
|
881
|
-
start: start ?? startCopy,
|
|
882
|
-
});
|
|
883
|
-
}
|
|
884
|
-
/** Grows the mobject from `point` (Manim `GrowFromPoint`). */
|
|
885
|
-
export function GrowFromPoint(mobject, point, opts = {}) {
|
|
886
|
-
return grow(engine.growFromPoint(raw(mobject), point, opts.pointColor), mobject, point, opts);
|
|
887
|
-
}
|
|
888
|
-
/** Grows the mobject from its center (Manim `GrowFromCenter`). */
|
|
889
|
-
export function GrowFromCenter(mobject, opts = {}) {
|
|
890
|
-
return grow(engine.growFromCenter(raw(mobject), opts.pointColor), mobject, mobject.getCenter(), opts);
|
|
891
|
-
}
|
|
892
|
-
/** Grows the mobject from the critical point in direction `edge` (Manim `GrowFromEdge`). */
|
|
893
|
-
export function GrowFromEdge(mobject, edge, opts = {}) {
|
|
894
|
-
const point = mobject.getCriticalPoint(edge);
|
|
895
|
-
return grow(engine.growFromEdge(raw(mobject), edge, opts.pointColor), mobject, point, opts);
|
|
896
|
-
}
|
|
897
|
-
/** Grows an arrow from its start (Manim `GrowArrow`). */
|
|
898
|
-
export function GrowArrow(arrow, opts = {}) {
|
|
899
|
-
const point = arrow.getStart();
|
|
900
|
-
return grow(engine.growArrow(raw(arrow), opts.pointColor), arrow, point, opts, () => {
|
|
901
|
-
const s = arrow.copy().scale(0, { aboutPoint: point });
|
|
902
|
-
return opts.pointColor === undefined ? s : s.setColor(opts.pointColor);
|
|
903
|
-
});
|
|
904
|
-
}
|
|
905
|
-
/** Manim's `spiral_path(angle)`: each point turns by `(alpha - 1) * angle` about its start. */
|
|
906
|
-
function spiralPath(angle) {
|
|
907
|
-
return (starts, ends, alpha) => starts.map((p, i) => {
|
|
908
|
-
const e = ends[i] ?? p;
|
|
909
|
-
const [s, c] = [Math.sin((alpha - 1) * angle), Math.cos((alpha - 1) * angle)];
|
|
910
|
-
const d = sub(e, p);
|
|
911
|
-
return [p[0] + alpha * (d[0] * c - d[1] * s), p[1] + alpha * (d[0] * s + d[1] * c)];
|
|
912
|
-
});
|
|
913
|
-
}
|
|
914
|
-
/** Grows the mobject from its center while it turns by `angle` (Manim `SpinInFromNothing`;
|
|
915
|
-
* π/2). */
|
|
916
|
-
export function SpinInFromNothing(mobject, opts = {}) {
|
|
917
|
-
const { angle = PI / 2, ...rest } = opts;
|
|
918
|
-
return GrowFromCenter(mobject, { ...rest, pathFunc: spiralPath(angle) });
|
|
919
|
-
}
|
|
920
|
-
// --- indication -----------------------------------------------------------------------
|
|
921
|
-
/** Scales the mobject by `scaleFactor` and gives it `color`, then goes back (Manim `Indicate`;
|
|
922
|
-
* rate `thereAndBack`). The default colour is Manim's `PURE_YELLOW`. */
|
|
923
|
-
export function Indicate(mobject, opts = {}) {
|
|
924
|
-
const { scaleFactor = 1.2, color = PURE_YELLOW, ...rest } = opts;
|
|
925
|
-
return transformOf(engine.indicate(raw(mobject), scaleFactor, color), mobject, undefined, rest, {
|
|
926
|
-
rate: thereAndBack,
|
|
927
|
-
}, {
|
|
928
|
-
target: () => mobject.copy().scale(scaleFactor).setColor(color),
|
|
929
|
-
});
|
|
930
|
-
}
|
|
931
|
-
/** A dot that covers the frame shrinks onto `focusPoint` (a point or a mobject), then goes
|
|
932
|
-
* (Manim `FocusOn`; 2 seconds). */
|
|
933
|
-
export function FocusOn(focusPoint, opts = {}) {
|
|
934
|
-
const { opacity = 0.2, color = GREY, ...rest } = opts;
|
|
935
|
-
const radius = FRAME_WIDTH / 2 + FRAME_HEIGHT / 2;
|
|
936
|
-
const start = Dot(centerOf(focusPoint), { radius, strokeWidth: 0, fillColor: color, fillOpacity: 0 });
|
|
937
|
-
const little = Dot(centerOf(focusPoint), { radius: 0 }).setFill(color, { opacity });
|
|
938
|
-
little.addUpdater((d) => d.moveTo(centerOf(focusPoint)));
|
|
939
|
-
return transformOf(engine.transform(raw(start), raw(little)), start, little, { runTime: 2, ...rest }, {
|
|
940
|
-
remover: true,
|
|
941
|
-
}, {
|
|
942
|
-
target: () => little,
|
|
943
|
-
});
|
|
944
|
-
}
|
|
945
|
-
/** Manim `Flash`: its lines and their animations. */
|
|
946
|
-
export class FlashAnimation extends Animation {
|
|
947
|
-
point = ORIGIN;
|
|
948
|
-
lineLength = 0.2;
|
|
949
|
-
numLines = 12;
|
|
950
|
-
flashRadius = 0.1;
|
|
951
|
-
lineStrokeWidth = 3;
|
|
952
|
-
color = PURE_YELLOW;
|
|
953
|
-
timeWidth = 1;
|
|
954
|
-
/** The lines, as a VGroup (Manim `create_lines`). */
|
|
955
|
-
createLines() {
|
|
956
|
-
const lines = Array.from({ length: this.numLines }, (_, i) => Line(this.point, [this.point[0] + this.lineLength, this.point[1]])
|
|
957
|
-
.shift([this.flashRadius, 0])
|
|
958
|
-
.rotate((i * TAU) / this.numLines, { aboutPoint: this.point }));
|
|
959
|
-
return VGroup(lines).setColor(this.color).setStroke({ width: this.lineStrokeWidth });
|
|
960
|
-
}
|
|
961
|
-
/** A ShowPassingFlash for each line (Manim `create_line_anims`). */
|
|
962
|
-
createLineAnims() {
|
|
963
|
-
return this.createLines().submobjects.map((line) => ShowPassingFlash(line, { timeWidth: this.timeWidth, runTime: this.getRunTime() }));
|
|
964
|
-
}
|
|
965
|
-
}
|
|
966
|
-
/** Lines that flash outward around a point, or around the center of a mobject (Manim `Flash`).
|
|
967
|
-
* Each line runs from `flashRadius` to `flashRadius + lineLength` from the center. */
|
|
968
|
-
export function Flash(point, opts = {}) {
|
|
969
|
-
const { lineLength = 0.2, numLines = 12, flashRadius = 0.1, lineStrokeWidth = 3, color = PURE_YELLOW, timeWidth = 1, ...rest } = opts;
|
|
970
|
-
const center = centerOf(point);
|
|
971
|
-
const options = { lineLength, numLines, flashRadius, lineStrokeWidth, color, timeWidth };
|
|
972
|
-
const a = engineAnimation(FlashAnimation, engine.flash(center, options), undefined, rest);
|
|
973
|
-
Object.assign(a, { point: center, ...options });
|
|
974
|
-
return a;
|
|
975
|
-
}
|
|
976
|
-
/** A part of each path, of length `timeWidth`, passes along it (Manim `ShowPassingFlash`). The
|
|
977
|
-
* mobject is removed at the end. */
|
|
978
|
-
export function ShowPassingFlash(mobject, opts = {}) {
|
|
979
|
-
const { timeWidth = 0.1, ...rest } = opts;
|
|
980
|
-
const a = new PassingFlash(mobject, { remover: true, introducer: true, ...rest });
|
|
981
|
-
a.timeWidth = timeWidth;
|
|
982
|
-
return a;
|
|
983
|
-
}
|
|
984
|
-
class PassingFlash extends ShowPartialAnimation {
|
|
985
|
-
timeWidth = 0.1;
|
|
986
|
-
getBounds(alpha) {
|
|
987
|
-
const upper = mix(0, 1 + this.timeWidth, alpha);
|
|
988
|
-
return [Math.max(upper - this.timeWidth, 0), Math.min(upper, 1)];
|
|
989
|
-
}
|
|
990
|
-
finish() {
|
|
991
|
-
super.finish();
|
|
992
|
-
for (const [submob, start] of this.getAllFamiliesZipped()) {
|
|
993
|
-
if (submob !== undefined && start !== undefined)
|
|
994
|
-
becomePartial(submob, start, 0, 1);
|
|
995
|
-
}
|
|
996
|
-
}
|
|
997
|
-
}
|
|
998
|
-
/** `ShowPassingFlash` of `nSegments` copies, thinner toward the tail (Manim
|
|
999
|
-
* `ShowPassingFlashWithThinningStrokeWidth`). */
|
|
1000
|
-
export function ShowPassingFlashWithThinningStrokeWidth(vmobject, opts = {}) {
|
|
1001
|
-
const { nSegments = 10, timeWidth = 0.1, remover = true, ...rest } = opts;
|
|
1002
|
-
const width = vmobject.getStrokeWidth();
|
|
1003
|
-
const steps = Array.from({ length: nSegments }, (_, i) => (nSegments === 1 ? 0 : i / (nSegments - 1)));
|
|
1004
|
-
return AnimationGroup(...steps.map((k) => ShowPassingFlash(vmobject.copy().setStroke({ width: width * k }), {
|
|
1005
|
-
...rest,
|
|
1006
|
-
remover,
|
|
1007
|
-
timeWidth: timeWidth * (1 - k),
|
|
1008
|
-
})));
|
|
1009
|
-
}
|
|
1010
|
-
/** Manim `Wiggle`: grows a little and rocks, then settles back. */
|
|
1011
|
-
export class WiggleAnimation extends Animation {
|
|
1012
|
-
scaleValue = 1.1;
|
|
1013
|
-
rotationAngle = 0.01 * TAU;
|
|
1014
|
-
nWiggles = 6;
|
|
1015
|
-
scaleAboutPoint;
|
|
1016
|
-
rotateAboutPoint;
|
|
1017
|
-
/** The point of the scaling: `scaleAboutPoint`, or the center (Manim `get_scale_about_point`). */
|
|
1018
|
-
getScaleAboutPoint() {
|
|
1019
|
-
return this.scaleAboutPoint ?? this.mobject?.getCenter() ?? ORIGIN;
|
|
1020
|
-
}
|
|
1021
|
-
/** The point of the rotation (Manim `get_rotate_about_point`). */
|
|
1022
|
-
getRotateAboutPoint() {
|
|
1023
|
-
return this.rotateAboutPoint ?? this.mobject?.getCenter() ?? ORIGIN;
|
|
1024
|
-
}
|
|
1025
|
-
interpolateSubmobject(submobject, startingSubmobject, alpha) {
|
|
1026
|
-
submobject.setPoints(startingSubmobject.getPoints());
|
|
1027
|
-
submobject.scale(mix(1, this.scaleValue, thereAndBack(alpha)), { aboutPoint: this.getScaleAboutPoint() });
|
|
1028
|
-
submobject.rotate(wiggle(alpha, this.nWiggles) * this.rotationAngle, {
|
|
1029
|
-
aboutPoint: this.getRotateAboutPoint(),
|
|
1030
|
-
});
|
|
1031
|
-
return this;
|
|
1032
|
-
}
|
|
1033
|
-
}
|
|
1034
|
-
/** Grows the mobject by `scaleValue` and rocks it `nWiggles` times by `rotationAngle`, then
|
|
1035
|
-
* settles back (Manim `Wiggle`; 2 seconds). */
|
|
1036
|
-
export function Wiggle(mobject, opts = {}) {
|
|
1037
|
-
const { scaleValue = 1.1, rotationAngle = 0.01 * TAU, nWiggles = 6, scaleAboutPoint, rotateAboutPoint, ...rest } = opts;
|
|
1038
|
-
const a = new WiggleAnimation(mobject, { runTime: 2, ...rest });
|
|
1039
|
-
Object.assign(a, { scaleValue, rotationAngle, nWiggles, scaleAboutPoint, rotateAboutPoint });
|
|
1040
|
-
return a;
|
|
1041
|
-
}
|
|
1042
|
-
/** A temporary line around the mobject: `Circumscribe(mob, m.Circle, opts)` or
|
|
1043
|
-
* `Circumscribe(mob, opts)`. With no fades, a flash of `timeWidth` passes around the shape.
|
|
1044
|
-
* `fadeIn` fades the shape in (else it is drawn), and `fadeOut` fades it out (else it is
|
|
1045
|
-
* undrawn), each in half the run time. */
|
|
1046
|
-
export function Circumscribe(mobject, shape = {}, opts = {}) {
|
|
1047
|
-
const o = typeof shape === "function" ? { ...opts, shape } : shape;
|
|
1048
|
-
const { shape: frame, fadeIn = false, fadeOut = false, timeWidth = 0.3, buff = SMALL_BUFF, color = PURE_YELLOW, strokeWidth = 4, ...rest } = o;
|
|
1049
|
-
const options = {
|
|
1050
|
-
shape: frame !== undefined && circleFrames.has(frame) ? "circle" : "rectangle",
|
|
1051
|
-
fadeIn,
|
|
1052
|
-
fadeOut,
|
|
1053
|
-
timeWidth,
|
|
1054
|
-
buff,
|
|
1055
|
-
color,
|
|
1056
|
-
strokeWidth,
|
|
1057
|
-
};
|
|
1058
|
-
return engineAnimation(Animation, engine.circumscribe(raw(mobject), options), mobject, rest, { rate: linear });
|
|
1059
|
-
}
|
|
1060
|
-
/** Shows and hides the mobject `blinks` times (Manim `Blink`): `timeOn` (0.5) seconds visible,
|
|
1061
|
-
* `timeOff` (0.5) hidden. With `hideAtEnd` it ends hidden. */
|
|
1062
|
-
export function Blink(mobject, opts = {}) {
|
|
1063
|
-
const { timeOn = 0.5, timeOff = 0.5, blinks = 1, hideAtEnd = false, ...rest } = opts;
|
|
1064
|
-
const on = () => UpdateFromFunc(mobject, (m) => m.setOpacity(1), { runTime: timeOn });
|
|
1065
|
-
const off = () => UpdateFromFunc(mobject, (m) => m.setOpacity(0), { runTime: timeOff });
|
|
1066
|
-
const steps = Array.from({ length: blinks }, () => [on(), off()]).flat();
|
|
1067
|
-
if (!hideAtEnd)
|
|
1068
|
-
steps.push(on());
|
|
1069
|
-
return Succession(...steps, rest);
|
|
1070
|
-
}
|
|
1071
|
-
/** Manim `Homotopy`: every point moves by a function of its place and the time. */
|
|
1072
|
-
export class HomotopyAnimation extends Animation {
|
|
1073
|
-
homotopy = (x, y) => [x, y];
|
|
1074
|
-
/** Manim's `apply_function_kwargs`: the point that the homotopy is relative to. */
|
|
1075
|
-
applyFunctionKwargs = {};
|
|
1076
|
-
/** The function of the points at time `t` (Manim `function_at_time_t`), relative to
|
|
1077
|
-
* `aboutPoint` (the origin) or to the critical point `aboutEdge`. */
|
|
1078
|
-
functionAtTimeT(t) {
|
|
1079
|
-
const { aboutPoint, aboutEdge } = this.applyFunctionKwargs;
|
|
1080
|
-
const o = aboutPoint ?? (aboutEdge === undefined ? ORIGIN : (this.mobject?.getCriticalPoint(aboutEdge) ?? ORIGIN));
|
|
1081
|
-
return (p) => {
|
|
1082
|
-
const r = this.homotopy(p[0] - o[0], p[1] - o[1], 0, t);
|
|
1083
|
-
return [(r[0] ?? p[0] - o[0]) + o[0], (r[1] ?? p[1] - o[1]) + o[1]];
|
|
1084
|
-
};
|
|
1085
|
-
}
|
|
1086
|
-
interpolateSubmobject(submobject, startingSubmobject, alpha) {
|
|
1087
|
-
const f = this.functionAtTimeT(alpha);
|
|
1088
|
-
submobject.setPoints(startingSubmobject.getPoints().map(f));
|
|
1089
|
-
return this;
|
|
1090
|
-
}
|
|
1091
|
-
}
|
|
1092
|
-
class SmoothedHomotopy extends HomotopyAnimation {
|
|
1093
|
-
interpolateSubmobject(submobject, startingSubmobject, alpha) {
|
|
1094
|
-
super.interpolateSubmobject(submobject, startingSubmobject, alpha);
|
|
1095
|
-
const anchors = submobject.getPoints().filter((_, i) => i % 4 === 0 || i % 4 === 3);
|
|
1096
|
-
const unique = anchors.filter((p, i) => i === 0 || norm(sub(p, anchors[i - 1] ?? p)) > 1e-9);
|
|
1097
|
-
if (unique.length > 1)
|
|
1098
|
-
submobject.setPointsSmoothly(unique);
|
|
1099
|
-
return this;
|
|
1100
|
-
}
|
|
1101
|
-
}
|
|
1102
|
-
/** Moves every point by `homotopy(x, y, z, t)` while `t` goes from 0 to 1 (Manim `Homotopy`; 3
|
|
1103
|
-
* seconds). */
|
|
1104
|
-
export function Homotopy(homotopy, mobject, opts = {}) {
|
|
1105
|
-
const { applyFunctionKwargs = {}, ...rest } = opts;
|
|
1106
|
-
const a = new HomotopyAnimation(mobject, { runTime: 3, ...rest });
|
|
1107
|
-
a.homotopy = homotopy;
|
|
1108
|
-
a.applyFunctionKwargs = applyFunctionKwargs;
|
|
1109
|
-
return a;
|
|
1110
|
-
}
|
|
1111
|
-
/** `Homotopy` that keeps the curves smooth (Manim `SmoothedVectorizedHomotopy`). */
|
|
1112
|
-
export function SmoothedVectorizedHomotopy(homotopy, mobject, opts = {}) {
|
|
1113
|
-
const a = new SmoothedHomotopy(mobject, { runTime: 3, ...opts });
|
|
1114
|
-
a.homotopy = homotopy;
|
|
1115
|
-
return a;
|
|
1116
|
-
}
|
|
1117
|
-
/** A wave passes along the mobject from left to right and moves its points in `direction` by
|
|
1118
|
-
* up to `amplitude` (Manim `ApplyWave`; 2 seconds). `waveFunc` (smooth) shapes the wave,
|
|
1119
|
-
* `timeWidth` (1) is its width and `ripples` (1) its number of ripples. */
|
|
1120
|
-
export function ApplyWave(mobject, opts = {}) {
|
|
1121
|
-
const { direction = UP, amplitude = 0.2, waveFunc = smooth, timeWidth = 1, ripples = 1, ...rest } = opts;
|
|
1122
|
-
const xMin = mobject.getLeft()[0];
|
|
1123
|
-
const xMax = mobject.getRight()[0];
|
|
1124
|
-
const n = norm(direction);
|
|
1125
|
-
const vect = n === 0 ? ORIGIN : [(amplitude * direction[0]) / n, (amplitude * direction[1]) / n];
|
|
1126
|
-
const wave = (time) => {
|
|
1127
|
-
let t = 1 - time;
|
|
1128
|
-
if (t >= 1 || t <= 0)
|
|
1129
|
-
return 0;
|
|
1130
|
-
const phases = ripples * 2;
|
|
1131
|
-
let phase = Math.trunc(t * phases);
|
|
1132
|
-
if (phase === 0)
|
|
1133
|
-
return waveFunc(t * phases);
|
|
1134
|
-
if (phase === phases - 1) {
|
|
1135
|
-
t -= phase / phases;
|
|
1136
|
-
return (1 - waveFunc(t * phases)) * (2 * (ripples % 2) - 1);
|
|
1137
|
-
}
|
|
1138
|
-
phase = Math.trunc((phase - 1) / 2);
|
|
1139
|
-
t -= (2 * phase + 1) / phases;
|
|
1140
|
-
return (1 - 2 * waveFunc(t * ripples)) * (1 - 2 * (phase % 2));
|
|
1141
|
-
};
|
|
1142
|
-
return Homotopy((x, y, _z, t) => {
|
|
1143
|
-
const upper = mix(0, 1 + timeWidth, t);
|
|
1144
|
-
const lower = upper - timeWidth;
|
|
1145
|
-
const nudge = wave(unmix(lower, upper, unmix(xMin, xMax, x)));
|
|
1146
|
-
return [x + nudge * vect[0], y + nudge * vect[1]];
|
|
1147
|
-
}, mobject, { runTime: 2, ...rest });
|
|
1148
|
-
}
|
|
1149
|
-
/** `Homotopy` of a complex function `(z, t) => w`, with a complex number as `[re, im]` (Manim
|
|
1150
|
-
* `ComplexHomotopy`). */
|
|
1151
|
-
export function ComplexHomotopy(complexHomotopy, mobject, opts = {}) {
|
|
1152
|
-
return Homotopy((x, y, _z, t) => complexHomotopy([x, y], t), mobject, opts);
|
|
1153
|
-
}
|
|
1154
|
-
/** Manim `PhaseFlow`: the points flow along the vector field `function`. */
|
|
1155
|
-
class PhaseFlowAnimation extends Animation {
|
|
1156
|
-
func = () => ORIGIN;
|
|
1157
|
-
virtualTime = 1;
|
|
1158
|
-
#last;
|
|
1159
|
-
interpolateMobject(alpha) {
|
|
1160
|
-
const t = this.rateFunc(alpha);
|
|
1161
|
-
const last = this.#last;
|
|
1162
|
-
this.#last = t;
|
|
1163
|
-
if (last === undefined)
|
|
1164
|
-
return;
|
|
1165
|
-
const dt = this.virtualTime * (t - last);
|
|
1166
|
-
this.mobject?.applyFunction((p) => {
|
|
1167
|
-
const v = this.func(p);
|
|
1168
|
-
return [p[0] + dt * v[0], p[1] + dt * v[1]];
|
|
1169
|
-
});
|
|
1170
|
-
}
|
|
1171
|
-
}
|
|
1172
|
-
/** Moves every point along the vector field `function` for `virtualTime` (Manim `PhaseFlow`;
|
|
1173
|
-
* rate `linear`). */
|
|
1174
|
-
export function PhaseFlow(func, mobject, opts = {}) {
|
|
1175
|
-
const { virtualTime = 1, ...rest } = opts;
|
|
1176
|
-
const a = new PhaseFlowAnimation(mobject, { suspendMobjectUpdating: false, rateFunc: linear, ...rest });
|
|
1177
|
-
a.func = func;
|
|
1178
|
-
a.virtualTime = virtualTime;
|
|
1179
|
-
return a;
|
|
1180
|
-
}
|
|
1181
|
-
/** Manim `MoveAlongPath`: the center goes along the path. */
|
|
1182
|
-
class AlongPath extends Animation {
|
|
1183
|
-
path;
|
|
1184
|
-
interpolateMobject(alpha) {
|
|
1185
|
-
const p = this.path?.pointFromProportion(this.rateFunc(alpha));
|
|
1186
|
-
if (p !== undefined)
|
|
1187
|
-
this.mobject?.moveTo(p);
|
|
1188
|
-
}
|
|
1189
|
-
}
|
|
1190
|
-
/** Moves the center of the mobject along `path` (Manim `MoveAlongPath`). */
|
|
1191
|
-
export function MoveAlongPath(mobject, path, opts = {}) {
|
|
1192
|
-
const a = new AlongPath(mobject, { suspendMobjectUpdating: false, ...opts });
|
|
1193
|
-
a.path = path;
|
|
1194
|
-
return a;
|
|
1195
|
-
}
|
|
1196
|
-
// --- numbers --------------------------------------------------------------------------
|
|
1197
|
-
function checkDecimal(input) {
|
|
1198
|
-
if (!(input instanceof DecimalNumberMobject)) {
|
|
1199
|
-
wrong("ChangingDecimal", "decimalMob must be a DecimalNumber", "make it with m.DecimalNumber or m.Integer");
|
|
1200
|
-
}
|
|
1201
|
-
}
|
|
1202
|
-
/** Manim `ChangingDecimal`: the number shows `numberUpdateFunc(alpha)`. */
|
|
1203
|
-
export class ChangingDecimalAnimation extends Animation {
|
|
1204
|
-
numberUpdateFunc = () => 0;
|
|
1205
|
-
/** An error when the mobject is not a DecimalNumber (Manim `check_validity_of_input`). */
|
|
1206
|
-
checkValidityOfInput(decimalMob) {
|
|
1207
|
-
checkDecimal(decimalMob);
|
|
1208
|
-
}
|
|
1209
|
-
interpolateMobject(alpha) {
|
|
1210
|
-
const m = this.mobject;
|
|
1211
|
-
if (m instanceof DecimalNumberMobject)
|
|
1212
|
-
m.setValue(this.numberUpdateFunc(this.rateFunc(alpha)));
|
|
1213
|
-
}
|
|
1214
|
-
}
|
|
1215
|
-
/** The number shows `numberUpdateFunc(alpha)` while alpha goes from 0 to 1 (Manim
|
|
1216
|
-
* `ChangingDecimal`). */
|
|
1217
|
-
export function ChangingDecimal(decimalMob, numberUpdateFunc, opts = {}) {
|
|
1218
|
-
checkDecimal(decimalMob);
|
|
1219
|
-
const a = new ChangingDecimalAnimation(decimalMob, { suspendMobjectUpdating: false, ...opts });
|
|
1220
|
-
a.numberUpdateFunc = numberUpdateFunc;
|
|
1221
|
-
return a;
|
|
1222
|
-
}
|
|
1223
|
-
/** The number counts to `targetNumber` (Manim `ChangeDecimalToValue`). */
|
|
1224
|
-
export function ChangeDecimalToValue(decimalMob, targetNumber, opts = {}) {
|
|
1225
|
-
checkDecimal(decimalMob);
|
|
1226
|
-
const start = decimalMob.getValue();
|
|
1227
|
-
return ChangingDecimal(decimalMob, (a) => mix(start, targetNumber, a), opts);
|
|
1228
|
-
}
|
|
1229
|
-
function aboutOf(mobject, o) {
|
|
1230
|
-
if (o.aboutPoint !== undefined)
|
|
1231
|
-
return o.aboutPoint;
|
|
1232
|
-
return o.aboutEdge === undefined ? mobject.getCenter() : mobject.getCriticalPoint(o.aboutEdge);
|
|
1233
|
-
}
|
|
1234
|
-
/** Turns the mobject by `angle` about `aboutPoint`, or about the critical point `aboutEdge`, or
|
|
1235
|
-
* about its center (Manim `Rotate`). */
|
|
1236
|
-
export function Rotate(mobject, angle = PI, opts = {}) {
|
|
1237
|
-
const { aboutPoint, aboutEdge, ...rest } = opts;
|
|
1238
|
-
const about = aboutOf(mobject, {
|
|
1239
|
-
...(aboutPoint === undefined ? {} : { aboutPoint }),
|
|
1240
|
-
...(aboutEdge === undefined ? {} : { aboutEdge }),
|
|
1241
|
-
});
|
|
1242
|
-
return transformOf(engine.rotate(raw(mobject), angle, about), mobject, undefined, rest, {}, {
|
|
1243
|
-
target: () => mobject.copy().rotate(angle, { aboutPoint: about }),
|
|
1244
|
-
});
|
|
1245
|
-
}
|
|
1246
|
-
/** Manim `Rotating`: turns by `angle` over the run time. */
|
|
1247
|
-
class RotatingAnimation extends Animation {
|
|
1248
|
-
angle = TAU;
|
|
1249
|
-
about = {};
|
|
1250
|
-
interpolateMobject(alpha) {
|
|
1251
|
-
const m = this.mobject;
|
|
1252
|
-
const start = this.startingMobject;
|
|
1253
|
-
if (m === undefined || start === undefined)
|
|
1254
|
-
return;
|
|
1255
|
-
m.becomePointsOf(start);
|
|
1256
|
-
m.rotate(this.rateFunc(alpha) * this.angle, { aboutPoint: aboutOf(m, this.about) });
|
|
1257
|
-
}
|
|
1258
|
-
}
|
|
1259
|
-
/** Turns the mobject by `angle` (TAU) over the run time (Manim `Rotating`; 5 seconds, rate
|
|
1260
|
-
* `linear`). */
|
|
1261
|
-
export function Rotating(mobject, opts = {}) {
|
|
1262
|
-
const { angle = TAU, aboutPoint, aboutEdge, ...rest } = opts;
|
|
1263
|
-
const a = new RotatingAnimation(mobject, { runTime: 5, rateFunc: linear, ...rest });
|
|
1264
|
-
a.angle = angle;
|
|
1265
|
-
a.about = { ...(aboutPoint === undefined ? {} : { aboutPoint }), ...(aboutEdge === undefined ? {} : { aboutEdge }) };
|
|
1266
|
-
return a;
|
|
1267
|
-
}
|
|
1268
|
-
// --- specialized ----------------------------------------------------------------------
|
|
1269
|
-
/** Rings of `mobject` spread out from `focalPoint` and fade (Manim `Broadcast`): `nMobs` (5)
|
|
1270
|
-
* copies, from `initialWidth` (0) and `initialOpacity` (1) to the size of the mobject and
|
|
1271
|
-
* `finalOpacity` (0); 3 seconds, `lagRatio` 0.2. */
|
|
1272
|
-
export function Broadcast(mobject, opts = {}) {
|
|
1273
|
-
const { focalPoint = ORIGIN, nMobs = 5, initialOpacity = 1, finalOpacity = 0, initialWidth = 0, remover = true, lagRatio = 0.2, runTime = 3, ...rest } = opts;
|
|
1274
|
-
const filled = mobject.getFillOpacity() > 0;
|
|
1275
|
-
const setOpacity = (m, o) => {
|
|
1276
|
-
if (filled)
|
|
1277
|
-
m.setOpacity(o);
|
|
1278
|
-
else
|
|
1279
|
-
m.setStroke({ opacity: o });
|
|
1280
|
-
};
|
|
1281
|
-
const anims = Array.from({ length: nMobs }, () => {
|
|
1282
|
-
const mob = mobject.copy();
|
|
1283
|
-
setOpacity(mob, finalOpacity);
|
|
1284
|
-
mob.moveTo(focalPoint);
|
|
1285
|
-
mob.saveState();
|
|
1286
|
-
mob.scaleToFitWidth(Math.max(initialWidth, 1e-6));
|
|
1287
|
-
setOpacity(mob, initialOpacity);
|
|
1288
|
-
return Restore(mob, { remover });
|
|
1289
|
-
});
|
|
1290
|
-
return LaggedStart(...anims, { ...rest, lagRatio, runTime });
|
|
1291
|
-
}
|
|
1292
|
-
// --- speed ----------------------------------------------------------------------------
|
|
1293
|
-
/** The `dt` of the updaters of `ChangeSpeed.addUpdater` while a ChangeSpeed plays. */
|
|
1294
|
-
let speedDt;
|
|
1295
|
-
/** Manim `ChangeSpeed`: plays `anim` at the speeds of `speedinfo`. */
|
|
1296
|
-
export class ChangeSpeedAnimation extends Animation {
|
|
1297
|
-
anim;
|
|
1298
|
-
speedinfo = [];
|
|
1299
|
-
/** The updaters of `addUpdater` get the changed `dt` while it plays. */
|
|
1300
|
-
affectsSpeedUpdaters = true;
|
|
1301
|
-
/** The run time of `anim` at speed 1. */
|
|
1302
|
-
animRunTime = 1;
|
|
1303
|
-
#t = 0;
|
|
1304
|
-
/** The run time of `anim` at speed 1, as a fraction (Manim `get_scaled_total_time`). */
|
|
1305
|
-
getScaledTotalTime() {
|
|
1306
|
-
let total = 0;
|
|
1307
|
-
let prev = 0;
|
|
1308
|
-
let speed = this.speedinfo[0]?.[1] ?? 1;
|
|
1309
|
-
for (const [node, final] of this.speedinfo.slice(1)) {
|
|
1310
|
-
total += (node - prev) * (2 / (speed + final));
|
|
1311
|
-
prev = node;
|
|
1312
|
-
speed = final;
|
|
1313
|
-
}
|
|
1314
|
-
return total;
|
|
1315
|
-
}
|
|
1316
|
-
/** Prepares `anim` (Manim `setup`): returns it. */
|
|
1317
|
-
setup(anim) {
|
|
1318
|
-
return anim;
|
|
1319
|
-
}
|
|
1320
|
-
/** Adds an updater to `mobject` (Manim's class method `ChangeSpeed.add_updater`). While a
|
|
1321
|
-
* ChangeSpeed with `affectsSpeedUpdaters` plays, its `dt` follows the changed speed. */
|
|
1322
|
-
addUpdater(mobject, updateFunction, callUpdater = false) {
|
|
1323
|
-
mobject.addUpdater((m, dt) => updateFunction(m, speedDt ?? dt), { callUpdater, clock: "move" });
|
|
1324
|
-
}
|
|
1325
|
-
begin() {
|
|
1326
|
-
this.anim?.begin();
|
|
1327
|
-
}
|
|
1328
|
-
interpolate(alpha) {
|
|
1329
|
-
const t = this.#warp(alpha);
|
|
1330
|
-
if (this.affectsSpeedUpdaters)
|
|
1331
|
-
speedDt = (t - this.#t) * this.animRunTime;
|
|
1332
|
-
this.#t = t;
|
|
1333
|
-
this.anim?.interpolate(t);
|
|
1334
|
-
}
|
|
1335
|
-
updateMobjects(dt) {
|
|
1336
|
-
this.anim?.updateMobjects(dt);
|
|
1337
|
-
}
|
|
1338
|
-
finish() {
|
|
1339
|
-
speedDt = undefined;
|
|
1340
|
-
this.anim?.finish();
|
|
1341
|
-
}
|
|
1342
|
-
cleanUpFromScene(scene) {
|
|
1343
|
-
this.anim?.cleanUpFromScene(scene);
|
|
1344
|
-
}
|
|
1345
|
-
/** The progress of `anim` at progress `t` of this animation. */
|
|
1346
|
-
#warp(alpha) {
|
|
1347
|
-
const t = this.rateFunc(alpha);
|
|
1348
|
-
const total = this.getScaledTotalTime();
|
|
1349
|
-
let time = 0;
|
|
1350
|
-
let prev = 0;
|
|
1351
|
-
let speed = this.speedinfo[0]?.[1] ?? 1;
|
|
1352
|
-
for (const [node, final] of this.speedinfo.slice(1)) {
|
|
1353
|
-
const dur = node - prev;
|
|
1354
|
-
const span = (2 / (speed + final)) * dur;
|
|
1355
|
-
if (t * total <= time + span + 1e-12) {
|
|
1356
|
-
const x = (total * t - time) / dur;
|
|
1357
|
-
return (((final ** 2 - speed ** 2) * x ** 2) / 4) * dur + speed * x * dur + prev;
|
|
1358
|
-
}
|
|
1359
|
-
time += span;
|
|
1360
|
-
prev = node;
|
|
1361
|
-
speed = final;
|
|
1362
|
-
}
|
|
1363
|
-
return 1;
|
|
1364
|
-
}
|
|
1365
|
-
}
|
|
1366
|
-
/** Plays `anim` with speeds that change: `speedinfo` maps a point of the animation (0 to 1)
|
|
1367
|
-
* to a speed, and the speed changes smoothly between them (Manim `ChangeSpeed`). */
|
|
1368
|
-
export function ChangeSpeed(anim, speedinfo, opts = {}) {
|
|
1369
|
-
const { affectsSpeedUpdaters = true, ...rest } = opts;
|
|
1370
|
-
const info = new Map(Object.entries(speedinfo).map(([k, v]) => [Number(k), v]));
|
|
1371
|
-
if (!info.has(0))
|
|
1372
|
-
info.set(0, 1);
|
|
1373
|
-
if (!info.has(1))
|
|
1374
|
-
info.set(1, [...info.entries()].toSorted((a, b) => a[0] - b[0]).at(-1)?.[1] ?? 1);
|
|
1375
|
-
const sorted = [...info.entries()].toSorted((a, b) => a[0] - b[0]);
|
|
1376
|
-
const runTime = anim.getRunTime();
|
|
1377
|
-
const a = new ChangeSpeedAnimation(anim.mobject, {
|
|
1378
|
-
rateFunc: anim.rateFunc,
|
|
1379
|
-
suspendMobjectUpdating: false,
|
|
1380
|
-
...rest,
|
|
1381
|
-
runTime: 1,
|
|
1382
|
-
});
|
|
1383
|
-
a.anim = anim;
|
|
1384
|
-
a.speedinfo = sorted;
|
|
1385
|
-
a.affectsSpeedUpdaters = affectsSpeedUpdaters;
|
|
1386
|
-
a.animRunTime = runTime;
|
|
1387
|
-
anim.setRateFunc(linear);
|
|
1388
|
-
a.setRunTime(a.getScaledTotalTime() * runTime);
|
|
1389
|
-
return a;
|
|
1390
|
-
}
|
|
1391
|
-
// --- composition ----------------------------------------------------------------------
|
|
1392
|
-
/** Manim `AnimationGroup`, `LaggedStart` and `Succession`: the animations and their timings. */
|
|
1393
|
-
export class AnimationGroupAnimation extends Animation {
|
|
1394
|
-
/** The animations of the group, in order. */
|
|
1395
|
-
animations = [];
|
|
1396
|
-
/** The mobject of the group (Manim `group`). */
|
|
1397
|
-
group;
|
|
1398
|
-
/** Each animation's run time when the group was made. */
|
|
1399
|
-
runTimes = [];
|
|
1400
|
-
getAllMobjects() {
|
|
1401
|
-
return this.group === undefined ? [] : [this.group];
|
|
1402
|
-
}
|
|
1403
|
-
/** The run time of the group: `runTime`, or the end of its last animation (Manim
|
|
1404
|
-
* `init_run_time`). */
|
|
1405
|
-
initRunTime(runTime) {
|
|
1406
|
-
const timings = this.buildAnimationsWithTimings();
|
|
1407
|
-
const end = Math.max(0, ...timings.map(([, , e]) => e));
|
|
1408
|
-
if (runTime !== undefined)
|
|
1409
|
-
this.setRunTime(runTime);
|
|
1410
|
-
return runTime ?? end;
|
|
1411
|
-
}
|
|
1412
|
-
/** Each animation with its start and end in seconds (Manim `build_animations_with_timings`):
|
|
1413
|
-
* each starts when the one before it is `lagRatio` done. */
|
|
1414
|
-
buildAnimationsWithTimings() {
|
|
1415
|
-
let start = 0;
|
|
1416
|
-
return this.animations.map((anim, i) => {
|
|
1417
|
-
const length = this.runTimes[i] ?? 1;
|
|
1418
|
-
const timing = [anim, start, start + length];
|
|
1419
|
-
start += length * this.lagRatio;
|
|
1420
|
-
return timing;
|
|
1421
|
-
});
|
|
1422
|
-
}
|
|
1423
|
-
}
|
|
1424
|
-
function groupOf(label, items, o, make, lagRatio) {
|
|
1425
|
-
const list = animationList(items);
|
|
1426
|
-
const runTimes = list.map((a) => a.getRunTime());
|
|
1427
|
-
const { group, ...rest } = o;
|
|
1428
|
-
const members = list.map((a) => a.mobject).filter((x) => x !== undefined);
|
|
1429
|
-
const a = engineAnimation(AnimationGroupAnimation, make(animations(label, items)), undefined, rest, {
|
|
1430
|
-
rate: linear,
|
|
1431
|
-
lagRatio,
|
|
1432
|
-
});
|
|
1433
|
-
a.animations = list;
|
|
1434
|
-
a.runTimes = runTimes;
|
|
1435
|
-
a.group = group ?? VGroup(members);
|
|
1436
|
-
return a;
|
|
1437
|
-
}
|
|
1438
|
-
/** Plays animations together (Manim `AnimationGroup`; `lagRatio` 0, rate `linear` over the
|
|
1439
|
-
* group). */
|
|
1440
|
-
export function AnimationGroup(...args) {
|
|
1441
|
-
const [items, o = {}] = split(args);
|
|
1442
|
-
return groupOf("AnimationGroup", items, o, (raws) => engine.animationGroup(raws), 0);
|
|
1443
|
-
}
|
|
1444
|
-
/** Starts each animation when the one before it is `lagRatio` (0.05) done (Manim `LaggedStart`). */
|
|
1445
|
-
export function LaggedStart(...args) {
|
|
1446
|
-
const [items, o = {}] = split(args);
|
|
1447
|
-
const { lagRatio = 0.05, ...rest } = o;
|
|
1448
|
-
const a = groupOf("LaggedStart", items, rest, (raws) => engine.laggedStart(raws, lagRatio), lagRatio);
|
|
1449
|
-
a.lagRatio = lagRatio;
|
|
1450
|
-
return a;
|
|
1451
|
-
}
|
|
1452
|
-
/** Plays the animations one after another (Manim `Succession`; `lagRatio` 1). */
|
|
1453
|
-
export function Succession(...args) {
|
|
1454
|
-
const [items, o = {}] = split(args);
|
|
1455
|
-
const { lagRatio: _lag, ...rest } = o;
|
|
1456
|
-
return groupOf("Succession", items, rest, (raws) => engine.succession(raws), 1);
|
|
1457
|
-
}
|
|
1458
|
-
/** `LaggedStart` of `animationClass(...argCreator(sub))` for each submobject of `mobject`
|
|
1459
|
-
* (Manim `LaggedStartMap`; 2 seconds). */
|
|
1460
|
-
export function LaggedStartMap(animationClass, mobject, opts = {}) {
|
|
1461
|
-
const { argCreator = (m) => [m], runTime = 2, ...rest } = opts;
|
|
1462
|
-
const anims = mobject.submobjects.map((part) => {
|
|
1463
|
-
const made = Reflect.apply(animationClass, undefined, argCreator(part));
|
|
1464
|
-
if (!(made instanceof Animation)) {
|
|
1465
|
-
return wrong("LaggedStartMap", "animationClass must return an Animation", "give an animation such as m.FadeIn");
|
|
1466
|
-
}
|
|
1467
|
-
return made;
|
|
1468
|
-
});
|
|
1469
|
-
return LaggedStart(...anims, { ...rest, runTime });
|
|
1470
|
-
}
|
|
1471
|
-
/** Holds the picture for `runTime` seconds, as an animation (Manim `Wait`): a pause in a
|
|
1472
|
-
* `Succession`. */
|
|
1473
|
-
export function Wait(runTime = 1, opts = {}) {
|
|
1474
|
-
const { frozenFrame: _frozen, ...rest } = opts;
|
|
1475
|
-
return engineAnimation(Animation, engine.wait(), undefined, { ...rest, runTime }, { rate: linear });
|
|
1476
|
-
}
|
|
1477
|
-
/** Adds the mobjects to the stage, at once (Manim `Add`; run time 0). */
|
|
1478
|
-
export function Add(...args) {
|
|
1479
|
-
const [items, o = {}] = split(args);
|
|
1480
|
-
const first = items[0];
|
|
1481
|
-
const mob = items.length === 1 && first !== undefined ? first : VGroup(items);
|
|
1482
|
-
return new Animation(mob, { runTime: 0, introducer: true, ...o });
|
|
1483
|
-
}
|
|
1484
|
-
/** Plays animations to their end at once, off stage (not in Manim). */
|
|
1485
|
-
export function settle(...items) {
|
|
1486
|
-
engine.settle(animations("settle", items));
|
|
1487
|
-
}
|
|
1488
|
-
/** Manim's `TransformMatchingShapes` and `TransformMatchingTex`: the parts and their keys. */
|
|
1489
|
-
export class TransformMatchingAnimation extends TransformAnimation {
|
|
1490
|
-
#tex = false;
|
|
1491
|
-
/** The parts of `mobject` that match (Manim `get_mobject_parts`): the submobjects of a
|
|
1492
|
-
* formula, or the family members with points. */
|
|
1493
|
-
getMobjectParts(mobject) {
|
|
1494
|
-
return this.#tex ? mobject.submobjects : withPoints(mobject);
|
|
1495
|
-
}
|
|
1496
|
-
/** The key of a part (Manim `get_mobject_key`): its LaTeX, or its shape. */
|
|
1497
|
-
getMobjectKey(mobject) {
|
|
1498
|
-
if (!this.#tex)
|
|
1499
|
-
return shapeKey(mobject);
|
|
1500
|
-
for (const f of [this.mobject, this.targetMobject]) {
|
|
1501
|
-
if (f === undefined || !("texStrings" in f))
|
|
1502
|
-
continue;
|
|
1503
|
-
const i = f.submobjects.indexOf(mobject);
|
|
1504
|
-
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- checked: the formula has texStrings
|
|
1505
|
-
if (i >= 0)
|
|
1506
|
-
return f.texStrings[i] ?? "";
|
|
1507
|
-
}
|
|
1508
|
-
return "";
|
|
1509
|
-
}
|
|
1510
|
-
static tex(a) {
|
|
1511
|
-
a.#tex = true;
|
|
1512
|
-
}
|
|
1513
|
-
}
|
|
1514
|
-
/** Manim's key of `TransformMatchingShapes`: the own points, centred, at height 1, rounded to
|
|
1515
|
-
* 3 decimals. */
|
|
1516
|
-
function shapeKey(m) {
|
|
1517
|
-
const points = m.getPoints();
|
|
1518
|
-
if (points.length === 0)
|
|
1519
|
-
return "";
|
|
1520
|
-
const xs = points.map((p) => p[0]);
|
|
1521
|
-
const ys = points.map((p) => p[1]);
|
|
1522
|
-
const center = [(Math.min(...xs) + Math.max(...xs)) / 2, (Math.min(...ys) + Math.max(...ys)) / 2];
|
|
1523
|
-
const height = Math.max(...ys) - Math.min(...ys);
|
|
1524
|
-
const k = height > 1e-9 ? 1 / height : 1;
|
|
1525
|
-
const round = (x) => Math.round(x * k * 1000) / 1000 + 0;
|
|
1526
|
-
return points
|
|
1527
|
-
.map((p) => `${String(round(p[0] - (center[0] ?? 0)))},${String(round(p[1] - (center[1] ?? 0)))}`)
|
|
1528
|
-
.join(";");
|
|
1529
|
-
}
|
|
1530
|
-
function mismatchMode(o) {
|
|
1531
|
-
if (o.transformMismatches === true)
|
|
1532
|
-
return "transform";
|
|
1533
|
-
return o.fadeTransformMismatches === true ? "fade-transform" : "fade";
|
|
1534
|
-
}
|
|
1535
|
-
/** Parts with the same LaTeX morph into each other, the others fade; at the end
|
|
1536
|
-
* `targetMobject` takes the place of `mobject` (Manim `TransformMatchingTex`). */
|
|
1537
|
-
export function TransformMatchingTex(mobject, targetMobject, opts = {}) {
|
|
1538
|
-
const { transformMismatches: _t, fadeTransformMismatches: _f, keyMap = {}, ...rest } = opts;
|
|
1539
|
-
const keys = mobject.texStrings.map((k) => keyMap[k] ?? k);
|
|
1540
|
-
const handle = engine.transformMatching(raw(mobject), raw(targetMobject), keys, [...targetMobject.texStrings], mismatchMode(opts));
|
|
1541
|
-
const a = engineAnimation(TransformMatchingAnimation, handle, mobject, rest);
|
|
1542
|
-
TransformMatchingAnimation.tex(a);
|
|
1543
|
-
a.targetMobject = targetMobject;
|
|
1544
|
-
a.replaceMobjectWithTargetInScene = true;
|
|
1545
|
-
a.pathArc = rest.pathArc ?? 0;
|
|
1546
|
-
return a;
|
|
1547
|
-
}
|
|
1548
|
-
/** Parts with the same shape (the same points after each is centred and scaled to height 1)
|
|
1549
|
-
* morph into each other, the others fade; at the end `targetMobject` takes the place of
|
|
1550
|
-
* `mobject` (Manim `TransformMatchingShapes`). */
|
|
1551
|
-
export function TransformMatchingShapes(mobject, targetMobject, opts = {}) {
|
|
1552
|
-
const { transformMismatches: _t, fadeTransformMismatches: _f, keyMap = {}, ...rest } = opts;
|
|
1553
|
-
const handle = engine.transformMatchingShapes(raw(mobject), raw(targetMobject), Object.entries(keyMap), mismatchMode(opts));
|
|
1554
|
-
const a = engineAnimation(TransformMatchingAnimation, handle, mobject, rest);
|
|
1555
|
-
a.targetMobject = targetMobject;
|
|
1556
|
-
a.replaceMobjectWithTargetInScene = true;
|
|
1557
|
-
a.pathArc = rest.pathArc ?? 0;
|
|
1558
|
-
return a;
|
|
1559
|
-
}
|
|
1560
|
-
// --- updaters -------------------------------------------------------------------------
|
|
1561
|
-
/** An error when `method` is not a method of a Mobject (Manim `assert_is_mobject_method`). */
|
|
1562
|
-
export function assertIsMobjectMethod(method) {
|
|
1563
|
-
checkMethod("assertIsMobjectMethod", method);
|
|
1564
|
-
}
|
|
1565
|
-
/** Calls `method(...args)` on its Mobject on every frame (Manim `always`):
|
|
1566
|
-
* `m.always(label.nextTo, dot, m.UP)`. Returns the Mobject. */
|
|
1567
|
-
export function always(method, ...args) {
|
|
1568
|
-
checkMethod("always", method);
|
|
1569
|
-
const self = method[METHOD_SELF];
|
|
1570
|
-
const f = method[METHOD_FUNC];
|
|
1571
|
-
return self.addUpdater((m) => {
|
|
1572
|
-
Reflect.apply(f, m, args);
|
|
1573
|
-
});
|
|
1574
|
-
}
|
|
1575
|
-
/** Calls `method(...argGenerators.map((g) => g()))` on its Mobject on every frame (Manim
|
|
1576
|
-
* `f_always`). Returns the Mobject. */
|
|
1577
|
-
export function fAlways(method, ...argGenerators) {
|
|
1578
|
-
checkMethod("fAlways", method);
|
|
1579
|
-
const self = method[METHOD_SELF];
|
|
1580
|
-
const f = method[METHOD_FUNC];
|
|
1581
|
-
return self.addUpdater((m) => {
|
|
1582
|
-
Reflect.apply(f, m, argGenerators.map((g) => g()));
|
|
1583
|
-
});
|
|
1584
|
-
}
|
|
1585
|
-
/** Moves the mobject by `rate` units a second in `direction` (Manim `always_shift`). */
|
|
1586
|
-
export function alwaysShift(mobject, direction = RIGHT, rate = 0.1) {
|
|
1587
|
-
const n = norm(direction);
|
|
1588
|
-
const unit = n === 0 ? ORIGIN : [direction[0] / n, direction[1] / n];
|
|
1589
|
-
return mobject.addUpdater((m, dt) => m.shift(mul(unit, dt * rate)));
|
|
1590
|
-
}
|
|
1591
|
-
/** Turns the mobject by `rate` radians a second (Manim `always_rotate`; 20°). */
|
|
1592
|
-
export function alwaysRotate(mobject, rate = 20 * DEGREES, opts = {}) {
|
|
1593
|
-
return mobject.addUpdater((m, dt) => m.rotate(dt * rate, opts));
|
|
1594
|
-
}
|
|
1595
|
-
/** Plays `animation` in an updater of its mobject, from now, after `delay` seconds (Manim
|
|
1596
|
-
* `turn_animation_into_updater`). With `cycle`, it starts again at its end. Returns the mobject. */
|
|
1597
|
-
export function turnAnimationIntoUpdater(animation, opts = {}) {
|
|
1598
|
-
const { cycle = false, delay = 0 } = opts;
|
|
1599
|
-
const mobject = animation.mobject;
|
|
1600
|
-
if (mobject === undefined) {
|
|
1601
|
-
return wrong("turnAnimationIntoUpdater", "the animation has no mobject", "give an animation of one mobject");
|
|
1602
|
-
}
|
|
1603
|
-
animation.suspendMobjectUpdating = false;
|
|
1604
|
-
animation.begin();
|
|
1605
|
-
let total = -delay;
|
|
1606
|
-
const update = (m, dt) => {
|
|
1607
|
-
if (total >= 0) {
|
|
1608
|
-
const runTime = animation.getRunTime();
|
|
1609
|
-
if (runTime <= 0) {
|
|
1610
|
-
animation.interpolate(1);
|
|
1611
|
-
animation.updateMobjects(dt);
|
|
1612
|
-
animation.finish();
|
|
1613
|
-
m.removeUpdater(update);
|
|
1614
|
-
return;
|
|
1615
|
-
}
|
|
1616
|
-
const ratio = total / runTime;
|
|
1617
|
-
const alpha = cycle ? ratio % 1 : Math.min(1, Math.max(0, ratio));
|
|
1618
|
-
if (!cycle && alpha >= 1) {
|
|
1619
|
-
animation.finish();
|
|
1620
|
-
m.removeUpdater(update);
|
|
1621
|
-
return;
|
|
1622
|
-
}
|
|
1623
|
-
animation.interpolate(alpha);
|
|
1624
|
-
animation.updateMobjects(dt);
|
|
1625
|
-
}
|
|
1626
|
-
total += dt;
|
|
1627
|
-
};
|
|
1628
|
-
return mobject.addUpdater(update);
|
|
1629
|
-
}
|
|
1630
|
-
/** `turnAnimationIntoUpdater` with `cycle` (Manim `cycle_animation`). */
|
|
1631
|
-
export function cycleAnimation(animation, opts = {}) {
|
|
1632
|
-
return turnAnimationIntoUpdater(animation, { ...opts, cycle: true });
|
|
1633
|
-
}
|
|
1634
|
-
/** Manim `UpdateFromFunc` and `UpdateFromAlphaFunc`. */
|
|
1635
|
-
export class UpdateFromFuncAnimation extends Animation {
|
|
1636
|
-
updateFunction = () => undefined;
|
|
1637
|
-
withAlpha = false;
|
|
1638
|
-
interpolateMobject(alpha) {
|
|
1639
|
-
const m = this.mobject;
|
|
1640
|
-
if (m === undefined)
|
|
1641
|
-
return;
|
|
1642
|
-
if (this.withAlpha)
|
|
1643
|
-
this.updateFunction(m, this.rateFunc(alpha));
|
|
1644
|
-
else
|
|
1645
|
-
this.updateFunction(m, alpha);
|
|
1646
|
-
}
|
|
1647
|
-
}
|
|
1648
|
-
/** Calls `updateFunction(mobject)` on every frame of the animation (Manim `UpdateFromFunc`). */
|
|
1649
|
-
export function UpdateFromFunc(mobject, updateFunction, opts = {}) {
|
|
1650
|
-
const a = new UpdateFromFuncAnimation(mobject, { suspendMobjectUpdating: false, ...opts });
|
|
1651
|
-
a.updateFunction = (m) => {
|
|
1652
|
-
updateFunction(m);
|
|
1653
|
-
};
|
|
1654
|
-
return a;
|
|
1655
|
-
}
|
|
1656
|
-
/** Calls `updateFunction(mobject, alpha)` on every frame, with the progress after the rate
|
|
1657
|
-
* function (Manim `UpdateFromAlphaFunc`). */
|
|
1658
|
-
export function UpdateFromAlphaFunc(mobject, updateFunction, opts = {}) {
|
|
1659
|
-
const a = new UpdateFromFuncAnimation(mobject, { suspendMobjectUpdating: false, ...opts });
|
|
1660
|
-
a.updateFunction = updateFunction;
|
|
1661
|
-
a.withAlpha = true;
|
|
1662
|
-
return a;
|
|
1663
|
-
}
|
|
1664
|
-
/** Keeps the mobject at the same offset from `trackedMobject` during the animation (Manim
|
|
1665
|
-
* `MaintainPositionRelativeTo`). */
|
|
1666
|
-
export function MaintainPositionRelativeTo(mobject, trackedMobject, opts = {}) {
|
|
1667
|
-
const diff = sub(mobject.getCenter(), trackedMobject.getCenter());
|
|
1668
|
-
return UpdateFromFunc(mobject, (m) => {
|
|
1669
|
-
const target = trackedMobject.getCenter();
|
|
1670
|
-
const at = m.getCenter();
|
|
1671
|
-
m.shift([target[0] - at[0] + diff[0], target[1] - at[1] + diff[1]]);
|
|
1672
|
-
}, { suspendMobjectUpdating: true, ...opts });
|
|
1673
|
-
}
|
|
1674
|
-
// --- changing mobjects ----------------------------------------------------------------
|
|
1675
|
-
/** Manim `AnimatedBoundary`: two copies of the outline of a mobject that draw and fade in
|
|
1676
|
-
* turn, in `colors`. */
|
|
1677
|
-
export class AnimatedBoundaryMobject extends Mobject {
|
|
1678
|
-
vmobject;
|
|
1679
|
-
colors = [];
|
|
1680
|
-
maxStrokeWidth = 3;
|
|
1681
|
-
cycleRate = 0.5;
|
|
1682
|
-
backAndForth = true;
|
|
1683
|
-
drawRateFunc = smooth;
|
|
1684
|
-
fadeRateFunc = smooth;
|
|
1685
|
-
totalTime = 0;
|
|
1686
|
-
/** Draws and fades the two copies at the time after `dt` more seconds (Manim
|
|
1687
|
-
* `update_boundary_copies`). */
|
|
1688
|
-
updateBoundaryCopies(dt) {
|
|
1689
|
-
const time = this.totalTime * this.cycleRate;
|
|
1690
|
-
const [growing, fading] = this.submobjects;
|
|
1691
|
-
const vmobject = this.vmobject;
|
|
1692
|
-
if (growing === undefined || fading === undefined || vmobject === undefined || this.colors.length === 0)
|
|
1693
|
-
return;
|
|
1694
|
-
const n = this.colors.length;
|
|
1695
|
-
const index = Math.floor(time % n);
|
|
1696
|
-
const alpha = time % 1;
|
|
1697
|
-
const draw = this.drawRateFunc(alpha);
|
|
1698
|
-
const fade = this.fadeRateFunc(alpha);
|
|
1699
|
-
const bounds = this.backAndForth && Math.floor(time) % 2 === 1 ? [1 - draw, 1] : [0, draw];
|
|
1700
|
-
this.fullFamilyBecomePartial(growing, vmobject, ...bounds);
|
|
1701
|
-
growing.setStroke(this.colors[index], { width: this.maxStrokeWidth });
|
|
1702
|
-
if (time >= 1) {
|
|
1703
|
-
this.fullFamilyBecomePartial(fading, vmobject, 0, 1);
|
|
1704
|
-
fading.setStroke(this.colors[(index - 1 + n) % n], { width: (1 - fade) * this.maxStrokeWidth });
|
|
1705
|
-
}
|
|
1706
|
-
this.totalTime += dt;
|
|
1707
|
-
}
|
|
1708
|
-
/** Each family member of `mob1` takes the part from `a` to `b` of the member of `mob2`
|
|
1709
|
-
* (Manim `full_family_become_partial`). */
|
|
1710
|
-
fullFamilyBecomePartial(mob1, mob2, a, b) {
|
|
1711
|
-
const f1 = withPoints(mob1);
|
|
1712
|
-
const f2 = withPoints(mob2);
|
|
1713
|
-
f1.forEach((m, i) => {
|
|
1714
|
-
const other = f2[i];
|
|
1715
|
-
if (other !== undefined)
|
|
1716
|
-
becomePartial(m, other, a, b);
|
|
1717
|
-
});
|
|
1718
|
-
return this;
|
|
1719
|
-
}
|
|
1720
|
-
}
|
|
1721
|
-
/** The outline of `vmobject`, drawn again and again in the colours of `colors` (Manim
|
|
1722
|
-
* `AnimatedBoundary`). Add it to the scene; its updater runs it. */
|
|
1723
|
-
export function AnimatedBoundary(vmobject, opts = {}) {
|
|
1724
|
-
const copies = [0, 1].map(() => vmobject.copy().setStroke({ width: 0 }).setFill({ opacity: 0 }));
|
|
1725
|
-
const b = new AnimatedBoundaryMobject(raw(VGroup(copies)).copy());
|
|
1726
|
-
b.vmobject = vmobject;
|
|
1727
|
-
b.colors = [...(opts.colors ?? [BLUE_D, BLUE_B, BLUE_E, GREY_BROWN])];
|
|
1728
|
-
b.maxStrokeWidth = opts.maxStrokeWidth ?? 3;
|
|
1729
|
-
b.cycleRate = opts.cycleRate ?? 0.5;
|
|
1730
|
-
b.backAndForth = opts.backAndForth ?? true;
|
|
1731
|
-
b.drawRateFunc = opts.drawRateFunc ?? smooth;
|
|
1732
|
-
b.fadeRateFunc = opts.fadeRateFunc ?? smooth;
|
|
1733
|
-
// It follows `vmobject`, which the move changes: the move clock.
|
|
1734
|
-
return b.addUpdater((m, dt) => {
|
|
1735
|
-
m.updateBoundaryCopies(dt);
|
|
1736
|
-
}, { clock: "move" });
|
|
1737
|
-
}
|
|
1738
|
-
/** Manim `TracedPath`: a path that follows a point. */
|
|
1739
|
-
export class TracedPathMobject extends Mobject {
|
|
1740
|
-
tracedPointFunc = () => ORIGIN;
|
|
1741
|
-
dissipatingTime;
|
|
1742
|
-
time;
|
|
1743
|
-
/** Adds the point of `tracedPointFunc` to the path (Manim `update_path`). */
|
|
1744
|
-
// oxlint-disable-next-line no-unused-vars -- Manim's signature: the updater passes the path itself
|
|
1745
|
-
updatePath(mob, dt) {
|
|
1746
|
-
const p = this.tracedPointFunc();
|
|
1747
|
-
const points = this.getPoints();
|
|
1748
|
-
const last = points.at(-1) ?? p;
|
|
1749
|
-
const curve = [
|
|
1750
|
-
last,
|
|
1751
|
-
[mix(last[0], p[0], 1 / 3), mix(last[1], p[1], 1 / 3)],
|
|
1752
|
-
[mix(last[0], p[0], 2 / 3), mix(last[1], p[1], 2 / 3)],
|
|
1753
|
-
p,
|
|
1754
|
-
];
|
|
1755
|
-
let next = [...points, ...curve];
|
|
1756
|
-
if (this.dissipatingTime !== undefined && this.time !== undefined) {
|
|
1757
|
-
this.time += dt;
|
|
1758
|
-
if (this.time - 1 > this.dissipatingTime)
|
|
1759
|
-
next = next.slice(4);
|
|
1760
|
-
}
|
|
1761
|
-
this.setPoints(next);
|
|
1762
|
-
}
|
|
1763
|
-
}
|
|
1764
|
-
/** A line that follows the point that `tracedPointFunc` gives on every frame (Manim
|
|
1765
|
-
* `TracedPath`). With `dissipatingTime`, the oldest part goes after that many seconds. */
|
|
1766
|
-
export function TracedPath(tracedPointFunc, opts = {}) {
|
|
1767
|
-
const { strokeWidth = 2, strokeColor = WHITE, dissipatingTime } = opts;
|
|
1768
|
-
const start = Line(tracedPointFunc(), tracedPointFunc()).setStroke(strokeColor, { width: strokeWidth });
|
|
1769
|
-
const path = new TracedPathMobject(raw(start).copy());
|
|
1770
|
-
path.setPoints([]);
|
|
1771
|
-
path.tracedPointFunc = tracedPointFunc;
|
|
1772
|
-
path.dissipatingTime = dissipatingTime;
|
|
1773
|
-
path.time = dissipatingTime === undefined ? undefined : 1;
|
|
1774
|
-
// It follows a point that the move changes: the move clock.
|
|
1775
|
-
return path.addUpdater((m, dt) => {
|
|
1776
|
-
m.updatePath(m, dt);
|
|
1777
|
-
}, { clock: "move" });
|
|
1778
|
-
}
|