Class: Transform

Inherits:
Object show all
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

Tween

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

Methods included from CodeEvents

#fire, #ignore, #upon

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(options={})
  @subject = @subject || self
	@options = OPTIONS.merge(options)
	@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

#cancelObject



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

#completeObject



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

#pauseObject



130
131
132
133
# File 'lib/source/redshift/transform.rb', line 130

def pause
  self.stop_timer
  self
end

#resumeObject



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_transitionObject



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_timerObject



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

#stepObject



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_timerObject



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