|
1 /* |
|
2 * TERMS OF USE - EASING EQUATIONS |
|
3 * |
|
4 * Open source under the BSD License. |
|
5 * |
|
6 * Copyright 2001 Robert Penner |
|
7 * All rights reserved. |
|
8 * |
|
9 * Redistribution and use in source and binary forms, with or without |
|
10 * modification, are permitted provided that the following conditions are met: |
|
11 * |
|
12 * - Redistributions of source code must retain the above copyright notice, this |
|
13 * list of conditions and the following disclaimer. |
|
14 * |
|
15 * - Redistributions in binary form must reproduce the above copyright notice, |
|
16 * this list of conditions and the following disclaimer in the documentation |
|
17 * and/or other materials provided with the distribution. |
|
18 * |
|
19 * - Neither the name of the author nor the names of contributors may be used to |
|
20 * endorse or promote products derived from this software without specific |
|
21 * prior written permission. |
|
22 * |
|
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
|
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
33 * POSSIBILITY OF SUCH DAMAGE. |
|
34 */ |
|
35 |
|
36 var d3_ease_quad = d3_ease_poly(2), |
|
37 d3_ease_cubic = d3_ease_poly(3); |
|
38 |
|
39 var d3_ease = { |
|
40 linear: function() { return d3_ease_linear; }, |
|
41 poly: d3_ease_poly, |
|
42 quad: function() { return d3_ease_quad; }, |
|
43 cubic: function() { return d3_ease_cubic; }, |
|
44 sin: function() { return d3_ease_sin; }, |
|
45 exp: function() { return d3_ease_exp; }, |
|
46 circle: function() { return d3_ease_circle; }, |
|
47 elastic: d3_ease_elastic, |
|
48 back: d3_ease_back, |
|
49 bounce: function() { return d3_ease_bounce; } |
|
50 }; |
|
51 |
|
52 var d3_ease_mode = { |
|
53 "in": function(f) { return f; }, |
|
54 "out": d3_ease_reverse, |
|
55 "in-out": d3_ease_reflect, |
|
56 "out-in": function(f) { return d3_ease_reflect(d3_ease_reverse(f)); } |
|
57 }; |
|
58 |
|
59 d3.ease = function(name) { |
|
60 var i = name.indexOf("-"), |
|
61 t = i >= 0 ? name.substring(0, i) : name, |
|
62 m = i >= 0 ? name.substring(i + 1) : "in"; |
|
63 return d3_ease_clamp(d3_ease_mode[m](d3_ease[t].apply(null, Array.prototype.slice.call(arguments, 1)))); |
|
64 }; |
|
65 |
|
66 function d3_ease_clamp(f) { |
|
67 return function(t) { |
|
68 return t <= 0 ? 0 : t >= 1 ? 1 : f(t); |
|
69 }; |
|
70 } |
|
71 |
|
72 function d3_ease_reverse(f) { |
|
73 return function(t) { |
|
74 return 1 - f(1 - t); |
|
75 }; |
|
76 } |
|
77 |
|
78 function d3_ease_reflect(f) { |
|
79 return function(t) { |
|
80 return .5 * (t < .5 ? f(2 * t) : (2 - f(2 - 2 * t))); |
|
81 }; |
|
82 } |
|
83 |
|
84 function d3_ease_linear(t) { |
|
85 return t; |
|
86 } |
|
87 |
|
88 function d3_ease_poly(e) { |
|
89 return function(t) { |
|
90 return Math.pow(t, e); |
|
91 } |
|
92 } |
|
93 |
|
94 function d3_ease_sin(t) { |
|
95 return 1 - Math.cos(t * Math.PI / 2); |
|
96 } |
|
97 |
|
98 function d3_ease_exp(t) { |
|
99 return Math.pow(2, 10 * (t - 1)); |
|
100 } |
|
101 |
|
102 function d3_ease_circle(t) { |
|
103 return 1 - Math.sqrt(1 - t * t); |
|
104 } |
|
105 |
|
106 function d3_ease_elastic(a, p) { |
|
107 var s; |
|
108 if (arguments.length < 2) p = 0.45; |
|
109 if (arguments.length < 1) { a = 1; s = p / 4; } |
|
110 else s = p / (2 * Math.PI) * Math.asin(1 / a); |
|
111 return function(t) { |
|
112 return 1 + a * Math.pow(2, 10 * -t) * Math.sin((t - s) * 2 * Math.PI / p); |
|
113 }; |
|
114 } |
|
115 |
|
116 function d3_ease_back(s) { |
|
117 if (!s) s = 1.70158; |
|
118 return function(t) { |
|
119 return t * t * ((s + 1) * t - s); |
|
120 }; |
|
121 } |
|
122 |
|
123 function d3_ease_bounce(t) { |
|
124 return t < 1 / 2.75 ? 7.5625 * t * t |
|
125 : t < 2 / 2.75 ? 7.5625 * (t -= 1.5 / 2.75) * t + .75 |
|
126 : t < 2.5 / 2.75 ? 7.5625 * (t -= 2.25 / 2.75) * t + .9375 |
|
127 : 7.5625 * (t -= 2.625 / 2.75) * t + .984375; |
|
128 } |