p5.js setup() Function

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.
Syntax:
setup()
Below examples illustrate the setup() function in p5.js:
Example 1:
| functionsetup() {           // Create Canvas of given size      createCanvas(400, 300);   }   functiondraw() {           background(220);           // Use color() function      let c = color('green');       // Use fill() function to fill color      fill(c);           // Draw a rectangle      rect(50, 50, 300, 200);       }   | 
Output:
Example 2:
| functionsetup() {            // Create Canvas of given size      varcvs = createCanvas(600, 250); }   functiondraw() {         // Set the background color     background('green');          // Use createDiv() function to     // create a div element     varmyDiv = createDiv('zambiatek');         varmyDiv1 = createDiv('A computer science portal for zambiatek');         // Use child() function     myDiv.child(myDiv1);         // Set the position of div element     myDiv.position(150, 100);           myDiv.style('text-align', 'center');         // Set the font-size of text     myDiv.style('font-size', '24px');         // Set the font color     myDiv.style('color', 'white');   }  | 
Output:
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, zambiatek Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
 
				 
					



