Class: Plushie::Animation::Transition

Inherits:
Data
  • Object
show all
Defined in:
lib/plushie/animation/transition.rb

Overview

Renderer-side timed transition descriptor.

Declares animation intent in the view. The renderer handles interpolation locally with zero wire traffic during animation.

Examples:

Basic transition

button("fade", "Click",
  opacity: Transition.build(300, to: 0.0))

With easing and delay

container("slide",
  translate_y: Transition.build(200, to: 0, from: 20,
    easing: :ease_out, delay: 50))

Looping

text("pulse", "!",
  opacity: Transition.loop(800, to: 0.4, from: 1.0))

Completion event

container("item",
  opacity: Transition.build(300, to: 0.0, on_complete: :faded_out))

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(to:, duration: nil, easing: :ease_in_out, delay: 0, from: nil, repeat: nil, auto_reverse: false, on_complete: nil) ⇒ Transition

Returns a new instance of Transition.

Parameters:

  • to (Object)

    target value (required)

  • duration (Integer, nil) (defaults to: nil)

    duration in milliseconds

  • easing (Symbol) (defaults to: :ease_in_out)

    easing curve name (default: :ease_in_out)

  • delay (Integer) (defaults to: 0)

    delay before start in milliseconds (default: 0)

  • from (Object, nil) (defaults to: nil)

    starting value (nil = current value)

  • repeat (Integer, :forever, nil) (defaults to: nil)

    repeat count

  • auto_reverse (Boolean) (defaults to: false)

    reverse on each cycle (default: false)

  • on_complete (Symbol, nil) (defaults to: nil)

    event tag fired on completion



39
40
41
42
43
44
# File 'lib/plushie/animation/transition.rb', line 39

def initialize(
  to:, duration: nil, easing: :ease_in_out, delay: 0,
  from: nil, repeat: nil, auto_reverse: false, on_complete: nil
)
  super
end

Instance Attribute Details

#auto_reverseObject (readonly)

Returns the value of attribute auto_reverse

Returns:

  • (Object)

    the current value of auto_reverse



27
28
29
# File 'lib/plushie/animation/transition.rb', line 27

def auto_reverse
  @auto_reverse
end

#delayObject (readonly)

Returns the value of attribute delay

Returns:

  • (Object)

    the current value of delay



27
28
29
# File 'lib/plushie/animation/transition.rb', line 27

def delay
  @delay
end

#durationObject (readonly)

Returns the value of attribute duration

Returns:

  • (Object)

    the current value of duration



27
28
29
# File 'lib/plushie/animation/transition.rb', line 27

def duration
  @duration
end

#easingObject (readonly)

Returns the value of attribute easing

Returns:

  • (Object)

    the current value of easing



27
28
29
# File 'lib/plushie/animation/transition.rb', line 27

def easing
  @easing
end

#fromObject (readonly)

Returns the value of attribute from

Returns:

  • (Object)

    the current value of from



27
28
29
# File 'lib/plushie/animation/transition.rb', line 27

def from
  @from
end

#on_completeObject (readonly)

Returns the value of attribute on_complete

Returns:

  • (Object)

    the current value of on_complete



27
28
29
# File 'lib/plushie/animation/transition.rb', line 27

def on_complete
  @on_complete
end

#repeatObject (readonly)

Returns the value of attribute repeat

Returns:

  • (Object)

    the current value of repeat



27
28
29
# File 'lib/plushie/animation/transition.rb', line 27

def repeat
  @repeat
end

#toObject (readonly)

Returns the value of attribute to

Returns:

  • (Object)

    the current value of to



27
28
29
# File 'lib/plushie/animation/transition.rb', line 27

def to
  @to
end

Class Method Details

.build(duration, **opts) ⇒ Transition

Create a transition with duration as a positional argument.

Parameters:

  • duration (Integer)

    duration in milliseconds

  • opts (Hash)

    transition options (must include :to)

Returns:

Raises:

  • (ArgumentError)


64
65
66
67
68
69
# File 'lib/plushie/animation/transition.rb', line 64

def self.build(duration, **opts)
  raise ArgumentError, "duration must be an Integer" unless duration.is_a?(Integer)
  raise ArgumentError, "transition requires a :to value" unless opts.key?(:to)

  new(duration: duration, **opts)
end

.loop(duration, cycles: nil, reverse: true, **opts) ⇒ Transition

Create a looping transition.

Parameters:

  • duration (Integer)

    duration per cycle in milliseconds

  • cycles (Integer, nil) (defaults to: nil)

    number of cycles (nil = forever)

  • reverse (Boolean) (defaults to: true)

    auto-reverse on each cycle (default: true)

  • opts (Hash)

    must include :to

Returns:



78
79
80
81
82
83
# File 'lib/plushie/animation/transition.rb', line 78

def self.loop(duration, cycles: nil, reverse: true, **opts)
  build(duration,
    repeat: cycles || :forever,
    auto_reverse: reverse,
    **opts)
end

Instance Method Details

#to_wireHash

Returns wire-ready descriptor map.

Returns:

  • (Hash)

    wire-ready descriptor map



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/plushie/animation/transition.rb', line 47

def to_wire
  h = {type: "transition", to: to}
  h[:duration] = duration if duration
  h[:easing] = easing.to_s unless easing == :ease_in_out
  h[:delay] = delay unless delay == 0
  h[:from] = from unless from.nil?
  h[:repeat] = (repeat == :forever) ? -1 : repeat if repeat
  h[:auto_reverse] = auto_reverse if auto_reverse
  h[:on_complete] = on_complete.to_s if on_complete
  h
end