Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9ad2c32

Browse files
committed
[RN] improve elastic easing
Summary: 1) Makes params more intuitive (only one now, bounciness, which maps intuitively to number of oscillations). 2) Satisfies boundary conditions (f(0) = 0, f(1) = 1) so animation actually goes where you tell it (before it would finish at a random location depending on the input params). 3) Simple test to verify boundary conditions.
1 parent d87480e commit 9ad2c32

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

Libraries/Animated/Easing.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,21 +59,22 @@ class Easing {
5959
return Math.pow(2, 10 * (t - 1));
6060
}
6161

62-
static elastic(a: number, p: number): (t: number) => number {
63-
var tau = Math.PI * 2;
64-
// flow isn't smart enough to figure out that s is always assigned to a
65-
// number before being used in the returned function
66-
var s: any;
67-
if (arguments.length < 2) {
68-
p = 0.45;
62+
/**
63+
* A simple elastic interaction, similar to a spring. Default bounciness
64+
* is 1, which overshoots a little bit once. 0 bounciness doesn't overshoot
65+
* at all, and bounciness of N > 1 will overshoot about N times.
66+
*
67+
* Wolfram Plots:
68+
*
69+
* http://tiny.cc/elastic_b_1 (default bounciness = 1)
70+
* http://tiny.cc/elastic_b_3 (bounciness = 3)
71+
*/
72+
static elastic(bounciness: number): (t: number) => number {
73+
if (arguments.length === 0) {
74+
bounciness = 1;
6975
}
70-
if (arguments.length) {
71-
s = p / tau * Math.asin(1 / a);
72-
} else {
73-
a = 1;
74-
s = p / 4;
75-
}
76-
return (t) => 1 + a * Math.pow(2, -10 * t) * Math.sin((t - s) * tau / p);
76+
var p = bounciness * Math.PI;
77+
return (t) => 1 - Math.pow(Math.cos(t * Math.PI / 2), 3) * Math.cos(t * p);
7778
};
7879

7980
static back(s: number): (t: number) => number {

Libraries/Animated/__tests__/Easing-test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ describe('Easing', () => {
7171
}
7272
});
7373

74+
it('should satisfy boundary conditions with elastic', () => {
75+
for (var b = 0; b < 4; b += 0.3) {
76+
var easing = Easing.elastic(b);
77+
expect(easing(0)).toBe(0);
78+
expect(easing(1)).toBe(1);
79+
}
80+
});
81+
7482
function sampleEasingFunction(easing) {
7583
var DURATION = 300;
7684
var tickCount = Math.round(DURATION * 60 / 1000);

0 commit comments

Comments
 (0)