diff options
| author | Eltrac <hi@guhub.cn> | 2025-09-18 18:41:51 +0800 |
|---|---|---|
| committer | Eltrac <hi@guhub.cn> | 2025-09-18 18:41:51 +0800 |
| commit | f383f1cecc713ae5f245cc61a825bd9db9db39bb (patch) | |
| tree | 9bcab20928591ba3a7c593ed6a5bf6b22a8a2ead | |
| parent | 6c3e6e0e3f1a96e92299747d2ec5f552908728cb (diff) | |
refactor: avoid repeating animation logic
| -rw-r--r-- | src/shadow.js | 23 |
1 files changed, 9 insertions, 14 deletions
diff --git a/src/shadow.js b/src/shadow.js index 4e06263..b02fad5 100644 --- a/src/shadow.js +++ b/src/shadow.js @@ -75,25 +75,20 @@ export default class Shadow { this.original.style.visibility = 'hidden'; if (!document.body.contains(this.element)) this.placeItself(); - // Wait for image to load before animating - if (this.element.naturalWidth === 0 || this.element.naturalHeight === 0) { - this.element.addEventListener( - 'load', - () => { - Animation.transformMatrix( - this.element, - this.originalPosition, - Animation.calculateFinalState(this.element), - ); - }, - { once: true }, - ); - } else { + // play animation + const animate = () => Animation.transformMatrix( this.element, this.originalPosition, Animation.calculateFinalState(this.element), ); + + if (this.element.naturalWidth === 0 || this.element.naturalHeight === 0) { + // Wait for image to load before animating + this.element.addEventListener('load', animate, { once: true }); + } else { + // animate directly if loaded + animate(); } } |
