Languages
[Edit]
EN

JavaScript - how to set line width (thickness / size) drawing on canvas element?

2 points
Created by:
Kevin
797

Simple solution:

context.lineWidth = 20; // 20px

// 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.lineWidth = 20;

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

    drawLine();

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

Merged questions

  1. JavaScript - canvas line with lineWidth property example
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

JavaScript - HTML5 canvas tutorial

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join