UNPKG

canvasky

Version:

An easy and powerful image manipulator

165 lines (141 loc) 6.07 kB
const Canvas = require('@napi-rs/canvas'); const chalk = require("chalk"); const formatVariable = (prefix, variable) => { const formattedVariable = variable.toLowerCase() .split("-").map((word) => word.charAt(0).toUpperCase() + word.substr(1, word.length).toLowerCase()).join(""); return prefix + formattedVariable; } module.exports = class Greeting { constructor() { this.username = "Clyde"; this.guildName = "ServerName"; this.avatar = `${__dirname}/../assets/img/0.png`; this.backgroundImage = `${__dirname}/../assets/img/defaultBackground.png`; this.backgroundColor = "#000000"; this.backgroundVariable = "image"; this.opacityBox = 0.5; this.opacityAlpha = 0; this.colorOpacity = "#000000"; this.colorBorder = "#FFFFFF"; this.colorText = "#FFFFFF"; } /** * Set background * @param {"color"|"image"} image The kind of background you want * @returns {Greeting} */ setBackground(image) { this.backgroundImage = image; return this; } /** * Set avatar * @param {string|Buffer} image The avatar * @returns {Greeting} */ setAvatar(image) { this.avatar = image; return this; } /** * Set username * @param {string} name The username * @returns {Greeting} */ setUsername(name) { this.username = name; return this; } /** * Set guild name * @param {string} name The guild name * @returns {Greeting} */ setGuildName(name) { this.guildName = name; return this; } /** * Sets opacity of a given variable * @param {"box"|"alpha"} option The variable to set the opacity at * @param {number} number The opacity * @returns {Greeting} */ setOpacity(option, number) { const formattedVariable = formatVariable("opacity", option); if (this[formattedVariable]) this[formattedVariable] = number; return this; } /** * Sets color of a given variable * @param {"opacity"|"text"|"border"} option The variable to set the color at * @param {string} color The color * @returns {Greeting} */ setColor(option, color) { const formattedVariable = formatVariable("color", option); if (this[formattedVariable]) this[formattedVariable] = color; return this; } async build() { //Creating the canvas const canvas = Canvas.createCanvas(1024, 450); const ctx = canvas.getContext("2d"); if (!/^#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/.test(this.colorBorder)) throw new TypeError(`${chalk.redBright(["Canvasky"])} ${chalk.whiteBright("Border color is required to be a hexidecimal color.")}`) ctx.beginPath(); ctx.lineWidth = 8; ctx.strokeStyle = this.colorBorder ctx.moveTo(55, 15); ctx.lineTo(canvas.width - 55, 15); ctx.quadraticCurveTo(canvas.width - 20, 20, canvas.width - 15, 55); ctx.lineTo(canvas.width - 15, canvas.height - 55); ctx.quadraticCurveTo(canvas.width - 20, canvas.height - 20, canvas.width - 55, canvas.height - 15); ctx.lineTo(55, canvas.height - 15); ctx.quadraticCurveTo(20, canvas.height - 20, 15, canvas.height - 55); ctx.lineTo(15, 55); ctx.quadraticCurveTo(20, 20, 55, 15); ctx.lineTo(56, 15); ctx.stroke(); ctx.closePath(); ctx.beginPath(); ctx.moveTo(65, 25); ctx.lineTo(canvas.width - 65, 25); ctx.quadraticCurveTo(canvas.width - 25, 25, canvas.width - 25, 65); ctx.lineTo(canvas.width - 25, canvas.height - 65); ctx.quadraticCurveTo(canvas.width - 25, canvas.height - 25, canvas.width - 65, canvas.height - 25); ctx.lineTo(65, canvas.height - 25); ctx.quadraticCurveTo(25, canvas.height - 25, 25, canvas.height - 65); ctx.lineTo(25, 65); ctx.quadraticCurveTo(25, 25, 65, 25); ctx.lineTo(66, 25); ctx.closePath(); ctx.clip(); if (isNaN(this.opacityAlpha)) throw new TypeError(`${chalk.redBright(["Canvasky"])} ${chalk.whiteBright("Opacity is required to be a number.")}`) ctx.globalAlpha = this.opacityAlpha //Creating background if (this.backgroundImage !== undefined) { let background = await Canvas.loadImage(this.backgroundImage); ctx.drawImage(background, 10, 10, canvas.width - 20, canvas.height - 20); } else { let background = await Canvas.loadImage(`${__dirname}/../assets/img/defaultBackground.png`); ctx.drawImage(background, 10, 10, canvas.width - 20, canvas.height - 20); } if (isNaN(this.opacityBox)) throw new TypeError(`${chalk.redBright(["Canvasky"])} ${chalk.whiteBright("Opacity is required to be a number.")}`) if (!/^#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/.test(this.colorOpacity)) throw new TypeError(`${chalk.redBright(["Canvasky"])} ${chalk.whiteBright("Opacity color is required to be a hexidecimal color.")}`) // ctx.beginPath(); // ctx.globalAlpha = this.opacityBox; // ctx.fillStyle = this.colorOpacity; // ctx.moveTo(75, 45); // ctx.lineTo(canvas.width - 75, 45); // ctx.quadraticCurveTo(canvas.width - 45, 45, canvas.width - 45, 75); // ctx.lineTo(canvas.width - 45, canvas.height - 75); // ctx.quadraticCurveTo(canvas.width - 45, canvas.height - 45, canvas.width - 75, canvas.height - 45); // ctx.lineTo(75, canvas.height - 45); // ctx.quadraticCurveTo(45, canvas.height - 45, 45, canvas.height - 75); // ctx.lineTo(45, 75); // ctx.quadraticCurveTo(45, 45, 75, 45); // ctx.fill(); // ctx.closePath(); return await canvas.toBuffer("image/png"); } }