Skip to content

Commit 2caf310

Browse files
committed
improve canvas guide
- as #181 points out we can remove the (deprecated) snekfetch and fully remove a need for fetch modules due to canvas accepting URLS directly. - mark tips where appropriate - close #181
1 parent ef79fc7 commit 2caf310

File tree

2 files changed

+11
-17
lines changed

2 files changed

+11
-17
lines changed

code-samples/popular-topics/canvas/index.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const Discord = require('discord.js');
22
const Canvas = require('canvas');
3-
const snekfetch = require('snekfetch');
43

54
const client = new Discord.Client();
65

@@ -45,8 +44,7 @@ client.on('guildMemberAdd', async member => {
4544
ctx.closePath();
4645
ctx.clip();
4746

48-
const { body: buffer } = await snekfetch.get(member.user.displayAvatarURL);
49-
const avatar = await Canvas.loadImage(buffer);
47+
const avatar = await Canvas.loadImage(member.user.displayAvatarURL);
5048
ctx.drawImage(avatar, 25, 25, 200, 200);
5149

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

guide/popular-topics/canvas.md

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ Here is the base code you'll be using to get started:
3535
```js
3636
const Discord = require('discord.js');
3737
const Canvas = require('canvas');
38-
const snekfetch = require('snekfetch');
3938

4039
const client = new Discord.Client();
4140

@@ -113,7 +112,9 @@ client.on('guildMemberAdd', async member => {
113112

114113
![Basic canvas preview](~@/images/8CQvVRV.png)
115114

115+
::: tip
116116
If you get an error such as `Error: error while reading from input stream`, then the provided path to the file was incorrect.
117+
:::
117118

118119
### Manipulating images
119120

@@ -159,10 +160,8 @@ client.on('guildMemberAdd', async member => {
159160
ctx.strokeStyle = '#74037b';
160161
ctx.strokeRect(0, 0, canvas.width, canvas.height);
161162

162-
// Get the icon in the form of a buffer
163-
const { body: buffer } = await snekfetch.get(member.user.displayAvatarURL);
164163
// Wait for Canvas to load the image
165-
const avatar = await Canvas.loadImage(buffer);
164+
const avatar = await Canvas.loadImage(member.user.displayAvatarURL);
166165
// Draw a shape onto the main canvas
167166
ctx.drawImage(avatar, 25, 0, 200, canvas.height);
168167

@@ -190,8 +189,7 @@ client.on('guildMemberAdd', async member => {
190189
ctx.strokeStyle = '#74037b';
191190
ctx.strokeRect(0, 0, canvas.width, canvas.height);
192191

193-
const { body: buffer } = await snekfetch.get(member.user.displayAvatarURL);
194-
const avatar = await Canvas.loadImage(buffer);
192+
const avatar = await Canvas.loadImage(member.user.displayAvatarURL);
195193
// Move the image downwards vertically and constrain its height to 200, so it's a square
196194
ctx.drawImage(avatar, 25, 25, 200, 200);
197195

@@ -230,8 +228,7 @@ client.on('guildMemberAdd', async member => {
230228
// Clip off the region you drew on
231229
ctx.clip();
232230

233-
const { body: buffer } = await snekfetch.get(member.user.displayAvatarURL);
234-
const avatar = await Canvas.loadImage(buffer);
231+
const avatar = await Canvas.loadImage(member.user.displayAvatarURL);
235232
ctx.drawImage(avatar, 25, 25, 200, 200);
236233

237234
const attachment = new Discord.Attachment(canvas.toBuffer(), 'welcome-image.png');
@@ -276,8 +273,7 @@ client.on('guildMemberAdd', async member => {
276273
ctx.closePath();
277274
ctx.clip();
278275

279-
const { body: buffer } = await snekfetch.get(member.user.displayAvatarURL);
280-
const avatar = await Canvas.loadImage(buffer);
276+
const avatar = await Canvas.loadImage(member.user.displayAvatarURL);
281277
ctx.drawImage(avatar, 25, 25, 200, 200);
282278

283279
const attachment = new Discord.Attachment(canvas.toBuffer(), 'welcome-image.png');
@@ -288,7 +284,9 @@ client.on('guildMemberAdd', async member => {
288284

289285
![Image](~@/images/3rLGb1s.png)
290286

287+
::: tip
291288
If you get an error like `Fontconfig error: Cannot load default config file`, it means you do not have any fonts installed on your system. On Linux, you can run the following command to fix this: `sudo apt-get install fontconfig`. This might also need to be installed if you see boxes where the text should be. As for Windows, you will need to find a way to install fonts.
289+
:::
292290

293291
You may have noticed or considered that if a member's username is too long, then the output won't be quite nice. This is because the text overflows out of the canvas, and you don't have any measures in place for that. Let's take care of this issue!
294292

@@ -333,8 +331,7 @@ client.on('guildMemberAdd', async member => {
333331
ctx.closePath();
334332
ctx.clip();
335333

336-
const { body: buffer } = await snekfetch.get(member.user.displayAvatarURL);
337-
const avatar = await Canvas.loadImage(buffer);
334+
const avatar = await Canvas.loadImage(member.user.displayAvatarURL);
338335
ctx.drawImage(avatar, 25, 25, 200, 200);
339336

340337
const attachment = new Discord.Attachment(canvas.toBuffer(), 'welcome-image.png');
@@ -382,8 +379,7 @@ client.on('guildMemberAdd', async member => {
382379
ctx.closePath();
383380
ctx.clip();
384381

385-
const { body: buffer } = await snekfetch.get(member.user.displayAvatarURL);
386-
const avatar = await Canvas.loadImage(buffer);
382+
const avatar = await Canvas.loadImage(member.user.displayAvatarURL);
387383
ctx.drawImage(avatar, 25, 25, 200, 200);
388384

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

0 commit comments

Comments
 (0)