Drafter.Animation (drafter v0.3.2)

Copy Markdown View Source

GenServer that manages timed property animations for widgets.

Animations interpolate numeric values, RGB color tuples, or discrete values between a start and end state over a specified duration. The server ticks at 60 fps, applies easing, and sends {:apply_animation, widget_id, property, value} messages to the app loop for each active animation.

Supported easing atoms: :linear, :ease, :ease_in, :ease_out, :ease_in_out, :ease_in_quad, :ease_out_quad, :ease_in_out_quad, :ease_in_cubic, :ease_out_cubic, :ease_in_out_cubic, :ease_in_elastic, :ease_out_elastic, :ease_out_bounce, :ease_in_bounce, :ease_in_out_bounce, :ease_in_back, :ease_out_back.

Summary

Functions

Animate property of the widget with id widget_id towards end_value.

Returns a specification to start this module under a supervisor.

The animations currently running for widget_id, or [] if it has none.

The value property has reached in the animation running for widget_id.

Start the animation server, registered under this module's name.

Stop the animation started by the call that returned animation_id.

Stop every running animation for widget_id, as stop/1 does for one. Asynchronous.

Advance every running animation by one frame immediately.

Types

animation()

@type animation() :: %Drafter.Animation{
  duration: non_neg_integer(),
  easing: atom(),
  end_value: any(),
  id: reference(),
  interpolator: function() | nil,
  on_complete: function() | nil,
  property: atom(),
  session: term(),
  start_time: integer(),
  start_value: any(),
  widget_id: atom()
}

Functions

animate(widget_id, property, end_value, opts \\ [])

@spec animate(atom(), atom(), any(), keyword()) :: reference()

Animate property of the widget with id widget_id towards end_value.

The starting value is the widget's current value for that property. Numbers are interpolated numerically, {r, g, b} tuples channel by channel, and any other value switches at the end of the duration.

Options:

  • :duration — milliseconds the animation runs for, default 300
  • :easing — one of the easing atoms listed in the module documentation, default :ease_out
  • :on_complete — zero-argument function called once the animation finishes; not called when the animation is stopped early

Returns a reference identifying the animation, which stop/1 takes. Starting a second animation for the same widget and property leaves the first running.

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

get_animations(widget_id)

@spec get_animations(atom()) :: [animation()]

The animations currently running for widget_id, or [] if it has none.

get_value(widget_id, property)

@spec get_value(atom(), atom()) :: {:ok, any()} | :none

The value property has reached in the animation running for widget_id.

Returns {:ok, value}, or :none when no animation is running for that widget and property.

start_link(opts \\ [])

@spec start_link(keyword()) :: GenServer.on_start()

Start the animation server, registered under this module's name.

One runs per node, started by the Drafter application supervisor. opts are accepted and discarded; the tick rate is fixed at 60 fps.

stop(animation_id)

@spec stop(reference()) :: :ok

Stop the animation started by the call that returned animation_id.

The property keeps whatever value it had reached and :on_complete is not run. An unknown reference is ignored. Asynchronous.

stop_all(widget_id)

@spec stop_all(atom()) :: :ok

Stop every running animation for widget_id, as stop/1 does for one. Asynchronous.

tick()

@spec tick() :: :ok

Advance every running animation by one frame immediately.

The server already ticks itself at 60 fps; calling this only brings the next frame forward. Asynchronous.