Created
October 6, 2017 19:59
-
-
Save mareksuscak/527721bd6653cb37ee12e5e5471b3ac0 to your computer and use it in GitHub Desktop.
calculate maximum IG lightbox width given image dimensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Constants | |
const MAX_PLACEHOLDER_WIDTH = 640 | |
const MAX_PLACEHOLDER_HEIGHT = 640 | |
const SIDEBAR_WIDTH = 320 | |
function calcPlaceholderWidth(imageWidth, imageHeight) { | |
const aspectRatio = imageHeight / imageWidth | |
let placeholderWidth | |
if (aspectRatio <= 1) { // wide and square images, we want them to fill the available horizontal space | |
placeholderWidth = MAX_PLACEHOLDER_WIDTH | |
} else { // tall images, we want them to fill the available vertical space | |
placeholderWidth = ((imageWidth * MAX_PLACEHOLDER_HEIGHT) / imageHeight) | |
} | |
return Math.min(placeholderWidth, MAX_PLACEHOLDER_WIDTH) | |
} | |
function calcMaxLightboxWidth(imageWidth, imageHeight) { | |
return calcPlaceholderWidth(imageWidth, imageHeight) + SIDEBAR_WIDTH | |
} | |
console.log(calcMaxLightboxWidth(1080, 1350)) | |
console.log(calcMaxLightboxWidth(1080, 1080)) | |
console.log(calcMaxLightboxWidth(1080, 810)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment