Class: Transform
- Includes:
- CodeEvents
- Defined in:
- lib/source/redshift/transform.rb
Overview
Class Transform
provides the ability for transition from a starting number value to a final number value by stepping through a series of sequential transformations using a transition algorithm.
Direct Known Subclasses
Defined Under Namespace
Modules: Parser
Constant Summary collapse
- OPTIONS =
{:fps => 50, :unit => false, :duration => 500, :link => 'ignore' }
- DURATIONS =
{:short => 250, :normal => 500, :long => 1000 }
- ALGORITHMS =
{'linear' => `function(p){return -(Math.cos(Math.PI * p) - 1) / 2;}`}
- Parsers =
[Parser::Color, Parser::Number, Parser::String]
Class Method Summary collapse
Instance Method Summary collapse
- #cancel ⇒ Object
- #check(caller) ⇒ Object
- #complete ⇒ Object
- #compute(from, to, delta) ⇒ Object
-
#initialize(options = {}) ⇒ Transform
constructor
A new instance of Transform.
- #pause ⇒ Object
- #resume ⇒ Object
- #set(now) ⇒ Object
- #set_transition ⇒ Object
- #start(from, to) ⇒ Object
- #start_timer ⇒ Object
- #step ⇒ Object
- #stop_timer ⇒ Object
- #transition(transition, *args) ⇒ Object
Methods included from CodeEvents
Constructor Details
#initialize(options = {}) ⇒ Transform
Returns a new instance of Transform.
59 60 61 62 63 64 65 |
# File 'lib/source/redshift/transform.rb', line 59 def initialize(={}) @subject = @subject || self @options = OPTIONS.merge() @options[:duration] = Transform::DURATIONS[@options[:duration]] || @options[:duration].to_i wait = @options[:wait] @options[:link] = 'cancel' if wait === false end |
Class Method Details
.add_transition(name, func) ⇒ Object
20 21 22 23 24 25 |
# File 'lib/source/redshift/transform.rb', line 20 def self.add_transition(name, func) Transform::ALGORITHMS[name] = func Transform::ALGORITHMS["#{name}:in"] = `function(pos){return func(pos, params);}` Transform::ALGORITHMS["#{name}:out"] = `function(pos){return 1 - func(1 - pos, params);}` Transform::ALGORITHMS["#{name}:in:out"] = `function(pos){return (pos <= 0.5) ? func(2 * pos, params) / 2 : (2 - func(2 * (1 - pos), params)) / 2;}` end |
.compute(from, to, delta) ⇒ Object
55 56 57 |
# File 'lib/source/redshift/transform.rb', line 55 def self.compute(from, to, delta) `(to - from) * delta + from` end |
Instance Method Details
#cancel ⇒ Object
124 125 126 127 128 |
# File 'lib/source/redshift/transform.rb', line 124 def cancel self.fire(:cancellation) self.stop_timer self end |
#check(caller) ⇒ Object
93 94 95 96 97 98 99 100 101 |
# File 'lib/source/redshift/transform.rb', line 93 def check(caller) ` if (!this.__timer__) return true; switch (#{@options[:link]}){ case 'cancel': this.cancel(); return true; case 'chain' : this.chain(caller.bind(this, Array.slice(arguments, 1))); return false; }` return false end |
#complete ⇒ Object
118 119 120 121 122 |
# File 'lib/source/redshift/transform.rb', line 118 def complete self.fire(:completion) self.stop_timer self end |
#compute(from, to, delta) ⇒ Object
89 90 91 |
# File 'lib/source/redshift/transform.rb', line 89 def compute(from, to, delta) return Transform.compute(from, to, delta) end |
#pause ⇒ Object
130 131 132 133 |
# File 'lib/source/redshift/transform.rb', line 130 def pause self.stop_timer self end |
#resume ⇒ Object
135 136 137 138 |
# File 'lib/source/redshift/transform.rb', line 135 def resume self.start_timer self end |
#set(now) ⇒ Object
85 86 87 |
# File 'lib/source/redshift/transform.rb', line 85 def set(now) return now end |
#set_transition ⇒ Object
81 82 83 |
# File 'lib/source/redshift/transform.rb', line 81 def set_transition `this.__transition__ = #{Transform::ALGORITHMS[(@options[:transition] || 'sine:in:out')]}` end |
#start(from, to) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/source/redshift/transform.rb', line 103 def start(from,to) `if (!this.m$check(arguments.callee, from, to)) return this` `this.__from__ = from` `this.__to__ = to` `this.__time__ = 0` # `this.m$set_transition` `this.__transition__ = function(p){ return -(Math.cos(Math.PI * p) - 1) / 2; }` self.start_timer self.fire(:start) return self end |
#start_timer ⇒ Object
147 148 149 150 151 152 |
# File 'lib/source/redshift/transform.rb', line 147 def start_timer `if (this.__timer__) return false` `this.__time__ = (+new Date) - this.__time__` `this.__timer__ = this.m$step.periodical(Math.round(1000 / #{@options[:fps]}), this)` return true end |
#step ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/source/redshift/transform.rb', line 67 def step ` var time = +new Date if (time < this.__time__ + #{@options[:duration]}){ var delta = this.__transition__((time - this.__time__) / #{@options[:duration]}); this.m$set(this.m$compute(this.__from__, this.__to__, delta)); } else { this.m$set(this.m$compute(this.__from__, this.__to__, 1)); this.m$complete(); } ` return nil end |
#stop_timer ⇒ Object
140 141 142 143 144 145 |
# File 'lib/source/redshift/transform.rb', line 140 def stop_timer `if (!this.__timer__) return false` `this.__time__ = (+new Date) - this.__time__` `this.__timer__ = $clear(this.__timer__)` return true end |
#transition(transition, *args) ⇒ Object
27 28 29 |
# File 'lib/source/redshift/transform.rb', line 27 def transition(transition, *args) `#{ALGORITHMS[transition]}(args)` end |