Skip to content

Instantly share code, notes, and snippets.

@bh-lay
Last active January 10, 2020 17:04
Show Gist options
  • Select an option

  • Save bh-lay/0359b8c1089265fd3d6a9645f9c8c7cf to your computer and use it in GitHub Desktop.

Select an option

Save bh-lay/0359b8c1089265fd3d6a9645f9c8c7cf to your computer and use it in GitHub Desktop.
两种常见的图像放置在区域内的计算方法
function getImgContainToRectConfig (outerWidth, outerHeight, imgWidth, imgHeight) => {
let newWidth = outerWidth;
let newHeight = outerWidth * imgHeight / imgWidth;
if (newHeight > outerHeight) {
newHeight = outerHeight;
newWidth = outerHeight * imgWidth / imgHeight;
}
return {
top: (outerHeight - newHeight) / 2,
left: (outerWidth - newWidth) / 2,
width: newWidth,
height: newHeight
}
}
function getImgCoverToRectConfig(outerWidth, outerHeight, imgWidth, imgHeight) => {
let newWidth = outerWidth;
let newHeight = outerWidth * imgHeight / imgWidth;
if (newHeight < outerHeight) {
newHeight = outerHeight;
newWidth = outerHeight * imgWidth / imgHeight;
}
return {
top: (outerHeight - newHeight) / 2,
left: (outerWidth - newWidth) / 2,
width: newWidth,
height: newHeight
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment