The lineTo
method is used to draw a line on the canvas. Below are the steps to draw a line on the canvas:
beginPath()
method to start a new path.moveTo(x,y)
method to move the drawing cursor to a specific coordinate.lineTo(x,y)
method to draw a line from the current position to the provided x
and y
point.lineWidth
property to set the width of the line.stroke()
function to add stroke to the given path.The below code demonstrates how to draw a line on the canvas:
canvas
element.drawLine
method that will take the drawing context, line start and end coordinate, stroke color, and line width as arguments. This method will draw a line for the given coordinate.canvas
element and store it in the canvas
variable.getContext
method to get the 2D rendering context of the canvas.drawLine
method to draw a line from coordinate (10
, 10
) to coordinate (100
, 100
).