tickle/modules/utils/getAspectRatio.mjs

22 lines
547 B
JavaScript
Raw Permalink Normal View History

2023-06-07 20:07:47 +00:00
//@ts-check
/**
* @typedef {{width: number;height: number}} Size
*/
/**
* Calculates an ideal ratio
* @param {Size} initial the initial size
* @param {Size} current the current size
* @returns
*/
export const getAspectRatio = (initial, current) => {
const ratioW = current.width / initial.width;
const ratioH = current.height / initial.height;
const ratio = Math.min(ratioW, ratioH);
const width = initial.width * ratio;
const height = initial.height * ratio;
return { width, height, ratio };
};
export default getAspectRatio