This commit is contained in:
Dragon Fire
2024-04-28 11:45:57 -04:00
parent de4a743363
commit d1c656f678
+19 -19
View File
@@ -277,26 +277,26 @@ module.exports = class CanvasUtil {
static centerImagePart(img, boxWidth, boxHeight, boxX, boxY) {
const imgAspectRatio = img.width / img.height;
const boxAspectRatio = boxWidth / boxHeight;
let drawWidth;
const boxAspectRatio = boxWidth / boxHeight;
let drawWidth;
let drawHeight;
if (imgAspectRatio > boxAspectRatio) {
drawWidth = boxWidth;
drawHeight = drawWidth / imgAspectRatio;
if (drawHeight > boxHeight) {
drawHeight = boxHeight;
drawWidth = drawHeight * imgAspectRatio;
}
} else {
drawHeight = boxHeight;
drawWidth = drawHeight * imgAspectRatio;
if (drawWidth > boxWidth) {
drawWidth = boxWidth;
drawHeight = drawWidth / imgAspectRatio;
}
}
const drawX = boxX + ((boxWidth - drawWidth) / 2);
const drawY = boxY + ((boxHeight - drawHeight) / 2);
if (imgAspectRatio > boxAspectRatio) {
drawWidth = boxWidth;
drawHeight = drawWidth / imgAspectRatio;
if (drawHeight > boxHeight) {
drawHeight = boxHeight;
drawWidth = drawHeight * imgAspectRatio;
}
} else {
drawHeight = boxHeight;
drawWidth = drawHeight * imgAspectRatio;
if (drawWidth > boxWidth) {
drawWidth = boxWidth;
drawHeight = drawWidth / imgAspectRatio;
}
}
const drawX = boxX + ((boxWidth - drawWidth) / 2);
const drawY = boxY + ((boxHeight - drawHeight) / 2);
return { x: drawX, y: drawY, width: drawWidth, height: drawHeight };
}
};