Repository URL to install this package:
Version:
0.9.5 ▾
|
/**
* Conserve aspect ratio of the orignal region. Useful when shrinking/enlarging
* images to fit into a certain area.
*
* @param {number} srcWidth width of source image
* @param {number} srcHeight height of source image
* @param {number} maxWidth maximum available width
* @param {number} maxHeight maximum available height
* @returns {object} { width, height }
*/
function calculateAspectRatio(
srcWidth: number,
srcHeight: number,
maxWidth: number,
maxHeight: number
) {
const ratio = Math.min(maxWidth / srcWidth, maxHeight / srcHeight)
return {
width: srcWidth * ratio,
height: srcHeight * ratio,
}
}
export { calculateAspectRatio }
export default calculateAspectRatio