HTML | canvas lineWidth Property

The HTML canvas lineWidth property is used to set or return the width of the line (thickness of the line). The width of the line is set in terms of pixels. The default value of this property is 1.

Syntax:

context.lineWidth = number;

Property Values:

  • number: This property value indicates the number which specifies the line width in terms of pixels. This property ignores the zero, negative, infinity, and NaN values.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head
    <title
        HTML canvas lineWidth Property
    </title
</head
  
<body style = "text-align:center;">
      
    <h1 style="color:green"
        GeeksforGeeks 
    </h1
      
    <h2
        HTML canvas lineWidth() Property 
    </h2
  
    <canvas id="canvas" width="350" height="380"></canvas>
  
    <script>
        var canv = document.getElementById("canvas");
        var context = canv.getContext("2d");
        context.beginPath();
        context.lineWidth = 20;
        context.moveTo(40, 40);
        context.lineTo(200, 150);
        context.lineTo(40, 200);
        context.strokeStyle = "green";
        context.stroke();
    </script>
</body>
  
</html>                    

chevron_right


Output:

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head
    <title
        HTML canvas lineWidth Property
    </title
</head
  
<body style = "text-align:center;">
      
    <h1 style="color:green"
        GeeksforGeeks 
    </h1
      
    <h2
        HTML canvas lineWidth() Property 
    </h2
  
    <canvas id="canvas" width="350" height="380"></canvas>
  
    <script>
        var canv = document.getElementById("canvas");
        var context = canv.getContext("2d");
        context.beginPath();
        context.lineWidth = 10;
        context.strokeStyle ="green";
        context.strokeRect(110, 40, 120, 100);
    </script>
</body>
  
</html>                    

chevron_right


Output:

Supported Browsers: The browser supported by HTML canvas lineWidth property are listed below:

  • Google Chrome
  • Internet Explorer 9.0
  • Firefox
  • Safari
  • Opera


My Personal Notes arrow_drop_up

Budding Web DeveloperKeen learnerAverage CoderDancer&Social Activist

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.