summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEltrac <hi@guhub.cn>2025-09-17 21:29:23 +0800
committerEltrac <hi@guhub.cn>2025-09-17 21:29:23 +0800
commitbca54cbb5026564b4139fe850b286239c5a54929 (patch)
treea2253db705af3aacccd81c27a75d713088826864
parent23ad3ed2d45ab5cdb4e320ab5c64b4f1a16ed3b1 (diff)
refactor: less 'if's
-rw-r--r--src/animation.js15
1 files changed, 7 insertions, 8 deletions
diff --git a/src/animation.js b/src/animation.js
index 6952a72..d4e1897 100644
--- a/src/animation.js
+++ b/src/animation.js
@@ -3,18 +3,17 @@ export default class Animation {
let finals = {}
const margin = 50
- const ratio = element.naturalWidth / element.naturalHeight;
+ const nw = element.naturalWidth;
+ const nh = element.naturalHeight;
+ const ratio = nw / nh;
const ww = window.innerWidth - 2*margin;
const wh = window.innerHeight - 2*margin;
- if (element.naturalWidth > ww) {
- finals.width = ww;
- finals.height = ww / ratio;
- } else {
- finals.width = element.naturalWidth;
- finals.height = element.naturalHeight;
- }
+ // size image based on width first
+ finals.width = (nw > ww) ? ww : nw;
+ finals.height = (nw > ww) ? ww / ratio : nh;
+ // if height is too big, adjust based on height
if (finals.height > wh) {
finals.height = wh;
finals.width = wh * ratio;