There are a few more p5 events that we should discuss. This will make your work, potentially, more interesting.
A few details first; in p5, whenever a recognizable event occurs, the environment “does something”. For example, when the mouse moves, the p5 environment updates the mouseX
and mouseY
variables. There are a number of events that p5 is capable of responding to. For a complete list, please look at the p5 reference “events” section.
In addition to mouse position variables, p5 also has a reference to whether the mouse is currently pressed or not. This value is stored as a Boolean. It can be referenced from the p5 environment variable mouseIsPressed
.
Since this p5 variable stores a Boolean representing the mouse press state, we can pass it directly to conditional statements.
In the following example, we do just that. As a result, if the mouse is not pressed (i.e. mouseIsPressed == false
), we see a drab brown canvas. However, when the mouse is pressed (mouseIsPressed == true
), the background changes colors randomly, and a ellipses pop around!
[ Code Download ] | [ View on GitHub ] | [ Live Example ] |
In addition to the p5 environment variable mouseIsPressed
, p5 also includes an event function for the mouse, that is executed whenever the “mouse is pressed”.
This is similar, in some ways, to p5’s setup()
and draw()
functions, in that, we can define a mousePressed()
function, which p5 will execute when the “mouse is pressed”.
To use this function, you simply need to define the mousePressed()
function somewhere in your sketch, *outside of setup()
and draw()
.
function mousePressed() {
// statements to execute when the mouse pressed
// always include the following line at the end
return false;
}
Please be advised: per the p5 reference page, that some browsers have default behavior for
mousePressed()
functions. “Browsers may have different default behaviors attached to various mouse events. To prevent any default behavior for this event, addreturn false
to the end of the method.” JUST DO IT!
In the following example;
mouseCounter
, which is initialized to 0
.mousePressed()
function definition.
mouseCounter
variable should be incremented, each time the mouse is pressed.draw()
function, the mouseCounter
variable is added to a string of text, and displayed on the canvas. This shows the suer how many times the mouse button has been pressed.[ Code Download ] | [ View on GitHub ] | [ Live Example ] |
mouseIsPressed
& mousePressed()
It is not apparent at first, but there are significant differences between the p5 environment variable mouseIsPressed
and the function mousePressed()
.
mouseIsPressed
mouseIsPressed
to be either true
or false
.true
or false
when called.mousePressed()
mousePressed()
is called when ever the mouse is pressed.mousePressed()
function is only executed once per each mouse click. The mouse has to be unpressed, and repressed, before the function will execute again.draw()
function queries the variable mouseIsPressed
and does something when it returns true
that will occur every frame, as long as the mouse is pressed.The following code demonstrates the differences in number 2.
width * 1/4
and width * 3/4
.mouseIsPressed_SIZE
. This variable will be incremented in the draw()
loop when mouseIsPressed == true
.mousePressFunction_SIZE
. This variable will be incremented by the mousePressed()
function.[ Code Download ] | [ View on GitHub ] | [ Live Example ] |
Notice, while you hold the mouse down, the left, mouseIsPressed
-based ellipse continues to grow non-stop. Whereas, the right, mousePressed()
-based ellipse only grows 1 pixel every time the mouse button is pressed. The right ellipse requires the user to lift their finger, and repress the mouse button in order to make it grow again. This is because the mousePressed()
function only executes once every time the mouse is pressed.
If you look at the image up above, from the p5 reference site, there are many event-based functions and p5 environment variables. There is a set of keyboard based events, as well as events having to do with ‘touch’ and ‘device orientation’. The latter two categories are for mobile phones of course.
Think about how you could compose a sketch that took advantage of mobile phone gestures or touch to alter a p5 sketch?