Skip to content

Commit

Permalink
feat: 计算等比例缩放盒子大小
Browse files Browse the repository at this point in the history
  • Loading branch information
galaxy-s10 committed Feb 16, 2023
1 parent 3558a9a commit db7f2c9
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/js/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
/**
* @description 等比例适配盒子大小
* @param width 盒子宽度
* @param height 盒子高度
* @param maxWidth 最大盒子宽度
* @param maxHeight 最大盒子高度
* @returns {{width: number, height: number}} 返回适配好的盒子宽高
*/
export function computeBox({ width, height, maxWidth, maxHeight }) {
// w = h / ratio, h = w * ratio
const ratio = height / width;

let w = width;
let h = height;

if (w > maxWidth) {
w = maxWidth;
h = maxWidth * ratio;
}
if (h > maxHeight) {
w = maxHeight / ratio;
h = maxHeight;
}

return {
width: w,
height: h,
};
}
/**
* @description 下载图片
*/
Expand Down

0 comments on commit db7f2c9

Please sign in to comment.