manim-web 0.22.0 → 0.23.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.
@@ -0,0 +1,211 @@
1
+ import { Vector3Tuple } from '../../core/Mobject';
2
+ import { TexturedMobject } from '../../core/TexturedMobject';
3
+ import * as THREE from 'three';
4
+ /**
5
+ * LottieMobject - display a Lottie (Bodymovin JSON) animation as a mobject.
6
+ *
7
+ * ManimCE cannot import After Effects / Lottie vector animations. The browser
8
+ * can: `lottie-web` renders the JSON into an offscreen `<canvas>`, which this
9
+ * mobject presents through a `THREE.CanvasTexture` on a plane - exactly how
10
+ * {@link VideoMobject} and {@link ImageMobject} draw their frames. The plane
11
+ * then participates in the standard mobject transforms (shift, scale, rotate),
12
+ * z-index/depth, opacity, and `scene.add`.
13
+ *
14
+ * The animation is time-driven: call {@link syncToTime} on scrub/seek so the
15
+ * displayed frame follows the Player clock, or {@link play} for live playback
16
+ * via the updater. `lottie-web` is pulled in through a lazy dynamic `import()`
17
+ * so the base bundle and SSR never load it. Tests inject a player (via the
18
+ * `player` option or {@link setLottiePlayerFactory}) to avoid the real library
19
+ * and any DOM/canvas - mirroring ModelMobject's loader-injection seam.
20
+ *
21
+ * @example
22
+ * ```typescript
23
+ * const anim = await LottieMobject.fromUrl('spinner.json', { width: 4, loop: true });
24
+ * scene.add(anim);
25
+ * anim.play();
26
+ * ```
27
+ */
28
+ /**
29
+ * Parsed Bodymovin JSON. Only `w`, `h` and `fr` are read here (for the natural
30
+ * size and frame rate); the whole object is handed to the player unchanged.
31
+ */
32
+ export interface LottieAnimationData {
33
+ /** Composition width in pixels. */
34
+ w?: number;
35
+ /** Composition height in pixels. */
36
+ h?: number;
37
+ /** Frame rate in frames per second. */
38
+ fr?: number;
39
+ [key: string]: unknown;
40
+ }
41
+ /**
42
+ * The subset of a `lottie-web` AnimationItem this mobject drives. The real
43
+ * AnimationItem satisfies it structurally; tests implement it with a stub.
44
+ */
45
+ export interface LottieAnimation {
46
+ /** Total number of frames in the composition. */
47
+ totalFrames: number;
48
+ /** Frame rate in frames per second. */
49
+ frameRate: number;
50
+ /** Seek to a frame (`isFrame` true) or a time, and stop there. */
51
+ goToAndStop(value: number, isFrame?: boolean): void;
52
+ /** Set the live-playback speed multiplier. */
53
+ setSpeed?(speed: number): void;
54
+ /** Release the player's resources. */
55
+ destroy(): void;
56
+ }
57
+ /** Parameters passed to the player's `loadAnimation`. */
58
+ export interface LottieLoadParams {
59
+ /** The canvas element that backs the texture. */
60
+ container: unknown;
61
+ /** Always `'canvas'`: this mobject renders into an offscreen canvas. */
62
+ renderer: 'canvas';
63
+ /** Loop the composition during live playback. */
64
+ loop: boolean;
65
+ /** Never autoplay: playback is driven by the updater or by seeks. */
66
+ autoplay: boolean;
67
+ /** The parsed Bodymovin JSON. */
68
+ animationData: LottieAnimationData;
69
+ /** Canvas renderer settings (the 2D context to draw into). */
70
+ rendererSettings: {
71
+ context?: unknown;
72
+ canvas?: unknown;
73
+ clearCanvas?: boolean;
74
+ };
75
+ }
76
+ /**
77
+ * The minimal player contract. `lottie-web`'s default export satisfies it.
78
+ */
79
+ export interface LottiePlayer {
80
+ loadAnimation(params: LottieLoadParams): LottieAnimation;
81
+ }
82
+ /** Builds a player. The DI seam set by {@link setLottiePlayerFactory}. */
83
+ export type LottiePlayerFactory = () => LottiePlayer | Promise<LottiePlayer>;
84
+ /**
85
+ * Inject a player factory, replacing the lazy-loaded `lottie-web`. Pass `null`
86
+ * to restore the default. Tests use this to avoid the real library.
87
+ */
88
+ export declare function setLottiePlayerFactory(factory: LottiePlayerFactory | null): void;
89
+ /** Options for creating a {@link LottieMobject}. */
90
+ export interface LottieMobjectOptions {
91
+ /** Parsed Bodymovin JSON. Required unless using {@link LottieMobject.fromUrl}. */
92
+ animationData?: LottieAnimationData;
93
+ /** Loop the composition during live playback and time wrapping. Default: false. */
94
+ loop?: boolean;
95
+ /** Playback speed multiplier (1 = normal). Default: 1. */
96
+ speed?: number;
97
+ /** Width in scene units. Default: auto from the composition aspect ratio. */
98
+ width?: number;
99
+ /** Height in scene units. Default: auto from the composition aspect ratio. */
100
+ height?: number;
101
+ /** If both width and height given, fit inside the box preserving aspect. Default: true. */
102
+ scaleToFit?: boolean;
103
+ /** Center position [x, y, z]. Default: [0, 0, 0]. */
104
+ center?: Vector3Tuple;
105
+ /** Opacity from 0 to 1. Default: 1. */
106
+ opacity?: number;
107
+ /** Render both sides of the plane. Default: false. */
108
+ doubleSided?: boolean;
109
+ /** Inject a player directly (bypasses the factory and the network). */
110
+ player?: LottiePlayer;
111
+ }
112
+ /** Options for {@link LottieMobject.fromUrl}. */
113
+ export interface LottieFromUrlOptions extends Omit<LottieMobjectOptions, 'animationData'> {
114
+ /** Inject a `fetch` implementation (bypasses the global; used by tests). */
115
+ fetchImpl?: typeof fetch;
116
+ }
117
+ export declare class LottieMobject extends TexturedMobject {
118
+ protected _animationData: LottieAnimationData | undefined;
119
+ protected _loop: boolean;
120
+ protected _speed: number;
121
+ protected _width: number | undefined;
122
+ protected _height: number | undefined;
123
+ protected _scaleToFit: boolean;
124
+ protected _centerPoint: Vector3Tuple;
125
+ protected _doubleSided: boolean;
126
+ protected _injectedPlayer: LottiePlayer | undefined;
127
+ protected _canvas: HTMLCanvasElement | null;
128
+ protected _texture: THREE.CanvasTexture | null;
129
+ protected _animation: LottieAnimation | null;
130
+ protected _totalFrames: number;
131
+ protected _frameRate: number;
132
+ protected _naturalWidth: number;
133
+ protected _naturalHeight: number;
134
+ protected _loaded: boolean;
135
+ protected _loadError: Error | null;
136
+ protected _playing: boolean;
137
+ protected _playhead: number;
138
+ private _mesh;
139
+ private _loadPromise;
140
+ private _loadResolve;
141
+ private _loadReject;
142
+ constructor(options: LottieMobjectOptions);
143
+ /**
144
+ * Load a Lottie JSON from a URL and return a ready LottieMobject.
145
+ *
146
+ * The JSON is fetched and parsed, then handed to the player. Inject a player
147
+ * (via the `player` option or {@link setLottiePlayerFactory}) and a `fetchImpl`
148
+ * to avoid the network, e.g. in tests.
149
+ */
150
+ static fromUrl(url: string, options?: LottieFromUrlOptions): Promise<LottieMobject>;
151
+ /**
152
+ * Create the offscreen canvas and its CanvasTexture. Node/SSR-safe: when
153
+ * `document` is missing, the load fails gracefully instead of throwing.
154
+ */
155
+ private _createCanvasTexture;
156
+ /** Resolve the player, load the animation, and show the first frame. */
157
+ private _load;
158
+ /** Record a load error and reject the ready promise once. */
159
+ private _failLoad;
160
+ /** Promise that resolves when the animation is ready and rejects on error. */
161
+ waitForLoad(): Promise<void>;
162
+ /** The captured load error, or null when no error occurred. */
163
+ getLoadError(): Error | null;
164
+ /** Whether the animation is loaded and seekable. */
165
+ isLoaded(): boolean;
166
+ /** Total number of frames in the composition (0 until loaded). */
167
+ getTotalFrames(): number;
168
+ /** Frame rate in frames per second. */
169
+ getFrameRate(): number;
170
+ /** Natural duration of one loop in seconds (0 until loaded). */
171
+ get duration(): number;
172
+ /** The frame index for a scene time, honoring speed and loop. */
173
+ private _frameForTime;
174
+ /** Seek the player to a frame and flag the texture for upload. */
175
+ private _seekFrame;
176
+ /** Show the frame for a scene time without moving the live playhead. */
177
+ private _applyTime;
178
+ /**
179
+ * Follow the Player clock: show the frame at `sceneTime` seconds. Use this on
180
+ * scrub and seek so the displayed frame tracks the timeline position.
181
+ */
182
+ syncToTime(sceneTime: number): this;
183
+ /** Seek to a progress fraction in [0, 1] of the composition. */
184
+ seekToProgress(progress: number): this;
185
+ /** Start live playback from the current playhead (advanced by the updater). */
186
+ play(): this;
187
+ /** Pause live playback. */
188
+ pause(): this;
189
+ /** Set the playback speed multiplier. */
190
+ setSpeed(speed: number): this;
191
+ /** Enable or disable looping. */
192
+ setLoop(loop: boolean): this;
193
+ /** Display dimensions from width/height options and the natural size. */
194
+ private _calculateDimensions;
195
+ protected _createThreeObject(): THREE.Object3D;
196
+ getDisplayMeshLength(): number;
197
+ getDisplayMeshes(): THREE.Mesh[];
198
+ applyTextureFrom(other: TexturedMobject): void;
199
+ applyVisualSize(width: number, height: number): void;
200
+ applyContentFrom(other: TexturedMobject): void;
201
+ protected _updateGeometry(): void;
202
+ protected _syncMaterialToThree(): void;
203
+ getBoundingBox(): {
204
+ width: number;
205
+ height: number;
206
+ depth: number;
207
+ };
208
+ copy(): LottieMobject;
209
+ dispose(): void;
210
+ }
211
+ //# sourceMappingURL=LottieMobject.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"LottieMobject.d.ts","sourceRoot":"","sources":["../../../src/mobjects/lottie/LottieMobject.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAE7D;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH;;;GAGG;AACH,MAAM,WAAW,mBAAmB;IAClC,mCAAmC;IACnC,CAAC,CAAC,EAAE,MAAM,CAAC;IACX,oCAAoC;IACpC,CAAC,CAAC,EAAE,MAAM,CAAC;IACX,uCAAuC;IACvC,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAED;;;GAGG;AACH,MAAM,WAAW,eAAe;IAC9B,iDAAiD;IACjD,WAAW,EAAE,MAAM,CAAC;IACpB,uCAAuC;IACvC,SAAS,EAAE,MAAM,CAAC;IAClB,kEAAkE;IAClE,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,IAAI,CAAC;IACpD,8CAA8C;IAC9C,QAAQ,CAAC,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IAC/B,sCAAsC;IACtC,OAAO,IAAI,IAAI,CAAC;CACjB;AAED,yDAAyD;AACzD,MAAM,WAAW,gBAAgB;IAC/B,iDAAiD;IACjD,SAAS,EAAE,OAAO,CAAC;IACnB,wEAAwE;IACxE,QAAQ,EAA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AAM,EAAE,MAAM,GAAG,IAAI;IAOpD,gBAAgB,CAAC,KAAK,EAAE,eAAe,GAAG,IAAI;IAO9C,SAAS,CAAC,eAAe,IAAI,IAAI;cAad,oBAAoB,IAAI,IAAI;IAYtC,cAAc,IAAI;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE;IASlE,IAAI,IAAI,aAAa;IAsBrB,OAAO,IAAI,IAAI;CAazB"}
@@ -0,0 +1,2 @@
1
+ export { LottieMobject, setLottiePlayerFactory, type LottieMobjectOptions, type LottieFromUrlOptions, type LottieAnimationData, type LottieAnimation, type LottiePlayer, type LottiePlayerFactory, type LottieLoadParams, } from './LottieMobject';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/mobjects/lottie/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,EACb,sBAAsB,EACtB,KAAK,oBAAoB,EACzB,KAAK,oBAAoB,EACzB,KAAK,mBAAmB,EACxB,KAAK,eAAe,EACpB,KAAK,YAAY,EACjB,KAAK,mBAAmB,EACxB,KAAK,gBAAgB,GACtB,MAAM,iBAAiB,CAAC"}