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.
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();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.