Animation¶
CircuitPython Animation Class to make it easy to move around displayio and vectorio graphical elements.
Author(s): Kevin Matocha
Implementation Notes¶
Hardware:
Software and Dependencies:
Adafruit CircuitPython firmware for the supported boards: https://github.com/adafruit/circuitpython/releases
-
class
displayio_animation.Animation(**kwargs)¶ An Animation class to make it easy to making moving animations with CircuitPython’s displayio and vectorio graphical elements.
After instancing an
Animation()object, useAnimation.add_entry()to add frame animation sections. Once all your animation entries are added, then perform the frame animation usingAnimation.execute_frame().-
add_entry(group, frame_start, frame_end, function, **kwargs)¶ Adds an animation entry into the Animation list.
- Parameters
group (displayio.Group) – the displayio Group that will be animated within this frame range
frame_start (float) – the starting frame position for this animation
frame_end (float) – the ending frame position for this animation
function – the name of the function to be called to mutate the
groupduring the frame range**kwargs – the additional arguments that should be passed to
functionwhenAnimation.execute_frame()is called to trigger the animation to execute
Note: See the definition of
Animation.execute_frame()to understand what other parameters are sent tofunctionduring animations.
-
execute_frame(frame)¶ The function that performs the actual frame animation execution.
This function searches through all
Entryitems that have been added to the Animation instance to determine if this frame is within the window offrame_starttoframe_end. If the requested frame is within the window, this calls theEntry.functionwith several “internal” parameters along with the additional “user” parameters that were input as additional arguments in theAnimation.add_entry()function.The parameters that are sent to
Entry.function()are: - float position: a value between 0.0 and 1.0 representing the currentframedistance within the window offrame_startandframe_end. For example, if the currentframeis equal toframe_start, thenpositionis 0.0. - displayio.Group group: the group to be animated usingfunction- int x0: the x-position of the group atframe_start- int y0: the y-position of the group atframe_start- float frame: the current frame that is being executed - float frame_start: the starting frame for this frame window - float frame_end: the ending frame for this frame window - **kwargs: any other parameters that were defined in the EntryThe
functionshould be designed to ignore any unneeded input parameters by including**kwargsas one of the input parameters. This will cause the function to ignore excess arguments.Note: If a function requires the (x0,y0) values, you must initally perform
Animation.execute_frame()at frame == frame_start. TheAnimation.execute_frame()initializes the (x0, y0) values only when called with the value offrame_start.Other Note: The frame window is “exclusive”, so no animation is performed when
frame == frame_end.- Parameters
frame (float) – The frame to be displayed. Note: This is a float, so subframes can be animated.
-
-
class
displayio_animation.Entry(group, frame_start, frame_end, function, kwargs)¶ This
Entryclass is a holder for the conditions that define an animated frame range. This holds the group, the “augmentation” function and arguments that are run at each call ofAnimation.execute_frame.Before running your loop with
Animation.execute_frame, add all of your entries to the Animation object usingAnimation.add_entry(). Any excess arguments that are not handled byAnimation.execute_framewill be passed to thefunctionparameter (see notes on using thekwargsnotation).Here is a code example. Append some display elements into
group1, create an Animation instance and then add an animation entry:group1=displayio.Group(max_size=1) animation=Animation() animation.add_entry(group=group1, frame_start=5, frame_end=20, function=translate, x1=50, y1=20, x2=50, y2=50, easing_function_x=quadratic_easeinout, easing_function_y=quadratic_easeinout)
- Parameters
group (displayio.Group) – the group that is animated in this set of frames
frame_start (float) – the starting frame for this animation
frame_end (float) – the ending frame for this animation
function – the function that mutates the group to cause the animation
kwargs – a set of additional arguments that will be passed to the
functionduring the animation
-
displayio_animation.color_morph_label(*, color_start, color_end, label, position, **kwargs)¶ Performs color morphing for a text label between the
color_startandcolor_endvalues based on the position parameter.- Parameters
color_start (int) – the starting color
color_end (int) – the ending_color
label – the label whose color is to be morphed
position (float) – float position: a linear interpolation of the current frame’s position between
frame_startandframe_end. If usingAnimation.execute_frame()thepositionparameter will be included automatically.
-
displayio_animation.color_morph_palette(*, palette_start, color_end, palette_target, position, **kwargs)¶ Performs color morphing for a color palette between the
palette_startand a singlecolor_endvalue based on the position parameter. At the final position=1.0, thepalette_targetwill be filled with thecolor_endvalue.- Parameters
palette_start (displayio.palette) – the starting palette for the image
color_end (int) – the single ending_color for all the colors in the palette
palette_target (displayio.palette) – the destination palette to be “morphed” to the ending_color
position (float) – float position: a linear interpolation of the current frame’s position between
frame_startandframe_end. If usingAnimation.execute_frame()thepositionparameter will be included automatically.
-
displayio_animation.color_morph_vector_shape(*, color_start, color_end, vector_shape, position, **kwargs)¶ Performs color morphing for a vector shape, with color between the
color_startandcolor_endvalues based on the position parameter.- Parameters
color_start (int) – the starting color
color_end (int) – the ending_color
vector_shape (vectorio.VectorShape) – the VectorShape whose palette color index 1 should be morphed.
position (float) – float position: a linear interpolation of the current frame’s position between
frame_startandframe_end. If usingAnimation.execute_frame()thepositionparameter will be included automatically.
-
displayio_animation.translate(*, x1, y1, x2, y2, easing_function_x=adafruit_displayio_layout.widgets.easing.linear_interpolation, easing_function_y=adafruit_displayio_layout.widgets.easing.linear_interpolation, group, position, **kwargs)¶ Performs a translation animation between two endpoints. Use two different easing functions to get all kinds of variety of cool motion.
- Parameters
x1 (int) – initial x-position of
groupy1 (int) – initial y-position of
groupx2 (int) – final x-position of
groupy2 (int) – final y-position of
groupeasing_function_x (function) – easing function that modifies the
positionvalue for the x-motion (default: linear_interpolation)easing_function_y (function) – easing function that modifies the
positionvalue for the y-motion (default: linear_interpolation)group (displayio.Group) – the display group that is sent to the
function. If usingAnimation.execute_frame()the group input parameter will be included automatically from the Entry.position (float) – float position: a linear interpolation of the current frame’s position between
frame_startandframe_end. If usingAnimation.execute_frame()thepositionparameter will be included automatically.
-
displayio_animation.translate_relative(*, delta_x, delta_y, easing_function_x=adafruit_displayio_layout.widgets.easing.linear_interpolation, easing_function_y=adafruit_displayio_layout.widgets.easing.linear_interpolation, group, x0, y0, position, **kwargs)¶ Performs a relative translation animation between two endpoints. Use two different easing functions to get all kinds of variety of cool motion.
Note: To use relative translations, be sure to run
execute_frameatframe_startfirst so the initial (x0, y0) position is stored. For example, if you run the frames in reverse, you must runexecute_frameatframe_startat least once initialize the initial (x0, y0) position.Special note: Relative translations can get complicated. If you want to tightly control predefined positions, then
translateis the best approach. By combining overlapping relative translations, you can probably come up with all kinds of clever and confusing animations. Perhaps thetranslate_relativefunction is an avenue to create animated “generative art” projects.- Parameters
x2 (int) – final x-position of
groupy2 (int) – final y-position of
groupeasing_function_x (function) – easing function that modifies the
positionvalue for the x-motion (default: linear_interpolation)easing_function_y (function) – easing function that modifies the
positionvalue for the y-motion (default: linear_interpolation)group (displayio.Group) – the display group that is sent to the
function. If usingAnimation.execute_frame()thegroupinput parameter will be included automatically from the Entry.x0 (int) – initial x-position of
group. If usingAnimation.execute_frame()thex0input parameter will be included automatically from the Entry.y0 (int) – initial y-position of
group. If usingAnimation.execute_frame()they0input parameter will be included automatically from the Entry.position (float) – float position: a linear interpolation of the current frame’s position between
frame_startandframe_end. If usingAnimation.execute_frame()thepositionparameter will be included automatically.
-
displayio_animation.wiggle(*, delta_x=0, delta_y=0, xsteps=None, ysteps=None, group, x0, y0, frame_start, frame, **kwargs)¶ Performs a nervous wiggling animation around the starting point. To achieve a random-looking wiggle, set
xstepsandystepsto two different prime numbers.Note: To use
wiggle, be sure to runexecute_frameatframe_startfirst so the initial (x0, y0) position is stored. For example, if you run the frames in reverse, you must runexecute_frameatframe_startat least once initialize the initial (x0, y0) position.- Parameters
delta_x (int) – amount of x-movement, in pixels (default = 0)
delta_y (int) – amount of y-movement, in pixels (default = 0)
xsteps (int) – number of frame steps it takes to make a full x-direction wiggle
ysteps (int) – number of frame steps it takes to make a full y-direction wiggle
group (displayio.Group) – the display group that is sent to the
function. If usingAnimation.execute_frame()thegroupinput parameter will be included automatically from the Entry.x0 (int) – initial x-position of
group. If usingAnimation.execute_frame()thex0input parameter will be included automatically from the Entry.y0 (int) – initial y-position of
group. If usingAnimation.execute_frame()they0input parameter will be included automatically from the Entry.frame_start (float) – the starting frame of this animation entry. If using
Animation.execute_frame()theframe_startparameter will be included automatically from the Entry.frame (float) – the current frame being animated. If using
Animation.execute_frame()theframeparameter will be included automatically.