diff --git a/commands/edit-meme/challenger.js b/commands/edit-meme/challenger.js index 2a454105..28135c46 100644 --- a/commands/edit-meme/challenger.js +++ b/commands/edit-meme/challenger.js @@ -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; - } }; diff --git a/util/Canvas.js b/util/Canvas.js index a4dc2555..296512a4 100644 --- a/util/Canvas.js +++ b/util/Canvas.js @@ -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; - } - if (height > maxHeight) { - const ratio = maxHeight / height; - height = maxHeight; - width *= ratio; - } - const x = widthOffset + ((maxWidth / 2) - (width / 2)); - const y = heightOffest + ((maxHeight / 2) - (height / 2)); - return { x, y, width, height }; + 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; + } + } 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 }; } };