Skip to content

Instantly share code, notes, and snippets.

@amorino
Created January 19, 2017 01:00
Show Gist options
  • Select an option

  • Save amorino/df6a21e8262037afd94935a6864c7439 to your computer and use it in GitHub Desktop.

Select an option

Save amorino/df6a21e8262037afd94935a6864c7439 to your computer and use it in GitHub Desktop.
ratio = (parent = {width: window.innerWidth, height: window.innerHeight }, child) => {
let width, height
if (child.width / parent.width < child.height / parent.height) {
width = parent.width
height = width / child.width * child.height
} else {
height = parent.height
width = height / child.height * child.width
}
const result = {
width,
height,
left: (parent.width - width) / 2,
top: (parent.height - height) / 2,
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment