Class: Transform

Inherits:
Object show all
Includes:
CodeEvents
Defined in:
lib/source/redshift/transform.rb

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.



44
45
46
47
48
49
50
# File 'lib/source/redshift/transform.rb', line 44

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



40
41
42
# File 'lib/source/redshift/transform.rb', line 40

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

Instance Method Details

#cancelObject



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

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

#check(caller) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/source/redshift/transform.rb', line 74

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



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

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

#compute(from, to, delta) ⇒ Object



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

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

#pauseObject



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

def pause
  self.stop_timer
  self
end

#resumeObject



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

def resume
 self.start_timer
 self
end

#set(now) ⇒ Object



66
67
68
# File 'lib/source/redshift/transform.rb', line 66

def set(now)
  return now
end

#start(from, to) ⇒ Object



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

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



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

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



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/source/redshift/transform.rb', line 52

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



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

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