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
+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;