Skip to content

Instantly share code, notes, and snippets.

@moimikey
Created August 6, 2014 14:07
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 moimikey/2dc958319220771a8230 to your computer and use it in GitHub Desktop.
Save moimikey/2dc958319220771a8230 to your computer and use it in GitHub Desktop.
# Returns an object with a new width and height
# constraining the proportions of the given max width
# and max height
#
# examples:
# Util.scaleProportion(200, 200, 2000, 2000);
# > Object {width: 200, height: 200}
# scaleProportion(200, 200, 2582, 2394)
# > Object {width: 200, height: 185}
#
Util.scaleProportion = (maxW, maxH, currW, currH) ->
ratio = currH / currW
if currW >= maxW and ratio <= 1
currW = maxW
currH = currW * ratio
else if currH >= maxH
currH = maxH
currW = currH / ratio
width: ~~currW, height: ~~currH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment