Skip to content

Instantly share code, notes, and snippets.

@fronterior
Created February 14, 2024 02:54
Show Gist options
  • Save fronterior/835312f0e3ca7f0ea207b6da922d14db to your computer and use it in GitHub Desktop.
Save fronterior/835312f0e3ca7f0ea207b6da922d14db to your computer and use it in GitHub Desktop.
getContain.ts
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