Created
February 14, 2024 02:54
-
-
Save fronterior/835312f0e3ca7f0ea207b6da922d14db to your computer and use it in GitHub Desktop.
getContain.ts
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 getContain( | |
targetWidth: number, | |
targetHeight: number, | |
containerWidth: number, | |
containerHeight: number, | |
xOffset = 0.5, | |
yOffset = 0.5, | |
) { | |
const targetAspectRatio = targetWidth / targetHeight; | |
const containerAspectRatio = containerWidth / containerHeight; | |
// 가로 기준 | |
if (targetAspectRatio > containerAspectRatio) { | |
const scale = containerWidth / targetWidth; | |
const width = containerWidth; | |
const height = targetHeight * scale; | |
const x = 0; | |
const y = (containerHeight - height) * yOffset; | |
return { | |
height, | |
scale, | |
width, | |
x, | |
y, | |
}; | |
// 세로 기준 | |
} | |
const scale = containerHeight / targetHeight; | |
const width = targetWidth * scale; | |
const height = containerHeight; | |
const x = (containerWidth - width) * xOffset; | |
const y = 0; | |
return { | |
height, | |
scale, | |
width, | |
x, | |
y, | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment