UNPKG

@masa-dev/react-signage

Version:

This is a react library for signage.

20 lines (19 loc) 595 B
export function calculateOffset(srcSize, targetSize) { const srcRatio = srcSize.width / srcSize.height; const targetRatio = targetSize.width / targetSize.height; let newWidth, newHeight; if (srcRatio > targetRatio) { newWidth = targetSize.width; newHeight = targetSize.width / srcRatio; } else { newHeight = targetSize.height; newWidth = targetSize.height * srcRatio; } return { xOffset: (targetSize.width - newWidth) / 2, yOffset: (targetSize.height - newHeight) / 2, newWidth, newHeight, }; }