Your First Animation
Make a Python file in Circuitry (+ → Code → Python) and follow along. After each step, press Run and watch the preview.
1. Show a shape
from pymations import *
circle = Circle()
play(Create(circle))
Circle() makes a circle. On its own it is not on screen yet: play(Create(circle)) draws it, as if with a pen, over one second.
The circle is red because that is a circle's colour unless you say otherwise. The picture is 8 units tall, with (0, 0) in the middle; a circle's radius is 1 unless you give another.
2. Choose colours and fill
from pymations import *
circle = Circle(radius=1.5, color=BLUE, fill_opacity=0.5)
play(Create(circle))
color sets the outline (and the fill, unless you set fill_color). fill_opacity is how solid the inside is: 0 is empty, 1 is solid. Colours have names in capitals, such as BLUE, YELLOW and TEAL; see Colours.
3. Move it
from pymations import *
circle = Circle(radius=1.5, color=BLUE, fill_opacity=0.5)
play(Create(circle))
play(circle.animate.shift(LEFT * 3))
play(circle.animate.scale(0.5).set_color(YELLOW))
Put .animate before a change and the change is played as a smooth movement instead of happening at once. shift(LEFT * 3) moves 3 units left; LEFT, RIGHT, UP and DOWN are one unit each. Changes after .animate can be chained, and they all happen together.
4. Change one shape into another
from pymations import *
circle = Circle(radius=1.5, color=BLUE, fill_opacity=0.5)
square = Square(side_length=3, color=ORANGE, fill_opacity=0.5)
play(Create(circle))
play(Transform(circle, square))
wait()
Transform(circle, square) changes the circle until it looks like the square. wait() holds the picture for a second (wait(3) for three).
5. Add words and maths
from pymations import *
title = Text("Area of a circle").to_edge(UP)
formula = MathTex(r"A = \pi r^2", font_size=72)
play(Write(title))
play(Write(formula))
play(formula.animate.next_to(title, DOWN, buff=1))
wait()
Text is ordinary words. MathTex is maths, written the way LaTeX writes it (\pi for π, ^2 for a square). Put an r before the quotes so Python leaves the backslashes alone. to_edge(UP) puts the title at the top; next_to(title, DOWN) places the formula under it. See Text and maths and Placing things.
6. Play several things at once
from pymations import *
shapes = VGroup(Circle(), Square(), Triangle()).arrange(RIGHT, buff=1)
play(Create(shapes[0]), Create(shapes[1]), Create(shapes[2]))
play(FadeOut(shapes, shift=DOWN))
Everything in one play(...) happens together. VGroup keeps shapes together so they can be placed and animated as one; arrange(RIGHT) lines them up in a row. shapes[0] is the first one.
Try this
- Make the circle go round in a square path: four
shiftsteps. - Give
playarun_time:play(Create(circle), run_time=3)takes three seconds. - Use
FadeIninstead ofCreate, orGrowFromCenter. The full list is in Animations.