Class: Castaway::Animation

Inherits:
Object
  • Object
show all
Defined in:
lib/castaway/animation.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(interpolator, from, to) ⇒ Animation

Returns a new instance of Animation.



23
24
25
26
27
28
# File 'lib/castaway/animation.rb', line 23

def initialize(interpolator, from, to)
  @interpolator = interpolator
  @from = from.to_f
  @to = to.to_f
  @duration = @to - @from
end

Instance Attribute Details

#durationObject (readonly)

Returns the value of attribute duration.



6
7
8
# File 'lib/castaway/animation.rb', line 6

def duration
  @duration
end

#fromObject (readonly)

Returns the value of attribute from.



6
7
8
# File 'lib/castaway/animation.rb', line 6

def from
  @from
end

#toObject (readonly)

Returns the value of attribute to.



6
7
8
# File 'lib/castaway/animation.rb', line 6

def to
  @to
end

Class Method Details

.from_options(options) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/castaway/animation.rb', line 8

def self.from_options(options)
  factory = Castaway::Interpolation.lookup(options)
  initial = options[:initial] || 0.0
  final = options[:final] || (initial + 1.0)
  interpolator = factory.new(initial, final)

  from  = options[:from] ||
          raise(ArgumentError, 'animations require `from` time')
  to    = options[:to] ||
          (options[:length] ? from + options[:length] : nil) ||
          raise(ArgumentError, 'animations require `to` or `length`')

  new(interpolator, from, to)
end

Instance Method Details

#[](t) ⇒ Object



30
31
32
33
34
# File 'lib/castaway/animation.rb', line 30

def [](t)
  # adjust t to 0..1
  adjusted_t = duration.zero? ? 1.0 : (t - from) / duration.to_f
  @interpolator[adjusted_t]
end