p5.js | createCheckbox() Function

The createCheckbox() function in p5.js is used to create a checkbox element in the DOM (Document Object Model). This function includes the p5.dom library. Add the following syntax in the head section.

filter_none

edit
close

play_arrow

link
brightness_4
code

chevron_right


Syntax:

createCheckbox(label, value)

Parameters:



  • label: This parameter holds the label displayed beside the checkbox.
  • value: This parameter holds the state of the checkbox (true/false).

Example: This example uses a checkbox to change the background color from light to dark and vice-versa.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Create a variable for checkbox object
var checkbox;
  
// Create a function to change the background-color
function change_bg() {
    // Set dark color if box is checked
    if (this.checked()) {
        background("darkgreen");
    }
    // Set light color if box is unckecked
    else {
        background("lightgreen");
    }
}
  
function setup() {
    // Create a canvas
    createCanvas(400, 400);
    // Set the background-color
    background("lightgreen");
    // Create a checkbox object 
    // Initially unchecked
    checkbox = createCheckbox('Dark Background', false);
    // Posotion the checkbox object
    checkbox.position(160, 200);
    // Call the change_bg() function when the box
    // is cheched or unchecked
    checkbox.changed(change_bg);
}

chevron_right


Output:
Before checking the box:

After chechking the box:

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



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.