1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-21 09:38:01 +02:00
joplin/packages/app-mobile/components/CameraView/utils/fitRectIntoBounds.ts

25 lines
698 B
TypeScript

export interface Rect {
width: number;
height: number;
}
const fitRectIntoBounds = (rect: Rect, bounds: Rect) => {
const rectRatio = rect.width / rect.height;
const boundsRatio = bounds.width / bounds.height;
const newDimensions: Rect = { width: 0, height: 0 };
// Rect is more landscape than bounds - fit to width
if (rectRatio > boundsRatio) {
newDimensions.width = bounds.width;
newDimensions.height = rect.height * (bounds.width / rect.width);
} else { // Rect is more portrait than bounds - fit to height
newDimensions.width = rect.width * (bounds.height / rect.height);
newDimensions.height = bounds.height;
}
return newDimensions;
};
export default fitRectIntoBounds;