简体   繁体   中英

Characters are not displayed in Canvas

When I try to display text on a picture using Canvas, some characters and emoji are displayed incorrectly on Windows and Ubuntu. 1 screen - on Windows, 2 screen - on Ubuntu. With any font, this is the situation. Canvas that on Windows, that on Ubuntu I just installed via npm install canvas .

Windows

Ubuntu

Code:

const canvas = Canvas.createCanvas(1000,333)
const ctx = canvas.getContext('2d');
        
const background = await Canvas.loadImage('https://i.imgur.com/YzwG7yk.jpeg')
ctx.drawImage(background, 0, 0, canvas.width, canvas.height)
ctx.font = '300 30px "Arial"'
ctx.fillStyle = '#ffffff'
ctx.fillText(`💗💗💗\nマークとニック\nP𝔦𝔯𝔬z𝔬k`, 70, 70)
const attachment = new Discord.MessageAttachment(canvas.toBuffer(), 'xp.png');
return message.channel.send(attachment)

You need to have a font able to render these glyphs installed on your server.
If you can't control the fonts on your server, you can load a webfont using registerFont

Live example .

When I try to display text on a picture using Canvas, some characters and emoji are displayed incorrectly on Windows and Ubuntu. 1 screen - on Windows, 2 screen - on Ubuntu. With any font, this is the situation. Canvas that on Windows, that on Ubuntu I just installed via npm install canvas .

Windows

Ubuntu

Code:

const canvas = Canvas.createCanvas(1000,333)
const ctx = canvas.getContext('2d');
        
const background = await Canvas.loadImage('https://i.imgur.com/YzwG7yk.jpeg')
ctx.drawImage(background, 0, 0, canvas.width, canvas.height)
ctx.font = '300 30px "Arial"'
ctx.fillStyle = '#ffffff'
ctx.fillText(`💗💗💗\nマークとニック\nP𝔦𝔯𝔬z𝔬k`, 70, 70)
const attachment = new Discord.MessageAttachment(canvas.toBuffer(), 'xp.png');
return message.channel.send(attachment)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM