Skip to content

Commit 53ea370

Browse files
authored
Clean up easing functions (#8518)
* Clean up easing functions * Remove leftover comment
1 parent 7c75310 commit 53ea370

File tree

1 file changed

+60
-173
lines changed

1 file changed

+60
-173
lines changed

src/helpers/helpers.easing.js

Lines changed: 60 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -1,194 +1,84 @@
11
import {PI, TAU, HALF_PI} from './helpers.math';
22

3+
const atEdge = (t) => t === 0 || t === 1;
4+
const elasticIn = (t, s, p) => -(Math.pow(2, 10 * (t -= 1)) * Math.sin((t - s) * TAU / p));
5+
const elasticOut = (t, s, p) => Math.pow(2, -10 * t) * Math.sin((t - s) * TAU / p) + 1;
6+
37
/**
48
* Easing functions adapted from Robert Penner's easing equations.
59
* @namespace Chart.helpers.easing.effects
610
* @see http://www.robertpenner.com/easing/
711
*/
812
const effects = {
9-
linear(t) {
10-
return t;
11-
},
13+
linear: t => t,
1214

13-
easeInQuad(t) {
14-
return t * t;
15-
},
15+
easeInQuad: t => t * t,
1616

17-
easeOutQuad(t) {
18-
return -t * (t - 2);
19-
},
17+
easeOutQuad: t => -t * (t - 2),
2018

21-
easeInOutQuad(t) {
22-
if ((t /= 0.5) < 1) {
23-
return 0.5 * t * t;
24-
}
25-
return -0.5 * ((--t) * (t - 2) - 1);
26-
},
19+
easeInOutQuad: t => ((t /= 0.5) < 1)
20+
? 0.5 * t * t
21+
: -0.5 * ((--t) * (t - 2) - 1),
2722

28-
easeInCubic(t) {
29-
return t * t * t;
30-
},
23+
easeInCubic: t => t * t * t,
3124

32-
easeOutCubic(t) {
33-
return (t -= 1) * t * t + 1;
34-
},
25+
easeOutCubic: t => (t -= 1) * t * t + 1,
3526

36-
easeInOutCubic(t) {
37-
if ((t /= 0.5) < 1) {
38-
return 0.5 * t * t * t;
39-
}
40-
return 0.5 * ((t -= 2) * t * t + 2);
41-
},
27+
easeInOutCubic: t => ((t /= 0.5) < 1)
28+
? 0.5 * t * t * t
29+
: 0.5 * ((t -= 2) * t * t + 2),
4230

43-
easeInQuart(t) {
44-
return t * t * t * t;
45-
},
31+
easeInQuart: t => t * t * t * t,
4632

47-
easeOutQuart(t) {
48-
return -((t -= 1) * t * t * t - 1);
49-
},
33+
easeOutQuart: t => -((t -= 1) * t * t * t - 1),
5034

51-
easeInOutQuart(t) {
52-
if ((t /= 0.5) < 1) {
53-
return 0.5 * t * t * t * t;
54-
}
55-
return -0.5 * ((t -= 2) * t * t * t - 2);
56-
},
35+
easeInOutQuart: t => ((t /= 0.5) < 1)
36+
? 0.5 * t * t * t * t
37+
: -0.5 * ((t -= 2) * t * t * t - 2),
5738

58-
easeInQuint(t) {
59-
return t * t * t * t * t;
60-
},
39+
easeInQuint: t => t * t * t * t * t,
6140

62-
easeOutQuint(t) {
63-
return (t -= 1) * t * t * t * t + 1;
64-
},
41+
easeOutQuint: t => (t -= 1) * t * t * t * t + 1,
6542

66-
easeInOutQuint(t) {
67-
if ((t /= 0.5) < 1) {
68-
return 0.5 * t * t * t * t * t;
69-
}
70-
return 0.5 * ((t -= 2) * t * t * t * t + 2);
71-
},
43+
easeInOutQuint: t => ((t /= 0.5) < 1)
44+
? 0.5 * t * t * t * t * t
45+
: 0.5 * ((t -= 2) * t * t * t * t + 2),
7246

73-
easeInSine(t) {
74-
return -Math.cos(t * HALF_PI) + 1;
75-
},
47+
easeInSine: t => -Math.cos(t * HALF_PI) + 1,
7648

77-
easeOutSine(t) {
78-
return Math.sin(t * HALF_PI);
79-
},
49+
easeOutSine: t => Math.sin(t * HALF_PI),
8050

81-
easeInOutSine(t) {
82-
return -0.5 * (Math.cos(PI * t) - 1);
83-
},
51+
easeInOutSine: t => -0.5 * (Math.cos(PI * t) - 1),
8452

85-
easeInExpo(t) {
86-
return (t === 0) ? 0 : Math.pow(2, 10 * (t - 1));
87-
},
53+
easeInExpo: t => (t === 0) ? 0 : Math.pow(2, 10 * (t - 1)),
8854

89-
easeOutExpo(t) {
90-
return (t === 1) ? 1 : -Math.pow(2, -10 * t) + 1;
91-
},
55+
easeOutExpo: t => (t === 1) ? 1 : -Math.pow(2, -10 * t) + 1,
9256

93-
easeInOutExpo(t) {
94-
if (t === 0) {
95-
return 0;
96-
}
97-
if (t === 1) {
98-
return 1;
99-
}
100-
if ((t /= 0.5) < 1) {
101-
return 0.5 * Math.pow(2, 10 * (t - 1));
102-
}
103-
return 0.5 * (-Math.pow(2, -10 * --t) + 2);
104-
},
57+
easeInOutExpo: t => atEdge(t) ? t : t < 0.5
58+
? 0.5 * Math.pow(2, 10 * (t * 2 - 1))
59+
: 0.5 * (-Math.pow(2, -10 * (t * 2 - 1)) + 2),
10560

106-
easeInCirc(t) {
107-
if (t >= 1) {
108-
return t;
109-
}
110-
return -(Math.sqrt(1 - t * t) - 1);
111-
},
61+
easeInCirc: t => (t >= 1) ? t : -(Math.sqrt(1 - t * t) - 1),
11262

113-
easeOutCirc(t) {
114-
return Math.sqrt(1 - (t -= 1) * t);
115-
},
63+
easeOutCirc: t => Math.sqrt(1 - (t -= 1) * t),
11664

117-
easeInOutCirc(t) {
118-
if ((t /= 0.5) < 1) {
119-
return -0.5 * (Math.sqrt(1 - t * t) - 1);
120-
}
121-
return 0.5 * (Math.sqrt(1 - (t -= 2) * t) + 1);
122-
},
65+
easeInOutCirc: t => ((t /= 0.5) < 1)
66+
? -0.5 * (Math.sqrt(1 - t * t) - 1)
67+
: 0.5 * (Math.sqrt(1 - (t -= 2) * t) + 1),
12368

124-
easeInElastic(t) {
125-
let s = 1.70158;
126-
let p = 0;
127-
let a = 1;
128-
if (t === 0) {
129-
return 0;
130-
}
131-
if (t === 1) {
132-
return 1;
133-
}
134-
if (!p) {
135-
p = 0.3;
136-
}
137-
if (a < 1) {
138-
a = 1;
139-
s = p / 4;
140-
} else {
141-
s = p / TAU * Math.asin(1 / a);
142-
}
143-
return -(a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t - s) * TAU / p));
144-
},
69+
easeInElastic: t => atEdge(t) ? t : elasticIn(t, 0.075, 0.3),
14570

146-
easeOutElastic(t) {
147-
let s = 1.70158;
148-
let p = 0;
149-
let a = 1;
150-
if (t === 0) {
151-
return 0;
152-
}
153-
if (t === 1) {
154-
return 1;
155-
}
156-
if (!p) {
157-
p = 0.3;
158-
}
159-
if (a < 1) {
160-
a = 1;
161-
s = p / 4;
162-
} else {
163-
s = p / TAU * Math.asin(1 / a);
164-
}
165-
return a * Math.pow(2, -10 * t) * Math.sin((t - s) * TAU / p) + 1;
166-
},
71+
easeOutElastic: t => atEdge(t) ? t : elasticOut(t, 0.075, 0.3),
16772

16873
easeInOutElastic(t) {
169-
let s = 1.70158;
170-
let p = 0;
171-
let a = 1;
172-
if (t === 0) {
173-
return 0;
174-
}
175-
if ((t /= 0.5) === 2) {
176-
return 1;
177-
}
178-
if (!p) {
179-
p = 0.45;
180-
}
181-
if (a < 1) {
182-
a = 1;
183-
s = p / 4;
184-
} else {
185-
s = p / TAU * Math.asin(1 / a);
186-
}
187-
if (t < 1) {
188-
return -0.5 * (a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t - s) * TAU / p));
189-
}
190-
return a * Math.pow(2, -10 * (t -= 1)) * Math.sin((t - s) * TAU / p) * 0.5 + 1;
74+
const s = 0.1125;
75+
const p = 0.45;
76+
return atEdge(t) ? t :
77+
t < 0.5
78+
? 0.5 * elasticIn(t * 2, s, p)
79+
: 0.5 + 0.5 * elasticOut(t * 2 - 1, s, p);
19180
},
81+
19282
easeInBack(t) {
19383
const s = 1.70158;
19484
return t * t * ((s + 1) * t - s);
@@ -207,29 +97,26 @@ const effects = {
20797
return 0.5 * ((t -= 2) * t * (((s *= (1.525)) + 1) * t + s) + 2);
20898
},
20999

210-
easeInBounce(t) {
211-
return 1 - effects.easeOutBounce(1 - t);
212-
},
100+
easeInBounce: t => 1 - effects.easeOutBounce(1 - t),
213101

214102
easeOutBounce(t) {
215-
if (t < (1 / 2.75)) {
216-
return 7.5625 * t * t;
103+
const m = 7.5625;
104+
const d = 2.75;
105+
if (t < (1 / d)) {
106+
return m * t * t;
217107
}
218-
if (t < (2 / 2.75)) {
219-
return 7.5625 * (t -= (1.5 / 2.75)) * t + 0.75;
108+
if (t < (2 / d)) {
109+
return m * (t -= (1.5 / d)) * t + 0.75;
220110
}
221-
if (t < (2.5 / 2.75)) {
222-
return 7.5625 * (t -= (2.25 / 2.75)) * t + 0.9375;
111+
if (t < (2.5 / d)) {
112+
return m * (t -= (2.25 / d)) * t + 0.9375;
223113
}
224-
return 7.5625 * (t -= (2.625 / 2.75)) * t + 0.984375;
114+
return m * (t -= (2.625 / d)) * t + 0.984375;
225115
},
226116

227-
easeInOutBounce(t) {
228-
if (t < 0.5) {
229-
return effects.easeInBounce(t * 2) * 0.5;
230-
}
231-
return effects.easeOutBounce(t * 2 - 1) * 0.5 + 0.5;
232-
}
117+
easeInOutBounce: t => (t < 0.5)
118+
? effects.easeInBounce(t * 2) * 0.5
119+
: effects.easeOutBounce(t * 2 - 1) * 0.5 + 0.5,
233120
};
234121

235122
export default effects;

0 commit comments

Comments
 (0)