mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-24 22:34:46 +02:00
This is just better centerImagePart
This commit is contained in:
@@ -3,7 +3,7 @@ const { PermissionFlagsBits } = require('discord.js');
|
|||||||
const { createCanvas, loadImage } = require('canvas');
|
const { createCanvas, loadImage } = require('canvas');
|
||||||
const request = require('node-superfetch');
|
const request = require('node-superfetch');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const { silhouette, hasAlpha } = require('../../util/Canvas');
|
const { silhouette, hasAlpha, centerImagePart } = require('../../util/Canvas');
|
||||||
const games = {
|
const games = {
|
||||||
64: {
|
64: {
|
||||||
x: 207,
|
x: 207,
|
||||||
@@ -118,7 +118,8 @@ module.exports = class ChallengerCommand extends Command {
|
|||||||
const ctx = canvas.getContext('2d');
|
const ctx = canvas.getContext('2d');
|
||||||
ctx.drawImage(base, 0, 0);
|
ctx.drawImage(base, 0, 0);
|
||||||
const img = silhouetted ? this.silhouetteImage(data) : data;
|
const img = silhouetted ? this.silhouetteImage(data) : data;
|
||||||
this.centerInBox(ctx, img, gameData.x, gameData.y, gameData.maxWidth, gameData.maxHeight);
|
const { x, y, width, height } = centerImagePart(img, gameData.maxWidth, gameData.maxHeight, gameData.x, gameData.y);
|
||||||
|
ctx.drawImage(img, x, y, width, height);
|
||||||
return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'challenger.png' }] });
|
return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'challenger.png' }] });
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,30 +131,4 @@ module.exports = class ChallengerCommand extends Command {
|
|||||||
silhouette(ctx, 0, 0, image.width, image.height);
|
silhouette(ctx, 0, 0, image.width, image.height);
|
||||||
return canvas;
|
return canvas;
|
||||||
}
|
}
|
||||||
|
|
||||||
centerInBox(ctx, img, boxX, boxY, boxWidth, boxHeight) {
|
|
||||||
const imgAspectRatio = img.width / img.height;
|
|
||||||
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);
|
|
||||||
ctx.drawImage(img, drawX, drawY, drawWidth, drawHeight);
|
|
||||||
return ctx;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|||||||
+23
-15
@@ -275,20 +275,28 @@ module.exports = class CanvasUtil {
|
|||||||
return { x, y, width, height };
|
return { x, y, width, height };
|
||||||
}
|
}
|
||||||
|
|
||||||
static centerImagePart(data, maxWidth, maxHeight, widthOffset, heightOffest) {
|
static centerImagePart(img, boxWidth, boxHeight, boxX, boxY) {
|
||||||
let { width, height } = data;
|
const imgAspectRatio = img.width / img.height;
|
||||||
if (width > maxWidth) {
|
const boxAspectRatio = boxWidth / boxHeight;
|
||||||
const ratio = maxWidth / width;
|
let drawWidth;
|
||||||
width = maxWidth;
|
let drawHeight;
|
||||||
height *= ratio;
|
if (imgAspectRatio > boxAspectRatio) {
|
||||||
}
|
drawWidth = boxWidth;
|
||||||
if (height > maxHeight) {
|
drawHeight = drawWidth / imgAspectRatio;
|
||||||
const ratio = maxHeight / height;
|
if (drawHeight > boxHeight) {
|
||||||
height = maxHeight;
|
drawHeight = boxHeight;
|
||||||
width *= ratio;
|
drawWidth = drawHeight * imgAspectRatio;
|
||||||
}
|
}
|
||||||
const x = widthOffset + ((maxWidth / 2) - (width / 2));
|
} else {
|
||||||
const y = heightOffest + ((maxHeight / 2) - (height / 2));
|
drawHeight = boxHeight;
|
||||||
return { x, y, width, height };
|
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 };
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user