All files / src/timing cubicBezier.js

97.91% Statements 47/48
85.71% Branches 12/14
100% Functions 7/7
97.82% Lines 45/46

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160                  1x 1x 1x 1x 1x 1x 1x 1x                     15483x                       15282x                         8x   8x 80x 80x 40x     40x         8x                         3054x 12216x 12216x   12216x       12216x     3054x                       3066x           3066x 17019x   3066x     3066x   3066x 3066x   3066x 3054x   12x 4x     8x                       11x         11x 11x 11x 11x     11x 121x       3066x 3066x     11x    
// @ts-check
/**
 * CREDITS: Implementation based on Firefox's nsSMILKeySpline.cpp
 * MIT license
 *
 * origin: https://github.com/d3/d3-ease/pull/14/commits/c6734e347c280eeb129ea273c975f57034f3ff8e
 */
 
const
  BINARY_SEARCH_PRECISION = 1e-7,
  BINARY_SEARCH_ITERATIONS = 10,
  NEWTON_ITERATIONS = 4,
  NEWTON_MIN_SLOPE = 0.02,
  SAMPLES_COUNT = 11,
  SAMPLES_LAST = SAMPLES_COUNT - 1,
  SAMPLES_STEP = 1.0 / (SAMPLES_COUNT - 1),
  FLOAT_ARRAYS_AVAILABLE = typeof Float32Array === "function";
 
/**
 * @brief Calculate Px(t) or Py(t) given B and C points
 * @param {number} t
 * @param {number} b
 * @param {number} c
 * @return {number}
 */
function calcBezier(t, b, c) {
  // eslint-disable-next-line
  return (((1.0 + (b *= 3.0) - (c *= 3.0)) * t + c - 2.0 * b) * t + b) * t;
}
 
/**
 * @brief Calculate (dPx/dt)(t) or (dPy/dt)(t) given B and C points
 * @param {number} t
 * @param {number} b
 * @param {number} c
 * @return {number}
 */
function calcSlope(t, b, c) {
  // eslint-disable-next-line
  return ((1.0 + (b *= 3.0) - (c *= 3.0)) * t * 3.0 + 2.0 * c - 4.0 * b) * t + b;
}
 
/**
 * @brief Estimate t (from [t1, t2] interval) given x value and points B and C
 * @param  {number} x
 * @param  {number} t1
 * @param  {number} t2
 * @param  {number} b
 * @param  {number} c
 * @return {number}
 */
function binarySearch(x, t1, t2, b, c) {
  var t, i = 0, foundX;
 
  do {
    t = (t1 + t2) / 2.0;
    if (x < (foundX = calcBezier(t, b, c))) {
      t2 = t;
    }
    else {
      t1 = t;
    }
  } while (++i < BINARY_SEARCH_ITERATIONS &&
           Math.abs(x - foundX) > BINARY_SEARCH_PRECISION);
 
  return t;
}
 
/**
 * @brief Estimate t using Newton-Raphson method given points B and C
 * @param  {number} x
 * @param  {number} guessForT
 * @param  {number} b
 * @param  {number} c
 * @return {number}
 */
function newtonRaphsonIterate(x, guessForT, b, c) {
  var currentX, currentSlope;
  for (var i = 0; i < NEWTON_ITERATIONS; ++i) {
    currentX = calcBezier(guessForT, b, c) - x;
    currentSlope = calcSlope(guessForT, b, c);
 
    Iif (currentSlope === 0.0) {
      return guessForT;
    }
 
    guessForT -= currentX / currentSlope;
  }
 
  return guessForT;
}
 
/**
 * @brief Estimates t given points B and C, and a set of precalculated values
 * @param  {number} x
 * @param  {number} b
 * @param  {number} c
 * @param  {number[]|Float32Array} sampleValues
 * @return {number}
 */
function estimateT(x, b, c, sampleValues) {
  var currentSample = 0,
    dist,
    intervalStart,
    initialSlope,
    guessForT;
 
  while (currentSample !== SAMPLES_LAST && sampleValues[currentSample] <= x) {
    ++currentSample;
  }
  intervalStart = --currentSample * SAMPLES_STEP;
 
  // Interpolate to provide an initial guess for t
  dist = (x - sampleValues[currentSample]) /
         (sampleValues[currentSample + 1] - sampleValues[currentSample]);
  guessForT = intervalStart + (dist * SAMPLES_STEP);
  initialSlope = calcSlope(guessForT, b, c);
 
  if (initialSlope >= NEWTON_MIN_SLOPE) {
    return newtonRaphsonIterate(x, guessForT, b, c);
  }
  else if (initialSlope === 0.0) {
    return guessForT;
  }
  else {
    return binarySearch(x, intervalStart, intervalStart + SAMPLES_STEP, b, c);
  }
}
 
/**
 * @brief Assumes points A = (0, 0) and D = (1, 1)
 * @param {number} bx
 * @param {number} by
 * @param {number} cx
 * @param {number} cy
 */
export function cubicBezier(bx, by, cx, cy) {
  var sampleValues = (FLOAT_ARRAYS_AVAILABLE ?
    new Float32Array(SAMPLES_COUNT) :
    new Array(SAMPLES_COUNT));
 
  // Limit x to [0, 1] interval
  bx = Math.max(0.0, Math.min(1.0, +bx));
  cx = Math.max(0.0, Math.min(1.0, +cx));
  by = +by;
  cy = +cy;
 
  // Precompute sample x-values
  for (var i = 0; i < SAMPLES_COUNT; ++i) {
    sampleValues[i] = calcBezier(i * SAMPLES_STEP, bx, cx);
  }
 
  function bezier(/** @type {number} */ x) {
    var t = estimateT(x, bx, cx, sampleValues);
    return calcBezier(t, by, cy);
  }
 
  return bezier;
}