|
1 | | -import { isFunction } from '../../../core/utils/type'; |
| 1 | +import { isFunction } from '@js/core/utils/type'; |
2 | 2 |
|
3 | 3 | const CSS_TRANSITION_EASING_REGEX = /cubic-bezier\((\d+(?:\.\d+)?)\s*,\s*(\d+(?:\.\d+)?)\s*,\s*(\d+(?:\.\d+)?)\s*,\s*(\d+(?:\.\d+)?)\)/; |
4 | 4 |
|
5 | 5 | const TransitionTimingFuncMap = { |
6 | | - 'linear': 'cubic-bezier(0, 0, 1, 1)', |
7 | | - 'swing': 'cubic-bezier(0.445, 0.05, 0.55, 0.95)', |
8 | | - 'ease': 'cubic-bezier(0.25, 0.1, 0.25, 1)', |
9 | | - 'ease-in': 'cubic-bezier(0.42, 0, 1, 1)', |
10 | | - 'ease-out': 'cubic-bezier(0, 0, 0.58, 1)', |
11 | | - 'ease-in-out': 'cubic-bezier(0.42, 0, 0.58, 1)' |
| 6 | + linear: 'cubic-bezier(0, 0, 1, 1)', |
| 7 | + swing: 'cubic-bezier(0.445, 0.05, 0.55, 0.95)', |
| 8 | + ease: 'cubic-bezier(0.25, 0.1, 0.25, 1)', |
| 9 | + 'ease-in': 'cubic-bezier(0.42, 0, 1, 1)', |
| 10 | + 'ease-out': 'cubic-bezier(0, 0, 0.58, 1)', |
| 11 | + 'ease-in-out': 'cubic-bezier(0.42, 0, 0.58, 1)', |
12 | 12 | }; |
13 | 13 |
|
14 | | -const polynomBezier = function(x1, y1, x2, y2) { |
15 | | - const Cx = 3 * x1; |
16 | | - const Bx = 3 * (x2 - x1) - Cx; |
17 | | - const Ax = 1 - Cx - Bx; |
| 14 | +const polynomBezier = (x1: number, y1: number, x2: number, y2: number): ((t: number) => number) => { |
| 15 | + const Cx = 3 * x1; |
| 16 | + const Bx = 3 * (x2 - x1) - Cx; |
| 17 | + const Ax = 1 - Cx - Bx; |
18 | 18 |
|
19 | | - const Cy = 3 * y1; |
20 | | - const By = 3 * (y2 - y1) - Cy; |
21 | | - const Ay = 1 - Cy - By; |
| 19 | + const Cy = 3 * y1; |
| 20 | + const By = 3 * (y2 - y1) - Cy; |
| 21 | + const Ay = 1 - Cy - By; |
22 | 22 |
|
23 | | - const bezierX = function(t) { |
24 | | - return t * (Cx + t * (Bx + t * Ax)); |
25 | | - }; |
26 | | - |
27 | | - const bezierY = function(t) { |
28 | | - return t * (Cy + t * (By + t * Ay)); |
29 | | - }; |
30 | | - |
31 | | - const derivativeX = function(t) { |
32 | | - return Cx + t * (2 * Bx + t * 3 * Ax); |
33 | | - }; |
| 23 | + const bezierX = (t: number): number => t * (Cx + t * (Bx + t * Ax)); |
34 | 24 |
|
35 | | - const findXFor = function(t) { |
36 | | - let x = t; |
37 | | - let i = 0; |
38 | | - let z; |
| 25 | + const bezierY = (t: number): number => t * (Cy + t * (By + t * Ay)); |
39 | 26 |
|
40 | | - while(i < 14) { |
41 | | - z = bezierX(x) - t; |
42 | | - if(Math.abs(z) < 1e-3) { |
43 | | - break; |
44 | | - } |
| 27 | + const derivativeX = (t: number): number => Cx + t * (2 * Bx + t * 3 * Ax); |
45 | 28 |
|
46 | | - x = x - z / derivativeX(x); |
47 | | - i++; |
48 | | - } |
| 29 | + const findXFor = (t: number): number => { |
| 30 | + let x = t; |
| 31 | + let i = 0; |
| 32 | + // eslint-disable-next-line no-undef-init |
| 33 | + let z: number | undefined = undefined; |
49 | 34 |
|
50 | | - return x; |
51 | | - }; |
| 35 | + while (i < 14) { |
| 36 | + z = bezierX(x) - t; |
| 37 | + if (Math.abs(z) < 1e-3) { |
| 38 | + break; |
| 39 | + } |
52 | 40 |
|
53 | | - return function(t) { |
54 | | - return bezierY(findXFor(t)); |
55 | | - }; |
56 | | -}; |
57 | | - |
58 | | -let easing = {}; |
59 | | -export const convertTransitionTimingFuncToEasing = function(cssTransitionEasing) { |
60 | | - cssTransitionEasing = TransitionTimingFuncMap[cssTransitionEasing] || cssTransitionEasing; |
61 | | - |
62 | | - let coeffs = cssTransitionEasing.match(CSS_TRANSITION_EASING_REGEX); |
63 | | - let forceName; |
64 | | - |
65 | | - if(!coeffs) { |
66 | | - forceName = 'linear'; |
67 | | - coeffs = TransitionTimingFuncMap[forceName].match(CSS_TRANSITION_EASING_REGEX); |
| 41 | + x -= z / derivativeX(x); |
| 42 | + i += 1; |
68 | 43 | } |
69 | 44 |
|
70 | | - coeffs = coeffs.slice(1, 5); |
71 | | - for(let i = 0; i < coeffs.length; i++) { |
72 | | - coeffs[i] = parseFloat(coeffs[i]); |
73 | | - } |
| 45 | + return x; |
| 46 | + }; |
74 | 47 |
|
75 | | - const easingName = forceName || 'cubicbezier_' + coeffs.join('_').replace(/\./g, 'p'); |
| 48 | + return (t: number): number => bezierY(findXFor(t)); |
| 49 | +}; |
76 | 50 |
|
77 | | - if(!isFunction(easing[easingName])) { |
78 | | - easing[easingName] = function(x, t, b, c, d) { |
79 | | - return c * polynomBezier(coeffs[0], coeffs[1], coeffs[2], coeffs[3])(t / d) + b; |
80 | | - }; |
81 | | - } |
| 51 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 52 | +let easing: Record<string, any> = {}; |
| 53 | +export const convertTransitionTimingFuncToEasing = (cssTransitionEasing: string): string => { |
| 54 | + // eslint-disable-next-line no-param-reassign |
| 55 | + cssTransitionEasing = TransitionTimingFuncMap[cssTransitionEasing] || cssTransitionEasing; |
| 56 | + |
| 57 | + // eslint-disable-next-line @stylistic/max-len |
| 58 | + let coeffs: RegExpExecArray | string[] | null = CSS_TRANSITION_EASING_REGEX.exec(cssTransitionEasing); |
| 59 | + const numCoeffs: number[] = []; |
| 60 | + let forceName: string | null = null; |
| 61 | + |
| 62 | + if (!coeffs) { |
| 63 | + forceName = 'linear'; |
| 64 | + coeffs = TransitionTimingFuncMap[forceName].match(CSS_TRANSITION_EASING_REGEX); |
| 65 | + } |
| 66 | + |
| 67 | + // @ts-expect-error |
| 68 | + coeffs = coeffs.slice(1, 5); |
| 69 | + for (let i = 0; i < coeffs.length; i += 1) { |
| 70 | + numCoeffs[i] = parseFloat(coeffs[i]); |
| 71 | + } |
| 72 | + |
| 73 | + // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing |
| 74 | + const easingName = forceName || `cubicbezier_${numCoeffs.join('_').replace(/\./g, 'p')}`; |
| 75 | + |
| 76 | + if (!isFunction(easing[easingName])) { |
| 77 | + easing[easingName] = function (x: number, t: number, b: number, c: number, d: number): number { |
| 78 | + return c * polynomBezier(numCoeffs[0], numCoeffs[1], numCoeffs[2], numCoeffs[3])(t / d) + b; |
| 79 | + }; |
| 80 | + } |
82 | 81 |
|
83 | | - return easingName; |
| 82 | + return easingName; |
84 | 83 | }; |
85 | 84 |
|
86 | | -export function setEasing(value) { |
87 | | - easing = value; |
| 85 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 86 | +export function setEasing(value: Record<string, any>): void { |
| 87 | + easing = value; |
88 | 88 | } |
89 | 89 |
|
90 | | -export function getEasing(name) { |
91 | | - return easing[name]; |
| 90 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 91 | +export function getEasing(name: string): any { |
| 92 | + return easing[name]; |
92 | 93 | } |
0 commit comments