Settings canvas linewidth > 1 gives pointy polygons

16 hours ago 2
ARTICLE AD BOX

Problem

In my app I need to draw a lot of polygons fast, for this I am using the following canvas primitives

ctx.beginPath() ctx.moveTo(points[0], points[1]) ctx.stroke()

Unfortunately when I try to style the width of each line that is being drawn, the lines extend beyond the points I moveTo. This is unexpected, I expected only the thickness of the line being changed, not their endpoints.

Example

I have distilled my problem to the following minimal case. I have created a blue bounding box that should contain the drawn triangle / polygon. This works correctly when the linewidth is set to 1, but as soon as I increase the line width, the polygon exceeds the expected bounding box.

const canvas = document.getElementById('canvas') canvas.width = 256 canvas.height = 256 // Set a background const ctx = canvas.getContext('2d') ctx.fillStyle = "#EEEEEE"; ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height); // Draw expected bounding box with lineWidth 1, this is correct ctx.strokeStyle = 'blue' ctx.lineWidth = 1 ctx.strokeRect(100, 100, 12, 26) // Draw a triangle / polygon that exceeds the bounding box when the lineWidth > 1 const line = [112, 112, 100, 100, 112, 124, 112, 112] // [x1, y1, x2, y2, etc.] drawPolygon(line, 'red', 5) // Incorrect // drawPolygon(line, 'orange', 2) // Incorrect drawPolygon(line, 'green', 1) // Correct function drawPolygon(points, color, lineWidth) { ctx.strokeStyle = color ctx.lineWidth = lineWidth ctx.beginPath() ctx.moveTo(points[0], points[1]) for (let i = 2; i < points.length; i += 2) { ctx.lineTo(points[i], points[i + 1]) } ctx.stroke() } <canvas id="canvas" style="transform: scale(2);"></canvas>

Possible cause

It looks like the lineWidth is added to the line end points. I cannot find anything on MDN that would suggest this would happen.

I would like a solution that increases the thickness of the lines, but keeps the polygon inside the bounding box. The solution must be fast as well.

Read Entire Article