- replaced each $function to the relevant alternative.
[mootools/dkf.git] / Source / Fx / Fx.Transitions.js
blobd22262926b3be525955e0e54e78df586ded54da1
1 /*
2 ---
4 name: Fx.Transitions
6 description: Contains a set of advanced transitions to be used with any of the Fx Classes.
8 license: MIT-style license.
10 credits:
11   - Easing Equations by Robert Penner, <http://www.robertpenner.com/easing/>, modified and optimized to be used with MooTools.
13 requires: Fx
15 provides: Fx.Transitions
17 ...
20 Fx.implement({
22         getTransition: function(){
23                 var trans = this.options.transition || Fx.Transitions.Sine.easeInOut;
24                 if (typeof trans == 'string'){
25                         var data = trans.split(':');
26                         trans = Fx.Transitions;
27                         trans = trans[data[0]] || trans[data[0].capitalize()];
28                         if (data[1]) trans = trans['ease' + data[1].capitalize() + (data[2] ? data[2].capitalize() : '')];
29                 }
30                 return trans;
31         }
33 });
35 Fx.Transition = function(transition, params){
36         params = Array.from(params);
37         return Object.append(transition, {
38                 easeIn: function(pos){
39                         return transition(pos, params);
40                 },
41                 easeOut: function(pos){
42                         return 1 - transition(1 - pos, params);
43                 },
44                 easeInOut: function(pos){
45                         return (pos <= 0.5) ? transition(2 * pos, params) / 2 : (2 - transition(2 * (1 - pos), params)) / 2;
46                 }
47         });
50 Fx.Transitions = new Hash({
52         linear: function(zero){
53                 return zero;
54         }
56 });
58 Fx.Transitions.extend = function(transitions){
59         for (var transition in transitions) Fx.Transitions[transition] = new Fx.Transition(transitions[transition]);
62 Fx.Transitions.extend({
64         Pow: function(p, x){
65                 return Math.pow(p, x[0] || 6);
66         },
68         Expo: function(p){
69                 return Math.pow(2, 8 * (p - 1));
70         },
72         Circ: function(p){
73                 return 1 - Math.sin(Math.acos(p));
74         },
76         Sine: function(p){
77                 return 1 - Math.sin((1 - p) * Math.PI / 2);
78         },
80         Back: function(p, x){
81                 x = x[0] || 1.618;
82                 return Math.pow(p, 2) * ((x + 1) * p - x);
83         },
85         Bounce: function(p){
86                 var value;
87                 for (var a = 0, b = 1; 1; a += b, b /= 2){
88                         if (p >= (7 - 4 * a) / 11){
89                                 value = b * b - Math.pow((11 - 6 * a - 11 * p) / 4, 2);
90                                 break;
91                         }
92                 }
93                 return value;
94         },
96         Elastic: function(p, x){
97                 return Math.pow(2, 10 * --p) * Math.cos(20 * p * Math.PI * (x[0] || 1) / 3);
98         }
102 ['Quad', 'Cubic', 'Quart', 'Quint'].each(function(transition, i){
103         Fx.Transitions[transition] = new Fx.Transition(function(p){
104                 return Math.pow(p, [i + 2]);
105         });