Transform Animations
Animations that morph one mobject into another.
Transform
Morphs the source mobject into the shape of the target. The source mobject is modified.
class TransformExample(Scene):
def construct(self):
square = Square()
circle = Circle()
self.play(Create(square))
self.play(Transform(square, circle))
# Note: 'square' now looks like 'circle' but is still 'square'Important: After Transform, the original variable still references the mobject, even though it looks like the target.
ReplacementTransform
Morphs source into target and replaces the reference. More intuitive for most uses.
class ReplacementTransformExample(Scene):
def construct(self):
square = Square()
circle = Circle()
triangle = Triangle()
self.play(Create(square))
self.play(ReplacementTransform(square, circle))
# 'square' is removed, 'circle' is now in the scene
self.play(ReplacementTransform(circle, triangle))
# 'circle' is removed, 'triangle' is now in the sceneTransform vs ReplacementTransform
# Transform - source variable changes appearance
self.play(Transform(A, B))
# A is still in scene (but looks like B)
# B is NOT in scene
# ReplacementTransform - source is replaced by target
self.play(ReplacementTransform(A, B))
# A is removed from scene
# B is now in sceneTransformFromCopy
Creates a copy of source and morphs it to target. Original remains unchanged.
class TransformFromCopyExample(Scene):
def construct(self):
square = Square().shift(LEFT * 2)
circle = Circle().shift(RIGHT * 2)
self.add(square)
self.play(TransformFromCopy(square, circle))
# Both square and circle are now visibleTransformMatchingShapes
Intelligently matches and transforms corresponding parts.
class MatchingShapesExample(Scene):
def construct(self):
source = Text("ABC")
target = Text("ABCD")
self.play(Write(source))
self.play(TransformMatchingShapes(source, target))TransformMatchingTex
Matches LaTeX parts by their TeX strings.
class MatchingTexExample(Scene):
def construct(self):
eq1 = MathTex("a", "^2", "+", "b", "^2")
eq2 = MathTex("a", "^2", "+", "2ab", "+", "b", "^2")
self.play(Write(eq1))
self.play(TransformMatchingTex(eq1, eq2))MoveToTarget
Pre-set a target state and animate to it.
class MoveToTargetExample(Scene):
def construct(self):
square = Square()
self.add(square)
# Generate and modify target
square.generate_target()
square.target.shift(RIGHT * 2)
square.target.set_color(RED)
square.target.scale(2)
self.play(MoveToTarget(square))Path Arc Transforms
Control the path of transformation with path_arc.
class PathArcExample(Scene):
def construct(self):
dot1 = Dot(LEFT * 2)
dot2 = Dot(RIGHT * 2)
self.add(dot1)
# Transform along an arc
self.play(Transform(dot1, dot2, path_arc=PI/2))Chained Transformations
class ChainedExample(Scene):
def construct(self):
shape = Square()
self.play(Create(shape))
# Chain of transformations
for target in [Circle(), Triangle(), Star()]:
self.play(Transform(shape, target))
self.wait(0.5)Best Practices
- Use ReplacementTransform for clarity - More intuitive variable behavior
- Use TransformFromCopy to preserve original - When you need both visible
- Use TransformMatchingTex for equations - Better alignment of matching parts
- Set path_arc for visual interest - Curved paths look more dynamic