Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Javascript snippet to aspect fit bounds in bounds.
aspectFitBoundsInBounds(src, dest, center=true) {
let WIDTH_KEY = "width";
let HEIGHT_KEY = "height";
let largerSide = src.height > src.width ? HEIGHT_KEY : WIDTH_KEY;
let smallerSide = largerSide == HEIGHT_KEY ? WIDTH_KEY : HEIGHT_KEY;
let aspectRatio = src[smallerSide]/src[largerSide];
src[largerSide] = dest[largerSide];
src[smallerSide] = dest[largerSide]*aspectRatio;
src.x = dest.x;
src.y = dest.y;
if (center) {
src.x += (dest.width-src.width)/2;
src.y += (dest.height-src.height)/2;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.