/* Simulate object-fit: contain. * Takes an object's dimension (innerWidth/innerHeight) * and a container's dimensions (outerWidth/outerHeight) * and returns a new width/height where the inner object * will use as much of the container's space as possible * while respecting the inner object's aspect ratio */ function contain( innerWidth, innerHeight, outerWidth, outerHeight ) { const innerRatio = innerWidth / innerHeight const outerRatio = outerWidth / outerHeight if (innerRatio > outerRatio) { let scale = outerWidth / innerWidth return [ outerWidth, innerHeight * scale ] } else { let scale = outerHeight / innerHeight return [ innerWidth * scale, outerHeight ] } }