Last active
January 10, 2020 17:04
-
-
Save bh-lay/0359b8c1089265fd3d6a9645f9c8c7cf to your computer and use it in GitHub Desktop.
两种常见的图像放置在区域内的计算方法
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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