class Rustle::Transition

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 {Color object}.

Attributes

duration[R]
frames[R]

Public Class Methods

new(strip, duration, opts = {}) click to toggle source
# 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

Public Instance Methods

animate(led_index, starting_color, frame_num) click to toggle source

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}.

@param [Fixnum] led_index the index of the current LED, starting at 0. @param [Color] starting_color the color that the current LED was

displaying before the transition began

@param [Fixnum] frame_num the current frame number in the transition

@return [Color] a color object corresponding to what the new LED color

should be.

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)

# File lib/rustle/transition.rb, line 62
def animate(led_index, starting_color, frame_num); end
setup(opts) click to toggle source

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

@param [Hash] opts the options hash that is passed in when calling

{Strip#serialize}

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)

# File lib/rustle/transition.rb, line 43
def setup(opts); end

Private Instance Methods

render() click to toggle source
# File lib/rustle/transition.rb, line 66
def render
  starting_frame = @strip.current_frame

  @frames = Array.new(@total_frames) do |frame_num|
    Frame.new(
      Array.new(@strip.num_leds) do |led_index|
        # Gets called once per LED per frame
        animate(led_index, starting_frame.leds[led_index], frame_num)
      end
    )
  end

  @strip.queue_frames @frames

  @total_frames.times do
    @strip.next_frame!
    sleep @frame_duration
  end
end