Custom Animations

Combining built-in Mob methods and attribute changes inside animation contexts will handle most of your animation needs.

But if you need an animation which can’t be created by combining the existing ones, then you can make your own animations using the animated_function() decorator.

Before writing a custom animation function, check if one of these is what you need instead: * Combining standard Mob methods inside Combining Animations. * Built-in animations in Built-in Animations (e.g. ApplyMatrix, Homotopy, PhaseFlow). * An updater if the behavior should run continuously. * Animating Out of Order if you just need to schedule animations at specific timestamps.

Animated functions

An animated function describes one frame: given a parameter, it puts the Mob where it should be for that parameter’s value. The decorator does the animating, by sweeping the parameter across the animation and calling the body at every frame.

Example: BasicAnimatedFunction

from algan import *
import numpy as np

# Function mapping a scalar parameter t to a 3-D position
def path_func(t):
    return UP * np.sin(t) + RIGHT * (t - PI)

# An animated_function that moves our mob along that path.
@animated_function(animated_args={'t': 0})
def move_along_path(mob, t):
    mob.location = path_func(t)

square = Square().spawn()
square.location = path_func(0)   # Jump to the starting point.
move_along_path(square, 2 * PI)

Scene.save_video()

animated_args maps each animated parameter to its value at the start of the animation. Algan then interpolates from there to whatever you called the function with, evaluating the body at every frame. Above, t starts at 0 and the call passes 2 * PI, so the animation sweeps t from 0 to 2 * PI over one second.

Important

An animated_function() must take a Mob as its first argument, and every name listed in animated_args must be a float.

Note

Inside an animated_function(), attribute assignment is not separately animated; the function body describes a single frame, and the decorator does the animating.

An animated function is an animation like any other, so it obeys the surrounding animation context: wrap it in Seq(runtime=...) to set its runtime, put it inside Sync() to run it alongside other animations, and give it a easing to change its easing.

More than one animated argument

Any number of parameters can be animated at once, and each gets its own start value:

Example: CustomTwoAnimatedArgs

from algan import *
import numpy as np

@animated_function(animated_args={'turns': 0, 'radius': 0})
def spiral_out(mob, turns, radius):
    angle = turns * 2 * PI
    mob.location = (RIGHT * np.cos(angle) + UP * np.sin(angle)) * radius

dot = Dot(color=YELLOW).scale(2).spawn()
with Seq(runtime=3, easing=easings.identity):
    spiral_out(dot, turns=2.5, radius=3.0)

Scene.save_video()

See Also

  • Built-in Animations – the animations Algan already provides, several of which are animated functions themselves.

  • Combining Animations – the contexts an animated function composes with.

  • Updaters – the other way to write behaviour of your own, for rules that hold continuously.