class Scar::Tween
- Scar::Tween
- Reference
- Object
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.crConstructors
- 
        .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): 
Instance Method Summary
- 
        #abort
        
          Ends the tween without calling the #on_completedhook
- #aborted : Bool
- #completed?
- 
        #fraction : Float32
        
          Returns the current interpolated fraction (calculated by the Easing::EasingDefinition)
- 
        #linear_fraction
        
          Returns the current linear interpolated fraction (time spent / duration) 
- #on_completed : Scar::Tween -> Nil
- #on_update : Scar::Tween -> Nil
- 
        #paused=(paused : Bool)
        
          Can be used to pause the Tween, meaning that its #fractionwill stay the same until@pausedis false again
- #paused? : Bool
- 
        #reset
        
          Sets @time_spent to 0 / starts the tween over. 
- 
        #then(&block : Tween -> )
        
          Chains a new #on_completedhandler after the existing one.
- 
        #update(delta_time)
        
          (used internally) Advances the Tweenby the given delta time
Macro Summary
- 
        bind_value(duration, ease, val, modifier = "", on_completed = ->(t : Tween) do
end)
        
          This macro can be used for very simple value tweening 
Constructor Detail
Creates a new tween with the following parameters (see details):
- duration: The tweening duration
- ease: The Easing::EasingDefinitionused 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)
Instance Method Detail
Returns the current interpolated fraction (calculated by the Easing::EasingDefinition)
Can be used to pause the Tween, meaning that its #fraction will stay the same until @paused is false again
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
  }Macro Detail
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.