Class: CheekyPi::Animation::Base
- Inherits:
-
Object
- Object
- CheekyPi::Animation::Base
- Defined in:
- lib/cheeky-pi/animation/base.rb
Constant Summary collapse
- TICKRATE =
0.033
Instance Method Summary collapse
- #animate ⇒ Object
- #complete ⇒ Object
- #halt ⇒ Object
- #handle_complete ⇒ Object
-
#initialize(duration, light, options = {}) ⇒ Base
constructor
A new instance of Base.
- #percent ⇒ Object
- #promise ⇒ Object
- #safe_percent ⇒ Object
- #sine(percent) ⇒ Object
- #start ⇒ Object
- #tick ⇒ Object
-
#triangle(percent) ⇒ Object
convert [0-0.5-1] to [0-1-0].
Constructor Details
#initialize(duration, light, options = {}) ⇒ Base
Returns a new instance of Base.
11 12 13 14 15 16 17 18 19 |
# File 'lib/cheeky-pi/animation/base.rb', line 11 def initialize(duration, light, ={}) @duration = duration @light = light @deferred = EventMachine::Q.defer promise.then { handle_complete } start end |
Instance Method Details
#animate ⇒ Object
64 65 |
# File 'lib/cheeky-pi/animation/base.rb', line 64 def animate end |
#complete ⇒ Object
52 53 54 55 56 57 |
# File 'lib/cheeky-pi/animation/base.rb', line 52 def complete @timer.cancel @deferred.resolve self end |
#halt ⇒ Object
45 46 47 48 49 50 |
# File 'lib/cheeky-pi/animation/base.rb', line 45 def halt @timer.cancel @deferred.reject self end |
#handle_complete ⇒ Object
21 22 |
# File 'lib/cheeky-pi/animation/base.rb', line 21 def handle_complete end |
#percent ⇒ Object
36 37 38 39 |
# File 'lib/cheeky-pi/animation/base.rb', line 36 def percent return 0 if @duration.nil? (Time.now.to_f - @start_time) / @duration end |
#promise ⇒ Object
24 25 26 |
# File 'lib/cheeky-pi/animation/base.rb', line 24 def promise @promise ||= @deferred.promise end |
#safe_percent ⇒ Object
41 42 43 |
# File 'lib/cheeky-pi/animation/base.rb', line 41 def safe_percent [percent, 1].min end |
#sine(percent) ⇒ Object
72 73 74 |
# File 'lib/cheeky-pi/animation/base.rb', line 72 def sine(percent) Math::sin(percent * 2 * Math::PI - 0.5 * Math::PI) * 0.5 + 0.5 end |
#start ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/cheeky-pi/animation/base.rb', line 28 def start @timer.cancel if @timer @start_time = Time.now.to_f @timer = EventMachine.add_periodic_timer(TICKRATE) { tick } self end |
#tick ⇒ Object
59 60 61 62 |
# File 'lib/cheeky-pi/animation/base.rb', line 59 def tick complete if percent >=1 animate end |
#triangle(percent) ⇒ Object
convert [0-0.5-1] to [0-1-0]
68 69 70 |
# File 'lib/cheeky-pi/animation/base.rb', line 68 def triangle(percent) (0.5 - (percent % 1 - 0.5).abs) * 2 end |