p5.js print() function

The print() function in p5.js writes the content on console area of the web browser. It is helpful for looking at the data a program is producing which helps in debugging the code. The elements can be separated by using quotes (ā€œā€) and joined with the addition operator (+). Use print(ā€˜\n’) to display blank line.

Syntax:

print(c)

Parameters: The function accepts single parameter c which stores the any combination of Number, String, Object, Boolean, Array to print.

Below program illustrates the print() function in p5.js:

Example 1: This example uses print() function to display the content on console.




function setup() {
Ā Ā 
Ā Ā Ā Ā // Create canvas of given size
Ā Ā Ā Ā createCanvas(400, 400);
Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā // print function call
Ā Ā Ā Ā print("Hello GeeksForGeeks");
}
Ā Ā 
function draw() {
Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā // Set the background color
Ā Ā Ā Ā background(220);
}


Output:

Example 2: This example uses print() function to display the content on console.




function setup() {
Ā Ā 
Ā Ā Ā Ā // Create canvas of given size
Ā Ā Ā Ā createCanvas(400, 400);
Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā // print function call
Ā Ā Ā Ā for(let i = 0; i < 4; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā for(let j = 0; j < i; j++) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā print("*");
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā print("\n");
Ā Ā Ā Ā }
}
Ā Ā 
function draw() {
Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā // Set the background color
Ā Ā Ā Ā background(220);
}


Output:

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

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button