UNPKG

2.81 kBJavaScriptView Raw
1'use strict';
2
3/**
4 * Based on:
5 * https://stackoverflow.com/questions/7348009/y-coordinate-for-a-given-x-cubic-bezier
6 * https://math.stackexchange.com/questions/26846/is-there-an-explicit-form-for-cubic-b%C3%A9zier-curves
7 * TODO: Reduce rounding error
8 */
9/**
10 * EXPERIMENTAL
11 * Given a cubic-bezier curve, get the x value (time) given
12 * the y value (progression).
13 * Ex: cubic-bezier(0.32, 0.72, 0, 1);
14 * P0: (0, 0)
15 * P1: (0.32, 0.72)
16 * P2: (0, 1)
17 * P3: (1, 1)
18 *
19 * If you give a cubic bezier curve that never reaches the
20 * provided progression, this function will return an empty array.
21 */
22const getTimeGivenProgression = (p0, p1, p2, p3, progression) => {
23 return solveCubicBezier(p0[1], p1[1], p2[1], p3[1], progression).map(tValue => {
24 return solveCubicParametricEquation(p0[0], p1[0], p2[0], p3[0], tValue);
25 });
26};
27/**
28 * Solve a cubic equation in one dimension (time)
29 */
30const solveCubicParametricEquation = (p0, p1, p2, p3, t) => {
31 const partA = (3 * p1) * Math.pow(t - 1, 2);
32 const partB = (-3 * p2 * t) + (3 * p2) + (p3 * t);
33 const partC = p0 * Math.pow(t - 1, 3);
34 return t * (partA + (t * partB)) - partC;
35};
36/**
37 * Find the `t` value for a cubic bezier using Cardano's formula
38 */
39const solveCubicBezier = (p0, p1, p2, p3, refPoint) => {
40 p0 -= refPoint;
41 p1 -= refPoint;
42 p2 -= refPoint;
43 p3 -= refPoint;
44 const roots = solveCubicEquation(p3 - 3 * p2 + 3 * p1 - p0, 3 * p2 - 6 * p1 + 3 * p0, 3 * p1 - 3 * p0, p0);
45 return roots.filter(root => root >= 0 && root <= 1);
46};
47const solveQuadraticEquation = (a, b, c) => {
48 const discriminant = b * b - 4 * a * c;
49 if (discriminant < 0) {
50 return [];
51 }
52 else {
53 return [
54 (-b + Math.sqrt(discriminant)) / (2 * a),
55 (-b - Math.sqrt(discriminant)) / (2 * a)
56 ];
57 }
58};
59const solveCubicEquation = (a, b, c, d) => {
60 if (a === 0) {
61 return solveQuadraticEquation(b, c, d);
62 }
63 b /= a;
64 c /= a;
65 d /= a;
66 const p = (3 * c - b * b) / 3;
67 const q = (2 * b * b * b - 9 * b * c + 27 * d) / 27;
68 if (p === 0) {
69 return [Math.pow(-q, 1 / 3)];
70 }
71 else if (q === 0) {
72 return [Math.sqrt(-p), -Math.sqrt(-p)];
73 }
74 const discriminant = Math.pow(q / 2, 2) + Math.pow(p / 3, 3);
75 if (discriminant === 0) {
76 return [Math.pow(q / 2, 1 / 2) - b / 3];
77 }
78 else if (discriminant > 0) {
79 return [Math.pow(-(q / 2) + Math.sqrt(discriminant), 1 / 3) - Math.pow((q / 2) + Math.sqrt(discriminant), 1 / 3) - b / 3];
80 }
81 const r = Math.sqrt(Math.pow(-(p / 3), 3));
82 const phi = Math.acos(-(q / (2 * Math.sqrt(Math.pow(-(p / 3), 3)))));
83 const s = 2 * Math.pow(r, 1 / 3);
84 return [
85 s * Math.cos(phi / 3) - b / 3,
86 s * Math.cos((phi + 2 * Math.PI) / 3) - b / 3,
87 s * Math.cos((phi + 4 * Math.PI) / 3) - b / 3
88 ];
89};
90
91exports.getTimeGivenProgression = getTimeGivenProgression;