久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

.drawImage 函數為畫布拋出“TypeError: Image or Canvas

.drawImage function is throwing a quot;TypeError: Image or Canvas expectedquot;, for canvas(.drawImage 函數為畫布拋出“TypeError: Image or Canvas expected)
本文介紹了.drawImage 函數為畫布拋出“TypeError: Image or Canvas expected"的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在嘗試在我的不和諧機器人中添加排名卡,為此我正在嘗試使用畫布但是當我使用畫布時一切正常,直到我點擊 .drawImage 方法.它給了我一個錯誤,說TypeError:Image or Canvas expected".盡管我已經在全局范圍內需要 canvas,但與 canvas 相關的所有其他內容也都可以正常工作.

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.

我嘗試在函數內部 require('canvas') 但這也不能解決問題.

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(), 'welcome-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.

錯誤:

C:UsersDesktopDiscordiBotibot.js:25
    ctx.drawImage(background, 0, 0, canvas.width, canvas.height);
        ^

TypeError: Image or Canvas expected

推薦答案

node-canvas' loadImage() 方法返回一個 Promise,它被解析為一個 <Image>.

你不能直接傳遞這個 Promise,你必須等待:

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);

這篇關于.drawImage 函數為畫布拋出“TypeError: Image or Canvas expected"的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Using discord.js to detect image and respond(使用 discord.js 檢測圖像并響應)
Check if user ID exists in Discord server(檢查 Discord 服務器中是否存在用戶 ID)
Guild Member Add does not work (discordjs)(公會成員添加不起作用(discordjs))
Creating my first bot using REPLIT but always error Discord.JS(使用 REPLIT 創建我的第一個機器人,但總是錯誤 Discord.JS)
How do I code event/command handlers for my Discord.js bot?(如何為我的 Discord.js 機器人編寫事件/命令處理程序?)
How to find a User ID from a Username in Discord.js?(如何從 Discord.js 中的用戶名中查找用戶 ID?)
主站蜘蛛池模板: 午夜ww | 久草视频在线播放 | 成人在线一区二区 | 国产一区 | 久久国产香蕉 | 极情综合网 | 成年人黄色小视频 | 毛片黄片免费看 | 日韩国产专区 | 91av亚洲| 国产精品高潮呻吟久久av黑人 | 亚洲综合伊人 | 国产精品久久久亚洲 | 91久久精品一区二区二区 | 国产精品视频一区二区三区 | 18成人在线观看 | 国产91av视频 | 国产一区二区三区 | 青青草av | 日韩精品一二三区 | 久久成人综合 | 国产精品中文字幕在线 | 91在线精品视频 | 免费观看成人鲁鲁鲁鲁鲁视频 | 中文字幕国产一区 | 成人在线观看免费视频 | 欧美网址在线观看 | 久久99成人 | 亚洲国产日本 | a级免费视频 | 在线观看成人免费视频 | 7777在线视频 | 黄色在线网站 | 国产高清一区二区三区 | 日韩av电影在线观看 | 欧美视频免费在线 | 国产精品揄拍一区二区 | 日韩国产一区二区 | 亚洲精品日日夜夜 | 免费成人av网站 | 精品在线一区 |