In this article, we will learn how to make lines in different styles on Canvas in HTML. There are various methods used to draw a line on canvas like beginPath(), moveTo(), lineTo(), and stroke(). There are also various properties like lineWidth, strokeStyle, etc. can be used to give styles to a line with the help of JavaScript.
Syntax
canvas1.lineTo(100, 150);
Canvas Methods for Drawing Lines
Syntax
canvas1.beginPath();
canvas1.moveTo( 0, 0 );
canvas1.lineTo( 120, 150 );
canvas1.stroke();
Methods
- beginPath(): This method defines the new path.
- moveTo(x,y): This method is used to define the starting point.
- lineTo(x,y): This method is used to define the endpoint. Here, x specifies the x-axis coordinates(horizontal) and y specifies the y-axis coordinates(verticle) of the line's endpoint.
- stroke(): This method is used to draw the line.
Canvas Properties for Drawing Lines
Syntax
canvas2.lineWidth = 10;
canvas1.strokeStyle = "blue";
canvas2.lineCap = "round";
Properties
- lineWidth: In this property, we define the thickness or width of the line.
- strokeStyle: In this property, we define the style of the line.
- lineCap: In this property, we define the cap style that is applied on the top and bottom (start and end) of the line. There are three styles can be used butt, round, and square.
Using the lineTo() method and lineWidth property
The HTML Canvas 'lineTo' method delicately connects points, while the 'lineWidth' property subtly refines the strokes, resulting in a seamless fusion of precision and style on the digital canvas.
Example 1: In this example, we will draw a line with lineTo() and give the property lineWidth to define the width of the line.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width,
initial-scale=1.0">
<title>HTML CANVAS LINES</title>
<link rel="stylesheet"
href="style.css">
</head>
<body>
<p>GeeksforGeeks</p>
<h1>
HTML CANVAS LINES
</h1>
<div class="flex-class"></div>
<canvas height="200"
width="200"
class="canvas-element"
id="c1">
</canvas>
<canvas height="200"
width="200"
class="canvas-element"
id="c2">
</canvas>
<script src="script.js"></script>
</body>
</html>
CSS
@import url(
'https://fonts.googleapis.com/css2?family=Poppins&display=swap');
body {
font-family: 'Poppins', sans-serif;
}
.canvas-element {
border: 2px solid black;
}
p {
font-size: 50px;
color: green;
}
.flex-class {
display: flex;
flex-wrap: wrap;
}
JavaScript
const canvas1 =
document.getElementById("c1").getContext("2d")
const canvas2 =
document.getElementById("c2").getContext("2d")
canvas1.beginPath();
canvas1.moveTo(0, 0)
canvas1.lineTo(120, 150)
canvas1.stroke();
canvas2.beginPath();
canvas2.moveTo(10, 10)
canvas2.lineTo(100, 150)
canvas2.lineWidth = 10;
canvas2.stroke();
Output:

Using the lineTo() method and lineCap property
The 'lineCap' property gives your drawings a neat and professional finish and The HTML Canvas 'lineTo' method delicately connects points.
Example 2: In this example, we will draw a line with lineTo() with properties.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width,
initial-scale=1.0">
<title>HTML CANVAS LINE</title>
<link rel="stylesheet"
href="style.css">
</head>
<body>
<p>GeeksforGeeks</p>
<h1>
HTML CANVAS LINES
</h1>
<div class="flex-class"></div>
<canvas height="200"
width="200"
class="canvas-element"
id="c1">
</canvas>
<canvas height="200"
width="200"
class="canvas-element"
id="c2">
</canvas>
<script src="script.js"></script>
</body>
</html>
CSS
@import url(
'https://fonts.googleapis.com/css2?family=Poppins&display=swap');
body {
font-family: 'Poppins', sans-serif;
}
.canvas-element {
border: 2px solid black;
}
p {
font-size: 50px;
color: green;
}
.flex-class {
display: flex;
flex-wrap: wrap;
}
JavaScript
const canvas1 =
document.getElementById("c1").getContext("2d")
const canvas2 =
document.getElementById("c2").getContext("2d")
canvas1.beginPath();
canvas1.moveTo(20, 20)
canvas1.lineTo(70, 150)
canvas1.lineWidth = 3
canvas1.strokeStyle = "blue";
canvas1.stroke();
canvas2.beginPath();
canvas2.moveTo(10, 10)
canvas2.lineTo(100, 150)
canvas2.lineWidth = 10
canvas2.strokeStyle = "red"
canvas2.lineCap = "round";
canvas2.stroke();
Output:
Similar Reads
HTML Canvas Tutorial HTML Canvas facilitates the element <canvas> to draw graphics on Canvas with the help of JavaScript. HTML Canvas offers various methods for drawing different shapes and lines. The HTML Canvas is a rectangular area defined via height and width on an HTML page. By default, HTML Canvas has no bor
3 min read
HTML Canvas Basics In this article, we will know HTML Canvas Basics, and their implementation through the examples. The HTML "canvas" element is used to draw graphics via JavaScript. The "canvas" element is only a container for graphics. One must use JavaScript to actually draw the graphics. Canvas has several methods
5 min read
HTML Canvas Drawing HTML Canvas Drawing facilitates the <canvas> element, along with Properties that help to draw on the HTML canvas. The various properties, attributes & events can be used with <canvas> element. Utilizing JavaScript, we can manipulate the canvas element to draw shapes, paths, and image
2 min read
HTML Canvas Coordinates In this article, we will see the concept of coordinates in HTML Canvas Coordinates. In HTML canvas, the coordinates are very important because, with the understanding of these coordinates, we can easily draw any shapes or lines on the canvas. x-axis coordinates define the coordinates in horizontal a
2 min read
HTML Canvas Lines In this article, we will learn how to make lines in different styles on Canvas in HTML. There are various methods used to draw a line on canvas like beginPath(), moveTo(), lineTo(), and stroke(). There are also various properties like lineWidth, strokeStyle, etc. can be used to give styles to a line
3 min read
HTML Canvas Shapes HTML Canvas Shapes facilitate different shapes, i.e., rectangles, triangles, lines, arcs, and curves to draw on the HTML Canvas. For instance, to draw the line in the Canvas, the path will be used that implements the beginPath() method. Setting the path's starting point is achieved with moveTo(), w
3 min read
HTML Canvas Rectangles The HTML Canvas Rectangles facilitate the rect() method to draw rectangles on canvas. There are various attributes in the rect(x, y, width, height) method such as x and y defining the coordinates of the upper-left corner of the rectangle, width defining the width of the rectangle, and height definin
4 min read
HTML Canvas Circles HTML Canvas Circles facilitates the arc() method to make a circle where 0 is defined as the start angle and the end angle is 2*PI. The stroke() method is used to draw an outline of the circle and fill() is used to draw a filled circle we can give color with the fillStyle property. stroke() Method Cr
2 min read
HTML Canvas Curves HTML Canvas Curves facilitate the arc() method for making a circle or a part of a circle on the canvas. There are various attributes in the arc(x, y, r, s, e) method such as x and y defining the coordinates of the center of the circle, r defining the radius, s defining the start angle, and e definin
2 min read
HTML Canvas Gradients HTML Canvas Gradients is used to give a gradient effect on Canvas with the help of JavaScript. The different shapes, such as rectangles, circles, lines, text, etc, can be filled with Gradients. To create a gradient on Canvas, we can use two techniques, Linear Gradient and Radial Gradient. Linear Gra
3 min read