CanvasRenderingContext2D.lineJoin
The CanvasRenderingContext2D
.lineJoin
property of the Canvas 2D API determines how two connecting segments (of lines, arcs or curves) with non-zero lengths in a shape are joined together (degenerate segments with zero lengths, whose specified endpoints and control points are exactly at the same position, are skipped).
See also the chapter Applying styles and color in the Canvas Tutorial.
Syntax
<var>ctx.lineJoin = "bevel"; <var>ctx.lineJoin = "round"; <var>ctx.lineJoin = "miter";</var></var></var>
Options
There are three possible values for this property: round
, bevel
and miter
. By default this property is set to miter
. Note that the lineJoin
setting has no effect if the two connected segments have the same direction, because no joining area will be added in this case.
round
- Rounds off the corners of a shape by filling an additional sector of disc centered at the common endpoint of connected segments. The radius for these rounded corners is equal to the line width.
bevel
- Fills an additional triangular area between the common endpoint of connected segments, and the separate outside rectangular corners of each segment.
miter
- Connected segments are joined by extending their outside edges to connect at a single point, with the effect of filling an additional lozenge-shaped area. This setting is effected by the
miterLimit
property.
Examples
Using the lineJoin
property
This is just a simple code snippet using the lineJoin
property.
HTML
<canvas id="canvas"></canvas>
JavaScript
var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); ctx.lineWidth = 10; ctx.lineJoin = "round"; ctx.beginPath(); ctx.moveTo(0,0); ctx.lineTo(200, 100); ctx.lineTo(300,0); ctx.stroke();
Edit the code below and see your changes update live in the canvas:
Playable code
<canvas id="canvas" width="400" height="200" class="playable-canvas"></canvas> <div class="playable-buttons"> <input id="edit" type="button" value="Edit" /> <input id="reset" type="button" value="Reset" /> </div> <textarea id="code" class="playable-code" style="height:120px;"> ctx.lineWidth = 10; ctx.lineJoin = "round"; ctx.beginPath(); ctx.moveTo(0,0); ctx.lineTo(200, 100); ctx.lineTo(300,0); ctx.stroke();</textarea>
var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); var textarea = document.getElementById("code"); var reset = document.getElementById("reset"); var edit = document.getElementById("edit"); var code = textarea.value; function drawCanvas() { ctx.clearRect(0, 0, canvas.width, canvas.height); eval(textarea.value); } reset.addEventListener("click", function() { textarea.value = code; drawCanvas(); }); edit.addEventListener("click", function() { textarea.focus(); }) textarea.addEventListener("input", drawCanvas); window.addEventListener("load", drawCanvas);
A lineJoin
example
The example below draws three different paths, demonstrating each of these three lineJoin
property settings.
var ctx = document.getElementById('canvas').getContext('2d'); var lineJoin = ['round','bevel','miter']; ctx.lineWidth = 10; for (var i = 0; i < lineJoin.length; i++) { ctx.lineJoin = lineJoin[i]; ctx.beginPath(); ctx.moveTo(-5,5+i*40); ctx.lineTo(35,45+i*40); ctx.lineTo(75,5+i*40); ctx.lineTo(115,45+i*40); ctx.lineTo(155,5+i*40); ctx.stroke(); }
<canvas id="canvas" width="150" height="150"></canvas>
Screenshot | Live sample |
---|---|
Specifications
Specification | Status | Comment |
---|---|---|
WHATWG HTML Living Standard The definition of 'CanvasRenderingContext2D.lineJoin' in that specification. |
Living Standard |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
WebKit/Blink-specific notes
- In WebKit- and Blink-based Browsers, a non-standard and deprecated method
ctx.setLineJoin()
is implemented besides this property.
See also
- The interface defining it,
CanvasRenderingContext2D
CanvasRenderingContext2D.lineCap
CanvasRenderingContext2D.lineWidth
License
© 2016 Mozilla Contributors
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-us/docs/web/api/canvasrenderingcontext2d/linejoin