Open In App
Related Articles

p5.js loop() Function

Improve Article
Improve
Save Article
Save
Like Article
Like

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() {
    
  // Create canvas of given size
  createCanvas(500, 300);
    
  // Set the background color
  background('green');
  
}
  
function draw() {
    
  // Set the stroke color
  stroke('white');
    
  l = l + 0.5;
  if (l > width) {
    l = 0;
  }
    
  // Function to draw the line
  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
Previous
Next
Similar Reads
Complete Tutorials