All files / src/mathf cubic-bezier.ts

27.77% Statements 5/18
100% Branches 0/0
20% Functions 2/10
27.77% Lines 5/18

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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270                                                                                                                                                                          1x 1x 1x 1x                                                                                                                                                                                                                                                                                       1x                                                                                  
import type {point} from './mathf';
import type {Vector} from './vector';
 
export type EasingFunction = (percent: number) => number;
 
/**
 * Implements a basic cubic beizer class.
 *
 *
 * The interpolate and interpolateProgress methods are designed to work with
 * normalized values. The most common usecase for this might to apply a CubiicBezier as an
 * easing function.  To do this, you can use the makeEasingFunction which
 * creates the CubicBezier internals and allows you to pass a t (time/progress),
 * to get results.
 *
 * ```ts
 *   var func = CubicBezier.makeEasingFunction(0.2,0.17,0.83,0.67));
 *   func(0.2); // Pass progress.
 * ```
 *
 * Use as instance:
 *
 * ```ts
 * let myBezier = new CubicBezier(0.2, 0.17, 0.83, 0.67);
 * myBezier.interpolate(0);
 * myBezier.interpolate(0.2);
 * myBezier.interpolate(0.5);
 * myBezier.interpolate(0.8);
 * myBezier.interpolate(1);
 *
 * let x = mathf.ease(0, 500, this.progress,
 *            myBeizer.easingFunction());
 *
 * ```
 *
 *
 * Use static method.
 *
 * ```ts
 * CubicBezier.interpolateProgress(0, 0.2, 0.17, 0.83, 0.67);
 * CubicBezier.interpolateProgress(0.2, 0.2, 0.17, 0.83, 0.67);
 * CubicBezier.interpolateProgress(0.5, 0.2, 0.17, 0.83, 0.67);
 * CubicBezier.interpolateProgress(0.8, 0.2, 0.17, 0.83, 0.67);
 * CubicBezier.interpolateProgress(1, 0.2, 0.17, 0.83, 0.67);
 *
 *
 * let x = mathf.ease(0, 500, this.progress,
 *            CubicBezier.makeEasingFunction(0.2,0.17,0.83,0.67));
 *
 * ```
 *
 *
 * You can also use getPoint and getBezierPoint as way to calculate standard
 * bezier curves (non normalized).
 *
 *
 * ```ts
 *
 *   // Create control vectors.
 *   let c1 = new Vector(50, 400);
 *   let c2 = new Vector(100, 200);
 *   let c3 = new Vector(300, 300);
 *   let c4 = new Vector(400, 400);
 *
 *   // Find the vector on the cubic bezier at 0.3 progress / t.
 *   const vector = CubicBezier.getPoint(
 *     0.3, c1, c2, c3, c4
 *    );
 *
 * ```
 *
 *
 * References:
 * @see https://qiita.com/butchi_y/items/abb6d52fda6095b542e5
 * @see http://geom.web.fc2.com/
 * @see http://geom.web.fc2.com/geometry/bezier/cubic.html
 * @see https://stackoverflow.com/questions/27053888/how-to-get-time-value-from-bezier-curve-given-length
 */
export class CubicBezier {
  public x2: number;
  public y2: number;
  public x3: number;
  public y3: number;
 
  constructor(x2: number, y2: number, x3: number, y3: number) {
    this.x2 = x2;
    this.y2 = y2;
    this.x3 = x2;
    this.y3 = y3;
  }
 
  /**
   * Given 4 points, start, 2, 3 and end, generates the x,y point along the
   * bezier curve at progress (0-1) value.
   * @param progress
   * @param x1 Start point x
   * @param y1 Start point y
   * @param x2 Control point 2 x
   * @param y2 Control point 2 y
   * @param x3 Control point 3 x
   * @param y3 Control point 3 y
   * @param x4 End point x
   * @param y4 End point y
   */
  public static getBezierPoint(
    progress: number,
    x1: number,
    y1: number,
    x2: number,
    y2: number,
    x3: number,
    y3: number,
    x4: number,
    y4: number
  ): point {
    const tp = 1 - progress;
    const x =
      progress * progress * progress * x4 +
      3 * progress * progress * tp * x3 +
      3 * progress * tp * tp * x2 +
      tp * tp * tp * x1;
    const y =
      progress * progress * progress * y4 +
      3 * progress * progress * tp * y3 +
      3 * progress * tp * tp * y2 +
      tp * tp * tp * y1;
 
    return {
      x: x,
      y: y,
    };
  }
 
  /**
   * Gets a point along the cubic bezier.
   * @param progress
   * @param v1 The start vector
   * @param v2 The control2 vector
   * @param v3 The control3 vector
   * @param v4  The end vector
   */
  public static getPoint(
    progress: number,
    v1: Vector,
    v2: Vector,
    v3: Vector,
    v4: Vector
  ): point {
    return CubicBezier.getBezierPoint(
      progress,
      v1.x,
      v1.y,
      v2.x,
      v2.y,
      v3.x,
      v3.y,
      v4.x,
      v4.y
    );
  }
 
  /**
   * Runs a basic cubic bezier interporalation given progress and point values
   * for the 2nd and 3rd controls of the cubic bezier.
   *
   * ```ts
   * let x = CubicBezier.interpolateProgress(0.2, 0.17, 0.67, 0.83, 0.67);
   *
   * ```
   * @param progress The progress of the interpolation.  Value between 0-1.
   * @param x2 The x of the second control point.
   * @param y2 The y of the second control point.
   * @param x2 The x of the third control point.
   * @param y3 The y of the fourth control point.
   * @return The interpolated value.
   */
  public static interpolateProgress(
    progress: number,
    x2: number,
    y2: number,
    x3: number,
    y3: number
  ): number {
    // Get the point (x, y) for this current time.
    const point = CubicBezier.getBezierPoint(
      progress,
      0,
      0, // Start
      x2,
      y2, // Point2
      x3,
      y3, // Point 3
      1,
      1 // End
    );
 
    // Simply return y since x is time and y is progression.
    return point.y;
  }
 
  /**
   * Runs a basic cubic bezier interporalation.
   *
   * ```ts
   * let myBezier = new CubicBezier(0.2, 0.17, 0.83, 0.67);
   * myBezier.interporate(0);
   * myBezier.interporate(0.2);
   *
   * ```
   * @param progress The progress of the interpolation.  Value between 0-1.
   * @return The interpolated value.
   */
  public interpolate(progress: number): number {
    return CubicBezier.interpolateProgress(
      progress,
      this.x2,
      this.y2,
      this.x3,
      this.y3
    );
  }
 
  /**
   * Returns an easing function of this CubicBezier so it can be
   * used like other easing functions but used for instances.
   * @return A function that accepts a progresss value.
   */
  public easingFunction(): EasingFunction {
    return (progress: number) => {
      return this.interpolate(progress);
    };
  }
 
  /**
   * Returns an easing function of this CubicBezier so it can be
   * used like other easing functions.  A static method.
   * @param p0 The first control point.
   * @param p1 The second control point.
   * @param p2 The third control point.
   * @param p3 The fourth control point.
   * @return A function that accepts a progresss value.
   */
  public static makeEasingFunction(
    p0: number,
    p1: number,
    p2: number,
    p3: number
  ): Function {
    return (progress: number) => {
      return this.interpolateProgress(progress, p0, p1, p2, p3);
    };
  }
 
  /**
   * Makes an easing function from comma separated values.
   * ```
   * const myEasingFunction = CubicBezier.makeEasingFunctionFromString('0,0.2,0.93,1');
   * ```
   */
  public static makeEasingFunctionFromString(value: string) {
    const values = value.split(',');
    return CubicBezier.makeEasingFunction(
      +values[0],
      +values[1],
      +values[2],
      +values[3]
    );
  }
}