|
| 1 | +/* stylelint-disable */ |
| 2 | +.bezierEasingMixin() { |
| 3 | +@functions: ~`(function() { |
| 4 | + var NEWTON_ITERATIONS = 4; |
| 5 | + var NEWTON_MIN_SLOPE = 0.001; |
| 6 | + var SUBDIVISION_PRECISION = 0.0000001; |
| 7 | + var SUBDIVISION_MAX_ITERATIONS = 10; |
| 8 | + |
| 9 | + var kSplineTableSize = 11; |
| 10 | + var kSampleStepSize = 1.0 / (kSplineTableSize - 1.0); |
| 11 | + |
| 12 | + var float32ArraySupported = typeof Float32Array === 'function'; |
| 13 | + |
| 14 | + function A (aA1, aA2) { return 1.0 - 3.0 * aA2 + 3.0 * aA1; } |
| 15 | + function B (aA1, aA2) { return 3.0 * aA2 - 6.0 * aA1; } |
| 16 | + function C (aA1) { return 3.0 * aA1; } |
| 17 | + |
| 18 | + // Returns x(t) given t, x1, and x2, or y(t) given t, y1, and y2. |
| 19 | + function calcBezier (aT, aA1, aA2) { return ((A(aA1, aA2) * aT + B(aA1, aA2)) * aT + C(aA1)) * aT; } |
| 20 | + |
| 21 | + // Returns dx/dt given t, x1, and x2, or dy/dt given t, y1, and y2. |
| 22 | + function getSlope (aT, aA1, aA2) { return 3.0 * A(aA1, aA2) * aT * aT + 2.0 * B(aA1, aA2) * aT + C(aA1); } |
| 23 | + |
| 24 | + function binarySubdivide (aX, aA, aB, mX1, mX2) { |
| 25 | + var currentX, currentT, i = 0; |
| 26 | + do { |
| 27 | + currentT = aA + (aB - aA) / 2.0; |
| 28 | + currentX = calcBezier(currentT, mX1, mX2) - aX; |
| 29 | + if (currentX > 0.0) { |
| 30 | + aB = currentT; |
| 31 | + } else { |
| 32 | + aA = currentT; |
| 33 | + } |
| 34 | + } while (Math.abs(currentX) > SUBDIVISION_PRECISION && ++i < SUBDIVISION_MAX_ITERATIONS); |
| 35 | + return currentT; |
| 36 | + } |
| 37 | + |
| 38 | + function newtonRaphsonIterate (aX, aGuessT, mX1, mX2) { |
| 39 | + for (var i = 0; i < NEWTON_ITERATIONS; ++i) { |
| 40 | + var currentSlope = getSlope(aGuessT, mX1, mX2); |
| 41 | + if (currentSlope === 0.0) { |
| 42 | + return aGuessT; |
| 43 | + } |
| 44 | + var currentX = calcBezier(aGuessT, mX1, mX2) - aX; |
| 45 | + aGuessT -= currentX / currentSlope; |
| 46 | + } |
| 47 | + return aGuessT; |
| 48 | + } |
| 49 | + |
| 50 | + var BezierEasing = function (mX1, mY1, mX2, mY2) { |
| 51 | + if (!(0 <= mX1 && mX1 <= 1 && 0 <= mX2 && mX2 <= 1)) { |
| 52 | + throw new Error('bezier x values must be in [0, 1] range'); |
| 53 | + } |
| 54 | + |
| 55 | + // Precompute samples table |
| 56 | + var sampleValues = float32ArraySupported ? new Float32Array(kSplineTableSize) : new Array(kSplineTableSize); |
| 57 | + if (mX1 !== mY1 || mX2 !== mY2) { |
| 58 | + for (var i = 0; i < kSplineTableSize; ++i) { |
| 59 | + sampleValues[i] = calcBezier(i * kSampleStepSize, mX1, mX2); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + function getTForX (aX) { |
| 64 | + var intervalStart = 0.0; |
| 65 | + var currentSample = 1; |
| 66 | + var lastSample = kSplineTableSize - 1; |
| 67 | + |
| 68 | + for (; currentSample !== lastSample && sampleValues[currentSample] <= aX; ++currentSample) { |
| 69 | + intervalStart += kSampleStepSize; |
| 70 | + } |
| 71 | + --currentSample; |
| 72 | + |
| 73 | + // Interpolate to provide an initial guess for t |
| 74 | + var dist = (aX - sampleValues[currentSample]) / (sampleValues[currentSample + 1] - sampleValues[currentSample]); |
| 75 | + var guessForT = intervalStart + dist * kSampleStepSize; |
| 76 | + |
| 77 | + var initialSlope = getSlope(guessForT, mX1, mX2); |
| 78 | + if (initialSlope >= NEWTON_MIN_SLOPE) { |
| 79 | + return newtonRaphsonIterate(aX, guessForT, mX1, mX2); |
| 80 | + } else if (initialSlope === 0.0) { |
| 81 | + return guessForT; |
| 82 | + } else { |
| 83 | + return binarySubdivide(aX, intervalStart, intervalStart + kSampleStepSize, mX1, mX2); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return function BezierEasing (x) { |
| 88 | + if (mX1 === mY1 && mX2 === mY2) { |
| 89 | + return x; // linear |
| 90 | + } |
| 91 | + // Because JavaScript number are imprecise, we should guarantee the extremes are right. |
| 92 | + if (x === 0) { |
| 93 | + return 0; |
| 94 | + } |
| 95 | + if (x === 1) { |
| 96 | + return 1; |
| 97 | + } |
| 98 | + return calcBezier(getTForX(x), mY1, mY2); |
| 99 | + }; |
| 100 | + }; |
| 101 | + |
| 102 | + this.colorEasing = BezierEasing(0.26, 0.09, 0.37, 0.18); |
| 103 | + // less 3 requires a return |
| 104 | + return ''; |
| 105 | +})()`; |
| 106 | +} |
| 107 | +// It is hacky way to make this function will be compiled preferentially by less |
| 108 | +// resolve error: `ReferenceError: colorPalette is not defined` |
| 109 | +// https://github.com/ant-design/ant-motion/issues/44 |
| 110 | +.bezierEasingMixin(); |
0 commit comments