Skip to content

Instantly share code, notes, and snippets.

@waghcwb
Last active June 1, 2017 18:22
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 waghcwb/78a1e33123324ddb331b3aab90d6979b to your computer and use it in GitHub Desktop.
Save waghcwb/78a1e33123324ddb331b3aab90d6979b to your computer and use it in GitHub Desktop.
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
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