6 description: Contains the basic animation logic to be extended by all other Fx Classes.
8 license: MIT-style license.
10 requires: [Chain, Events, Options]
19 var Fx = this.Fx = new Class({
21 Implements: [Chain, Events, Options],
35 initialize: function(options){
36 this.subject = this.subject || this;
37 this.setOptions(options);
40 getTransition: function(){
42 return -(Math.cos(Math.PI * p) - 1) / 2;
47 var time = Date.now();
48 if (time < this.time + this.options.duration){
49 var delta = this.transition((time - this.time) / this.options.duration);
50 this.set(this.compute(this.from, this.to, delta));
52 this.set(this.compute(this.from, this.to, 1));
61 compute: function(from, to, delta){
62 return Fx.compute(from, to, delta);
66 if (!this.timer) return true;
67 switch (this.options.link){
68 case 'cancel': this.cancel(); return true;
69 case 'chain': this.chain(this.caller.pass(arguments, this)); return false;
74 start: function(from, to){
75 if (!this.check(from, to)) return this;
76 var duration = this.options.duration;
77 this.options.duration = Fx.Durations[duration] || duration.toInt();
81 this.transition = this.getTransition();
88 if (this.stopTimer()) this.onComplete();
93 if (this.stopTimer()) this.onCancel();
98 this.triggerEvent('start', this.subject);
101 onComplete: function(){
102 this.triggerEvent('complete', this.subject);
103 if (!this.callChain()) this.triggerEvent('chainComplete', this.subject);
106 onCancel: function(){
107 this.triggerEvent('cancel', this.subject).clearChain();
120 stopTimer: function(){
121 if (!this.timer) return false;
122 this.time = Date.now() - this.time;
123 this.timer = removeInstance(this);
127 startTimer: function(){
128 if (this.timer) return false;
129 this.time = Date.now() - this.time;
130 this.timer = addInstance(this);
136 Fx.compute = function(from, to, delta){
137 return (to - from) * delta + from;
140 Fx.Durations = {'short': 250, 'normal': 500, 'long': 1000};
144 var instances = {}, timers = {};
146 var loop = function(){
147 for (var i = this.length; i--;){
148 if (this[i]) this[i].step();
152 var addInstance = function(instance){
153 var fps = instance.options.fps,
154 list = instances[fps] || (instances[fps] = []);
156 if (!timers[fps]) timers[fps] = loop.periodical(Math.round(1000 / fps), list);
160 var removeInstance = function(instance){
161 var fps = instance.options.fps,
162 list = instances[fps] || [];
163 list.erase(instance);
164 if (!list.length && timers[fps]) timers[fps] = clearInterval(timers[fps]);