let strWidth = ctx.measureText(this.data.name).width;
const ellipsis ="..."
const ellipsisWidth = ctx.measureText(ellipsis).width;
if(strWidth<=120 || 120<=ellipsisWidth) {
ctx.fillText("测试:"+this.data.name, 190*dpr,590*dpr);
}else {
var len = this.data.name.length;
while (strWidth >= 100 - ellipsisWidth && len-- > 0) {
this.data.name = this.data.name.slice(0, len);
strWidth = ctx.measureText(this.data.name).width;
}
let newText = this.data.name + ellipsis;
ctx.fillText("测试:"+newText, 190*dpr,590*dpr);
}
注:这里面的this.data.name是内容,里面的涉及数字120或者是100都是最大长度,根据自己需求填写即可
效果展示: