p5.js | createSelect() Function

The createSelect() function in p5.js is used to create a dropdown menu element in the DOM (Document Object Model) for taking input. The .value() method is used to get the selected option. This function includes the p5.dom library. Add the following syntax in the head section.

Note: This function requires the p5.dom library. So add the following line in the head section of the index.html file.

<script src=”https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/addons/p5.dom.min.js”>
</script>



Syntax:

createSelect(multiple)

or

createSelect(existing)

Parameters:

  • multiple: It holds the state of input. Multiple if Ture, single if False.
  • existing: It holds the object DOM select element.

Return Value: This function returns the value of an attribute in p5.Element format.

Example: This example uses a dropdown menu to change the background color from the given options.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Create a variable for dropdown menu object
var dropdown;
  
function setup() {
    // Create a canvas
    createCanvas(400,400);
    // Create a dropdown menu object 
    dropdown = createSelect();
    // Position the dropdown menu
    dropdown.position(150,200);
    // Set options
    dropdown.option("orange");
    dropdown.option("green");
    dropdown.option("skyblue");
}
  
function draw() {
    // Set the background-color as choosen 
    // from the dropdown menu
    background(dropdown.value());
}

chevron_right


Output:

  • Before changing the color:
  • During changing the color:
  • After changing the color:
  • Reference: https://p5js.org/reference/#/p5/createSelect



    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.