Skip to content

Set maximum width to stroke and fill text when invoked to draw labels #859

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/helpers/helpers.canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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)];
Expand All @@ -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();
});
Expand Down