。drawImage函数为画布抛出“ TypeError:预期图像或画布”

问题描述 投票:2回答:1

我正在尝试在不和谐的bot中添加等级卡,为此,我尝试使用canvas,但是当我使用canvas时,一切正常,直到我点击.drawImage方法。它给我一个错误,提示“ TypeError:预期图像或画布”。尽管我已经在全局范围内使用了('canvas'),但是与画布有关的所有其他事情也都可以正常工作。

我试图在函数内部使用require('canvas'),但这也不能解决问题。

    const canvas = Canvas.createCanvas(934, 282);
    const ctx = canvas.getContext('2d');
    const background = 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);

[当它发送消息时,它应该附加图像,但现在它只是给我以下错误。

错误:

C:\Users\Desktop\Discord\iBot\ibot.js:25
    ctx.drawImage(background, 0, 0, canvas.width, canvas.height);
        ^

TypeError: Image or Canvas expected
javascript canvas discord.js
1个回答
0
投票

node-canvas' loadImage()方法返回一个loadImage(),该解析为Promise

您不能直接通过此Promise,您必须为此await

<Image>
© www.soinside.com 2019 - 2024. All rights reserved.