fenixenbot.js
Version:
Libreria Creacion De Bots De Discord
109 lines (91 loc) • 2.74 kB
JavaScript
const { createCanvas, loadImage, GlobalFonts } = require('@napi-rs/canvas');
GlobalFonts.registerFromPath('./fonts/arial.ttf', 'Arial');
function parseSize(value, maxSize) {
if (typeof value === 'string' && value.includes('%')) {
return (parseFloat(value) / 100) * maxSize;
}
return parseInt(value) || 0;
}
async function Canvas(options) {
const {
width = 500,
height = 250,
background = { type: 'color', value: '#000000' },
images = [],
text = [],
} = options;
const canvas = createCanvas(width, height);
const ctx = canvas.getContext('2d');
if (background.type === 'image') {
try {
const bgImage = await loadImage(background.value);
ctx.drawImage(bgImage, 0, 0, width, height);
} catch (error) {
console.error(
'Error al cargar la imagen de fondo:',
background.value,
'URL No existe'
);
ctx.fillStyle = '#000000';
ctx.fillRect(0, 0, width, height);
}
} else {
ctx.fillStyle = background.value;
ctx.fillRect(0, 0, width, height);
}
for (const img of images) {
let image;
try {
image = await loadImage(img.url);
} catch (error) {
console.error('Error al cargar la imagen:', img.url, 'URL no existe');
continue;
}
const imgWidth = parseSize(img.width, width);
const imgHeight = parseSize(img.height, height);
let x = parseSize(img.left, width);
let y = parseSize(img.top, height);
if (img.right) x = width - imgWidth - parseSize(img.right, width);
if (img.bottom) y = height - imgHeight - parseSize(img.bottom, height);
if (img.borderWidth) {
ctx.beginPath();
ctx.arc(
x + imgWidth / 2,
y + imgHeight / 2,
imgWidth / 2,
0,
Math.PI * 2
);
ctx.lineWidth = parseInt(img.borderWidth);
ctx.strokeStyle = img.borderColor || '#FFFFFF';
ctx.stroke();
ctx.closePath();
}
if (img.borderRadius === '50%') {
ctx.save();
ctx.beginPath();
ctx.arc(
x + imgWidth / 2,
y + imgHeight / 2,
imgWidth / 2,
0,
Math.PI * 2
);
ctx.closePath();
ctx.clip();
}
ctx.drawImage(image, x, y, imgWidth, imgHeight);
if (img.borderRadius === '50%') {
ctx.restore();
}
}
for (const txt of text) {
const x = parseSize(txt.left, width);
const y = parseSize(txt.top, height);
ctx.fillStyle = txt.color || '#FFFFFF';
ctx.font = txt.font || '16px Arial';
ctx.fillText(txt.content, x, y);
}
return await canvas.encode('png');
}
module.exports = Canvas;