mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-13 15:58:06 +02:00
shortenText Util Command
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user