.drawImage function is throwing a “TypeError: Image or Canvas expected”, for canvas

后端 未结 1 679
暖寄归人
暖寄归人 2021-01-20 05:02

I am trying to add a rank card in my discord bot, and in order to do so I am trying to use canvas but when I use canvas everything works fine until I hit the .drawImag

相关标签:
1条回答
  • 2021-01-20 06:01

    node-canvas' loadImage() method returns a Promise which get resolved to an <Image>.

    You can't pass this Promise directly, you'll have to await for it:

    const canvas = Canvas.createCanvas(934, 282);
    const ctx = canvas.getContext('2d');
    // we need to await the Promise gets resolved since loading of Image is async
    const background = await Canvas.loadImage('./images/Rank_Card.jpg');
    
    ctx.drawImage(background, 0, 0, canvas.width, canvas.height);  
    const attachment = new Discord.Attachment(canvas.toBuffer(), 'welcome-image.png');
    msg.channel.send(`Testing...`, attachment);
    
    0 讨论(0)
提交回复
热议问题