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) { static centerImagePart(img, boxWidth, boxHeight, boxX, boxY) {
const imgAspectRatio = img.width / img.height; const imgAspectRatio = img.width / img.height;
const boxAspectRatio = boxWidth / boxHeight; const boxAspectRatio = boxWidth / boxHeight;
let drawWidth; let drawWidth;
let drawHeight; let drawHeight;
if (imgAspectRatio > boxAspectRatio) { if (imgAspectRatio > boxAspectRatio) {
drawWidth = boxWidth; drawWidth = boxWidth;
drawHeight = drawWidth / imgAspectRatio; drawHeight = drawWidth / imgAspectRatio;
if (drawHeight > boxHeight) { if (drawHeight > boxHeight) {
drawHeight = boxHeight; drawHeight = boxHeight;
drawWidth = drawHeight * imgAspectRatio; drawWidth = drawHeight * imgAspectRatio;
} }
} else { } else {
drawHeight = boxHeight; drawHeight = boxHeight;
drawWidth = drawHeight * imgAspectRatio; drawWidth = drawHeight * imgAspectRatio;
if (drawWidth > boxWidth) { if (drawWidth > boxWidth) {
drawWidth = boxWidth; drawWidth = boxWidth;
drawHeight = drawWidth / imgAspectRatio; drawHeight = drawWidth / imgAspectRatio;
} }
} }
const drawX = boxX + ((boxWidth - drawWidth) / 2); const drawX = boxX + ((boxWidth - drawWidth) / 2);
const drawY = boxY + ((boxHeight - drawHeight) / 2); const drawY = boxY + ((boxHeight - drawHeight) / 2);
return { x: drawX, y: drawY, width: drawWidth, height: drawHeight }; return { x: drawX, y: drawY, width: drawWidth, height: drawHeight };
} }
}; };