From 818cfbcfe478bf070930101b8d0f35d1d97ade93 Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Sun, 28 Mar 2021 13:13:35 -0400 Subject: [PATCH] Vignette Command --- .gitignore | 1 + commands/edit-image/vignette.js | 44 +++++++++++++++++++++++++++++++++ package.json | 2 +- util/Canvas.js | 11 +++++++++ 4 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 commands/edit-image/vignette.js diff --git a/.gitignore b/.gitignore index 79b52548..2d4ded3d 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,4 @@ tf_models/ *.traineddata # In-Development Commands +commands/edit-image/vintage.js diff --git a/commands/edit-image/vignette.js b/commands/edit-image/vignette.js new file mode 100644 index 00000000..d20f2b1c --- /dev/null +++ b/commands/edit-image/vignette.js @@ -0,0 +1,44 @@ +const Command = require('../../structures/Command'); +const { createCanvas, loadImage } = require('canvas'); +const request = require('node-superfetch'); +const { vignette } = require('../../util/Canvas'); + +module.exports = class VignetteCommand extends Command { + constructor(client) { + super(client, { + name: 'vignette', + group: 'edit-image', + memberName: 'vignette', + description: 'Draws an image or a user\'s avatar with a vignette.', + throttling: { + usages: 2, + duration: 10 + }, + clientPermissions: ['ATTACH_FILES'], + args: [ + { + key: 'image', + prompt: 'What image would you like to edit?', + type: 'image-or-avatar', + 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'); + ctx.drawImage(data, 0, 0); + vignette(ctx, 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: 'vignette.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 5b5189df..b41032aa 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "134.3.2", + "version": "134.4.0", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": { diff --git a/util/Canvas.js b/util/Canvas.js index d68b1701..34827de5 100644 --- a/util/Canvas.js +++ b/util/Canvas.js @@ -133,6 +133,17 @@ module.exports = class CanvasUtil { return ctx; } + static vignette(ctx, width, height) { + const outerRadius = width * 0.5; + const innerRadius = width * 0.2; + const grd = ctx.createRadialGradient(width / 2, height / 2, innerRadius, width / 2, height / 2, outerRadius); + grd.addColorStop(0, 'rgba(0, 0, 0, 0.4)'); + grd.addColorStop(1, 'rgba(0, 0, 0, 0.9)'); + ctx.fillStyle = grd; + ctx.fillRect(0, 0, width, height); + return ctx; + } + static hasAlpha(image) { const canvas = createCanvas(image.width, image.height); const ctx = canvas.getContext('2d');