The canvas lineCap property is used to set or return the style of end caps of line. The line can have one of three cap styles: butt, round, or square. The default value of canvas lineCap property are butt. The lineCap property must be set before calling the stroke() function.
Syntax:
context.lineCap = "butt|round|square";
Property Values:
- butt: It is the default style. This property value adds the flat edge at the each end of the line.
- round: This property value adds the end cap at the each end of the line.
- square: This property value adds the square cap at the each end of the line.
Note: The value round and square makes the line slightly longer.
Example 1: This example illustrates the butt property value.
<!DOCTYPE html> <html> <head> <title> HTML canvas lineCap Property </title> </head> <body style="text-align:center;"> <h1>GeeksforGeeks</h1> <h2>HTML canvas lineCap Property</h2> <canvas id="GFG" width="500" height="200" style="border:1px solid black;"> </canvas> <script> var canvas_id = document.getElementById("GFG"); var context = canvas_id.getContext("2d"); context.beginPath(); context.lineWidth = 35; context.lineCap = "butt"; context.moveTo(50, 100); context.lineTo(450, 100); context.strokeStyle ="green"; context.stroke(); </script> </body> </html> |
Output:

Example 2: This example illustrates the round property value.
<!DOCTYPE html> <html> <head> <title> HTML canvas lineCap Property </title> </head> <body style="text-align:center;"> <h1>GeeksforGeeks</h1> <h2>HTML canvas lineCap Property</h2> <canvas id="GFG" width="500" height="200" style="border:1px solid black;"> </canvas> <script> var canvas_id = document.getElementById("GFG"); var context = canvas_id.getContext("2d"); context.beginPath(); context.lineWidth = 35; context.lineCap = "round"; context.moveTo(50, 100); context.lineTo(450, 100); context.strokeStyle ="green"; context.stroke(); </script> </body> </html> |
Output:

Example 3: This example illustrates the square property value.
<!DOCTYPE html> <html> <head> <title> HTML canvas lineCap Property </title> </head> <body style="text-align:center;"> <h1>GeeksforGeeks</h1> <h2>HTML canvas lineCap Property</h2> <canvas id="GFG" width="500" height="200" style="border:1px solid black;"> </canvas> <script> var canvas_id = document.getElementById("GFG"); var context = canvas_id.getContext("2d"); context.beginPath(); context.lineWidth = 35; context.lineCap = "square"; context.moveTo(50, 100); context.lineTo(450, 100); context.strokeStyle ="green"; context.stroke(); </script> </body> </html> |
Output:

Recommended Posts:
- CSS | stroke-linecap Property
- HTML | canvas lineWidth Property
- HTML | canvas textAlign Property
- HTML | canvas font Property
- HTML | canvas lineJoin Property
- HTML | canvas shadowColor Property
- HTML | canvas fillStyle Property
- HTML | canvas strokeStyle Property
- HTML | canvas shadowBlur Property
- HTML | canvas shadowOffsetX Property
- HTML | canvas shadowOffsetY Property
- HTML | canvas textBaseline Property
- HTML | canvas ImageData data Property
- HTML | canvas ImageData width Property
- HTML | canvas globalAlpha Property
- HTML | canvas ImageData height Property
- HTML | canvas miterLimit Property
- Web HTMLCanvasElement API | Canvas Element width property
- HTML | Canvas Basics
- How to resize an image in an HTML 5 canvas ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


