From a6e6a7d741f32141dd374b6f9cb3c6a6adddfd71 Mon Sep 17 00:00:00 2001 From: Lim Chee Aun Date: Wed, 19 Jun 2024 18:29:20 +0800 Subject: [PATCH] Simplify natural aspect ratio math --- src/components/media.jsx | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/src/components/media.jsx b/src/components/media.jsx index c23b839c..a231d63f 100644 --- a/src/components/media.jsx +++ b/src/components/media.jsx @@ -372,22 +372,14 @@ function Media({ ) { $media.dataset.hasSmallDimension = true; } else { - const naturalAspectRatio = ( - naturalWidth / naturalHeight - ).toFixed(2); - const displayAspectRatio = ( - clientWidth / clientHeight - ).toFixed(2); - const similarThreshold = 0.05; - if ( - naturalAspectRatio === displayAspectRatio || - Math.abs(naturalAspectRatio - displayAspectRatio) < - similarThreshold - ) { - // $media.dataset.hasNaturalAspectRatio = true; + const displayNaturalHeight = + (naturalHeight * clientWidth) / naturalWidth; + const almostSimilarHeight = + Math.abs(displayNaturalHeight - clientHeight) < 2; + + if (almostSimilarHeight) { setHasNaturalAspectRatio(true); } - // $media.dataset.aspectRatios = `${naturalAspectRatio} ${displayAspectRatio}`; } } }