How to fix "ReferenceError: applyText is not defined"

Hello, So i am trying to make my bot welcome new members with picture and this is the code but I got an error in the log so please help me

client.on('guildMemberAdd', async member => {
const channel = member.guild.channels.find(ch => ch.name === 'welcome-goodbye');
if (!channel) return;

const canvas = Canvas.createCanvas(700, 250);
const ctx = canvas.getContext('2d');

const background = await Canvas.loadImage('https://cdn.glitch.com/723b72a2-b8bb-43e5-9538-1b228671e634%2Fwelcome_4.png?v=1563295511880');
ctx.drawImage(background, 0, 0, canvas.width, canvas.height);

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

// Slightly smaller text placed above the member's display name
ctx.font = '28px sans-serif';
ctx.fillStyle = '#ffffff';
ctx.fillText('Welcome to the server,', canvas.width / 2.5, canvas.height / 3.5);

// Add an exclamation point here and below
ctx.font = applyText(canvas, `${member.displayName}!`);
ctx.fillStyle = '#ffffff';
ctx.fillText(`${member.displayName}!`, canvas.width / 2.5, canvas.height / 1.8);

ctx.beginPath();
ctx.arc(125, 125, 100, 0, Math.PI * 2, true);
ctx.closePath();
ctx.clip();

const avatar = await Canvas.loadImage(member.user.displayAvatarURL);
ctx.drawImage(avatar, 25, 25, 200, 200);

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

channel.send(`Welcome to the server, ${member}!`, attachment);
});

and this is the log

(node:20564) UnhandledPromiseRejectionWarning: ReferenceError: applyText is not defined

at Client.client.on (/app/server.js:3320:2)

at process._tickCallback (internal/process/next_tick.js:68:7)

(node:20564) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)

(node:20564) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Well, it simply says that this function (applyText()) is not defined :stuck_out_tongue:.

And I found the example you’ve copied:
discordjs/canvasGuide
That function used to shrink user’s displayName to fit in image. Here is the code you need:

// Pass the entire Canvas object because you'll need to access its width, as well its context
const applyText = (canvas, text) => {
	const ctx = canvas.getContext('2d');

	// Declare a base size of the font
	let fontSize = 70;

	do {
		// Assign the font to the context and decrement it so it can be measured again
		ctx.font = `${fontSize -= 10}px sans-serif`;
		// Compare pixel width of the text to the canvas minus the approximate avatar size
	} while (ctx.measureText(text).width > canvas.width - 300);

	// Return the result to use in the actual canvas
	return ctx.font;
};

Hope that helps!

Thank you very much that actually worked but I want to know how to change the coordinates of user.displayAvatarURL

I tried this but it didn’t work

ctx.drawImage(avatar, 25, 25, 200, 200, canvas.width / 2.5, canvas.height / 1.6);

it doesn’t work

I used this code

ctx.drawImage(avatar, 25, 25,);

but this appears