Skip to content

Instantly share code, notes, and snippets.

@mareksuscak
Created October 6, 2017 19:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mareksuscak/527721bd6653cb37ee12e5e5471b3ac0 to your computer and use it in GitHub Desktop.
Save mareksuscak/527721bd6653cb37ee12e5e5471b3ac0 to your computer and use it in GitHub Desktop.
calculate maximum IG lightbox width given image dimensions
// 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