-
Notifications
You must be signed in to change notification settings - Fork 2
/
easing.pde
182 lines (178 loc) · 4.68 KB
/
easing.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* The map2() function supports the following easing types */
final int LINEAR = 0;
final int QUADRATIC = 1;
final int CUBIC = 2;
final int QUARTIC = 3;
final int QUINTIC = 4;
final int SINUSOIDAL = 5;
final int EXPONENTIAL = 6;
final int CIRCULAR = 7;
final int SQRT = 8;
/* When the easing is applied (in, out, or both) */
final int EASE_IN = 0;
final int EASE_OUT = 1;
final int EASE_IN_OUT = 2;
/*
* A map() replacement that allows for specifying easing curves
* with arbitrary exponents.
*
* value : The value to map
* start1: The lower limit of the input range
* stop1 : The upper limit of the input range
* start2: The lower limit of the output range
* stop2 : The upper limit of the output range
* type : The type of easing (see above)
* when : One of EASE_IN, EASE_OUT, or EASE_IN_OUT
*/
float map2(float value, float start1, float stop1, float start2, float stop2, int type, int when) {
float b = start2;
float c = stop2 - start2;
float t = value - start1;
float d = stop1 - start1;
float p = 0.5;
switch (type) {
case LINEAR:
return c*t/d + b;
case SQRT:
if (when == EASE_IN) {
t /= d;
return c*pow(t, p) + b;
} else if (when == EASE_OUT) {
t /= d;
return c * (1 - pow(1 - t, p)) + b;
} else if (when == EASE_IN_OUT) {
t /= d/2;
if (t < 1) return c/2*pow(t, p) + b;
return c/2 * (2 - pow(2 - t, p)) + b;
}
break;
case QUADRATIC:
if (when == EASE_IN) {
t /= d;
return c*t*t + b;
} else if (when == EASE_OUT) {
t /= d;
return -c * t*(t-2) + b;
} else if (when == EASE_IN_OUT) {
t /= d/2;
if (t < 1) return c/2*t*t + b;
t--;
return -c/2 * (t*(t-2) - 1) + b;
}
break;
case CUBIC:
if (when == EASE_IN) {
t /= d;
return c*t*t*t + b;
} else if (when == EASE_OUT) {
t /= d;
t--;
return c*(t*t*t + 1) + b;
} else if (when == EASE_IN_OUT) {
t /= d/2;
if (t < 1) return c/2*t*t*t + b;
t -= 2;
return c/2*(t*t*t + 2) + b;
}
break;
case QUARTIC:
if (when == EASE_IN) {
t /= d;
return c*t*t*t*t + b;
} else if (when == EASE_OUT) {
t /= d;
t--;
return -c * (t*t*t*t - 1) + b;
} else if (when == EASE_IN_OUT) {
t /= d/2;
if (t < 1) return c/2*t*t*t*t + b;
t -= 2;
return -c/2 * (t*t*t*t - 2) + b;
}
break;
case QUINTIC:
if (when == EASE_IN) {
t /= d;
return c*t*t*t*t*t + b;
} else if (when == EASE_OUT) {
t /= d;
t--;
return c*(t*t*t*t*t + 1) + b;
} else if (when == EASE_IN_OUT) {
t /= d/2;
if (t < 1) return c/2*t*t*t*t*t + b;
t -= 2;
return c/2*(t*t*t*t*t + 2) + b;
}
break;
case SINUSOIDAL:
if (when == EASE_IN) {
return -c * cos(t/d * (PI/2)) + c + b;
} else if (when == EASE_OUT) {
return c * sin(t/d * (PI/2)) + b;
} else if (when == EASE_IN_OUT) {
return -c/2 * (cos(PI*t/d) - 1) + b;
}
break;
case EXPONENTIAL:
if (when == EASE_IN) {
return c * pow( 2, 10 * (t/d - 1) ) + b;
} else if (when == EASE_OUT) {
return c * ( -pow( 2, -10 * t/d ) + 1 ) + b;
} else if (when == EASE_IN_OUT) {
t /= d/2;
if (t < 1) return c/2 * pow( 2, 10 * (t - 1) ) + b;
t--;
return c/2 * ( -pow( 2, -10 * t) + 2 ) + b;
}
break;
case CIRCULAR:
if (when == EASE_IN) {
t /= d;
return -c * (sqrt(1 - t*t) - 1) + b;
} else if (when == EASE_OUT) {
t /= d;
t--;
return c * sqrt(1 - t*t) + b;
} else if (when == EASE_IN_OUT) {
t /= d/2;
if (t < 1) return -c/2 * (sqrt(1 - t*t) - 1) + b;
t -= 2;
return c/2 * (sqrt(1 - t*t) + 1) + b;
}
break;
};
return 0;
}
/*
* A map() replacement that allows for specifying easing curves
* with arbitrary exponents.
*
* value : The value to map
* start1: The lower limit of the input range
* stop1 : The upper limit of the input range
* start2: The lower limit of the output range
* stop2 : The upper limit of the output range
* v : The exponent value (e.g., 0.5, 0.1, 0.3)
* when : One of EASE_IN, EASE_OUT, or EASE_IN_OUT
*/
float map3(float value, float start1, float stop1, float start2, float stop2, float v, int when) {
float b = start2;
float c = stop2 - start2;
float t = value - start1;
float d = stop1 - start1;
float p = v;
float out = 0;
if (when == EASE_IN) {
t /= d;
out = c*pow(t, p) + b;
} else if (when == EASE_OUT) {
t /= d;
out = c * (1 - pow(1 - t, p)) + b;
} else if (when == EASE_IN_OUT) {
t /= d/2;
if (t < 1) return c/2*pow(t, p) + b;
out = c/2 * (2 - pow(2 - t, p)) + b;
}
return out;
}