0

hey so I've made this command for my discord bot using canvas that just quotes something a user says, but the image is only able to fit 26 characters of text, if I write something longer than that, the rest of the characters wont be visible, so I wanted to know if there is some method to wrap the rest of the text? I cant find anything on wrapping text in node canvas Text

here is my code:

client.on("message", async (message) => {
  if (message.author.bot) return;
  if (message.content.startsWith("R!write")) {
let sliced = message.content.slice(31)
let msg = `"${sliced}" - 2021`
const { registerFont, createCanvas } = require('canvas')
const canvas = Canvas.createCanvas(1920, 900);
let background = await Canvas.loadImage('./lmao.jpg');
const ctx = canvas.getContext('2d')
ctx.drawImage(background, 0, 0, canvas.width, canvas.height);
registerFont('./disc2.otf', { family: 'Comic Sans' })

const taggeduser = message.mentions.users.first();
if (!taggeduser)
return message.channel.send('mention someone pleb')

let avatar = await Canvas.loadImage(taggeduser.displayAvatarURL({ dynamic: false, format: 'jpg', size: 256 }));
ctx.drawImage(avatar, 160, 340);

ctx.font = '100px "Comic Sans"'
ctx.fillText(msg, 500, 500)

let attachment = new Discord.MessageAttachment(canvas.toBuffer(), 'example.png')



    return message.channel.send(attachment);
  }
});

lelbro420
  • 27
  • 4
  • 1
    see https://stackoverflow.com/questions/2936112/text-wrap-in-a-canvas-element – majidarif Nov 03 '21 at 13:12
  • You can also try using `jimp` instead. I've got a post here that uses jimp with discord.js to add correctly aligned multiline text: https://stackoverflow.com/a/68563498/6126373 – Zsolt Meszaros Nov 03 '21 at 13:27

0 Answers0