From a4115c57b85ee1cb60dba1b4cf0e4d8e61b50426 Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Fri, 29 Jan 2021 20:54:06 -0500 Subject: [PATCH] Motion Blur Command --- README.md | 3 +- commands/edit-image/motion-blur.js | 44 ++++++++++++++++++++++++++++++ package.json | 2 +- util/Canvas.js | 8 ++++++ 4 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 commands/edit-image/motion-blur.js diff --git a/README.md b/README.md index a6ba9705..5cf3e8c8 100644 --- a/README.md +++ b/README.md @@ -261,7 +261,7 @@ in the appropriate channel's topic to use it. ## Commands -Total: 585 +Total: 586 ### Utility: @@ -682,6 +682,7 @@ Total: 585 * **liquid-rescale:** Draws an image or a user's avatar but with liquid rescale from ImageMagick. * **minecraft-skin:** Sends the Minecraft skin for a user. * **mirror:** Draws an image or a user's avatar but mirrored on the X/Y axis (or both). +* **motion-blur:** Draws an image or a user's avatar with motion blur. * **needs-more-jpeg:** Draws an image or a user's avatar as a low quality JPEG. * **newspaper:** Creates a fake newspaper with the headline and body of your choice. * **noise:** Draws an image or a user's avatar but with noise. diff --git a/commands/edit-image/motion-blur.js b/commands/edit-image/motion-blur.js new file mode 100644 index 00000000..5a8029c1 --- /dev/null +++ b/commands/edit-image/motion-blur.js @@ -0,0 +1,44 @@ +const Command = require('../../structures/Command'); +const { createCanvas, loadImage } = require('canvas'); +const request = require('node-superfetch'); +const { motionBlur } = require('../../util/Canvas'); + +module.exports = class MotionBlurCommand extends Command { + constructor(client) { + super(client, { + name: 'motion-blur', + aliases: ['m-blur', 'motion'], + group: 'edit-image', + memberName: 'motion-blur', + description: 'Draws an image or a user\'s avatar with motion blur.', + throttling: { + usages: 1, + duration: 10 + }, + clientPermissions: ['ATTACH_FILES'], + args: [ + { + key: 'image', + prompt: 'What image would you like to edit?', + type: 'image', + default: msg => msg.author.displayAvatarURL({ format: 'png', size: 512 }) + } + ] + }); + } + + async run(msg, { image }) { + try { + const { body } = await request.get(image); + const data = await loadImage(body); + const canvas = createCanvas(data.width, data.height); + const ctx = canvas.getContext('2d'); + motionBlur(ctx, data, 0, 0, data.width, data.height); + const attachment = canvas.toBuffer(); + if (Buffer.byteLength(attachment) > 8e+6) return msg.reply('Resulting image was above 8 MB.'); + return msg.say({ files: [{ attachment, name: 'motion-blur.png' }] }); + } catch (err) { + return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } + } +}; diff --git a/package.json b/package.json index 338b640c..276ce12c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "128.0.2", + "version": "128.1.0", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": { diff --git a/util/Canvas.js b/util/Canvas.js index 1e6ddb6e..be361607 100644 --- a/util/Canvas.js +++ b/util/Canvas.js @@ -125,6 +125,13 @@ module.exports = class CanvasUtil { return ctx; } + static motionBlur(ctx, image, x, y, width, height) { + ctx.globalAlpha = 0.1; + for (let i = 0; i < 10; ++i) ctx.drawImage(image, x, y + i, width, height); + ctx.globalAlpha = 1; + return ctx; + } + static hasAlpha(image) { const canvas = createCanvas(image.width, image.height); const ctx = canvas.getContext('2d'); @@ -148,6 +155,7 @@ module.exports = class CanvasUtil { ctx.fillRect(x, y, width, height); ctx.fillStyle = fillStyle; ctx.globalAlpha = globalAlpha; + return ctx; } static shortenText(ctx, text, maxWidth) {