(element, props, duration, easing = "easeOutQuad", onComplete = null)
| 49 | } |
| 50 | |
| 51 | to(element, props, duration, easing = "easeOutQuad", onComplete = null) { |
| 52 | this.killAnimationsOf(element); |
| 53 | |
| 54 | const cssEasing = EASING_MAP[easing] || EASING_MAP.easeOutQuad; |
| 55 | |
| 56 | const transformProps = {}; |
| 57 | const otherProps = {}; |
| 58 | |
| 59 | for (const [key, value] of Object.entries(props)) { |
| 60 | if (key === "position") { |
| 61 | if (typeof value.x === "number") transformProps.x = value.x; |
| 62 | if (typeof value.y === "number") transformProps.y = value.y; |
| 63 | } else if (key === "x" || key === "y") { |
| 64 | transformProps[key] = value; |
| 65 | } else if (key === "scale") { |
| 66 | transformProps.scale = value; |
| 67 | } else if (key === "alpha" || key === "opacity") { |
| 68 | otherProps.opacity = value; |
| 69 | } else { |
| 70 | otherProps[key] = value; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | const computedStyle = getComputedStyle(element); |
| 75 | const matrix = new DOMMatrix(computedStyle.transform); |
| 76 | const currentScale = Math.sqrt( |
| 77 | matrix.m11 * matrix.m11 + matrix.m21 * matrix.m21, |
| 78 | ); |
| 79 | |
| 80 | transformProps.x ??= matrix.m41; |
| 81 | transformProps.y ??= matrix.m42; |
| 82 | transformProps.scale ??= currentScale; |
| 83 | |
| 84 | const initialTransform = this._buildTransformString( |
| 85 | matrix.m41, |
| 86 | matrix.m42, |
| 87 | currentScale, |
| 88 | ); |
| 89 | |
| 90 | const finalTransform = this._buildTransformString( |
| 91 | transformProps.x, |
| 92 | transformProps.y, |
| 93 | transformProps.scale, |
| 94 | ); |
| 95 | |
| 96 | const initialKeyframe = { transform: initialTransform }; |
| 97 | const finalKeyframe = { transform: finalTransform }; |
| 98 | |
| 99 | for (const [key, value] of Object.entries(otherProps)) { |
| 100 | const currentVal = |
| 101 | key === "opacity" |
| 102 | ? element.style.opacity || computedStyle.opacity |
| 103 | : element.style[key]; |
| 104 | initialKeyframe[key] = currentVal; |
| 105 | finalKeyframe[key] = value; |
| 106 | } |
| 107 | |
| 108 | const animation = element.animate([initialKeyframe, finalKeyframe], { |
no test coverage detected