Skip to content

Instantly share code, notes, and snippets.

@otienog1
Forked from garystorey/fitInto.js
Created February 22, 2018 09:38
Show Gist options
  • Save otienog1/2145c3d8ad6df90a008e3e9f6f392b9e to your computer and use it in GitHub Desktop.
Save otienog1/2145c3d8ad6df90a008e3e9f6f392b9e 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