The loop() function is used to call draw() function continuously. The loop() function can be stopped using noLoop() function.
Syntax:
loop()
Below example illustrates the loop() function in p5.js:
Example:
let l = 0;
function setup() {
createCanvas(500, 300);
background( 'green' );
}
function draw() {
stroke( 'white' );
l = l + 0.5;
if (l > width) {
l = 0;
}
line(l, 0, l, height);
}
function mousePressed() {
noLoop();
}
function mouseReleased() {
loop();
}
|
Output:

Online editor: https://editor.p5js.org/
Environment Setup: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks 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!
Last Updated :
16 Aug, 2023
Like Article
Save Article