class Eagle::Tween

Overview

Animates a value over time, so you don't have to track timers yourself.

A tween calls your block every frame with progress eased from 0 to 1, or with an interpolated value, until its duration is up. The engine updates tweens automatically, and they follow Clock.scale.

class Popup < Node2D
  def ready : Nil
    self.scale = 0.0
    # Grow in with a springy overshoot.
    Tween.value(0, 1, 0.4, ease: :back_out) { |s| self.scale = s }

    # Fade out after 2 seconds, then remove.
    Tween.value(Color::WHITE, Color::TRANSPARENT, 0.5, delay: 2) { |c| self.modulate = c }
      .on_complete { queue_free }
  end
end

# Run steps one after another.
Tween.sequence do |s|
  s.wait(1)
  s.to(0.5, ease: :quad_out) { |t| puts "sliding #{t}" }
  s.call { puts "done" }
end

Tween.after(3) { puts "three seconds later" }

Keep the returned Tween if you need to #stop it early, or set loop or ping_pong for animations that repeat.

Defined in:

eagle/core/tween.cr

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.after(seconds : Number, &block : -> ) : Tween #

Calls the block once after seconds. A timer without a node.


def self.new(duration : Number, ease : Symbol | Proc(Float32, Float32) = :linear, delay : Number = 0, &block : Float32 -> ) #

Creates a tween without starting it. Call #start when ready, or use Tween.to, which creates and starts in one step.


def self.to(duration : Number, ease : Symbol | Proc(Float32, Float32) = :linear, delay : Number = 0, &block : Float32 -> ) : Tween #

Starts a tween whose block receives eased progress from 0 to 1. Use it to drive any property you like.

start, target = v2(0, 0), v2(300, 200)
node = Node2D.new
Tween.to(1.5, ease: :sine_in_out) { |t| node.position = start.lerp(target, t) }

def self.value(from : Number, to : Number, duration : Number, ease : Symbol = :linear, delay : Number = 0, &block : Float32 -> ) : Tween #

Tweens a number from from to to and passes each value to the block.


def self.value(from : Vec2, to : Vec2, duration : Number, ease : Symbol = :linear, delay : Number = 0, &block : Vec2 -> ) : Tween #

Tweens a Vec2, for moving things between two points.


def self.value(from : Vec3, to : Vec3, duration : Number, ease : Symbol = :linear, delay : Number = 0, &block : Vec3 -> ) : Tween #

Tweens a Vec3.


def self.value(from : Color, to : Color, duration : Number, ease : Symbol = :linear, delay : Number = 0, &block : Color -> ) : Tween #

Tweens a Color, for fades and flashes.


Class Method Detail

def self.active_count : Int32 #

Number of tweens currently running.


def self.clear : Nil #

Stops every running tween, for example when changing scenes.


def self.sequence(&) : Sequence #

Builds a chain of steps with the yielded Sequence and starts it.


Instance Method Detail

def delay : Float32 #

Seconds to wait before starting.


def delay=(delay : Float32) #

Seconds to wait before starting.


def duration : Float32 #

Length of the tween in seconds, not counting #delay.


def elapsed : Float32 #

Seconds played so far.


def finished? : Bool #

True once the tween has reached the end and called its completion blocks.


def loop=(loop : Bool) #

Restarts from the beginning when it finishes, forever.


def loop? : Bool #

Restarts from the beginning when it finishes, forever.


def on_complete(&block : -> ) : self #

Adds a block to run when the tween finishes. Returns self so calls can be chained.


def paused=(paused : Bool) #

Freezes the tween in place while true.


def paused? : Bool #

Freezes the tween in place while true.


def ping_pong=(ping_pong : Bool) #

With loop, plays forward then backward alternately.


def ping_pong? : Bool #

With loop, plays forward then backward alternately.


def progress : Float32 #

Linear progress from 0 to 1, before easing.


def start : self #

Starts or resumes updating this tween. Returns self.


def stop : Nil #

Removes the tween from the update list. Its completion blocks won't run.