Created
July 11, 2018 04:38
-
-
Save aferriss/87a3f05b1e1060e80495d96a67cc65fc to your computer and use it in GitHub Desktop.
Cover and contain math functions
This file contains hidden or 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
function cover(imgWidth, imgHeight, containerWidth, containerHeight){ | |
let wRatio = containerWidth / imgWidth; | |
let hRatio = containerHeight / imgHeight; | |
let coverRatio = Math.max(wRatio, hRatio); | |
return [imgWidth * coverRatio, imgHeight * coverRatio]; | |
} | |
function contain(imgWidth, imgHeight, containerWidth, containerHeight){ | |
let wRatio = containerWidth / imgWidth; | |
let hRatio = containerHeight / imgHeight; | |
let containRatio = Math.min(wRatio, hRatio); | |
return [imgWidth * containRatio, imgHeight * containRatio]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment