{"version":3,"file":"cubic-bezier.mjs","sources":["../../../src/easing/cubic-bezier.ts"],"sourcesContent":["/*\n Bezier function generator\n This has been modified from Gaƫtan Renaudeau's BezierEasing\n https://github.com/gre/bezier-easing/blob/master/src/index.js\n https://github.com/gre/bezier-easing/blob/master/LICENSE\n\n t is found for a given x with Newton-Raphson, safeguarded by a bisection\n bracket: whenever a Newton step would leave the bracket (near flat parts\n of the curve), it bisects instead. This converges to full precision in\n ~5 iterations on typical curves, so output is smooth rather than snapped\n to a fixed bisection grid. The slope is estimated with a forward\n difference, and the lookup table is omitted, to save bytes.\n\n x = 0 and x = 1 return exactly 0 and 1. x outside [0, 1] starts outside\n the bracket, so it bisects towards the nearer end and saturates to within\n ~1e-12 of 0 or 1.\n Usage\n const easeOut = cubicBezier(.17,.67,.83,.67);\n const x = easeOut(0.5); // returns 0.627...\n*/\n\nimport { noop } from \"../noop\"\n\n// Returns x(t) given t, x1, and x2, or y(t) given t, y1, and y2.\n// Bernstein form, so it is exactly 0 at t = 0 and exactly 1 at t = 1.\nconst calcBezier = (t: number, a1: number, a2: number) =>\n t * (3 * (1 - t) * ((1 - t) * a1 + t * a2) + t * t)\n\n/*#__NO_SIDE_EFFECTS__*/\nexport function cubicBezier(\n mX1: number,\n mY1: number,\n mX2: number,\n mY2: number\n) {\n // If this is a linear gradient, return linear easing\n if (mX1 === mY1 && mX2 === mY2) return noop\n\n return (x: number) => {\n let lowerBound = 0\n let upperBound = 1\n let t = x\n\n for (let i = 0; ; i++) {\n // If t is outside the bracket, because x is out of range or a\n // Newton step overshot where the slope is ~0, bisect instead.\n if (!(t >= lowerBound && t <= upperBound)) {\n t = (lowerBound + upperBound) / 2\n }\n\n const currentX = calcBezier(t, mX1, mX2) - x\n if (currentX * currentX < 1e-28 || i === 40) break\n currentX > 0 ? (upperBound = t) : (lowerBound = t)\n\n // Newton-Raphson step, with dx/dt from a forward difference\n t -=\n (currentX * 1e-7) /\n (calcBezier(t + 1e-7, mX1, mX2) - x - currentX)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