UNPKG

@e-group/utils

Version:

eGroup team utils that share across projects.

32 lines (28 loc) 914 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = calculateAspectRatioFit; /** * Conserve aspect ratio of the original region. Useful when shrinking * 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 * @return {Object} { width, height } */ function calculateAspectRatioFit(srcWidth, srcHeight, maxWidth, maxHeight) { const ratio = Math.min(maxWidth / srcWidth, maxHeight / srcHeight); // If source width and height both smaller than max just return source. if (srcWidth <= maxWidth && srcHeight <= maxHeight) { return { width: srcWidth, height: srcHeight }; } return { width: srcWidth * ratio, height: srcHeight * ratio }; }