Languages
[Edit]
EN

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

2 points
Created by:
AnnLen
1795

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
Hey ūüĎč
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists 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,
    this community will help you.