shortenText Util Command

This commit is contained in:
Daniel Odendahl Jr
2017-12-12 04:11:58 +00:00
parent 8bcbb3902c
commit 1b16f3fc8c
3 changed files with 11 additions and 8 deletions
+2 -4
View File
@@ -2,6 +2,7 @@ const { Command } = require('discord.js-commando');
const { createCanvas, loadImage, registerFont } = require('canvas');
const snekfetch = require('snekfetch');
const path = require('path');
const { shortenText } = require('../../util/Canvas');
registerFont(path.join(__dirname, '..', '..', 'assets', 'fonts', 'Noto.ttf'), { family: 'Noto' });
registerFont(path.join(__dirname, '..', '..', 'assets', 'fonts', 'Noto-CJK.otf'), { family: 'Noto' });
registerFont(path.join(__dirname, '..', '..', 'assets', 'fonts', 'Noto-Emoji.ttf'), { family: 'Noto' });
@@ -48,10 +49,7 @@ module.exports = class SteamNowPlayingCommand extends Command {
ctx.fillStyle = '#90ba3c';
ctx.font = '10px Noto';
ctx.fillText(user.username, 63, 26);
let shorten;
if (ctx.measureText(game).width > 160) shorten = true;
while (ctx.measureText(game).width > 160) game = game.substr(0, game.length - 1);
ctx.fillText(shorten ? `${game}...` : game, 63, 54);
ctx.fillText(shortenText(ctx, game, 160), 63, 54);
return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'steam-now-playing.png' }] });
} catch (err) {
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
+2 -4
View File
@@ -1,6 +1,7 @@
const { Command } = require('discord.js-commando');
const { createCanvas, loadImage, registerFont } = require('canvas');
const path = require('path');
const { shortenText } = require('../../util/Canvas');
registerFont(path.join(__dirname, '..', '..', 'assets', 'fonts', 'Minecraftia.ttf'), { family: 'Minecraftia' });
module.exports = class AchievementCommand extends Command {
@@ -34,11 +35,8 @@ module.exports = class AchievementCommand extends Command {
ctx.font = '17px Minecraftia';
ctx.fillStyle = '#ffff00';
ctx.fillText('Achievement Get!', 60, 40);
let shorten;
if (ctx.measureText(text).width > 230) shorten = true;
while (ctx.measureText(text).width > 230) text = text.substr(0, text.length - 1);
ctx.fillStyle = '#ffffff';
ctx.fillText(shorten ? `${text}...` : text, 60, 60);
ctx.fillText(shortenText(ctx, text, 230), 60, 60);
return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'achievement.png' }] });
}
};
+7
View File
@@ -85,6 +85,13 @@ class CanvasUtil {
ctx.fillStyle = '#000000';
ctx.globalAlpha = 1;
}
static shortenText(ctx, text, maxWidth) {
let shorten;
if (ctx.measureText(text).width > maxWidth) shorten = true;
while (ctx.measureText(text).width > maxWidth) text = text.substr(0, text.length - 1);
return shorten ? `${text}...` : text;
}
}
module.exports = CanvasUtil;