如果我们单独查看每个维度,很容易看到调整的数学。MAX_DIMENSION = CURRENT_DIMENSION * ADJUSTMENT// We need to figure out what the adjustment is, we have the other two values// do some algebra and we getADJUSTMENT = MAX_DIMENSION / CURRENT_DIMENSION出现的问题是每个维度都有自己的调整值,这会导致图像被拉伸/压缩(纵横比不会保持不变)。所以我们只需要选择一个调整值来使用,但是哪一个呢?当然是最小的,否则其中一个维度会溢出。// Calculate which adjustment is the smallest, width or height// otherwise we'd overflow one of them.let widthPercent = MAX_WIDTH / iw;let heightPercent = MAX_HEIGHT / ih;let smallestPercent = Math.min(widthPercent, heightPercent);// This works for both scaling up and scaling downreturn { w: iw * smallestPercent, h: ih * smallestPercent}