Top community members
All Wiki Articles Create Wiki Article

Welcome to Dirask IT community! ❤ 💻
We are community that shares solutions with each other.

If you think you have something to add here.

you are welcome with your fresh ideas - List Articles

JavaScript - how to draw line on canvas element?

0 contributions
5 points

Simple solution:

// line from x1=10, y1=20 to x2=200, y2=100
context.beginPath();
context.moveTo(10, 20);
context.lineTo(200, 100);
context.stroke();

 

Using JavaScript it is possible to draw line in following way.

1. lineTo method example

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <style>
    
    #my-canvas { border: 1px solid gray; }
    
  </style>
</head>
<body>
  <canvas id="my-canvas" width="200" height="200"></canvas>
  <script>

    var canvas = document.querySelector('#my-canvas');
    var context = canvas.getContext('2d');

    function drawLine() {

        context.beginPath();
        context.moveTo(10, 50);
        context.lineTo(170, 150);
        context.stroke();
    }

    drawLine();

  </script>
</body>
</html>

 

0 contributions

Suggested wiki articles:

Suggested findings

Let's do the work and have some fun
  • Dirask is online IT community for professionals and hobbyist to share their knowledge and help each other in extraordinary easy way. ❤ 💻 🙂
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    we will help you. 👍 ✔ 🦄
Read more