数学/算法适合图像到屏幕保留宽高比数学/算法适合图像到屏幕保留宽高比(math/algorithm

2019-05-13 09:50发布

我理解接受的答案和它的作品,但我总觉得下面的方法是“最适合”的简单和简洁:

// prep
let maxWidth = 190,
    maxHeight = 150;
let imgWidth = photo.width,
    imgHeight = photo.height;

// calc
let widthRatio = maxWidth / imgWidth,
    heightRatio = maxHeight / imgHeight;
let bestRatio = Math.min(widthRatio, heightRatio);

// output
let newWidth = imgWidth * bestRatio,
    newHeight = imgHeight * bestRatio;
文章来源: math/algorithm Fit image to screen retain aspect ratio