Skip to content

Commit

Permalink
Even earlier exit for linear case.
Browse files Browse the repository at this point in the history
I have an application which evaluates lots of different beziers, where there's a high chance they might be linear. This is a simple optimisation for that case. Also in my opinion, this layout is slightly easier to follow.
  • Loading branch information
willstott101 committed May 16, 2018
1 parent e26d137 commit bd8d29b
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/index.js
Expand Up @@ -51,17 +51,23 @@ function newtonRaphsonIterate (aX, aGuessT, mX1, mX2) {
return aGuessT;
}

function LinearEasing (x) {
return x;
}

module.exports = function bezier (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');
}

if (mX1 === mY1 && mX2 === mY2) {
return LinearEasing;
}

// Precompute samples table
var sampleValues = float32ArraySupported ? new Float32Array(kSplineTableSize) : new Array(kSplineTableSize);
if (mX1 !== mY1 || mX2 !== mY2) {
for (var i = 0; i < kSplineTableSize; ++i) {
sampleValues[i] = calcBezier(i * kSampleStepSize, mX1, mX2);
}
for (var i = 0; i < kSplineTableSize; ++i) {
sampleValues[i] = calcBezier(i * kSampleStepSize, mX1, mX2);
}

function getTForX (aX) {
Expand Down Expand Up @@ -89,9 +95,6 @@ module.exports = function bezier (mX1, mY1, mX2, mY2) {
}

return function BezierEasing (x) {
if (mX1 === mY1 && mX2 === mY2) {
return x; // linear
}
// Because JavaScript number are imprecise, we should guarantee the extremes are right.
if (x === 0) {
return 0;
Expand Down

0 comments on commit bd8d29b

Please sign in to comment.