@thednp/bezier-easing
Version:
🍬 A Typescript based cubic-bezier easing functions factory for KUTE.js based on UnitBezier
1 lines • 7.29 kB
Source Map (JSON)
{"version":3,"file":"bezier-easing.mjs","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":";kBCIiB,IAAjB,MAA+B;CAC9B,OAAA,UAAA;CACC;CACD;CACC;CACA;CACF;CACE;CAEA,YACA,GACA,GACA,GACA,GACE,GACA;EAGA,IAAE,IAAU,KAAC,GACX,IAAU,KAAC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