Open In App

p5.js loop() Function

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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/


Last Updated : 16 Aug, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads