p5.js | fill() Function

The fill() function is used to fill the color to the shapes. This function supports all types of color object. For example RGB, RGBA, Hex CSS color, and all named color strings. The color object can also be set as string in terms of RGB, RGBA, Hex CSS color or named color string.

Syntax:

fill( v1, v2, v3, alpha )

or



fill( value )

or

fill( gray, alpha )

or

fill( values )

or

fill( 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 fill() 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, 300); 
  
function draw() { 
      
    // Set the background color 
    background(220); 
      
    // Use fill() function to fill color
    fill('green')
    // Draw a line 
    rect(50, 50, 150, 150); 
      
    // Use noFill() function
    noFill();
    
    // Draw a line 
    rect(100, 100, 150, 150); 

chevron_right


Output:

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

function setup() { 
      
    // Create Canvas of given size 
    createCanvas(400, 300); 
  
function draw() { 
      
    // Set the background color 
    background(220); 
      
    // Use noFill() function
    noFill();
      
    // Draw a circle 
    circle(140, 100, 150);
      
    // Use fill() function to fill color
    fill('green');
      
    // Draw a crrcle 
    circle(240, 100, 150); 

chevron_right


Output:

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

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.