/**
* https://github.com/gre/bezier-easing
* BezierEasing - use bezier curve for transition easing function
* by Gaëtan Renaudeau 2014 - 2015 – MIT License
*/
const NEWTON_ITERATIONS = 4;
const NEWTON_MIN_SLOPE = 0.001;
const SUBDIVISION_PRECISION = 0.0000001;
const SUBDIVISION_MAX_ITERATIONS = 10;
let kSplineTableSize = 11;
let kSampleStepSize = 1.0 / (kSplineTableSize - 1.0);
let float32ArraySupported = typeof Float32Array === 'function';
/* eslint new-cap: 0 */
/**
* 公因式A
*
* @param {number} aA1 控制分量
* @param {number} aA2 控制分量
* @return {number} 整个公式中的A公因式的值
*/
function A(aA1, aA2) {
return 1.0 - 3.0 * aA2 + 3.0 * aA1;
}
/**
* 公因式B
*
* @param {number} aA1 控制分量1
* @param {number} aA2 控制分量2
* @return {number} 整个公式中的B公因式的值
*/
function B(aA1, aA2) {
return 3.0 * aA2 - 6.0 * aA1;
}
/**
* 公因式C
*
* @param {number} aA1 控制分量1
* @param {number} aA2 控制分量2
* @return {number} 整个公式中的C公因式的值
*/
function C(aA1) {
return 3.0 * aA1;
}
/**
* 获取aT处的值
*
* @param {number} aT 三次贝塞尔曲线的t自变量
* @param {number} aA1 控制分量1
* @param {number} aA2 控制分量2
* @return {number} 三次贝塞尔公式的因变量
*/
function calcBezier(aT, aA1, aA2) {
return ((A(aA1, aA2) * aT + B(aA1, aA2)) * aT + C(aA1)) * aT;
}
/**
* 获取aT处的斜率
* @param {number} aT 三次贝塞尔曲线的t自变量
* @param {number} aA1 控制分量1
* @param {number} aA2 控制分量2
* @return {number} 三次贝塞尔公式的导数
*/
function getSlope(aT, aA1, aA2) {
return 3.0 * A(aA1, aA2) * aT * aT + 2.0 * B(aA1, aA2) * aT + C(aA1);
}
/**
*
* @param {number} aX
* @param {number} aA
* @param {number} aB
* @param {number} mX1
* @param {number} mX2
* @return {number} 二分法猜测t的值
*/
function binarySubdivide(aX, aA, aB, mX1, mX2) {
let currentX;
let currentT;
let i = 0;
do {
currentT = aA + (aB - aA) / 2.0;
currentX = calcBezier(currentT, mX1, mX2) - aX;
if (currentX > 0.0) {
aB = currentT;
} else {
aA = currentT;
}
} while (
Math.abs(currentX) > SUBDIVISION_PRECISION
&&
++i < SUBDIVISION_MAX_ITERATIONS
);
return currentT;
}
/**
* 牛顿迭代算法,进一步的获取精确的T值
* @param {number} aX
* @param {number} aGuessT
* @param {number} mX1
* @param {number} mX2
* @return {number} 获取更精确的T值
*/
function newtonRaphsonIterate(aX, aGuessT, mX1, mX2) {
for (let i = 0; i < NEWTON_ITERATIONS; ++i) {
let currentSlope = getSlope(aGuessT, mX1, mX2);
if (currentSlope === 0.0) {
return aGuessT;
}
let currentX = calcBezier(aGuessT, mX1, mX2) - aX;
aGuessT -= currentX / currentSlope;
}
return aGuessT;
}
/**
* cubic-bezier曲线的两个控制点,默认起始点为 0,结束点为 1
*
* @class
* @memberof JC
* @param {number} mX1 控制点1的x分量
* @param {number} mY1 控制点1的y分量
* @param {number} mX2 控制点2的x分量
* @param {number} mY2 控制点2的y分量
*/
function BezierEasing(mX1, mY1, mX2, mY2) {
if (!(0 <= mX1 && mX1 <= 1 && 0 <= mX2 && mX2 <= 1)) {
throw new Error('bezier x values must be in [0, 1] range');
}
this.mX1 = mX1;
this.mY1 = mY1;
this.mX2 = mX2;
this.mY2 = mY2;
this.sampleValues = float32ArraySupported ?
new Float32Array(kSplineTableSize):
new Array(kSplineTableSize);
this._preCompute();
}
BezierEasing.prototype._preCompute = function() {
// Precompute samples table
if (this.mX1 !== this.mY1 || this.mX2 !== this.mY2) {
for (let i = 0; i < kSplineTableSize; ++i) {
this.sampleValues[i] = calcBezier(
i * kSampleStepSize,
this.mX1,
this.mX2
);
}
}
};
BezierEasing.prototype._getTForX = function(aX) {
let intervalStart = 0.0;
let currentSample = 1;
let lastSample = kSplineTableSize - 1;
for (
;
currentSample !== lastSample && this.sampleValues[currentSample] <= aX;
++currentSample
) {
intervalStart += kSampleStepSize;
}
--currentSample;
// Interpolate to provide an initial guess for t
let dist = (aX - this.sampleValues[currentSample]) /
(this.sampleValues[currentSample + 1] - this.sampleValues[currentSample]);
let guessForT = intervalStart + dist * kSampleStepSize;
let initialSlope = getSlope(guessForT, this.mX1, this.mX2);
if (initialSlope >= NEWTON_MIN_SLOPE) {
return newtonRaphsonIterate(aX, guessForT, this.mX1, this.mX2);
} else if (initialSlope === 0.0) {
return guessForT;
} else {
return binarySubdivide(
aX,
intervalStart,
intervalStart + kSampleStepSize,
this.mX1,
this.mX2
);
}
};
/**
* 通过x轴近似获取y的值
*
* @param {number} x x轴的偏移量
* @return {number} y 与输入值x对应的y值
*/
BezierEasing.prototype.get = function(x) {
if (this.mX1 === this.mY1 && this.mX2 === this.mY2) return x;
if (x === 0) {
return 0;
}
if (x === 1) {
return 1;
}
return calcBezier(this._getTForX(x), this.mY1, this.mY2);
};
export {BezierEasing};