p5.js | strokeWeight() function

The strokeWeight() function in p5.js is used to set the width of the stroke used for lines, points, and the border around shapes. The stroke weight are set by using pixel.

Syntax:

strokeWidth(weight)

Parameters: This function accepts single parameter weight which stores the weight (in pixels) of the stroke.

Below programs illustrate the strokeWeight() function in p5.js:

Example 1: This example uses strokeWeight() function to set the width of stroke.



filter_none

edit
close

play_arrow

link
brightness_4
code

function setup() {
  
    // Create Canvas of given size
    createCanvas(380, 170);
}
  
function draw() {
      
    // Set the background color
    background(220);
      
    // Set the stroke width
    strokeWeight(20);
      
    // Set the filled color
    fill('green');
      
    // Draw the circle
    circle(60, 60, 34);
      
    // Set the text size
    textSize(20);
      
    // Set the text to print
    text("GeeksForGeeks", 120, 70);
}

chevron_right


Output:

Example 2: This example uses strokeWeight() function to set the width of stroke.

filter_none

edit
close

play_arrow

link
brightness_4
code

function setup() {
      
    // Create Canvas of given size
    createCanvas(380, 170);
}
  
function draw() {
      
    // Set the background color
    background(220);
      
    // Set the stroke clor
    stroke('orange');
      
    // Set the stroke width to 10
    strokeWeight(10); // Orange
      
    // Draw a line
    line(20, 70, 80, 70);
      
    // Set the stroke color
    stroke('white');
      
    // Set the stroke width to 8
    strokeWeight(8); // White
      
    // Draw a line
    line(20, 90, 80, 90);
      
    // Set stroke color
    stroke('green');
      
    // Set the stroke width to 6
    strokeWeight(6); // Green
      
    // Draw a line
    line(20, 110, 80, 110);
}

chevron_right


Output:

Reference: https://p5js.org/reference/#/p5/strokeWeight

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 :

Be the First to upvote.


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