The CanvasRenderingContext2D.lineCap property of the Canvas 2D API determines the shape used to draw the end points of lines.

Note: Lines can be drawn with the stroke()strokeRect(), and strokeText() methods.


ctx.lineCap = "butt" || "round" || "square";


The ends of lines are squared off at the endpoints. Default value.
The ends of lines are rounded.
The ends of lines are squared off by adding a box with an equal width and half the height of the line's thickness.


Changing the shape of line caps

This example rounds the end caps of a straight line.


<canvas id="canvas"></canvas>


const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');

ctx.moveTo(20, 20);
ctx.lineWidth = 15;
ctx.lineCap = 'round';
ctx.lineTo(100, 100);


Comparison of line caps

In this example three lines are drawn, each with a different value for the lineCap property. Two guides to see the exact differences between the three are added. Each of these lines starts and ends exactly on these guides.

The line on the left uses the default "butt" option. It's drawn completely flush with the guides. The second is set to use the "round" option. This adds a semicircle to the end that has a radius half the width of the line. The line on the right uses the "square" option. This adds a box with an equal width and half the height of the line thickness.

const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const lineCap = ['butt', 'round', 'square'];

// Draw guides
ctx.strokeStyle = '#09f';
ctx.moveTo(10, 10);
ctx.lineTo(140, 10);
ctx.moveTo(10, 140);
ctx.lineTo(140, 140);

// Draw lines
ctx.strokeStyle = 'black';
for (let i = 0; i < lineCap.length; i++) {
  ctx.lineWidth = 15;
  ctx.lineCap = lineCap[i];
  ctx.moveTo(25 + i * 50, 10);
  ctx.lineTo(25 + i * 50, 140);
ScreenshotLive sample


Specification Status Comment
HTML Living Standard
The definition of 'CanvasRenderingContext2D.lineCap' in that specification.
Living Standard  

Browser compatibility

Update compatibility data on GitHub
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
lineCapChrome Full support YesEdge Full support 12Firefox Full support 1.5IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes


Full support  
Full support
  • In WebKit- and Blink-based Browsers, a non-standard and deprecated method ctx.setLineCap() is implemented in addition to this property.

See also