Last active
April 28, 2022 14:20
-
-
Save iinfin/0f1e782dd868e4ee8716e385634a930e to your computer and use it in GitHub Desktop.
cubic bezier easing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // simplified version of: | |
| // https://github.com/gre/bezier-easing/blob/master/src/index.js | |
| // https://npmjs.com/package/bezier-easing | |
| function slopeFromT(t, A, B, C) { | |
| return 1.0 / (3.0 * A * t * t + 2.0 * B * t + C); | |
| } | |
| function xFromT(t, A, B, C, D) { | |
| return A * (t * t * t) + B * (t * t) + C * t + D; | |
| } | |
| function yFromT(t, E, F, G, H) { | |
| let tt = t * t; | |
| return E * (tt * t) + F * tt + G * t + H; | |
| } | |
| function cubicBezier(percent, x0, y0, x1, y1) { | |
| if (percent <= 0) return 0; | |
| if (percent >= 1) return 1; | |
| if (x0 === y0 && x1 === y1) return percent; // linear | |
| let x0a = 0; // initial x | |
| let y0a = 0; // initial y | |
| let x1a = x0; // 1st influence x | |
| let y1a = y0; // 1st influence y | |
| let x2a = x1; // 2nd influence x | |
| let y2a = y1; // 2nd influence y | |
| let x3a = 1; // final x | |
| let y3a = 1; // final y | |
| let A = x3a - 3.0 * x2a + 3.0 * x1a - x0a; | |
| let B = 3.0 * x2a - 6.0 * x1a + 3.0 * x0a; | |
| let C = 3.0 * x1a - 3.0 * x0a; | |
| let D = x0a; | |
| let E = y3a - 3.0 * y2a + 3.0 * y1a - y0a; | |
| let F = 3.0 * y2a - 6.0 * y1a + 3.0 * y0a; | |
| let G = 3.0 * y1a - 3.0 * y0a; | |
| let H = y0a; | |
| let current = percent; | |
| let i, currentx, currentslope; | |
| for (i = 0; i < 5; i++) { | |
| currentx = xFromT(current, A, B, C, D); | |
| currentslope = slopeFromT(current, A, B, C); | |
| if (currentslope === Infinity) currentslope = percent; | |
| current -= (currentx - percent) * currentslope; | |
| current = Math.min(Math.max(current, 0.0), 1.0); | |
| } | |
| return yFromT(current, E, F, G, H); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment