This is just better centerImagePart

This commit is contained in:
Dragon Fire
2024-04-28 11:35:10 -04:00
parent 7250e29f90
commit aa64faded2
2 changed files with 26 additions and 43 deletions
+3 -28
View File
@@ -3,7 +3,7 @@ const { PermissionFlagsBits } = require('discord.js');
const { createCanvas, loadImage } = require('canvas');
const request = require('node-superfetch');
const path = require('path');
const { silhouette, hasAlpha } = require('../../util/Canvas');
const { silhouette, hasAlpha, centerImagePart } = require('../../util/Canvas');
const games = {
64: {
x: 207,
@@ -118,7 +118,8 @@ module.exports = class ChallengerCommand extends Command {
const ctx = canvas.getContext('2d');
ctx.drawImage(base, 0, 0);
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' }] });
}
@@ -130,30 +131,4 @@ module.exports = class ChallengerCommand extends Command {
silhouette(ctx, 0, 0, image.width, image.height);
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;
}
};
+21 -13
View File
@@ -275,20 +275,28 @@ module.exports = class CanvasUtil {
return { x, y, width, height };
}
static centerImagePart(data, maxWidth, maxHeight, widthOffset, heightOffest) {
let { width, height } = data;
if (width > maxWidth) {
const ratio = maxWidth / width;
width = maxWidth;
height *= ratio;
static centerImagePart(img, boxWidth, boxHeight, boxX, boxY) {
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;
}
if (height > maxHeight) {
const ratio = maxHeight / height;
height = maxHeight;
width *= ratio;
} else {
drawHeight = boxHeight;
drawWidth = drawHeight * imgAspectRatio;
if (drawWidth > boxWidth) {
drawWidth = boxWidth;
drawHeight = drawWidth / imgAspectRatio;
}
const x = widthOffset + ((maxWidth / 2) - (width / 2));
const y = heightOffest + ((maxHeight / 2) - (height / 2));
return { x, y, width, height };
}
const drawX = boxX + ((boxWidth - drawWidth) / 2);
const drawY = boxY + ((boxHeight - drawHeight) / 2);
return { x: drawX, y: drawY, width: drawWidth, height: drawHeight };
}
};