All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

JavaScript Draw Line Between Two Points

Last Updated : Mar 11, 2024

JavaScript Draw Line Between Two Points

In this article we will show you the solution of JavaScript draw line between two points, we will draw a line between two points on an HTML page using JavaScript.

Here in the example below, we will use the HTML canvas property.

By defining the <canvas> tag, we can draw graphics inside an HTML page.

We must use JavaScript alongside it to draw graphics. We can define the height and width of the canvas and then use coordinates to draw inside it.

We will see one example of drawing a line using JavaScript.

Step By Step Guide On JavaScript Draw Line Between Two Points :-

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>javascript draw line between two points</title>
    <link rel="stylesheet" href="">
            background-color: aliceblue;
            font-family: Cambria, Cochin, Georgia, Times, 'Times New Roman', serif;
          color : rgb(95, 194, 95) ;
          font-size: 25px;
          font-weight : bolder ;
            color : rgb(95, 194, 95) ;
            font-size: 20px;
        <h1> TALKERSCODE </h1>
        <h3> javascript draw line between two points </h3>
    <canvas id="myCanva">
        const myCanva = document.getElementById("myCanva") ;
        myCanva.width = window.innerWidth;
        myCanva.height = window.innerHeight;
        let A = {x:300, y:100} ;
        let B = {x:350, y:350} ;
        const context = myCanva.getContext("2d") ;
        context.beginPath() ;
        context.moveTo(A.x, A.y) ;
        context.lineTo(B.x, B.y) ;
        context.stroke() ;
  1. writing HTML code <!DOCTYPE html> is used to specify the HTML version that a file is written in.
  2. The <HTML> tag used to specify the root of an HTML document must then be written. Additionally, add the </html> tag at the end.
  3. The metadata for the HTML file is contained in the <head> tag, which is closed with the </head> tag.
  4. The HTML document's <title> is then established using the title tag, which is followed by the < /title> tag.
  5. We'll use an external CSS file to style the HTML page.
  6. using <style> tag to add CSS
  7. create a <Center> tag the add a <h1> tag used to add a heading close it with </h1> tag and add a <h3> tag within it
  8. at first we have to create an HTML canvas using <canvas> tag and give it an id of ‘myCanvas’
  9. To write JavaScript, create a <script> tag
  10. Now use document.getElementById() with the canvas id with a constant const of myCanvas
  11. Adding the canvas width to window.innerWidth and height to window.innerHeight.
  12. Now consider two points A and B. Then set both x and y coordinates.
  13. Set the context using .getContext(“2d”)
  14. To begin the line, use the method beginPath().
  15. Using moveTo() attribute to set the starting point and lineTo() attribute to set the line's ending point.
  16. Lastly, use stroke() to stroke a line from starting point to the ending point.

Conclusion :-

At last, here in conclusion, we can say that with this article’s help, we know How to draw line between two points using JavaScript.

I hope this article on JavaScript draw line between two points helps you and the steps and method mentioned above are easy to follow and implement

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪