p5.js | stroke() Function

The stroke() function is used to draw the lines and border around the text and shapes. The color object can be set in terms of RGB or HSB depending on the color mode. The color object can also be set as string in terms of RGB, RGBA, Hex CSS color or named color string.

Syntax:

stroke( v1, v2, v3, alpha )

or

stroke( value )

or

stroke( gray, alpha )

or



stroke( values )

or

stroke( color )

Parameters:

  • v1: It is used to set the red or hue value relative to current color range.
  • v2: It is used to set the green or saturation value relative to current color range.
  • v3: It is used to set the blue or brightness value relative to current color range.
  • alpha: It is used to set the transparency of the drawing.
  • value: It is used to set the value of color string.
  • gray: It is used to set the gray value.
  • values: It is an array containing the red, green, blue and alpha value.
  • color: It is used to set the stroke color.

Below examples illustrate the stroke() function in p5.js:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

function setup() { 
  
    // Create Canvas of given size 
    createCanvas(400, 200); 
  
function draw() { 
      
    // Set the background color 
    background(220); 
      
    // Set the stroke width 
    strokeWeight(10); 
      
    // Set the stroke
    stroke('green');
    
    // Set the filled color 
    fill('white'); 
      
    // Draw the circle 
    circle(90, 90, 34); 
      
    // Set the text size 
    textSize(20); 
      
    // Set the text to print 
    text("GeeksForGeeks", 140, 100); 
}

chevron_right


Output:

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

function setup() { 
      
    // Create Canvas of given size 
    createCanvas(400, 200); 
  
function draw() { 
      
    // Set the background color 
    background(220); 
      
    // Set the stroke clor 
    stroke('orange'); 
      
    // Set the stroke width to 10 
    strokeWeight(30); // Orange 
      
    // Draw a line 
    line(100, 50, 300, 50); 
      
    // Set the stroke color 
    stroke('white'); 
      
    // Set the stroke width to 8 
    strokeWeight(30); // White 
      
    // Draw a line 
    line(100, 100, 300, 100); 
      
    // Set stroke color 
    stroke('green'); 
      
    // Set the stroke width to 6 
    strokeWeight(30); // Green 
      
    // Draw a line 
    line(100, 150, 300, 150); 

chevron_right


Output:

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

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.