Animations

An animation is something that happens over time: hand it to play(...) (self.play in a scene).

Bringing things in and out

AnimationWhat you see
Create(m)The outline is drawn as if with a pen, from its start
Uncreate(m)Create backwards; then it is gone
Write(m)Text or maths is written out, a stroke at a time
Unwrite(m)Write backwards
DrawBorderThenFill(m)The outline is drawn, then the inside fills
FadeIn(m)It fades in. shift=UP slides it in upwards; scale=0.5 grows it from half size
FadeOut(m)It fades out. shift=DOWN slides it away downwards
GrowFromCenter(m)It grows from nothing at its middle
GrowFromPoint(m, point)It grows from a point
GrowFromEdge(m, DOWN)It grows from one side
GrowArrow(arrow)An arrow grows from its tail
SpinInFromNothing(m)It grows while turning

FadeIn and FadeOut take several objects at once: FadeOut(a, b, c).

Changing things

AnimationWhat you see
m.animate.<change>Any change played smoothly: m.animate.shift(UP), .scale(2), .set_color(RED), .move_to(ORIGIN), .next_to(other, DOWN); chain several and they happen together
Transform(a, b)a changes until it looks like b. a stays the one on screen
ReplacementTransform(a, b)The same, but afterwards b is the one on screen (use b from then on)
TransformFromCopy(a, b)A copy of a changes into b; a stays where it was
TransformMatchingTex(a, b)Equation parts with the same text move to their new places; the rest fade (more)
TransformMatchingShapes(a, b)Pieces with the same shape move to their new places
Rotate(m, angle=PI)It turns about its middle (about_point= for another centre)
Rotating(m)A steady full turn over 5 seconds
MoveAlongPath(m, path)It travels along a line or curve
MoveToTarget(m)After m.generate_target() and changes to m.target, it moves to match
Restore(m)After m.save_state(), it goes back to how it was

m.animate.rotate(PI) and Rotate(m, angle=PI) look different: .animate slides every point straight to its end place (a square shrinks and grows back as it flips), while Rotate really turns it. Use Rotate for a turn you want to see.

Drawing attention

AnimationWhat you see
Indicate(m)It grows a little and turns yellow, then goes back
Circumscribe(m)A line runs round it once
Flash(point)Short lines burst out from a point
Wiggle(m)It wiggles

Pauses

wait() holds the picture for one second; wait(2.5) for two and a half.

How long, and how it moves

Every animation takes one second unless you say. Write of something long, DrawBorderThenFill and Wiggle take two.

from pymations import *

dot = Dot(LEFT * 4, color=YELLOW)
add(dot)
play(dot.animate.shift(RIGHT * 8), run_time=3, rate_func=linear)
  • run_time=3 makes it last three seconds. Given to play, it applies to every animation in that play. Given to one animation, Create(m, run_time=3), it applies to that one.
  • rate_func is how it speeds up and slows down. Most animations start and end gently (smooth). linear is steady; there_and_back goes and comes back; rush_into and rush_from start or end fast; there_and_back_with_pause, double_smooth, running_start and lingering are there too, and the standard easing curves such as ease_in_out_sine, ease_out_bounce and ease_in_back.

Several at once, or one after another

from pymations import *

dots = VGroup(*[Dot() for _ in range(6)]).arrange(RIGHT, buff=0.6)

play(LaggedStart(*[GrowFromCenter(d) for d in dots], lag_ratio=0.2))
play(Succession(dots[0].animate.shift(UP), dots[5].animate.shift(DOWN)))
play(AnimationGroup(*[d.animate.set_color(BLUE) for d in dots]))
GroupWhat it does
play(a, b, c)All together
AnimationGroup(a, b, c)All together, as one animation (so it can go inside another group)
Succession(a, b, c)One after another
LaggedStart(a, b, c, lag_ratio=0.2)Each starts a little after the one before: lag_ratio is how far through the previous one, 0.2 = a fifth of the way
LaggedStartMap(FadeIn, group)LaggedStart of one animation over every member of a group

Showing without animating

add(m) puts something on screen straight away and remove(m) takes it off, without using any time. Changes made without .animate (m.shift(UP)) also happen straight away.