From bf76dd77022d6a6742623190960a592499c56628 Mon Sep 17 00:00:00 2001 From: Clay Griffiths Date: Wed, 28 Apr 2021 11:25:23 -0500 Subject: [PATCH 1/2] Use existing maxWidth/maxHeight options if present in aspectRatio calculations rather than overriding them --- js/load-image-scale.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/js/load-image-scale.js b/js/load-image-scale.js index c5e381e..693625a 100644 --- a/js/load-image-scale.js +++ b/js/load-image-scale.js @@ -72,8 +72,8 @@ } } newOptions.crop = true - width = img.naturalWidth || img.width - height = img.naturalHeight || img.height + width = options.maxWidth || img.naturalWidth || img.width + height = options.maxHeight || img.naturalHeight || img.height if (width / height > aspectRatio) { newOptions.maxWidth = height * aspectRatio newOptions.maxHeight = height From cb89a6306a2e261554502ffd738254387eb99bba Mon Sep 17 00:00:00 2001 From: veryspry Date: Mon, 12 Jun 2023 16:04:19 +0200 Subject: [PATCH 2/2] Fix issue where `scaleDown()` and `scaleUp()` could sometimes return a float that would then get incorrectly rounded down when creating a canvas. --- js/load-image-scale.js | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/js/load-image-scale.js b/js/load-image-scale.js index 693625a..213e12a 100644 --- a/js/load-image-scale.js +++ b/js/load-image-scale.js @@ -158,8 +158,15 @@ (minHeight || destHeight) / destHeight ) if (scale > 1) { - destWidth *= scale - destHeight *= scale + /** + * These value are used to create canvas elements. Ensuring that they are integers + * (rather than floats) avoids a potential 1px rounding error in the canvas element + * as canvas elements will always round it down to the nearest integer rather than + * following standard rounding rules. For example, using a value of 199.9999999px + * for a canvas height would result in a height of 199px rather than 200px. + */ + destWidth = Math.round(destWidth * scale) + destHeight = Math.round(destHeight * scale) } } /** @@ -171,8 +178,15 @@ (maxHeight || destHeight) / destHeight ) if (scale < 1) { - destWidth *= scale - destHeight *= scale + /** + * These value are used to create canvas elements. Ensuring that they are integers + * (rather than floats) avoids a potential 1px rounding error in the canvas element + * as canvas elements will always round it down to the nearest integer rather than + * following standard rounding rules. For example, using a value of 199.9999999px + * for a canvas height would result in a height of 199px rather than 200px. + */ + destWidth = Math.round(destWidth * scale) + destHeight = Math.round(destHeight * scale) } } if (useCanvas) {