class Scar::Tween

Overview

This class provides simple inbetweening functionality.

You create a Tween with the parameters of animation duration and easing function. The obviously determines how long the Tween takes to complete, the easing function determines how the tween calculates its values.

A Tween is handled by an App after you register it via App#tween.

Example usage:

# Move the player 100 pixels to the right over the course of 5 seconds
origin = player.x
t = Scar::Tween.new(
  5,
  Scar::Easing::EaseInOutQuad,
  ->(t : Tween) { player.x = origin + t.fraction },
  ->(t : Tweeen) { puts "Player movement complete." }
)
app.tween(t)

Defined in:

scar/tween.cr

Constructors

Instance Method Summary

Macro Summary

Constructor Detail

def self.new(duration : Number, ease : Easing::EasingDefinition, on_update : Proc(Tween, Nil), on_completed : Proc(Tween, Nil) = ->(t : Tween) do end) #

Creates a new tween with the following parameters (see details):

  • duration: The tweening duration
  • ease: The Easing::EasingDefinition used to calculate the Tweens' values
  • on_update: This hook is called on every frame, implement whatever tweening logic you have in here
  • (optional) on_completed: This hook is called when the Tweens' duration is over (you could e. g. use this to chain Tweens)

[View source]

Instance Method Detail

def abort #

Ends the tween without calling the #on_completed hook


[View source]
def aborted : Bool #

[View source]
def completed? #

[View source]
def fraction : Float32 #

Returns the current interpolated fraction (calculated by the Easing::EasingDefinition)


[View source]
def linear_fraction #

Returns the current linear interpolated fraction (time spent / duration)


[View source]
def on_completed : Scar::Tween -> Nil #

[View source]
def on_update : Scar::Tween -> Nil #

[View source]
def paused=(paused : Bool) #

Can be used to pause the Tween, meaning that its #fraction will stay the same until @paused is false again


[View source]
def paused? : Bool #

[View source]
def reset #

Sets @time_spent to 0 / starts the tween over.


[View source]
def then(&block : Tween -> ) #

Chains a new #on_completed handler after the existing one. This can be used as an alternative to specifying the #on_completed handler on initialization.

Example usage:

x = 0
Tween.bind_value(1, Easing::Linear.new, x, "x + 5 * ")
  .then { |t|
    x = 0
    t.reset
  }

[View source]
def update(delta_time) #

(used internally) Advances the Tween by the given delta time


[View source]

Macro Detail

macro bind_value(duration, ease, val, modifier = "", on_completed = ->(t : Tween) do end) #

This macro can be used for very simple value tweening

Example usage:

tween Tween.bind_value(3, Easing::EaseInOutQuint.new, x, "80 *")
# This expands to
tween Tween.new(3, Easing::EaseInOutQuint.new, ->(t : Tween) { x = 80 * t.fraction }, ->(t : Tween) {})

Note that this does not work with individual entity transform components like e.position.x, as updating these directly does not update the underlying SF::Transform. You have to set e.position as a whole.


[View source]