Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

p5.js | createCheckbox() Function

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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.


createCheckbox(label, value)


  • 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. 


// 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()) {
    // Set light color if box is unchecked
    else {
function setup() {
    // Create a canvas
    createCanvas(400, 400);
    // Set the background-color
    // Create a checkbox object
    // Initially unchecked
    checkbox = createCheckbox('Dark Background', false);
    // Position the checkbox object
    checkbox.position(160, 200);
    // Call the change_bg() function when the box
    // is checked or unchecked

Before checking the box: 

After checking the box: 


My Personal Notes arrow_drop_up
Last Updated : 26 Nov, 2021
Like Article
Save Article
Similar Reads
Related Tutorials