Last active
June 1, 2017 18:22
-
-
Save waghcwb/78a1e33123324ddb331b3aab90d6979b to your computer and use it in GitHub Desktop.
This file contains 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
class Dimension | |
constructor: (@width, @height) -> | |
getScaledDimension = (size, boundary) -> | |
original = | |
width: size.width | |
height: size.height | |
bound = | |
width: boundary.width | |
height: boundary.height | |
newsize = | |
width: original.width | |
height: original.height | |
if original.width > bound.width | |
newsize.width = bound.width | |
newsize.height = (newsize.width * original.height) / original.width | |
if newsize.height > bound.height | |
newsize.height = bound.height | |
newsize.width = (newsize.height * original.width) / original.height | |
return new Dimension newsize.width, newsize.height |
This file contains 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
var Dimension, getScaledDimension; | |
Dimension = (function() { | |
function Dimension(width, height) { | |
this.width = width; | |
this.height = height; | |
} | |
return Dimension; | |
})(); | |
getScaledDimension = function(size, boundary) { | |
var bound, newsize, original; | |
original = { | |
width: size.width, | |
height: size.height | |
}; | |
bound = { | |
width: boundary.width, | |
height: boundary.height | |
}; | |
newsize = { | |
width: original.width, | |
height: original.height | |
}; | |
if (original.width > bound.width) { | |
newsize.width = bound.width; | |
newsize.height = (newsize.width * original.height) / original.width; | |
} | |
if (newsize.height > bound.height) { | |
newsize.height = bound.height; | |
newsize.width = (newsize.height * original.width) / original.height; | |
} | |
return new Dimension(newsize.width, newsize.height); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment