code: function solvePositionFromXValue_(xVal, x1, x2) {
    const EPSILON = 1e-6;
    const MAX_ITERATIONS = 8;
  
    if (xVal <= 0) {
      return 0;
    } else if (xVal >= 1) {
      return 1;
    }
  
    // Initial estimate of t using linear interpolation.
    let t = xVal;
  
    // Try gradient descent to solve for t. If it works, it is very fast.
    let tMin = 0;
    let tMax = 1;
    let value = 0;
    for (let i = 0; i < MAX_ITERATIONS; i++) {
      value = getBezierCoordinate_(t, x1, x2);
      const derivative = (getBezierCoordinate_(t + EPSILON, x1, x2) - value) / EPSILON;
      if (Math.abs(value - xVal) < EPSILON) {
        return t;
      } else if (Math.abs(derivative) < EPSILON) {
        break;
      } else {
        if (value < xVal) {
          tMin = t;
        } else {
          tMax = t;
        }
        t -= (value - xVal) / derivative;
      }
    }
  
    // If the gradient descent got stuck in a local minimum, e.g. because
    // the derivative was close to 0, use a Dichotomy refinement instead.
    // We limit the number of interations to 8.
    for (let i = 0; Math.abs(value - xVal) > EPSILON && i < MAX_ITERATIONS; i++) {
      if (value < xVal) {
        tMin = t;
        t = (t + tMax) / 2;
      } else {
        tMax = t;
        t = (t + tMin) / 2;
      }
      value = getBezierCoordinate_(t, x1, x2);
    }
    return t;
  }
docs: Project a point onto the Bezier curve, from a given X. Calculates the position t along the curve.
  Adapted from https://github.com/google/closure-library/blob/master/closure/goog/math/bezier.js.
  @param {number} xVal
  @param {number} x1
  @param {number} x2
  @return {number}