问题描述
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.displayavatar
);
// 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);
});
我一直在使用 discord.js 制作一个不和谐的机器人.我想制作一个画布欢迎信息,然后当它制作完成时,它可以正常工作,除了画布上的文字.原来是这样的:
i have been making a discord bot using discord.js. i wanted to make a canvas welcome message then when made it, it was working and all, except the words on the canvas. it was all like this:
我在谷歌上搜索了很多,但找不到任何尊龙凯时的解决方案.我得到的错误是(fontconfig 错误:无法加载默认配置文件).我知道这意味着我的系统中没有字体,但是如何添加它们.我正在使用 repl.it.
and i searched a lot on google but couldn't find any solution. the error i get is (fontconfig error: cannot load default config file). i know it means that there are no fonts in my system but how to add them. i'm using repl.it.
推荐答案
它应该在 node-canvas 2.0 中工作.签出新的 canvas.registerfont 方法:https://github.com/automattic/node-canvas/#registerfont
it should work in node-canvas 2.0 . checkout new canvas.registerfont method: https://github.com/automattic/node-canvas/#registerfont
要使用未安装为系统字体的字体文件,请使用 registerfont() 将字体注册到 canvas.这必须在创建画布之前完成.
to use a font file that is not installed as a system font, use registerfont() to register the font with canvas. this must be done before the canvas is created.
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)
例子:
ttf 文件将具有不同的字体名称.所以这里只有 'roboto' .否则它不起作用.
ttf file will have a different font name. so only 'roboto' here. otherwise it doesn't work.
registerfont('./fonts/roboto-medium.ttf', {family: 'roboto'})
不要将 fontname 定义为 'medium 28px roboto'.它不会工作.
don't define fontname as 'medium 28px roboto'. it won't work.
ctx.font = '12px roboto medium';
迷1样的女子