mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-17 00:07:36 +02:00
Fix
This commit is contained in:
@@ -133,18 +133,27 @@ module.exports = class ChallengerCommand extends Command {
|
|||||||
|
|
||||||
centerInBox(ctx, img, boxX, boxY, boxWidth, boxHeight) {
|
centerInBox(ctx, img, boxX, boxY, boxWidth, boxHeight) {
|
||||||
const imgAspectRatio = img.width / img.height;
|
const imgAspectRatio = img.width / img.height;
|
||||||
const boxAspectRatio = boxWidth / boxHeight;
|
const boxAspectRatio = boxWidth / boxHeight;
|
||||||
let drawWidth, drawHeight;
|
let drawWidth;
|
||||||
if (imgAspectRatio > boxAspectRatio) {
|
let drawHeight;
|
||||||
drawHeight = boxHeight;
|
if (imgAspectRatio > boxAspectRatio) {
|
||||||
drawWidth = img.width * (drawHeight / img.height);
|
drawWidth = boxWidth;
|
||||||
} else {
|
drawHeight = drawWidth / imgAspectRatio;
|
||||||
drawWidth = boxWidth;
|
if (drawHeight > boxHeight) {
|
||||||
drawHeight = img.height * (drawWidth / img.width);
|
drawHeight = boxHeight;
|
||||||
}
|
drawWidth = drawHeight * imgAspectRatio;
|
||||||
const drawX = boxX + ((boxWidth - drawWidth) / 2);
|
}
|
||||||
const drawY = boxY + ((boxHeight - drawHeight) / 2);
|
} else {
|
||||||
ctx.drawImage(img, drawX, drawY, drawWidth, drawHeight);
|
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);
|
||||||
|
ctx.drawImage(img, drawX, drawY, drawWidth, drawHeight);
|
||||||
return ctx;
|
return ctx;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user