Skip to content

Instantly share code, notes, and snippets.

@aferriss
Created July 11, 2018 04:38
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 aferriss/87a3f05b1e1060e80495d96a67cc65fc to your computer and use it in GitHub Desktop.
Save aferriss/87a3f05b1e1060e80495d96a67cc65fc to your computer and use it in GitHub Desktop.
Cover and contain math functions
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