p5art

Mouse Events

In this video, I show how to draw something if the mouse is pressed.

Link to Video

Here I’m using an if statement. The if statement is like the if/else block in Scratch. Pay special attention to the {} brackets, they are used to group a block of code together.

if(something-true){
  print('true')//do this!
} else {
  print('not true') //do this!
}

Inside the if statement there needs to be a statement that can be either true or false. In p5, we call these “boolean statements.”

Here are some examples of boolean statements…

p5 has a built in variable named mouseIsPressed. This variable will be true when the mouse is pressed, but false if it is not.

We can use this built-in variable to change what we are drawing depending on mouse. In this example I draw a circle if the mouse is down and a square if it is not.

if(mouseIsPressed){
    ellipse(mouseX, mouseY, 100, 100)
} else {
    rect(mouseX, mouseY, 100, 100)
}