Skip to content

Instantly share code, notes, and snippets.

@michaelwclark
Created February 12, 2019 22:57
Show Gist options
  • Save michaelwclark/391c19d01c1b0ab6c3d59f298d6f08f4 to your computer and use it in GitHub Desktop.
Save michaelwclark/391c19d01c1b0ab6c3d59f298d6f08f4 to your computer and use it in GitHub Desktop.
gets the aspect ratio. Leaves a bit to be wanted.
const aspectRatio = (height, width, seperator=':')=> {
const gcd = (a, b) => b === 0 ? a : gcd(b, a % b)
const [h1, w1] = height < width ? [width, height] : [height, width]
const divisor = gcd(h1, w1)
return `${h1/divisor}${seperator}${w1/divisor}`
}
console.log(aspectRatio(549,500)) // 549:500
console.log(aspectRatio(1024,768)) // 4:3
console.log(aspectRatio(533,320)) //533:320
console.log(aspectRatio(1106,2048)) // 1024:553
console.log(aspectRatio(100,100)) // 1:1
console.log(aspectRatio(533,320)) //533:320
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment