@redwilly/anima 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (239) hide show
  1. package/dist/cli/SceneLoader.d.ts +22 -0
  2. package/dist/cli/SceneLoader.js +47 -0
  3. package/dist/cli/commands/export-frame.d.ts +13 -0
  4. package/dist/cli/commands/export-frame.js +60 -0
  5. package/dist/cli/commands/list-scenes.d.ts +5 -0
  6. package/dist/cli/commands/list-scenes.js +22 -0
  7. package/dist/cli/commands/preview.d.ts +5 -0
  8. package/dist/cli/commands/preview.js +11 -0
  9. package/dist/cli/commands/render.d.ts +16 -0
  10. package/dist/cli/commands/render.js +76 -0
  11. package/dist/cli/index.d.ts +2 -0
  12. package/dist/cli/index.js +63 -0
  13. package/dist/core/animations/Animation.d.ts +41 -0
  14. package/dist/core/animations/Animation.js +76 -0
  15. package/dist/core/animations/camera/Follow.d.ts +70 -0
  16. package/dist/core/animations/camera/Follow.js +69 -0
  17. package/dist/core/animations/camera/Shake.d.ts +90 -0
  18. package/dist/core/animations/camera/Shake.js +87 -0
  19. package/dist/core/animations/camera/index.d.ts +2 -0
  20. package/dist/core/animations/camera/index.js +2 -0
  21. package/dist/core/animations/categories/ExitAnimation.d.ts +17 -0
  22. package/dist/core/animations/categories/ExitAnimation.js +15 -0
  23. package/dist/core/animations/categories/IntroductoryAnimation.d.ts +16 -0
  24. package/dist/core/animations/categories/IntroductoryAnimation.js +14 -0
  25. package/dist/core/animations/categories/TransformativeAnimation.d.ts +25 -0
  26. package/dist/core/animations/categories/TransformativeAnimation.js +25 -0
  27. package/dist/core/animations/categories/index.d.ts +3 -0
  28. package/dist/core/animations/categories/index.js +3 -0
  29. package/dist/core/animations/composition/Parallel.d.ts +37 -0
  30. package/dist/core/animations/composition/Parallel.js +79 -0
  31. package/dist/core/animations/composition/Sequence.d.ts +41 -0
  32. package/dist/core/animations/composition/Sequence.js +95 -0
  33. package/dist/core/animations/composition/index.d.ts +2 -0
  34. package/dist/core/animations/composition/index.js +3 -0
  35. package/dist/core/animations/draw/Draw.d.ts +30 -0
  36. package/dist/core/animations/draw/Draw.js +122 -0
  37. package/dist/core/animations/draw/Unwrite.d.ts +30 -0
  38. package/dist/core/animations/draw/Unwrite.js +120 -0
  39. package/dist/core/animations/draw/Write.d.ts +35 -0
  40. package/dist/core/animations/draw/Write.js +119 -0
  41. package/dist/core/animations/draw/index.d.ts +3 -0
  42. package/dist/core/animations/draw/index.js +3 -0
  43. package/dist/core/animations/draw/partialPath.d.ts +6 -0
  44. package/dist/core/animations/draw/partialPath.js +138 -0
  45. package/dist/core/animations/easing/bounce.d.ts +13 -0
  46. package/dist/core/animations/easing/bounce.js +37 -0
  47. package/dist/core/animations/easing/index.d.ts +7 -0
  48. package/dist/core/animations/easing/index.js +11 -0
  49. package/dist/core/animations/easing/manim.d.ts +46 -0
  50. package/dist/core/animations/easing/manim.js +102 -0
  51. package/dist/core/animations/easing/registry.d.ts +8 -0
  52. package/dist/core/animations/easing/registry.js +25 -0
  53. package/dist/core/animations/easing/standard.d.ts +113 -0
  54. package/dist/core/animations/easing/standard.js +151 -0
  55. package/dist/core/animations/easing/types.d.ts +6 -0
  56. package/dist/core/animations/easing/types.js +0 -0
  57. package/dist/core/animations/fade/FadeIn.d.ts +17 -0
  58. package/dist/core/animations/fade/FadeIn.js +22 -0
  59. package/dist/core/animations/fade/FadeOut.d.ts +17 -0
  60. package/dist/core/animations/fade/FadeOut.js +23 -0
  61. package/dist/core/animations/fade/index.d.ts +2 -0
  62. package/dist/core/animations/fade/index.js +2 -0
  63. package/dist/core/animations/index.d.ts +11 -0
  64. package/dist/core/animations/index.js +17 -0
  65. package/dist/core/animations/keyframes/KeyframeAnimation.d.ts +33 -0
  66. package/dist/core/animations/keyframes/KeyframeAnimation.js +40 -0
  67. package/dist/core/animations/keyframes/KeyframeTrack.d.ts +31 -0
  68. package/dist/core/animations/keyframes/KeyframeTrack.js +83 -0
  69. package/dist/core/animations/keyframes/index.d.ts +4 -0
  70. package/dist/core/animations/keyframes/index.js +5 -0
  71. package/dist/core/animations/keyframes/types.d.ts +25 -0
  72. package/dist/core/animations/keyframes/types.js +6 -0
  73. package/dist/core/animations/morph/MorphTo.d.ts +22 -0
  74. package/dist/core/animations/morph/MorphTo.js +42 -0
  75. package/dist/core/animations/morph/index.d.ts +1 -0
  76. package/dist/core/animations/morph/index.js +1 -0
  77. package/dist/core/animations/transform/MoveTo.d.ts +24 -0
  78. package/dist/core/animations/transform/MoveTo.js +38 -0
  79. package/dist/core/animations/transform/Rotate.d.ts +23 -0
  80. package/dist/core/animations/transform/Rotate.js +34 -0
  81. package/dist/core/animations/transform/Scale.d.ts +23 -0
  82. package/dist/core/animations/transform/Scale.js +35 -0
  83. package/dist/core/animations/transform/index.d.ts +3 -0
  84. package/dist/core/animations/transform/index.js +3 -0
  85. package/dist/core/animations/types.d.ts +52 -0
  86. package/dist/core/animations/types.js +6 -0
  87. package/dist/core/camera/Camera.d.ts +87 -0
  88. package/dist/core/camera/Camera.js +175 -0
  89. package/dist/core/camera/CameraFrame.d.ts +242 -0
  90. package/dist/core/camera/CameraFrame.js +322 -0
  91. package/dist/core/camera/index.d.ts +4 -0
  92. package/dist/core/camera/index.js +3 -0
  93. package/dist/core/camera/types.d.ts +17 -0
  94. package/dist/core/camera/types.js +1 -0
  95. package/dist/core/errors/AnimationErrors.d.ts +12 -0
  96. package/dist/core/errors/AnimationErrors.js +37 -0
  97. package/dist/core/errors/index.d.ts +1 -0
  98. package/dist/core/errors/index.js +1 -0
  99. package/dist/core/math/Vector2/Vector2.d.ts +23 -0
  100. package/dist/core/math/Vector2/Vector2.js +46 -0
  101. package/dist/core/math/Vector2/index.d.ts +1 -0
  102. package/dist/core/math/Vector2/index.js +1 -0
  103. package/dist/core/math/bezier/BezierPath.d.ts +38 -0
  104. package/dist/core/math/bezier/BezierPath.js +264 -0
  105. package/dist/core/math/bezier/evaluators.d.ts +9 -0
  106. package/dist/core/math/bezier/evaluators.js +36 -0
  107. package/dist/core/math/bezier/index.d.ts +8 -0
  108. package/dist/core/math/bezier/index.js +6 -0
  109. package/dist/core/math/bezier/length.d.ts +5 -0
  110. package/dist/core/math/bezier/length.js +27 -0
  111. package/dist/core/math/bezier/morphing.d.ts +16 -0
  112. package/dist/core/math/bezier/morphing.js +151 -0
  113. package/dist/core/math/bezier/sampling.d.ts +7 -0
  114. package/dist/core/math/bezier/sampling.js +153 -0
  115. package/dist/core/math/bezier/split.d.ts +19 -0
  116. package/dist/core/math/bezier/split.js +44 -0
  117. package/dist/core/math/bezier/types.d.ts +8 -0
  118. package/dist/core/math/bezier/types.js +0 -0
  119. package/dist/core/math/color/Color.d.ts +28 -0
  120. package/dist/core/math/color/Color.js +60 -0
  121. package/dist/core/math/color/conversions.d.ts +17 -0
  122. package/dist/core/math/color/conversions.js +100 -0
  123. package/dist/core/math/color/index.d.ts +2 -0
  124. package/dist/core/math/color/index.js +2 -0
  125. package/dist/core/math/index.d.ts +4 -0
  126. package/dist/core/math/index.js +5 -0
  127. package/dist/core/math/matrix/Matrix3x3.d.ts +23 -0
  128. package/dist/core/math/matrix/Matrix3x3.js +91 -0
  129. package/dist/core/math/matrix/factories.d.ts +12 -0
  130. package/dist/core/math/matrix/factories.js +44 -0
  131. package/dist/core/math/matrix/index.d.ts +2 -0
  132. package/dist/core/math/matrix/index.js +2 -0
  133. package/dist/core/renderer/FrameRenderer.d.ts +37 -0
  134. package/dist/core/renderer/FrameRenderer.js +75 -0
  135. package/dist/core/renderer/ProgressReporter.d.ts +19 -0
  136. package/dist/core/renderer/ProgressReporter.js +58 -0
  137. package/dist/core/renderer/Renderer.d.ts +36 -0
  138. package/dist/core/renderer/Renderer.js +102 -0
  139. package/dist/core/renderer/drawMobject.d.ts +8 -0
  140. package/dist/core/renderer/drawMobject.js +109 -0
  141. package/dist/core/renderer/formats/index.d.ts +3 -0
  142. package/dist/core/renderer/formats/index.js +3 -0
  143. package/dist/core/renderer/formats/png.d.ts +5 -0
  144. package/dist/core/renderer/formats/png.js +7 -0
  145. package/dist/core/renderer/formats/sprite.d.ts +6 -0
  146. package/dist/core/renderer/formats/sprite.js +24 -0
  147. package/dist/core/renderer/formats/video.d.ts +8 -0
  148. package/dist/core/renderer/formats/video.js +51 -0
  149. package/dist/core/renderer/index.d.ts +7 -0
  150. package/dist/core/renderer/index.js +9 -0
  151. package/dist/core/renderer/types.d.ts +87 -0
  152. package/dist/core/renderer/types.js +13 -0
  153. package/dist/core/scene/Scene.d.ts +104 -0
  154. package/dist/core/scene/Scene.js +225 -0
  155. package/dist/core/scene/index.d.ts +2 -0
  156. package/dist/core/scene/index.js +1 -0
  157. package/dist/core/scene/types.d.ts +23 -0
  158. package/dist/core/scene/types.js +0 -0
  159. package/dist/core/serialization/animation.d.ts +23 -0
  160. package/dist/core/serialization/animation.js +176 -0
  161. package/dist/core/serialization/easingLookup.d.ts +13 -0
  162. package/dist/core/serialization/easingLookup.js +65 -0
  163. package/dist/core/serialization/index.d.ts +23 -0
  164. package/dist/core/serialization/index.js +29 -0
  165. package/dist/core/serialization/mobject.d.ts +23 -0
  166. package/dist/core/serialization/mobject.js +248 -0
  167. package/dist/core/serialization/prettyPrint.d.ts +12 -0
  168. package/dist/core/serialization/prettyPrint.js +16 -0
  169. package/dist/core/serialization/primitives.d.ts +24 -0
  170. package/dist/core/serialization/primitives.js +98 -0
  171. package/dist/core/serialization/registry.d.ts +29 -0
  172. package/dist/core/serialization/registry.js +39 -0
  173. package/dist/core/serialization/scene.d.ts +28 -0
  174. package/dist/core/serialization/scene.js +114 -0
  175. package/dist/core/serialization/types.d.ts +152 -0
  176. package/dist/core/serialization/types.js +6 -0
  177. package/dist/core/timeline/Timeline.d.ts +70 -0
  178. package/dist/core/timeline/Timeline.js +144 -0
  179. package/dist/core/timeline/index.d.ts +5 -0
  180. package/dist/core/timeline/index.js +4 -0
  181. package/dist/core/timeline/types.d.ts +29 -0
  182. package/dist/core/timeline/types.js +0 -0
  183. package/dist/index.d.ts +18 -0
  184. package/dist/index.js +22 -0
  185. package/dist/mobjects/Mobject.d.ts +98 -0
  186. package/dist/mobjects/Mobject.js +343 -0
  187. package/dist/mobjects/VGroup/VGroup.d.ts +51 -0
  188. package/dist/mobjects/VGroup/VGroup.js +142 -0
  189. package/dist/mobjects/VGroup/index.d.ts +3 -0
  190. package/dist/mobjects/VGroup/index.js +2 -0
  191. package/dist/mobjects/VGroup/layout.d.ts +20 -0
  192. package/dist/mobjects/VGroup/layout.js +139 -0
  193. package/dist/mobjects/VMobject.d.ts +106 -0
  194. package/dist/mobjects/VMobject.js +216 -0
  195. package/dist/mobjects/geometry/Arc.d.ts +8 -0
  196. package/dist/mobjects/geometry/Arc.js +46 -0
  197. package/dist/mobjects/geometry/Arrow.d.ts +7 -0
  198. package/dist/mobjects/geometry/Arrow.js +34 -0
  199. package/dist/mobjects/geometry/Circle.d.ts +4 -0
  200. package/dist/mobjects/geometry/Circle.js +10 -0
  201. package/dist/mobjects/geometry/Line.d.ts +8 -0
  202. package/dist/mobjects/geometry/Line.js +19 -0
  203. package/dist/mobjects/geometry/Point.d.ts +5 -0
  204. package/dist/mobjects/geometry/Point.js +11 -0
  205. package/dist/mobjects/geometry/Polygon.d.ts +7 -0
  206. package/dist/mobjects/geometry/Polygon.js +21 -0
  207. package/dist/mobjects/geometry/Rectangle.d.ts +6 -0
  208. package/dist/mobjects/geometry/Rectangle.js +18 -0
  209. package/dist/mobjects/geometry/index.d.ts +7 -0
  210. package/dist/mobjects/geometry/index.js +7 -0
  211. package/dist/mobjects/graph/Graph.d.ts +28 -0
  212. package/dist/mobjects/graph/Graph.js +119 -0
  213. package/dist/mobjects/graph/GraphEdge.d.ts +26 -0
  214. package/dist/mobjects/graph/GraphEdge.js +64 -0
  215. package/dist/mobjects/graph/GraphNode.d.ts +19 -0
  216. package/dist/mobjects/graph/GraphNode.js +63 -0
  217. package/dist/mobjects/graph/index.d.ts +5 -0
  218. package/dist/mobjects/graph/index.js +5 -0
  219. package/dist/mobjects/graph/layouts/circular.d.ts +8 -0
  220. package/dist/mobjects/graph/layouts/circular.js +23 -0
  221. package/dist/mobjects/graph/layouts/forceDirected.d.ts +9 -0
  222. package/dist/mobjects/graph/layouts/forceDirected.js +102 -0
  223. package/dist/mobjects/graph/layouts/index.d.ts +3 -0
  224. package/dist/mobjects/graph/layouts/index.js +3 -0
  225. package/dist/mobjects/graph/layouts/tree.d.ts +9 -0
  226. package/dist/mobjects/graph/layouts/tree.js +99 -0
  227. package/dist/mobjects/graph/types.d.ts +35 -0
  228. package/dist/mobjects/graph/types.js +0 -0
  229. package/dist/mobjects/index.d.ts +6 -0
  230. package/dist/mobjects/index.js +6 -0
  231. package/dist/mobjects/text/Glyph.d.ts +11 -0
  232. package/dist/mobjects/text/Glyph.js +72 -0
  233. package/dist/mobjects/text/Text.d.ts +19 -0
  234. package/dist/mobjects/text/Text.js +76 -0
  235. package/dist/mobjects/text/index.d.ts +4 -0
  236. package/dist/mobjects/text/index.js +3 -0
  237. package/dist/mobjects/text/types.d.ts +12 -0
  238. package/dist/mobjects/text/types.js +8 -0
  239. package/package.json +51 -0
@@ -0,0 +1,104 @@
1
+ import { Color } from '../math/color/Color';
2
+ import { Timeline } from '../timeline';
3
+ import { Camera, CameraFrame } from '../camera';
4
+ import { Mobject } from '../../mobjects/Mobject';
5
+ import type { Animation } from '../animations/Animation';
6
+ import type { SceneConfig } from './types';
7
+ /**
8
+ * Scene is the core container that manages Mobjects and coordinates animations.
9
+ * It provides both a simple API for playing animations and access to the
10
+ * underlying Timeline and Camera for advanced control.
11
+ */
12
+ export declare class Scene {
13
+ private readonly config;
14
+ private readonly mobjects;
15
+ private readonly timeline;
16
+ private readonly _camera;
17
+ private playheadTime;
18
+ constructor(config?: SceneConfig);
19
+ get camera(): Camera;
20
+ get frame(): CameraFrame;
21
+ /** Get scene width in pixels. */
22
+ getWidth(): number;
23
+ /** Get scene height in pixels. */
24
+ getHeight(): number;
25
+ /** Get scene background color. */
26
+ getBackgroundColor(): Color;
27
+ /** Get scene frame rate. */
28
+ getFrameRate(): number;
29
+ /**
30
+ * Add mobjects to the scene and make them immediately visible.
31
+ * Use this for static elements or backgrounds that should be visible
32
+ * before any animations begin.
33
+ */
34
+ add(...mobjects: Mobject[]): this;
35
+ /**
36
+ * Remove mobjects from the scene.
37
+ */
38
+ remove(...mobjects: Mobject[]): this;
39
+ /**
40
+ * Check if a mobject is registered with this scene.
41
+ */
42
+ has(mobject: Mobject): boolean;
43
+ /**
44
+ * Get all mobjects in the scene.
45
+ */
46
+ getMobjects(): readonly Mobject[];
47
+ /**
48
+ * Schedule animations to play at the current playhead position.
49
+ *
50
+ * Accepts either Animation objects or Mobjects with queued fluent animations.
51
+ * When a Mobject is passed, its queued animation chain is automatically extracted.
52
+ *
53
+ * - Introductory animations (FadeIn, Create, Draw, Write) auto-register
54
+ * their targets with the scene if not already present.
55
+ * - Transformative animations (MoveTo, Rotate, Scale) require the target
56
+ * to already be in the scene, otherwise an error is thrown.
57
+ *
58
+ * @example
59
+ * // ProAPI style
60
+ * this.play(new FadeIn(circle), new MoveTo(rect, 2, 0));
61
+ *
62
+ * // FluentAPI style
63
+ * circle.fadeIn(1).moveTo(2, 0, 1);
64
+ * this.play(circle);
65
+ *
66
+ * // Mixed
67
+ * circle.fadeIn(1);
68
+ * this.play(circle, new FadeIn(rect));
69
+ */
70
+ play(...items: Array<Animation | Mobject>): this;
71
+ /**
72
+ * Add a delay before the next play() call.
73
+ * @param seconds Number of seconds to wait
74
+ */
75
+ wait(seconds: number): this;
76
+ /**
77
+ * Get the current playhead time.
78
+ */
79
+ getCurrentTime(): number;
80
+ /**
81
+ * Get the total duration of all scheduled animations.
82
+ */
83
+ getTotalDuration(): number;
84
+ /**
85
+ * Get the underlying Timeline for advanced control.
86
+ * Use this for direct manipulation of animation timing.
87
+ */
88
+ getTimeline(): Timeline;
89
+ /**
90
+ * Get the Camera for view control and frame dimensions.
91
+ * Camera calculates Manim-compatible frame dimensions from pixel resolution.
92
+ */
93
+ getCamera(): Camera;
94
+ /**
95
+ * Validates and registers animation targets based on lifecycle.
96
+ * Handles composition animations (Sequence, Parallel) by processing children.
97
+ */
98
+ private validateAndRegisterAnimation;
99
+ /**
100
+ * Gets children of composition animations (Sequence, Parallel).
101
+ * Returns empty array for non-composition animations.
102
+ */
103
+ private getAnimationChildren;
104
+ }
@@ -0,0 +1,225 @@
1
+ import { Color } from '../math/color/Color';
2
+ import { Timeline } from '../timeline';
3
+ import { Camera, CameraFrame } from '../camera';
4
+ import { Mobject } from '../../mobjects/Mobject';
5
+ import { AnimationTargetNotInSceneError } from '../errors';
6
+ /**
7
+ * Scene is the core container that manages Mobjects and coordinates animations.
8
+ * It provides both a simple API for playing animations and access to the
9
+ * underlying Timeline and Camera for advanced control.
10
+ */
11
+ export class Scene {
12
+ config;
13
+ mobjects = new Set();
14
+ timeline;
15
+ _camera;
16
+ playheadTime = 0;
17
+ constructor(config = {}) {
18
+ this.config = {
19
+ width: config.width ?? 1920,
20
+ height: config.height ?? 1080,
21
+ backgroundColor: config.backgroundColor ?? Color.BLACK,
22
+ frameRate: config.frameRate ?? 60,
23
+ };
24
+ this.timeline = new Timeline();
25
+ this._camera = new Camera({
26
+ pixelWidth: this.config.width,
27
+ pixelHeight: this.config.height,
28
+ });
29
+ }
30
+ // ========== Camera Shortcuts ==========
31
+ get camera() {
32
+ return this._camera;
33
+ }
34
+ get frame() {
35
+ return this._camera.frame;
36
+ }
37
+ // ========== Configuration Getters ==========
38
+ /** Get scene width in pixels. */
39
+ getWidth() {
40
+ return this.config.width;
41
+ }
42
+ /** Get scene height in pixels. */
43
+ getHeight() {
44
+ return this.config.height;
45
+ }
46
+ /** Get scene background color. */
47
+ getBackgroundColor() {
48
+ return this.config.backgroundColor;
49
+ }
50
+ /** Get scene frame rate. */
51
+ getFrameRate() {
52
+ return this.config.frameRate;
53
+ }
54
+ // ========== Mobject Management ==========
55
+ /**
56
+ * Add mobjects to the scene and make them immediately visible.
57
+ * Use this for static elements or backgrounds that should be visible
58
+ * before any animations begin.
59
+ */
60
+ add(...mobjects) {
61
+ for (const m of mobjects) {
62
+ this.mobjects.add(m);
63
+ m.setOpacity(1); // Immediately visible
64
+ }
65
+ return this;
66
+ }
67
+ /**
68
+ * Remove mobjects from the scene.
69
+ */
70
+ remove(...mobjects) {
71
+ for (const m of mobjects) {
72
+ this.mobjects.delete(m);
73
+ }
74
+ return this;
75
+ }
76
+ /**
77
+ * Check if a mobject is registered with this scene.
78
+ */
79
+ has(mobject) {
80
+ return this.mobjects.has(mobject);
81
+ }
82
+ /**
83
+ * Get all mobjects in the scene.
84
+ */
85
+ getMobjects() {
86
+ return [...this.mobjects];
87
+ }
88
+ // ========== Animation Scheduling ==========
89
+ /**
90
+ * Schedule animations to play at the current playhead position.
91
+ *
92
+ * Accepts either Animation objects or Mobjects with queued fluent animations.
93
+ * When a Mobject is passed, its queued animation chain is automatically extracted.
94
+ *
95
+ * - Introductory animations (FadeIn, Create, Draw, Write) auto-register
96
+ * their targets with the scene if not already present.
97
+ * - Transformative animations (MoveTo, Rotate, Scale) require the target
98
+ * to already be in the scene, otherwise an error is thrown.
99
+ *
100
+ * @example
101
+ * // ProAPI style
102
+ * this.play(new FadeIn(circle), new MoveTo(rect, 2, 0));
103
+ *
104
+ * // FluentAPI style
105
+ * circle.fadeIn(1).moveTo(2, 0, 1);
106
+ * this.play(circle);
107
+ *
108
+ * // Mixed
109
+ * circle.fadeIn(1);
110
+ * this.play(circle, new FadeIn(rect));
111
+ */
112
+ play(...items) {
113
+ if (items.length === 0) {
114
+ return this;
115
+ }
116
+ // Convert Mobjects to Animations
117
+ const animations = items.map(item => {
118
+ if (item instanceof Mobject) {
119
+ return item.toAnimation();
120
+ }
121
+ return item;
122
+ });
123
+ // Validate and auto-register based on animation lifecycle
124
+ for (const anim of animations) {
125
+ this.validateAndRegisterAnimation(anim);
126
+ }
127
+ this.timeline.scheduleParallel(animations, this.playheadTime);
128
+ // Advance playhead to end of longest animation
129
+ let maxDuration = 0;
130
+ for (const anim of animations) {
131
+ const totalTime = anim.getDuration() + anim.getDelay();
132
+ if (totalTime > maxDuration) {
133
+ maxDuration = totalTime;
134
+ }
135
+ }
136
+ this.playheadTime += maxDuration;
137
+ return this;
138
+ }
139
+ /**
140
+ * Add a delay before the next play() call.
141
+ * @param seconds Number of seconds to wait
142
+ */
143
+ wait(seconds) {
144
+ if (seconds < 0) {
145
+ throw new Error('Wait duration must be non-negative');
146
+ }
147
+ this.playheadTime += seconds;
148
+ return this;
149
+ }
150
+ /**
151
+ * Get the current playhead time.
152
+ */
153
+ getCurrentTime() {
154
+ return this.playheadTime;
155
+ }
156
+ /**
157
+ * Get the total duration of all scheduled animations.
158
+ */
159
+ getTotalDuration() {
160
+ return this.timeline.getTotalDuration();
161
+ }
162
+ // ========== ProAPI Access ==========
163
+ /**
164
+ * Get the underlying Timeline for advanced control.
165
+ * Use this for direct manipulation of animation timing.
166
+ */
167
+ getTimeline() {
168
+ return this.timeline;
169
+ }
170
+ /**
171
+ * Get the Camera for view control and frame dimensions.
172
+ * Camera calculates Manim-compatible frame dimensions from pixel resolution.
173
+ */
174
+ getCamera() {
175
+ return this._camera;
176
+ }
177
+ // ========== Private Helpers ==========
178
+ /**
179
+ * Validates and registers animation targets based on lifecycle.
180
+ * Handles composition animations (Sequence, Parallel) by processing children.
181
+ */
182
+ validateAndRegisterAnimation(anim) {
183
+ // Check if this is a composition animation with children
184
+ const children = this.getAnimationChildren(anim);
185
+ if (children.length > 0) {
186
+ // For composition animations, process each child
187
+ for (const child of children) {
188
+ this.validateAndRegisterAnimation(child);
189
+ }
190
+ return;
191
+ }
192
+ // Regular animation - validate/register based on lifecycle
193
+ const target = anim.getTarget();
194
+ switch (anim.lifecycle) {
195
+ case 'introductory':
196
+ // Auto-register if not already in scene
197
+ if (!this.mobjects.has(target)) {
198
+ this.mobjects.add(target);
199
+ }
200
+ break;
201
+ case 'transformative':
202
+ case 'exit':
203
+ // CameraFrame is exempt - it's owned by Camera, not added to scene
204
+ if (target instanceof CameraFrame) {
205
+ break;
206
+ }
207
+ // Validate object is in scene
208
+ if (!this.mobjects.has(target)) {
209
+ throw new AnimationTargetNotInSceneError(anim, target);
210
+ }
211
+ break;
212
+ }
213
+ }
214
+ /**
215
+ * Gets children of composition animations (Sequence, Parallel).
216
+ * Returns empty array for non-composition animations.
217
+ */
218
+ getAnimationChildren(anim) {
219
+ // Check for getChildren method (Sequence, Parallel have this)
220
+ if ('getChildren' in anim && typeof anim.getChildren === 'function') {
221
+ return anim.getChildren();
222
+ }
223
+ return [];
224
+ }
225
+ }
@@ -0,0 +1,2 @@
1
+ export { Scene } from './Scene';
2
+ export type { SceneConfig, ResolvedSceneConfig } from './types';
@@ -0,0 +1 @@
1
+ export { Scene } from './Scene';
@@ -0,0 +1,23 @@
1
+ import type { Color } from '../math/color/Color';
2
+ /**
3
+ * Configuration options for Scene.
4
+ */
5
+ export interface SceneConfig {
6
+ /** Pixel width of the scene. Default: 1920 */
7
+ readonly width?: number;
8
+ /** Pixel height of the scene. Default: 1080 */
9
+ readonly height?: number;
10
+ /** Background color. Default: BLACK */
11
+ readonly backgroundColor?: Color;
12
+ /** Frames per second. Default: 60 */
13
+ readonly frameRate?: number;
14
+ }
15
+ /**
16
+ * Resolved scene configuration with all defaults applied.
17
+ */
18
+ export interface ResolvedSceneConfig {
19
+ readonly width: number;
20
+ readonly height: number;
21
+ readonly backgroundColor: Color;
22
+ readonly frameRate: number;
23
+ }
File without changes
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Animation serialization.
3
+ * Note: Animations reference Mobjects, so we serialize target IDs.
4
+ */
5
+ import type { Animation } from '../animations/Animation';
6
+ import { Mobject } from '../../mobjects/Mobject';
7
+ import type { SerializedAnimation } from './types';
8
+ type MobjectRegistry = Map<string, Mobject>;
9
+ /**
10
+ * Serializes an animation instance to a plain object.
11
+ *
12
+ * @param anim The animation to serialize
13
+ * @param getMobjectId Callback to resolve mobject references to stable IDs
14
+ */
15
+ export declare function serializeAnimation(anim: Animation, getMobjectId: (m: Mobject) => string): SerializedAnimation;
16
+ /**
17
+ * Restores an animation instance from serialized data.
18
+ *
19
+ * @param data The serialized animation data
20
+ * @param registry Map of mobject IDs to restored instances for resolving targets
21
+ */
22
+ export declare function deserializeAnimation(data: SerializedAnimation, registry: MobjectRegistry): Animation;
23
+ export {};
@@ -0,0 +1,176 @@
1
+ /**
2
+ * Animation serialization.
3
+ * Note: Animations reference Mobjects, so we serialize target IDs.
4
+ */
5
+ import { FadeIn, FadeOut } from '../animations/fade';
6
+ import { MoveTo, Rotate, Scale } from '../animations/transform';
7
+ import { MorphTo } from '../animations/morph';
8
+ import { Draw, Write, Unwrite } from '../animations/draw';
9
+ import { Sequence, Parallel } from '../animations/composition';
10
+ import { serializeVector2 } from './primitives';
11
+ import { getEasingName, getEasingByName } from './easingLookup';
12
+ // ========== Animation Config Serialization ==========
13
+ function serializeAnimationConfig(anim) {
14
+ return {
15
+ durationSeconds: anim.getDuration(),
16
+ delaySeconds: anim.getDelay(),
17
+ easingName: getEasingName(anim.getEasing()),
18
+ };
19
+ }
20
+ /**
21
+ * Maps animation instance to serializable type name.
22
+ */
23
+ function getAnimationType(anim) {
24
+ if (anim instanceof FadeIn)
25
+ return 'FadeIn';
26
+ if (anim instanceof FadeOut)
27
+ return 'FadeOut';
28
+ if (anim instanceof MoveTo)
29
+ return 'MoveTo';
30
+ if (anim instanceof Rotate)
31
+ return 'Rotate';
32
+ if (anim instanceof Scale)
33
+ return 'Scale';
34
+ if (anim instanceof MorphTo)
35
+ return 'MorphTo';
36
+ if (anim instanceof Draw)
37
+ return 'Draw';
38
+ if (anim instanceof Write)
39
+ return 'Write';
40
+ if (anim instanceof Unwrite)
41
+ return 'Unwrite';
42
+ if (anim instanceof Sequence)
43
+ return 'Sequence';
44
+ if (anim instanceof Parallel)
45
+ return 'Parallel';
46
+ return 'Unknown';
47
+ }
48
+ // ========== Main Serialization ==========
49
+ /**
50
+ * Serializes an animation instance to a plain object.
51
+ *
52
+ * @param anim The animation to serialize
53
+ * @param getMobjectId Callback to resolve mobject references to stable IDs
54
+ */
55
+ export function serializeAnimation(anim, getMobjectId) {
56
+ const baseData = {
57
+ type: getAnimationType(anim),
58
+ targetId: getMobjectId(anim.getTarget()),
59
+ config: serializeAnimationConfig(anim),
60
+ };
61
+ // Type-specific data
62
+ if (anim instanceof MoveTo) {
63
+ const moveData = {
64
+ ...baseData,
65
+ type: 'MoveTo',
66
+ destination: serializeVector2(anim.getDestination()),
67
+ };
68
+ return moveData;
69
+ }
70
+ if (anim instanceof Rotate) {
71
+ const rotateData = {
72
+ ...baseData,
73
+ type: 'Rotate',
74
+ angle: anim.getAngle(),
75
+ };
76
+ return rotateData;
77
+ }
78
+ if (anim instanceof Scale) {
79
+ const scaleData = {
80
+ ...baseData,
81
+ type: 'Scale',
82
+ factor: anim.getFactor(),
83
+ };
84
+ return scaleData;
85
+ }
86
+ if (anim instanceof Sequence) {
87
+ const seqData = {
88
+ ...baseData,
89
+ type: 'Sequence',
90
+ animations: anim.getChildren().map((a) => serializeAnimation(a, getMobjectId)),
91
+ };
92
+ return seqData;
93
+ }
94
+ if (anim instanceof Parallel) {
95
+ const parData = {
96
+ ...baseData,
97
+ type: 'Parallel',
98
+ animations: anim.getChildren().map((a) => serializeAnimation(a, getMobjectId)),
99
+ };
100
+ return parData;
101
+ }
102
+ return baseData;
103
+ }
104
+ // ========== Deserialization ==========
105
+ function applyConfig(anim, config) {
106
+ anim.duration(config.durationSeconds);
107
+ anim.delay(config.delaySeconds);
108
+ const easing = getEasingByName(config.easingName);
109
+ if (easing) {
110
+ anim.ease(easing);
111
+ }
112
+ }
113
+ /**
114
+ * Restores an animation instance from serialized data.
115
+ *
116
+ * @param data The serialized animation data
117
+ * @param registry Map of mobject IDs to restored instances for resolving targets
118
+ */
119
+ export function deserializeAnimation(data, registry) {
120
+ const target = registry.get(data.targetId);
121
+ if (!target) {
122
+ throw new Error(`Mobject not found: ${data.targetId}`);
123
+ }
124
+ let anim;
125
+ switch (data.type) {
126
+ case 'FadeIn':
127
+ anim = new FadeIn(target);
128
+ break;
129
+ case 'FadeOut':
130
+ anim = new FadeOut(target);
131
+ break;
132
+ case 'MoveTo': {
133
+ const d = data;
134
+ anim = new MoveTo(target, d.destination.x, d.destination.y);
135
+ break;
136
+ }
137
+ case 'Rotate': {
138
+ const d = data;
139
+ anim = new Rotate(target, d.angle);
140
+ break;
141
+ }
142
+ case 'Scale': {
143
+ const d = data;
144
+ anim = new Scale(target, d.factor);
145
+ break;
146
+ }
147
+ case 'Create':
148
+ anim = new Write(target);
149
+ break;
150
+ case 'Draw':
151
+ anim = new Draw(target);
152
+ break;
153
+ case 'Write':
154
+ anim = new Write(target);
155
+ break;
156
+ case 'Unwrite':
157
+ anim = new Unwrite(target);
158
+ break;
159
+ case 'Sequence': {
160
+ const d = data;
161
+ const children = d.animations.map(a => deserializeAnimation(a, registry));
162
+ anim = new Sequence(children);
163
+ break;
164
+ }
165
+ case 'Parallel': {
166
+ const d = data;
167
+ const children = d.animations.map(a => deserializeAnimation(a, registry));
168
+ anim = new Parallel(children);
169
+ break;
170
+ }
171
+ default:
172
+ throw new Error(`Unknown animation type: ${data.type}`);
173
+ }
174
+ applyConfig(anim, data.config);
175
+ return anim;
176
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Easing function lookup for serialization.
3
+ * Maps easing functions to names and vice versa.
4
+ */
5
+ import type { EasingFunction } from '../animations/easing';
6
+ /**
7
+ * Resolves the name of an easing function for serialization.
8
+ */
9
+ export declare function getEasingName(fn: EasingFunction): string;
10
+ /**
11
+ * Resolves an easing function by its serialized name.
12
+ */
13
+ export declare function getEasingByName(name: string): EasingFunction | undefined;
@@ -0,0 +1,65 @@
1
+ /**
2
+ * Easing function lookup for serialization.
3
+ * Maps easing functions to names and vice versa.
4
+ */
5
+ import { linear, easeInQuad, easeOutQuad, easeInOutQuad, easeInCubic, easeOutCubic, easeInOutCubic, easeInQuart, easeOutQuart, easeInOutQuart, easeInQuint, easeOutQuint, easeInOutQuint, easeInSine, easeOutSine, easeInOutSine, easeInExpo, easeOutExpo, easeInOutExpo, easeInCirc, easeOutCirc, easeInOutCirc, easeInBack, easeOutBack, easeInOutBack, easeInElastic, easeOutElastic, easeInOutElastic, easeInBounce, easeOutBounce, easeInOutBounce, smooth, doubleSmooth, rushInto, rushFrom, slowInto, thereAndBack, wiggle, lingering, runningStart, } from '../animations/easing';
6
+ // Mapping from function reference to name
7
+ const easingToName = new Map();
8
+ const nameToEasing = new Map();
9
+ function register(name, fn) {
10
+ easingToName.set(fn, name);
11
+ nameToEasing.set(name, fn);
12
+ }
13
+ // Register all built-in easing functions
14
+ register('linear', linear);
15
+ register('easeInQuad', easeInQuad);
16
+ register('easeOutQuad', easeOutQuad);
17
+ register('easeInOutQuad', easeInOutQuad);
18
+ register('easeInCubic', easeInCubic);
19
+ register('easeOutCubic', easeOutCubic);
20
+ register('easeInOutCubic', easeInOutCubic);
21
+ register('easeInQuart', easeInQuart);
22
+ register('easeOutQuart', easeOutQuart);
23
+ register('easeInOutQuart', easeInOutQuart);
24
+ register('easeInQuint', easeInQuint);
25
+ register('easeOutQuint', easeOutQuint);
26
+ register('easeInOutQuint', easeInOutQuint);
27
+ register('easeInSine', easeInSine);
28
+ register('easeOutSine', easeOutSine);
29
+ register('easeInOutSine', easeInOutSine);
30
+ register('easeInExpo', easeInExpo);
31
+ register('easeOutExpo', easeOutExpo);
32
+ register('easeInOutExpo', easeInOutExpo);
33
+ register('easeInCirc', easeInCirc);
34
+ register('easeOutCirc', easeOutCirc);
35
+ register('easeInOutCirc', easeInOutCirc);
36
+ register('easeInBack', easeInBack);
37
+ register('easeOutBack', easeOutBack);
38
+ register('easeInOutBack', easeInOutBack);
39
+ register('easeInElastic', easeInElastic);
40
+ register('easeOutElastic', easeOutElastic);
41
+ register('easeInOutElastic', easeInOutElastic);
42
+ register('easeInBounce', easeInBounce);
43
+ register('easeOutBounce', easeOutBounce);
44
+ register('easeInOutBounce', easeInOutBounce);
45
+ register('smooth', smooth);
46
+ register('doubleSmooth', doubleSmooth);
47
+ register('rushInto', rushInto);
48
+ register('rushFrom', rushFrom);
49
+ register('slowInto', slowInto);
50
+ register('thereAndBack', thereAndBack);
51
+ register('wiggle', wiggle);
52
+ register('lingering', lingering());
53
+ register('runningStart', runningStart());
54
+ /**
55
+ * Resolves the name of an easing function for serialization.
56
+ */
57
+ export function getEasingName(fn) {
58
+ return easingToName.get(fn) ?? 'smooth';
59
+ }
60
+ /**
61
+ * Resolves an easing function by its serialized name.
62
+ */
63
+ export function getEasingByName(name) {
64
+ return nameToEasing.get(name);
65
+ }
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Serialization module for the Anima animation engine.
3
+ *
4
+ * This module provides functionality to convert Scenes, Mobjects, and Animations
5
+ * into JSON-compatible objects and back, enabling project persistence, undo/redo,
6
+ * and cross-process communication.
7
+ *
8
+ * Example usage:
9
+ * ```typescript
10
+ * import { serialize, deserialize } from './serialization';
11
+ *
12
+ * const json = serialize(scene);
13
+ * const restoredScene = deserialize(json);
14
+ * ```
15
+ */
16
+ export { serialize, deserialize, serializeScene, deserializeScene } from './scene';
17
+ export { prettyPrint, prettyPrintCompact } from './prettyPrint';
18
+ export { serializeMobject, deserializeMobject, resetIdCounter } from './mobject';
19
+ export { serializeAnimation, deserializeAnimation } from './animation';
20
+ export { serializeVector2, deserializeVector2, serializeMatrix3x3, deserializeMatrix3x3, serializeColor, deserializeColor, serializeBezierPath, deserializeBezierPath, } from './primitives';
21
+ export { registerSerializer, getSerializer, hasSerializer, unregisterSerializer, clearSerializerRegistry, } from './registry';
22
+ export { getEasingName, getEasingByName } from './easingLookup';
23
+ export type { SerializedVector2, SerializedColor, SerializedMatrix3x3, SerializedPathCommand, SerializedBezierPath, SerializedMobject, SerializedVMobject, SerializedVGroup, SerializedCircle, SerializedRectangle, SerializedLine, SerializedArc, SerializedPolygon, SerializedAnimation, SerializedAnimationConfig, SerializedMoveTo, SerializedRotate, SerializedScale, SerializedSequence, SerializedParallel, SerializedScheduledAnimation, SerializedTimeline, SerializedSceneConfig, SerializedScene, MobjectType, AnimationType, CustomSerializer, } from './types';