Animation support for the Kitty graphics protocol.
Provides frame sequencing, playback control, and animation management for Kitty terminal graphics. Uses a GenServer for frame scheduling and supports various animation modes.
Features
- Frame-based animation with configurable frame rates
- Loop modes: once, infinite, ping-pong
- Frame timing control
- Animation state management
- Integration with KittyGraphics
Usage
# Create an animation
{:ok, anim} = KittyAnimation.create_animation(%{
width: 100,
height: 100,
frame_rate: 30
})
# Add frames
anim = KittyAnimation.add_frame(anim, frame1_data)
anim = KittyAnimation.add_frame(anim, frame2_data)
# Start playback
{:ok, pid} = KittyAnimation.start(anim)
# Control playback
KittyAnimation.pause(pid)
KittyAnimation.resume(pid)
KittyAnimation.stop(pid)
Summary
Functions
Adds a frame to the animation.
Returns a specification to start this module under a supervisor.
Creates a new animation with the given options.
Generates Kitty protocol escape sequences for the animation.
Gets the current frame from the animation.
Gets a frame by index.
Gets the current animation state.
Advances to the next frame.
Pauses playback.
Starts playback (for already running process).
Resumes playback from current frame.
Seeks to a specific frame.
Sets the frame rate during playback.
Sets the loop mode during playback.
Starts playback of the animation as a GenServer process.
Stops playback and resets to first frame.
Types
@type frame() :: %{ data: binary(), duration_ms: non_neg_integer(), index: non_neg_integer() }
@type loop_mode() :: :once | :infinite | :ping_pong
@type playback_state() :: :stopped | :playing | :paused
@type t() :: %Raxol.Terminal.ANSI.KittyAnimation{ current_frame: non_neg_integer(), direction: :forward | :backward, format: Raxol.Terminal.ANSI.KittyGraphics.format(), frame_rate: pos_integer(), frames: [frame()], height: non_neg_integer(), image_id: non_neg_integer() | nil, loop_count: non_neg_integer(), loop_mode: loop_mode(), on_complete: (-> :ok) | nil, on_frame: (frame() -> :ok) | nil, state: playback_state(), width: non_neg_integer() }
Functions
Adds a frame to the animation.
Parameters
animation- The animation structframe_data- Binary pixel data for the frameopts- Optional frame options::duration_ms- Frame duration override in milliseconds
Returns
The updated animation with the new frame added.
Returns a specification to start this module under a supervisor.
See Supervisor.
Creates a new animation with the given options.
Options
:width- Image width in pixels (required):height- Image height in pixels (required):format- Image format (:rgb, :rgba, :png), defaults to :rgba:frame_rate- Frames per second, defaults to 30:loop_mode- Loop mode (:once, :infinite, :ping_pong), defaults to :infinite:image_id- Optional image ID for the animation
Returns
{:ok, animation}- New animation struct{:error, reason}- If required options are missing
Generates Kitty protocol escape sequences for the animation.
Parameters
animation- The animation struct
Returns
A list of escape sequences for each frame.
Gets the current frame from the animation.
Parameters
animation- The animation struct
Returns
The current frame struct, or nil if no frames exist.
@spec get_frame(t(), non_neg_integer()) :: frame() | nil
Gets a frame by index.
Parameters
animation- The animation structindex- The frame index
Returns
The frame at the specified index, or nil if not found.
@spec get_state(GenServer.server()) :: t()
Gets the current animation state.
Parameters
pid- The animation player process
Returns
The current animation struct.
Advances to the next frame.
Handles loop modes and direction for ping-pong animations.
Parameters
animation- The animation struct
Returns
{:ok, updated_animation}- Animation advanced to next frame{:complete, animation}- Animation completed (for :once mode)
@spec pause(GenServer.server()) :: :ok
Pauses playback.
Parameters
pid- The animation player process
Returns
:ok
@spec play(GenServer.server()) :: :ok
Starts playback (for already running process).
Parameters
pid- The animation player process
Returns
:ok
@spec resume(GenServer.server()) :: :ok
Resumes playback from current frame.
Parameters
pid- The animation player process
Returns
:ok
@spec seek(GenServer.server(), non_neg_integer()) :: :ok
Seeks to a specific frame.
Parameters
pid- The animation player processframe_index- The frame to seek to
Returns
:ok
@spec set_frame_rate(GenServer.server(), pos_integer()) :: :ok
Sets the frame rate during playback.
Parameters
pid- The animation player processfps- Frames per second
Returns
:ok
@spec set_loop_mode(GenServer.server(), loop_mode()) :: :ok
Sets the loop mode during playback.
Parameters
pid- The animation player processmode- Loop mode (:once, :infinite, :ping_pong)
Returns
:ok
@spec start( t(), keyword() ) :: GenServer.on_start()
Starts playback of the animation as a GenServer process.
Parameters
animation- The animation structopts- GenServer start options
Returns
{:ok, pid}- The animation player process{:error, reason}- If start fails
@spec stop(GenServer.server()) :: :ok
Stops playback and resets to first frame.
Parameters
pid- The animation player process
Returns
:ok