mirror of
https://github.com/laurent22/joplin.git
synced 2024-12-24 10:27:10 +02:00
25 lines
698 B
TypeScript
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;
|