From 33d540776cf8dce9ddbc253a33d76b7d7416ccde Mon Sep 17 00:00:00 2001 From: Zhang Yu <25325852@qq.com> Date: Mon, 5 Jul 2021 12:01:29 +0800 Subject: [PATCH] Improve text wrapping --- src/canvas/draw.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/canvas/draw.js b/src/canvas/draw.js index d0b03399..b1e9d1a6 100644 --- a/src/canvas/draw.js +++ b/src/canvas/draw.js @@ -238,12 +238,12 @@ class Draw { if (textWrap && txtWidth > npx(biw)) { let textLine = { w: 0, len: 0, start: 0 }; for (let i = 0; i < it.length; i += 1) { - if (textLine.w >= npx(biw)) { + textLine.w += ctx.measureText(it[i]).width; + textLine.len += 1; + if (textLine.w > npx(biw)) { ntxts.push(it.substr(textLine.start, textLine.len)); - textLine = { w: 0, len: 0, start: i }; + textLine = { w: ctx.measureText(it[i]).width, len: 1, start: i }; } - textLine.len += 1; - textLine.w += ctx.measureText(it[i]).width + 1; } if (textLine.len > 0) { ntxts.push(it.substr(textLine.start, textLine.len));