|
| 1 | +/** |
| 2 | + * https://github.com/gre/bezier-easing |
| 3 | + * BezierEasing - use bezier curve for transition easing function |
| 4 | + * by Gaëtan Renaudeau 2014 - 2015 – MIT License |
| 5 | + */ |
| 6 | + |
| 7 | +const cubicBezier = (() => { |
| 8 | + |
| 9 | +// These values are established by empiricism with tests (tradeoff: performance VS precision) |
| 10 | +var NEWTON_ITERATIONS = 4; |
| 11 | +var NEWTON_MIN_SLOPE = 0.001; |
| 12 | +var SUBDIVISION_PRECISION = 0.0000001; |
| 13 | +var SUBDIVISION_MAX_ITERATIONS = 10; |
| 14 | + |
| 15 | +var kSplineTableSize = 11; |
| 16 | +var kSampleStepSize = 1.0 / (kSplineTableSize - 1.0); |
| 17 | + |
| 18 | +var float32ArraySupported = typeof Float32Array === 'function'; |
| 19 | + |
| 20 | +function A (aA1, aA2) { return 1.0 - 3.0 * aA2 + 3.0 * aA1; } |
| 21 | +function B (aA1, aA2) { return 3.0 * aA2 - 6.0 * aA1; } |
| 22 | +function C (aA1) { return 3.0 * aA1; } |
| 23 | + |
| 24 | +// Returns x(t) given t, x1, and x2, or y(t) given t, y1, and y2. |
| 25 | +function calcBezier (aT, aA1, aA2) { return ((A(aA1, aA2) * aT + B(aA1, aA2)) * aT + C(aA1)) * aT; } |
| 26 | + |
| 27 | +// Returns dx/dt given t, x1, and x2, or dy/dt given t, y1, and y2. |
| 28 | +function getSlope (aT, aA1, aA2) { return 3.0 * A(aA1, aA2) * aT * aT + 2.0 * B(aA1, aA2) * aT + C(aA1); } |
| 29 | + |
| 30 | +function binarySubdivide (aX, aA, aB, mX1, mX2) { |
| 31 | + var currentX, currentT, i = 0; |
| 32 | + do { |
| 33 | + currentT = aA + (aB - aA) / 2.0; |
| 34 | + currentX = calcBezier(currentT, mX1, mX2) - aX; |
| 35 | + if (currentX > 0.0) { |
| 36 | + aB = currentT; |
| 37 | + } else { |
| 38 | + aA = currentT; |
| 39 | + } |
| 40 | + } while (Math.abs(currentX) > SUBDIVISION_PRECISION && ++i < SUBDIVISION_MAX_ITERATIONS); |
| 41 | + return currentT; |
| 42 | +} |
| 43 | + |
| 44 | +function newtonRaphsonIterate (aX, aGuessT, mX1, mX2) { |
| 45 | + for (var i = 0; i < NEWTON_ITERATIONS; ++i) { |
| 46 | + var currentSlope = getSlope(aGuessT, mX1, mX2); |
| 47 | + if (currentSlope === 0.0) { |
| 48 | + return aGuessT; |
| 49 | + } |
| 50 | + var currentX = calcBezier(aGuessT, mX1, mX2) - aX; |
| 51 | + aGuessT -= currentX / currentSlope; |
| 52 | + } |
| 53 | + return aGuessT; |
| 54 | +} |
| 55 | + |
| 56 | +function LinearEasing (x) { |
| 57 | + return x; |
| 58 | +} |
| 59 | + |
| 60 | +return function bezier (mX1, mY1, mX2, mY2) { |
| 61 | + if (!(0 <= mX1 && mX1 <= 1 && 0 <= mX2 && mX2 <= 1)) { |
| 62 | + throw new Error('bezier x values must be in [0, 1] range'); |
| 63 | + } |
| 64 | + |
| 65 | + if (mX1 === mY1 && mX2 === mY2) { |
| 66 | + return LinearEasing; |
| 67 | + } |
| 68 | + |
| 69 | + // Precompute samples table |
| 70 | + var sampleValues = float32ArraySupported ? new Float32Array(kSplineTableSize) : new Array(kSplineTableSize); |
| 71 | + for (var i = 0; i < kSplineTableSize; ++i) { |
| 72 | + sampleValues[i] = calcBezier(i * kSampleStepSize, mX1, mX2); |
| 73 | + } |
| 74 | + |
| 75 | + function getTForX (aX) { |
| 76 | + var intervalStart = 0.0; |
| 77 | + var currentSample = 1; |
| 78 | + var lastSample = kSplineTableSize - 1; |
| 79 | + |
| 80 | + for (; currentSample !== lastSample && sampleValues[currentSample] <= aX; ++currentSample) { |
| 81 | + intervalStart += kSampleStepSize; |
| 82 | + } |
| 83 | + --currentSample; |
| 84 | + |
| 85 | + // Interpolate to provide an initial guess for t |
| 86 | + var dist = (aX - sampleValues[currentSample]) / (sampleValues[currentSample + 1] - sampleValues[currentSample]); |
| 87 | + var guessForT = intervalStart + dist * kSampleStepSize; |
| 88 | + |
| 89 | + var initialSlope = getSlope(guessForT, mX1, mX2); |
| 90 | + if (initialSlope >= NEWTON_MIN_SLOPE) { |
| 91 | + return newtonRaphsonIterate(aX, guessForT, mX1, mX2); |
| 92 | + } else if (initialSlope === 0.0) { |
| 93 | + return guessForT; |
| 94 | + } else { |
| 95 | + return binarySubdivide(aX, intervalStart, intervalStart + kSampleStepSize, mX1, mX2); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return function BezierEasing (x) { |
| 100 | + // Because JavaScript number are imprecise, we should guarantee the extremes are right. |
| 101 | + if (x === 0) { |
| 102 | + return 0; |
| 103 | + } |
| 104 | + if (x === 1) { |
| 105 | + return 1; |
| 106 | + } |
| 107 | + return calcBezier(getTForX(x), mY1, mY2); |
| 108 | + }; |
| 109 | +}; |
| 110 | + |
| 111 | +})(); |
0 commit comments