From 30602630c42b2462378ba00c5e737567ee440f0d Mon Sep 17 00:00:00 2001 From: AliMHijazi <69132997+AliMHijazi@users.noreply.github.com> Date: Fri, 5 May 2023 22:32:19 -0500 Subject: [PATCH] Update MMM-XKCD.js This updated section allows you to set a maximum for both the width and height and scales the final size so the comic is no larger than either. Making either 0 will still scale the entire comic to the max size of the other parameter. --- MMM-XKCD.js | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/MMM-XKCD.js b/MMM-XKCD.js index 5b4f731..3c84111 100644 --- a/MMM-XKCD.js +++ b/MMM-XKCD.js @@ -80,10 +80,26 @@ Module.register("MMM-XKCD", { // Limit width or height of comic on load comic.onload = function() { const width = this.width; - if (this.config.limitComicHeight > 0) { + + // This updated section allows you to set a maximum for both the width and height and + // scales the final size so the comic is no larger than either. Making either 0 will + // still scale the entire comic to the max size of the other parameter. + const height = this.height; + let comicRatio = width / height; + + if (this.config.limitComicWidth != 0 && + this.config.limitComicHeight != 0) { + if (width / this.config.limitComicWidth > height / this.config.limitComicHeight) { + comic.style.width = this.config.limitComicWidth + "px"; + comic.style.height = this.config.limitComicWidth / comicRatio + "px"; + } else { + comic.style.height = this.config.limitComicHeight + "px"; + comic.style.width = this.config.limitComicHeight * comicRatio + "px"; + } + } else if (this.config.limitComicHeight > 0) { comic.style.height = this.config.limitComicHeight + "px"; comic.style.width = "auto"; - } else if (this.config.limitComicWidth > 0 && width > this.config.limitComicWidth) { + } else { comic.style.width = this.config.limitComicWidth + "px"; comic.style.height = "auto"; } @@ -105,4 +121,4 @@ Module.register("MMM-XKCD", { this.updateDom(this.animationSpeed); } } -}); \ No newline at end of file +});