如何修复错误:字体配置错误:无法加载默认配置文件

Has*_*_BH 1 javascript node.js discord.js

client.on('guildMemberAdd', async (member) => {
 const channel = member.guild.channels.cache.find(
  (channel) => channel.name === 'general'
 );

 if (!channel) return;

 const canvas = Canvas.createCanvas(700, 250);

 const ctx = canvas.getContext('2d');

 const background = await Canvas.loadImage('./wallpaper.jpg');
 ctx.drawImage(background, 0, 0, canvas.width, canvas.height);

 ctx.strokeStyle = '#ffffff';
 ctx.strokeRect(0, 0, canvas.width, canvas.height);

 // Select the font size and type from one of the natively available fonts
 ctx.font = '60px ArialCE.ttf';
 // Select the style that will be used to fill the text in
 ctx.fillStyle = '#ffffff';
 // Actually fill the text with a solid color
 ctx.fillText(member.displayName, canvas.width / 2.5, canvas.height / 1.8);

 ctx.beginPath();
 // Start the arc to form a circle
 ctx.arc(125, 125, 100, 0, Math.PI * 2, true);
 // Put the pen down
 ctx.closePath();
 // Clip off the region you drew on
 ctx.clip();

 const avatar = await Canvas.loadImage(
  member.user.displayAvatarURL({ format: 'jpg' })
 );
 // Move the image downwards vertically and constrain its height to 200, so it's a square
 ctx.drawImage(avatar, 25, 25, 200, 200);

 const attachment = new Discord.MessageAttachment(
  canvas.toBuffer(),
  'welcome-image.png'
 );

 channel.send(`Welcome ${member.toString()} to the server!`, attachment);
});
Run Code Online (Sandbox Code Playgroud)

我一直在使用discord.js 制作一个discord 机器人。我想在画布上制作一条欢迎信息,然后制作完成后,除了画布上的文字之外,它一切正常。事情是这样的:

截屏

我在谷歌上搜索了很多,但找不到任何解决方案。我得到的错误是(字体配置错误:无法加载默认配置文件)。我知道这意味着我的系统中没有字体,但如何添加它们。我正在使用 repl.it。

Lak*_*bam 10

它应该在 node-canvas 2.0+ 中工作。签出新Canvas.registerFont方法: https: //github.com/Automattic/node-canvas/#registerfont

要使用未作为系统字体安装的字体文件,请使用registerFont()Canvas 注册该字体。这必须在创建 Canvas 之前完成。

const { registerFont, createCanvas } = require('canvas')
registerFont('./fontFolder/comicsans.ttf', { family: 'Comic Sans' })

const canvas = createCanvas(500, 500)
const ctx = canvas.getContext('2d')

ctx.font = '12px "Comic Sans"'
ctx.fillText('Everyone hates this font :(', 250, 10)
Run Code Online (Sandbox Code Playgroud)

例子:

TTF 文件将具有不同的字体名称。所以只有'Roboto'这里。否则它不起作用。

registerFont('./fonts/Roboto-Medium.ttf', {family: 'Roboto'})
Run Code Online (Sandbox Code Playgroud)

不要将 fontName 定义为'Medium 28px Roboto'。这是行不通的。

ctx.font = '12px Roboto Medium';
Run Code Online (Sandbox Code Playgroud)