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



47
48
49
50
51
52
53
# File 'lib/source/redshift/transform.rb', line 47

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

.compute(from, to, delta) ⇒ Object



43
44
45
# File 'lib/source/redshift/transform.rb', line 43

def self.compute(from, to, delta)
	`(to - from) * delta + from`
end

Instance Method Details

#cancelObject



106
107
108
109
110
# File 'lib/source/redshift/transform.rb', line 106

def cancel
 self.fire(:cancellation)
 self.stop_timer
	 self
end

#check(caller) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/source/redshift/transform.rb', line 77

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



100
101
102
103
104
# File 'lib/source/redshift/transform.rb', line 100

def complete
 self.fire(:completion)
 self.stop_timer
 self
end

#compute(from, to, delta) ⇒ Object



73
74
75
# File 'lib/source/redshift/transform.rb', line 73

def compute(from, to, delta)
   return Transform.compute(from, to, delta) 
end

#pauseObject



112
113
114
115
# File 'lib/source/redshift/transform.rb', line 112

def pause
  self.stop_timer
  self
end

#resumeObject



117
118
119
120
# File 'lib/source/redshift/transform.rb', line 117

def resume
 self.start_timer
 self
end

#set(now) ⇒ Object



69
70
71
# File 'lib/source/redshift/transform.rb', line 69

def set(now)
  return now
end

#start(from, to) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/source/redshift/transform.rb', line 87

def start(from,to)
   `if (!this.m$check(arguments.callee, from, to)) return this`
	`this.__from__ = from`
	`this.__to__   = to`
	`this.__time__ = 0`
	`this.__transition__ = function(p){
		return -(Math.cos(Math.PI * p) - 1) / 2;
	}`
   self.start_timer
	self.fire(:start)
	return self
end

#start_timerObject



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

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



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/source/redshift/transform.rb', line 55

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



122
123
124
125
126
127
# File 'lib/source/redshift/transform.rb', line 122

def stop_timer
	`if (!this.__timer__) return false`
	`this.__time__ = (+new Date) - this.__time__`
	`this.__timer__ = $clear(this.__timer__)`
	return true
end