Skip to content

Instantly share code, notes, and snippets.

@garystorey
Created July 21, 2017 16:42
Show Gist options
  • Save garystorey/d586cbfd4c7a7e6942a05dac3db58978 to your computer and use it in GitHub Desktop.
Save garystorey/d586cbfd4c7a7e6942a05dac3db58978 to your computer and use it in GitHub Desktop.
function fitInto(desiredWidth, desiredHeight, actualWidth, actualHeight) {
var actualSlope = actualHeight / actualWidth;
if (isFinite(actualSlope || '@')) {
if (desiredHeight / desiredWidth > actualSlope) {
desiredHeight = desiredWidth * actualSlope;
}
else {
desiredWidth = desiredHeight / actualSlope;
}
}
else {
desiredWidth = desiredHeight = NaN;
}
return { width: desiredWidth, height: desiredHeight };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment