From ab2d13f37c1911d0f98f2dad017fe604ac581675 Mon Sep 17 00:00:00 2001 From: stockiNail Date: Mon, 27 Feb 2023 16:42:16 +0100 Subject: [PATCH] Set maxWidth to stroke and fill text when invoked to draw labels --- src/helpers/helpers.canvas.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/helpers/helpers.canvas.js b/src/helpers/helpers.canvas.js index 436902b8b..65f13ba4f 100644 --- a/src/helpers/helpers.canvas.js +++ b/src/helpers/helpers.canvas.js @@ -144,9 +144,9 @@ export function drawLabel(ctx, rect, options) { ctx.textBaseline = 'middle'; ctx.textAlign = options.textAlign; if (setTextStrokeStyle(ctx, options)) { - applyLabelDecoration(ctx, {x, y}, labels, fonts); + applyLabelDecoration(ctx, {x, y, maxWidth: rect.width}, labels, fonts); } - applyLabelContent(ctx, {x, y}, labels, {fonts, colors}); + applyLabelContent(ctx, {x, y, maxWidth: rect.width}, labels, {fonts, colors}); ctx.restore(); } @@ -297,20 +297,20 @@ function calculateLabelSize(ctx, lines, fonts, strokeWidth) { return {width, height}; } -function applyLabelDecoration(ctx, {x, y}, labels, fonts) { +function applyLabelDecoration(ctx, {x, y, maxWidth}, labels, fonts) { ctx.beginPath(); let lhs = 0; labels.forEach(function(l, i) { const f = fonts[Math.min(i, fonts.length - 1)]; const lh = f.lineHeight; ctx.font = f.string; - ctx.strokeText(l, x, y + lh / 2 + lhs); + ctx.strokeText(l, x, y + lh / 2 + lhs, maxWidth); lhs += lh; }); ctx.stroke(); } -function applyLabelContent(ctx, {x, y}, labels, {fonts, colors}) { +function applyLabelContent(ctx, {x, y, maxWidth}, labels, {fonts, colors}) { let lhs = 0; labels.forEach(function(l, i) { const c = colors[Math.min(i, colors.length - 1)]; @@ -319,7 +319,7 @@ function applyLabelContent(ctx, {x, y}, labels, {fonts, colors}) { ctx.beginPath(); ctx.font = f.string; ctx.fillStyle = c; - ctx.fillText(l, x, y + lh / 2 + lhs); + ctx.fillText(l, x, y + lh / 2 + lhs, maxWidth); lhs += lh; ctx.fill(); });