Class: Rustle::Transition

Inherits:
Object
  • Object
show all
Defined in:
lib/rustle/transition.rb

Overview

Transitions

Transitions are created by subclassing this class. See FadeToTransition for the most basic example.

Creating a Transition

You’ll need to create a subclass of Transition and require it in your project.

Override the subclass’s #setup and #animate methods.

Use setup to create any instance variables you may need to calculate before the transitio renders. The opts passed in when calling Strip#transition are preserved and sent to this method.

#animate is called once-per-LED-per-frame, and expects you to return a object.

Direct Known Subclasses

FadeToTransition, WipeToTransition

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(strip, duration, opts = {}) ⇒ Transition

Returns a new instance of Transition.



21
22
23
24
25
26
27
28
29
30
# File 'lib/rustle/transition.rb', line 21

def initialize(strip, duration, opts = {})
  @strip = strip
  @duration = duration
  @total_frames = (duration.to_f / (1000/Rustle::FRAME_RATE.to_f)).ceil
  @frame_duration = 1.0/Rustle::FRAME_RATE

  self.setup(opts)

  render
end

Instance Attribute Details

#durationObject (readonly)

Returns the value of attribute duration.



19
20
21
# File 'lib/rustle/transition.rb', line 19

def duration
  @duration
end

#framesObject (readonly)

Returns the value of attribute frames.



19
20
21
# File 'lib/rustle/transition.rb', line 19

def frames
  @frames
end

Instance Method Details

#animate(led_index, starting_color, frame_num) ⇒ Color

Gets called once-per-LED-per-frame. Override it, and do your calculations therein. You will have access to any instance variables you declare in #setup.

Within this method, you also have the following available:

  • @total_frames: the total number of frames in the animation

  • @duration: the duration of the animation in milliseconds

  • @frame_duration: the duration of an individual frame (generally only used internally)

Parameters:

  • led_index (Fixnum)

    the index of the current LED, starting at 0.

  • starting_color (Color)

    the color that the current LED was displaying before the transition began

  • frame_num (Fixnum)

    the current frame number in the transition

Returns:

  • (Color)

    a color object corresponding to what the new LED color should be.



62
# File 'lib/rustle/transition.rb', line 62

def animate(led_index, starting_color, frame_num); end

#setup(opts) ⇒ Object

Gets called before the transition begins. Override it to set up any instance variables you need in the #animate method.

Within this method, you also have the following available:

  • @total_frames: the total number of frames in the animation

  • @duration: the duration of the animation in milliseconds

  • @frame_duration: the duration of an individual frame (generally only used internally)

Parameters:

  • opts (Hash)

    the options hash that is passed in when calling Strip#serialize



43
# File 'lib/rustle/transition.rb', line 43

def setup(opts); end