admin管理员组文章数量:1022949
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 .drawImage
method. Where it gives me an error saying "TypeError: Image or Canvas expected". Although I've already required canvas
globally, and everything else that has to do with canvas works properly aswell.
I've tried to require('canvas')
inside the function but that doesn't fix the problem either.
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(), 'wele-image.png');
msg.channel.send(`Testing...`, attachment);
When it sends the message it should attach the image with it, but right now its just giving me the following error.
Error:
C:\Users\Desktop\Discord\iBot\ibot.js:25
ctx.drawImage(background, 0, 0, canvas.width, canvas.height);
^
TypeError: Image or Canvas expected
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 .drawImage
method. Where it gives me an error saying "TypeError: Image or Canvas expected". Although I've already required canvas
globally, and everything else that has to do with canvas works properly aswell.
I've tried to require('canvas')
inside the function but that doesn't fix the problem either.
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(), 'wele-image.png');
msg.channel.send(`Testing...`, attachment);
When it sends the message it should attach the image with it, but right now its just giving me the following error.
Error:
C:\Users\Desktop\Discord\iBot\ibot.js:25
ctx.drawImage(background, 0, 0, canvas.width, canvas.height);
^
TypeError: Image or Canvas expected
Share
Improve this question
edited Nov 30, 2020 at 13:14
Tyler2P
2,37030 gold badges25 silver badges33 bronze badges
asked Oct 17, 2019 at 3:42
anonymousanonymous
691 gold badge2 silver badges9 bronze badges
1 Answer
Reset to default 6node-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(), 'wele-image.png');
msg.channel.send(`Testing...`, attachment);
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 .drawImage
method. Where it gives me an error saying "TypeError: Image or Canvas expected". Although I've already required canvas
globally, and everything else that has to do with canvas works properly aswell.
I've tried to require('canvas')
inside the function but that doesn't fix the problem either.
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(), 'wele-image.png');
msg.channel.send(`Testing...`, attachment);
When it sends the message it should attach the image with it, but right now its just giving me the following error.
Error:
C:\Users\Desktop\Discord\iBot\ibot.js:25
ctx.drawImage(background, 0, 0, canvas.width, canvas.height);
^
TypeError: Image or Canvas expected
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 .drawImage
method. Where it gives me an error saying "TypeError: Image or Canvas expected". Although I've already required canvas
globally, and everything else that has to do with canvas works properly aswell.
I've tried to require('canvas')
inside the function but that doesn't fix the problem either.
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(), 'wele-image.png');
msg.channel.send(`Testing...`, attachment);
When it sends the message it should attach the image with it, but right now its just giving me the following error.
Error:
C:\Users\Desktop\Discord\iBot\ibot.js:25
ctx.drawImage(background, 0, 0, canvas.width, canvas.height);
^
TypeError: Image or Canvas expected
Share
Improve this question
edited Nov 30, 2020 at 13:14
Tyler2P
2,37030 gold badges25 silver badges33 bronze badges
asked Oct 17, 2019 at 3:42
anonymousanonymous
691 gold badge2 silver badges9 bronze badges
1 Answer
Reset to default 6node-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(), 'wele-image.png');
msg.channel.send(`Testing...`, attachment);
本文标签:
版权声明:本文标题:javascript - .drawImage function is throwing a "TypeError: Image or Canvas expected", for canvas - Stack Overf 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745550040a2155587.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论