1

I want to run the startDraw() function when I press a key, and have it run constantly after the key is pressed. I tried to use the if(mouseIsPressed) statement, and the mousePressed() function that is predefined in p5.js, however it only runs the startDraw() function for less than 1 second (the time that the key is pressed).

function draw(){
    background(220);

    startDraw();
}

For instance:

function draw(){

    if (mouseIsPressed) {
        startDraw();
    }
}

This only works for the duration of the period that a key is pressed, not afterwards.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Mr.Ulis
  • 157
  • 6

1 Answers1

1

Create a state variable (drawing) which is initialized False and set it True in the mousePressed() callback:

let drawing = False;

function draw(){
      background(220);

      if (drawing) {
          startDraw();
      }
}

function mousePressed() {
    drawing = True;
}

Alternatively you can start the application with the execution of draw() stopped, by noLoop() and start it by loop() when the mouse is pressed:

function setup() {

    // [...]

    noLoop();
}   

function mousePressed() {
    loop();
}

function draw(){
    background(220);
    startDraw();
}
Rabbid76
  • 202,892
  • 27
  • 131
  • 174