@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,29 @@
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
+ // Main entry points
17
+ export { serialize, deserialize, serializeScene, deserializeScene } from './scene';
18
+ // Pretty printing
19
+ export { prettyPrint, prettyPrintCompact } from './prettyPrint';
20
+ // Mobject serialization
21
+ export { serializeMobject, deserializeMobject, resetIdCounter } from './mobject';
22
+ // Animation serialization
23
+ export { serializeAnimation, deserializeAnimation } from './animation';
24
+ // Primitive serialization
25
+ export { serializeVector2, deserializeVector2, serializeMatrix3x3, deserializeMatrix3x3, serializeColor, deserializeColor, serializeBezierPath, deserializeBezierPath, } from './primitives';
26
+ // Custom serializer registry
27
+ export { registerSerializer, getSerializer, hasSerializer, unregisterSerializer, clearSerializerRegistry, } from './registry';
28
+ // Easing lookup
29
+ export { getEasingName, getEasingByName } from './easingLookup';
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Mobject and VMobject serialization.
3
+ */
4
+ import { Mobject } from '../../mobjects/Mobject';
5
+ import type { SerializedMobject } from './types';
6
+ /**
7
+ * Reset ID counter (useful for testing).
8
+ */
9
+ export declare function resetIdCounter(): void;
10
+ /**
11
+ * Serializes a Mobject or any of its subclasses to a plain object.
12
+ *
13
+ * This function handles built-in types like Circle, Rectangle, etc.,
14
+ * and also checks the custom serializer registry for user-defined types.
15
+ */
16
+ export declare function serializeMobject(m: Mobject): SerializedMobject;
17
+ /**
18
+ * Restores a Mobject or subclass instance from serialized data.
19
+ *
20
+ * Uses the 'type' field to determine the correct class to instantiate
21
+ * and restores its state including transformations and styles.
22
+ */
23
+ export declare function deserializeMobject(data: SerializedMobject): Mobject;
@@ -0,0 +1,248 @@
1
+ /**
2
+ * Mobject and VMobject serialization.
3
+ */
4
+ import { VMobject } from '../../mobjects/VMobject';
5
+ import { VGroup } from '../../mobjects/VGroup/VGroup';
6
+ import { Circle, Rectangle, Line, Arrow, Arc, Polygon, Point } from '../../mobjects/geometry';
7
+ import { serializeMatrix3x3, deserializeMatrix3x3, serializeColor, deserializeColor, serializeBezierPath, deserializeBezierPath, serializeVector2, deserializeVector2, } from './primitives';
8
+ import { getSerializer } from './registry';
9
+ // ID generation - uses a simple incrementing counter
10
+ let idCounter = 0;
11
+ const mobjectIdMap = new WeakMap();
12
+ function getMobjectId(m) {
13
+ let id = mobjectIdMap.get(m);
14
+ if (!id) {
15
+ id = `mob_${idCounter++}`;
16
+ mobjectIdMap.set(m, id);
17
+ }
18
+ return id;
19
+ }
20
+ /**
21
+ * Reset ID counter (useful for testing).
22
+ */
23
+ export function resetIdCounter() {
24
+ idCounter = 0;
25
+ }
26
+ /**
27
+ * Get the type name for a mobject instance.
28
+ */
29
+ function getMobjectType(m) {
30
+ if (m instanceof Circle)
31
+ return 'Circle';
32
+ if (m instanceof Rectangle)
33
+ return 'Rectangle';
34
+ if (m instanceof Arrow)
35
+ return 'Arrow';
36
+ if (m instanceof Line)
37
+ return 'Line';
38
+ if (m instanceof Arc)
39
+ return 'Arc';
40
+ if (m instanceof Polygon)
41
+ return 'Polygon';
42
+ if (m instanceof Point)
43
+ return 'Point';
44
+ if (m instanceof VGroup)
45
+ return 'VGroup';
46
+ if (m instanceof VMobject)
47
+ return 'VMobject';
48
+ return 'Mobject';
49
+ }
50
+ // ========== Base Mobject Serialization ==========
51
+ function serializeMobjectBase(m) {
52
+ return {
53
+ type: getMobjectType(m),
54
+ id: getMobjectId(m),
55
+ matrix: serializeMatrix3x3(m.matrix),
56
+ opacity: m.opacity,
57
+ };
58
+ }
59
+ // ========== VMobject Serialization ==========
60
+ function serializeVMobjectBase(v) {
61
+ const base = serializeMobjectBase(v);
62
+ return {
63
+ ...base,
64
+ paths: v.paths.map(serializeBezierPath),
65
+ strokeColor: serializeColor(v.getStrokeColor()),
66
+ strokeWidth: v.getStrokeWidth(),
67
+ fillColor: serializeColor(v.getFillColor()),
68
+ fillOpacity: v.getFillOpacity(),
69
+ };
70
+ }
71
+ // ========== Primitive-Specific Serialization ==========
72
+ function serializeCircle(c) {
73
+ const base = serializeVMobjectBase(c);
74
+ // Circle extends Arc, extract radius from the path bounding
75
+ const arc = c;
76
+ return { ...base, type: 'Circle', radius: arc.radius };
77
+ }
78
+ function serializeRectangle(r) {
79
+ const base = serializeVMobjectBase(r);
80
+ return { ...base, type: 'Rectangle', width: r.width, height: r.height };
81
+ }
82
+ function serializeLine(l) {
83
+ const base = serializeVMobjectBase(l);
84
+ return {
85
+ ...base,
86
+ type: 'Line',
87
+ start: serializeVector2(l.start),
88
+ end: serializeVector2(l.end),
89
+ };
90
+ }
91
+ function serializeArc(a) {
92
+ const base = serializeVMobjectBase(a);
93
+ return {
94
+ ...base,
95
+ type: 'Arc',
96
+ radius: a.radius,
97
+ startAngle: a.startAngle,
98
+ endAngle: a.endAngle,
99
+ };
100
+ }
101
+ function serializePolygon(p) {
102
+ const base = serializeVMobjectBase(p);
103
+ return {
104
+ ...base,
105
+ type: 'Polygon',
106
+ vertices: p.vertices.map(serializeVector2),
107
+ };
108
+ }
109
+ function serializePoint(p) {
110
+ // Point extends Circle, serialize as VMobject with Point type
111
+ const base = serializeVMobjectBase(p);
112
+ return { ...base, type: 'Point' };
113
+ }
114
+ // ========== VGroup Serialization ==========
115
+ function serializeVGroup(g) {
116
+ const base = serializeMobjectBase(g);
117
+ return {
118
+ ...base,
119
+ type: 'VGroup',
120
+ children: g.getChildren().map(serializeMobject),
121
+ };
122
+ }
123
+ // ========== Main Serialization Entry Point ==========
124
+ /**
125
+ * Serializes a Mobject or any of its subclasses to a plain object.
126
+ *
127
+ * This function handles built-in types like Circle, Rectangle, etc.,
128
+ * and also checks the custom serializer registry for user-defined types.
129
+ */
130
+ export function serializeMobject(m) {
131
+ // Check for custom serializer first
132
+ const typeName = m.constructor.name;
133
+ const customSerializer = getSerializer(typeName);
134
+ if (customSerializer) {
135
+ return customSerializer.serialize(m);
136
+ }
137
+ // Handle built-in types
138
+ if (m instanceof Circle)
139
+ return serializeCircle(m);
140
+ if (m instanceof Rectangle)
141
+ return serializeRectangle(m);
142
+ if (m instanceof Arrow)
143
+ return serializeLine(m);
144
+ if (m instanceof Line)
145
+ return serializeLine(m);
146
+ if (m instanceof Arc)
147
+ return serializeArc(m);
148
+ if (m instanceof Polygon)
149
+ return serializePolygon(m);
150
+ if (m instanceof Point)
151
+ return serializePoint(m);
152
+ if (m instanceof VGroup)
153
+ return serializeVGroup(m);
154
+ if (m instanceof VMobject)
155
+ return serializeVMobjectBase(m);
156
+ return serializeMobjectBase(m);
157
+ }
158
+ // ========== Deserialization ==========
159
+ function applyMobjectBase(m, data) {
160
+ const matrix = deserializeMatrix3x3(data.matrix);
161
+ m.applyMatrix(matrix);
162
+ m.setOpacity(data.opacity);
163
+ }
164
+ function applyVMobjectBase(v, data) {
165
+ applyMobjectBase(v, data);
166
+ v.paths = data.paths.map(deserializeBezierPath);
167
+ v.stroke(deserializeColor(data.strokeColor), data.strokeWidth);
168
+ v.fill(deserializeColor(data.fillColor), data.fillOpacity);
169
+ }
170
+ /**
171
+ * Restores a Mobject or subclass instance from serialized data.
172
+ *
173
+ * Uses the 'type' field to determine the correct class to instantiate
174
+ * and restores its state including transformations and styles.
175
+ */
176
+ export function deserializeMobject(data) {
177
+ // Check for custom serializer first
178
+ const customSerializer = getSerializer(data.type);
179
+ if (customSerializer) {
180
+ return customSerializer.deserialize(data);
181
+ }
182
+ switch (data.type) {
183
+ case 'Circle': {
184
+ const d = data;
185
+ const circle = new Circle(d.radius);
186
+ applyVMobjectBase(circle, d);
187
+ return circle;
188
+ }
189
+ case 'Rectangle': {
190
+ const d = data;
191
+ const rect = new Rectangle(d.width, d.height);
192
+ applyVMobjectBase(rect, d);
193
+ return rect;
194
+ }
195
+ case 'Line': {
196
+ const d = data;
197
+ const start = deserializeVector2(d.start);
198
+ const end = deserializeVector2(d.end);
199
+ const line = new Line(start.x, start.y, end.x, end.y);
200
+ applyVMobjectBase(line, d);
201
+ return line;
202
+ }
203
+ case 'Arc': {
204
+ const d = data;
205
+ const arc = new Arc(d.radius, d.startAngle, d.endAngle);
206
+ applyVMobjectBase(arc, d);
207
+ return arc;
208
+ }
209
+ case 'Polygon': {
210
+ const d = data;
211
+ const vertices = d.vertices.map(deserializeVector2);
212
+ const polygon = new Polygon(vertices);
213
+ applyVMobjectBase(polygon, d);
214
+ return polygon;
215
+ }
216
+ case 'Point': {
217
+ // Point constructor takes location, but we restore state via matrix
218
+ const d = data;
219
+ const point = new Point();
220
+ applyVMobjectBase(point, d);
221
+ return point;
222
+ }
223
+ case 'VGroup': {
224
+ const d = data;
225
+ const children = d.children.map(deserializeMobject);
226
+ const group = new VGroup(...children);
227
+ applyMobjectBase(group, d);
228
+ return group;
229
+ }
230
+ case 'VMobject': {
231
+ const d = data;
232
+ const vmob = new VMobject();
233
+ applyVMobjectBase(vmob, d);
234
+ return vmob;
235
+ }
236
+ default: {
237
+ // Generic Mobject fallback
238
+ const mob = new VMobject();
239
+ if ('paths' in data) {
240
+ applyVMobjectBase(mob, data);
241
+ }
242
+ else {
243
+ applyMobjectBase(mob, data);
244
+ }
245
+ return mob;
246
+ }
247
+ }
248
+ }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Pretty printer for serialized scenes.
3
+ */
4
+ import type { SerializedScene } from './types';
5
+ /**
6
+ * Formats a serialized scene as a JSON string with 2-space indentation.
7
+ */
8
+ export declare function prettyPrint(scene: SerializedScene): string;
9
+ /**
10
+ * Formats a serialized scene as a JSON string using a compact yet readable format.
11
+ */
12
+ export declare function prettyPrintCompact(scene: SerializedScene): string;
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Pretty printer for serialized scenes.
3
+ */
4
+ /**
5
+ * Formats a serialized scene as a JSON string with 2-space indentation.
6
+ */
7
+ export function prettyPrint(scene) {
8
+ return JSON.stringify(scene, null, 2);
9
+ }
10
+ /**
11
+ * Formats a serialized scene as a JSON string using a compact yet readable format.
12
+ */
13
+ export function prettyPrintCompact(scene) {
14
+ // Current implementation uses 2-space indentation; could be adjusted for more compactness.
15
+ return JSON.stringify(scene, null, 2);
16
+ }
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Serializers for primitive math types.
3
+ */
4
+ import { Vector2 } from '../math/Vector2/Vector2';
5
+ import { Matrix3x3 } from '../math/matrix/Matrix3x3';
6
+ import { Color } from '../math/color/Color';
7
+ import { BezierPath } from '../math/bezier/BezierPath';
8
+ import type { SerializedVector2, SerializedMatrix3x3, SerializedColor, SerializedBezierPath } from './types';
9
+ /** Converts a Vector2 to a plain object for serialization. */
10
+ export declare function serializeVector2(v: Vector2): SerializedVector2;
11
+ /** Restores a Vector2 from serialized data. */
12
+ export declare function deserializeVector2(data: SerializedVector2): Vector2;
13
+ /** Converts a Matrix3x3 to a plain object for serialization. */
14
+ export declare function serializeMatrix3x3(m: Matrix3x3): SerializedMatrix3x3;
15
+ /** Restores a Matrix3x3 from serialized data. */
16
+ export declare function deserializeMatrix3x3(data: SerializedMatrix3x3): Matrix3x3;
17
+ /** Converts a Color to a plain object for serialization. */
18
+ export declare function serializeColor(c: Color): SerializedColor;
19
+ /** Restores a Color from serialized data. */
20
+ export declare function deserializeColor(data: SerializedColor): Color;
21
+ /** Converts a BezierPath to a plain object for serialization. */
22
+ export declare function serializeBezierPath(path: BezierPath): SerializedBezierPath;
23
+ /** Restores a BezierPath from serialized data. */
24
+ export declare function deserializeBezierPath(data: SerializedBezierPath): BezierPath;
@@ -0,0 +1,98 @@
1
+ /**
2
+ * Serializers for primitive math types.
3
+ */
4
+ import { Vector2 } from '../math/Vector2/Vector2';
5
+ import { Matrix3x3 } from '../math/matrix/Matrix3x3';
6
+ import { Color } from '../math/color/Color';
7
+ import { BezierPath } from '../math/bezier/BezierPath';
8
+ // ========== Vector2 ==========
9
+ /** Converts a Vector2 to a plain object for serialization. */
10
+ export function serializeVector2(v) {
11
+ return { x: v.x, y: v.y };
12
+ }
13
+ /** Restores a Vector2 from serialized data. */
14
+ export function deserializeVector2(data) {
15
+ return new Vector2(data.x, data.y);
16
+ }
17
+ // ========== Matrix3x3 ==========
18
+ /** Converts a Matrix3x3 to a plain object for serialization. */
19
+ export function serializeMatrix3x3(m) {
20
+ return { values: Array.from(m.values) };
21
+ }
22
+ /** Restores a Matrix3x3 from serialized data. */
23
+ export function deserializeMatrix3x3(data) {
24
+ const values = new Float32Array(data.values);
25
+ return new Matrix3x3(values);
26
+ }
27
+ // ========== Color ==========
28
+ /** Converts a Color to a plain object for serialization. */
29
+ export function serializeColor(c) {
30
+ return { r: c.r, g: c.g, b: c.b, a: c.a };
31
+ }
32
+ /** Restores a Color from serialized data. */
33
+ export function deserializeColor(data) {
34
+ return new Color(data.r, data.g, data.b, data.a);
35
+ }
36
+ // ========== PathCommand ==========
37
+ function serializePathCommand(cmd) {
38
+ const result = {
39
+ type: cmd.type,
40
+ end: serializeVector2(cmd.end),
41
+ };
42
+ // Add control points if they exist
43
+ const withControl1 = cmd.control1
44
+ ? { ...result, control1: serializeVector2(cmd.control1) }
45
+ : result;
46
+ const withControl2 = cmd.control2
47
+ ? { ...withControl1, control2: serializeVector2(cmd.control2) }
48
+ : withControl1;
49
+ return withControl2;
50
+ }
51
+ function deserializePathCommand(data) {
52
+ const result = {
53
+ type: data.type,
54
+ end: deserializeVector2(data.end),
55
+ };
56
+ if (data.control1) {
57
+ return { ...result, control1: deserializeVector2(data.control1) };
58
+ }
59
+ if (data.control2) {
60
+ return { ...result, control2: deserializeVector2(data.control2) };
61
+ }
62
+ return result;
63
+ }
64
+ // ========== BezierPath ==========
65
+ /** Converts a BezierPath to a plain object for serialization. */
66
+ export function serializeBezierPath(path) {
67
+ const commands = path.getCommands().map(serializePathCommand);
68
+ return { commands };
69
+ }
70
+ /** Restores a BezierPath from serialized data. */
71
+ export function deserializeBezierPath(data) {
72
+ const path = new BezierPath();
73
+ for (const cmd of data.commands) {
74
+ const end = deserializeVector2(cmd.end);
75
+ switch (cmd.type) {
76
+ case 'Move':
77
+ path.moveTo(end);
78
+ break;
79
+ case 'Line':
80
+ path.lineTo(end);
81
+ break;
82
+ case 'Quadratic':
83
+ if (cmd.control1) {
84
+ path.quadraticTo(deserializeVector2(cmd.control1), end);
85
+ }
86
+ break;
87
+ case 'Cubic':
88
+ if (cmd.control1 && cmd.control2) {
89
+ path.cubicTo(deserializeVector2(cmd.control1), deserializeVector2(cmd.control2), end);
90
+ }
91
+ break;
92
+ case 'Close':
93
+ path.closePath();
94
+ break;
95
+ }
96
+ }
97
+ return path;
98
+ }
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Registry for custom serializers to support user-defined VMobject subclasses.
3
+ */
4
+ import type { CustomSerializer } from './types';
5
+ /**
6
+ * Register a custom serializer for a type.
7
+ * @param serializer The serializer implementation
8
+ */
9
+ export declare function registerSerializer<T>(serializer: CustomSerializer<T>): void;
10
+ /**
11
+ * Get a registered serializer by type name.
12
+ * @param typeName The type name to look up
13
+ * @returns The serializer or undefined if not registered
14
+ */
15
+ export declare function getSerializer(typeName: string): CustomSerializer<unknown> | undefined;
16
+ /**
17
+ * Check if a serializer is registered for a type.
18
+ * @param typeName The type name to check
19
+ */
20
+ export declare function hasSerializer(typeName: string): boolean;
21
+ /**
22
+ * Unregister a serializer.
23
+ * @param typeName The type name to unregister
24
+ */
25
+ export declare function unregisterSerializer(typeName: string): boolean;
26
+ /**
27
+ * Clear all registered serializers.
28
+ */
29
+ export declare function clearSerializerRegistry(): void;
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Registry for custom serializers to support user-defined VMobject subclasses.
3
+ */
4
+ const registry = new Map();
5
+ /**
6
+ * Register a custom serializer for a type.
7
+ * @param serializer The serializer implementation
8
+ */
9
+ export function registerSerializer(serializer) {
10
+ registry.set(serializer.typeName, serializer);
11
+ }
12
+ /**
13
+ * Get a registered serializer by type name.
14
+ * @param typeName The type name to look up
15
+ * @returns The serializer or undefined if not registered
16
+ */
17
+ export function getSerializer(typeName) {
18
+ return registry.get(typeName);
19
+ }
20
+ /**
21
+ * Check if a serializer is registered for a type.
22
+ * @param typeName The type name to check
23
+ */
24
+ export function hasSerializer(typeName) {
25
+ return registry.has(typeName);
26
+ }
27
+ /**
28
+ * Unregister a serializer.
29
+ * @param typeName The type name to unregister
30
+ */
31
+ export function unregisterSerializer(typeName) {
32
+ return registry.delete(typeName);
33
+ }
34
+ /**
35
+ * Clear all registered serializers.
36
+ */
37
+ export function clearSerializerRegistry() {
38
+ registry.clear();
39
+ }
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Scene serialization - the main entry point for serializing/deserializing Scenes.
3
+ */
4
+ import { Scene } from '../scene';
5
+ import type { SerializedScene } from './types';
6
+ /**
7
+ * Serializes a Scene into a plain object format.
8
+ *
9
+ * This captures all added Mobjects, their hierarchical structure,
10
+ * and the entire timeline of scheduled animations.
11
+ */
12
+ export declare function serializeScene(scene: Scene): SerializedScene;
13
+ /**
14
+ * Serializes a Scene into a JSON-formatted string.
15
+ *
16
+ * Useful for saving scene state to a file or transmitting it over a network.
17
+ */
18
+ export declare function serialize(scene: Scene): string;
19
+ /**
20
+ * Restores a complete Scene from a serialized object.
21
+ *
22
+ * Reconstructs all Mobjects and reschedules all animations onto the timeline.
23
+ */
24
+ export declare function deserializeScene(data: SerializedScene): Scene;
25
+ /**
26
+ * Restores a complete Scene from a JSON-formatted string.
27
+ */
28
+ export declare function deserialize(json: string): Scene;
@@ -0,0 +1,114 @@
1
+ /**
2
+ * Scene serialization - the main entry point for serializing/deserializing Scenes.
3
+ */
4
+ import { Scene } from '../scene';
5
+ import { serializeMobject, deserializeMobject, resetIdCounter } from './mobject';
6
+ import { serializeAnimation, deserializeAnimation } from './animation';
7
+ import { serializeColor, deserializeColor } from './primitives';
8
+ const SERIALIZATION_VERSION = '1.0.0';
9
+ // Track mobject IDs during serialization
10
+ const serializationIdMap = new Map();
11
+ let serializationIdCounter = 0;
12
+ function getSerializationId(m) {
13
+ let id = serializationIdMap.get(m);
14
+ if (!id) {
15
+ id = `mob_${serializationIdCounter++}`;
16
+ serializationIdMap.set(m, id);
17
+ }
18
+ return id;
19
+ }
20
+ function resetSerializationState() {
21
+ serializationIdMap.clear();
22
+ serializationIdCounter = 0;
23
+ resetIdCounter();
24
+ }
25
+ // ========== Scene Serialization ==========
26
+ function serializeSceneConfig(scene) {
27
+ return {
28
+ width: scene.getWidth(),
29
+ height: scene.getHeight(),
30
+ backgroundColor: serializeColor(scene.getBackgroundColor()),
31
+ frameRate: scene.getFrameRate(),
32
+ };
33
+ }
34
+ function serializeTimeline(scene) {
35
+ const timeline = scene.getTimeline();
36
+ const scheduled = timeline.getScheduled();
37
+ const serializedScheduled = scheduled.map(s => ({
38
+ animation: serializeAnimation(s.animation, getSerializationId),
39
+ startTime: s.startTime,
40
+ }));
41
+ return {
42
+ loop: timeline.isLooping(),
43
+ scheduled: serializedScheduled,
44
+ };
45
+ }
46
+ /**
47
+ * Serializes a Scene into a plain object format.
48
+ *
49
+ * This captures all added Mobjects, their hierarchical structure,
50
+ * and the entire timeline of scheduled animations.
51
+ */
52
+ export function serializeScene(scene) {
53
+ resetSerializationState();
54
+ const mobjects = scene.getMobjects();
55
+ const serializedMobjects = mobjects.map(m => serializeMobject(m));
56
+ return {
57
+ version: SERIALIZATION_VERSION,
58
+ config: serializeSceneConfig(scene),
59
+ mobjects: serializedMobjects,
60
+ timeline: serializeTimeline(scene),
61
+ };
62
+ }
63
+ /**
64
+ * Serializes a Scene into a JSON-formatted string.
65
+ *
66
+ * Useful for saving scene state to a file or transmitting it over a network.
67
+ */
68
+ export function serialize(scene) {
69
+ const data = serializeScene(scene);
70
+ return JSON.stringify(data);
71
+ }
72
+ // ========== Scene Deserialization ==========
73
+ /**
74
+ * Restores a complete Scene from a serialized object.
75
+ *
76
+ * Reconstructs all Mobjects and reschedules all animations onto the timeline.
77
+ */
78
+ export function deserializeScene(data) {
79
+ // Verify version compatibility
80
+ if (!data.version) {
81
+ throw new Error('Missing serialization version');
82
+ }
83
+ // Create scene with config
84
+ const scene = new Scene({
85
+ width: data.config.width,
86
+ height: data.config.height,
87
+ backgroundColor: deserializeColor(data.config.backgroundColor),
88
+ frameRate: data.config.frameRate,
89
+ });
90
+ // Build mobject registry for animation deserialization
91
+ const mobjectRegistry = new Map();
92
+ const mobjects = [];
93
+ for (const serializedMob of data.mobjects) {
94
+ const mob = deserializeMobject(serializedMob);
95
+ mobjectRegistry.set(serializedMob.id, mob);
96
+ mobjects.push(mob);
97
+ }
98
+ // Add mobjects to scene
99
+ scene.add(...mobjects);
100
+ // Deserialize and schedule animations
101
+ const timeline = scene.getTimeline();
102
+ for (const scheduled of data.timeline.scheduled) {
103
+ const animation = deserializeAnimation(scheduled.animation, mobjectRegistry);
104
+ timeline.schedule(animation, scheduled.startTime);
105
+ }
106
+ return scene;
107
+ }
108
+ /**
109
+ * Restores a complete Scene from a JSON-formatted string.
110
+ */
111
+ export function deserialize(json) {
112
+ const data = JSON.parse(json);
113
+ return deserializeScene(data);
114
+ }