DiscordJS / Canvas - Image not loading from URL

1.1k views Asked by At

Code:

const Discord = require('discord.js'), client = new Discord.Client();
const Canvas = require('canvas');

module.exports = {
    name: 'canvas',
async execute(m, args) {
const canvas = Canvas.createCanvas(762, 528);
const ctx = canvas.getContext('2d');
const background = await Canvas.loadImage('./background.png');
ctx.drawImage(background, 0, 0, canvas.width, canvas.height);
function loadAndDrawImage(url)
{
    var image = new Canvas.Image();
    image.onload = function()
    {
      ctx.drawImage(image, 300, 300)
    }
    image.src = url;
}
loadAndDrawImage("http://www.w3.org/html/logo/img/mark-word-icon.png")
const attachment = new Discord.MessageAttachment(canvas.toBuffer(), `picture.png`);
m.channel.send(attachment);
}}

When I execute the command to generate the image, the bot gives an image with only the background visible and not the HTML5 logo from w3.org
Console isn't giving any errors..

1

There are 1 answers

0
Kamil Rogala On

I think you're not loading this image at all, and canvas will give you default white background. It can be CORS. Try with the same image on local or on your own server, try also https