Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

p5.js | setup() Function

  • Last Updated : 23 Aug, 2019

The setup() function runs when the program starts. It is used to set the initial environment properties such as text-color, screen size, background-color and load the media file such as images and fonts. The program contains only one setup() function. The setup() function can not be called again after its initial execution.

Note: The variable declaration within setup() function can not be access by other functions including draw() function.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!



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

Example 1:

function setup() { 
    // Create Canvas of given size 
    createCanvas(400, 300); 
function draw() { 
    // Use color() function 
    let c = color('green'); 
    // Use fill() function to fill color 
    // Draw a rectangle 
    rect(50, 50, 300, 200); 


Example 2:

function setup() {  
    // Create Canvas of given size 
    var cvs = createCanvas(600, 250);
function draw() {
    // Set the background color
    // Use createDiv() function to
    // create a div element
    var myDiv = createDiv('GeeksforGeeks');
    var myDiv1 = createDiv('A computer science portal for geeks');
    // Use child() function
    // Set the position of div element
    myDiv.position(150, 100);  
'text-align', 'center');
    // Set the font-size of text'font-size', '24px');
    // Set the font color'color', 'white');


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!