@thednp/bezier-easing
Version:
🍬 A Typescript based cubic-bezier easing functions factory for KUTE.js based on UnitBezier
1 lines • 7.27 kB
Source Map (JSON)
{"version":3,"file":"bezier-easing.cjs","names":[],"sources":["../package.json","../src/index.ts"],"sourcesContent":["{\n \"name\": \"@thednp/bezier-easing\",\n \"version\": \"1.0.14\",\n \"description\": \"🍬 A Typescript based cubic-bezier easing functions factory for KUTE.js based on UnitBezier\",\n \"homepage\": \"https://github.com/thednp/bezier-easing\",\n \"author\": \"thednp\",\n \"license\": \"MIT\",\n \"main\": \"./dist/bezier-easing.js\",\n \"module\": \"./dist/bezier-easing.mjs\",\n \"exports\": {\n \".\": {\n \"types\": \"./dist/bezier-easing.d.ts\",\n \"require\": \"./dist/bezier-easing.cjs\",\n \"import\": \"./dist/bezier-easing.mjs\"\n }\n },\n \"files\": [\n \"dist\",\n \"package.json\",\n \"README.md\",\n \"LICENSE\"\n ],\n \"bugs\": {\n \"url\": \"https://github.com/thednp/bezier-easing/issues\"\n },\n \"publishConfig\": {\n \"access\": \"public\",\n \"registry\": \"https://registry.npmjs.org/\"\n },\n \"scripts\": {\n \"pre-test\": \"pnpm clean-coverage\",\n \"clean-coverage\": \"rm -rf coverage .nyc_output\",\n \"test\": \"pnpm pre-test && vitest\",\n \"test-ui\": \"pnpm pre-test && vitest --ui\",\n \"format\": \"deno fmt src\",\n \"lint\": \"pnpm lint:ts && pnpm lint:types\",\n \"lint:ts\": \"deno lint src\",\n \"lint:types\": \"tsc --noEmit\",\n \"fix:ts\": \"deno lint src --fix\",\n \"build\": \"vite build\",\n \"prepublishOnly\": \"pnpm update --latest && pnpm format && pnpm lint && pnpm build\"\n },\n \"repository\": {\n \"type\": \"git\",\n \"url\": \"git+https://github.com/thednp/bezier-easing.git\"\n },\n \"keywords\": [\n \"kute.js\",\n \"cubic-bezier\",\n \"cubic-bezier-easing\",\n \"easing\",\n \"ease\",\n \"easing functions\",\n \"animation engine\",\n \"animation\",\n \"animations\",\n \"typescript\"\n ],\n \"devDependencies\": {\n \"@testing-library/dom\": \"^10.4.1\",\n \"@types/node\": \"^25.5.0\",\n \"@vitest/coverage-istanbul\": \"^4.1.2\",\n \"@vitest/ui\": \"^4.1.2\",\n \"happy-dom\": \"^20.8.8\",\n \"typescript\": \"^6.0.2\",\n \"vite\": \"^8.0.3\",\n \"vite-plugin-dts\": \"^4.5.4\",\n \"vite-plugin-strip-comments\": \"^0.0.10\",\n \"vitest\": \"^4.1.2\"\n },\n \"packageManager\": \"pnpm@10.33.0\",\n \"engines\": {\n \"node\": \">=20\",\n \"pnpm\": \">=8.6.0\"\n }\n}\n","import type { BezierEasingFunction } from \"./easing-function.ts\";\nimport { version } from \"../package.json\";\n\n/**\n * Creates cubic-bezier easing functions for animation engines.\n *\n * @see http://svn.webkit.org/repository/webkit/trunk/Source/WebCore/platform/graphics/UnitBezier.h\n *\n * @class\n */\nexport default class CubicBezier {\n static version = version;\n public cx: number;\n public bx: number;\n public ax: number;\n public cy: number;\n public by: number;\n public ay: number;\n /**\n * @constructor\n * @param x1 - first point horizontal position\n * @param y1 - first point vertical position\n * @param x2 - second point horizontal position\n * @param y2 - second point vertical position\n * @param functionName - an optional function name\n * @returns a new CubicBezier easing function\n */\n constructor(\n x1?: number,\n y1?: number,\n x2?: number,\n y2?: number,\n functionName?: string,\n ) {\n // pre-calculate the polynomial coefficients\n // First and last control points are implied to be (0.0, 0.0) and (1.0, 1.0)\n const p1x = x1 || 0;\n const p1y = y1 || 0;\n const p2x = x2 || 1;\n const p2y = y2 || 1;\n const isNumber = (n: unknown): n is number => typeof n === \"number\";\n const allNumbers = [x1, y1, x2, y2].every(isNumber);\n const name = functionName\n ? functionName\n : allNumbers\n ? `cubic-bezier(${[p1x, p1y, p2x, p2y].join(\",\")})`\n : \"linear\";\n\n this.cx = 3 * p1x;\n this.bx = 3 * (p2x - p1x) - this.cx;\n this.ax = 1 - this.cx - this.bx;\n this.cy = 3 * p1y;\n this.by = 3 * (p2y - p1y) - this.cy;\n this.ay = 1 - this.cy - this.by;\n\n const BezierEasing = (t: number) => this.sampleCurveY(this.solveCurveX(t));\n\n // this function needs a name\n Object.defineProperty(BezierEasing, \"name\", { writable: true });\n BezierEasing.name = name;\n\n return BezierEasing as BezierEasingFunction;\n }\n\n /**\n * @param t - progress [0-1]\n * @return - sampled X value\n */\n sampleCurveX(t: number) {\n return ((this.ax * t + this.bx) * t + this.cx) * t;\n }\n\n /**\n * @param t - progress [0-1]\n * @return - sampled Y value\n */\n sampleCurveY(t: number) {\n return ((this.ay * t + this.by) * t + this.cy) * t;\n }\n\n /**\n * @param t - progress [0-1]\n * @return - sampled curve derivative X value\n */\n sampleCurveDerivativeX(t: number) {\n return (3 * this.ax * t + 2 * this.bx) * t + this.cx;\n }\n\n /**\n * @param x - progress [0-1]\n * @return - solved curve X value\n */\n solveCurveX(x: number) {\n // Set Precision\n const epsilon = 1e-6;\n\n // Skip values out of range\n if (x <= 0) return 0;\n if (x >= 1) return 1;\n\n let t2 = x;\n let x2 = 0;\n let d2 = 0;\n\n // First try a few iterations of Newton's method\n // -- usually very fast.\n for (let i = 0; i < 8; i += 1) {\n x2 = this.sampleCurveX(t2) - x;\n if (Math.abs(x2) < epsilon) return t2;\n d2 = this.sampleCurveDerivativeX(t2);\n /* istanbul ignore next @preserve */\n if (Math.abs(d2) < epsilon) break;\n t2 -= x2 / d2;\n }\n\n // No solution found - use bi-section\n let t0 = 0;\n let t1 = 1;\n t2 = x;\n\n while (t0 < t1) {\n x2 = this.sampleCurveX(t2);\n if (Math.abs(x2 - x) < epsilon) return t2;\n if (x > x2) t0 = t2;\n else t1 = t2;\n\n t2 = (t1 - t0) * 0.5 + t0;\n }\n\n // Give up\n /* istanbul ignore next @preserve */\n return t2;\n }\n}\n"],"mappings":"eCIiB,EAAjB,KAA+B,CAC9B,OAAA,QAAA,EACC,GACD,GACC,GACA,GACF,GACE,GAEA,YACA,EACA,EACA,EACA,EACE,EACA,CAGA,IAAE,EAAU,GAAC,EACX,EAAU,GAAC,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