Mouse Inputs in Processing Fall 2009
Interacting with the Mouse mouseX and mouseY: pg 205-211 The position of the mouse in the canvas pmouseX and pmouseY: pg 208 The previous position of the mouse (in the last frame) mouseButton: pg 212-213 Which mouse button has pressed mousePressed: pg 212-213 true if the mouse button is pressed, false otherwise mousePressed() : pg 229-231 Functions that run when the mouse is pressed
Hello Mouse void setup() { size(400, 400); stroke(255); } void draw() { // try moving background() to setup() background(192, 64, 200); // mouseX is a variable that stores the horizontal position // of the mouse. // mouseY is a variable that stores the vertical position line(150, 25, mouseX, mouseY);
Moving with the Mouse // Circle follows the cursor void setup() { size(100, 100); smooth(); noStroke(); } void draw() { background(126); ellipse(mouseX, mouseY, 33, 33);
Highlighting with the Mouse // Cursor position selects a quadrant of the display window void setup() { size(100, 100); noStroke(); fill(0); } void draw() { background(204); if ((mouseX <= 50) && (mouseY <= 50)) { rect(0, 0, 50, 50); // Upper-left } else if ((mouseX <= 50) && (mouseY > 50)) { rect(0, 50, 50, 50); // Lower-left } else if ((mouseX > 50) && (mouseY < 50)) { rect(50, 0, 50, 50); // Upper-right } else { rect(50, 50, 50, 50); // Lower-right
pmouse // Draw a line between the current and previous positions void setup() { size(100, 100); strokeWeight(8); smooth(); } void draw() { background(204); // pmouseX is a variable that stores the horizontal position // of the mouse in the previous frame. // pmouseY is a variable that stores the vertical position of // the mouse in the previous frame. line(mouseX, mouseY, pmouseX, pmouseY);
mousePressed and mouseButton Like mouseX and mouseY, mousePressed is a property of the mouse (a variable), but it is of type boolean. The mousePressed variable stores true if any mouse button is pressed, and false otherwise. It reverts to false as soon as the button is released. The mouseButton variable is LEFT, CENTER, or RIGHT depending on the mouse button most recently pressed. mouseButton retains its value until a different mouse button is pressed
A Simple Paint Example int brushSize = 20; void setup() { smooth(); noStroke(); background(255); } void draw() { if (mousePressed) { brush(mouseX, mouseY); void brush(int x, int y) { fill(#CC3300); // note the color is specified as a hexadecimal number arc(x, y, brushSize, brushSize, 0, PI); fill(#003333); // note the color is specified as a hexadecimal number arc(x, y, brushSize, brushSize, PI, TWO_PI);
mousePressed() We can try to get the same functionality by using the mousePressed() event instead of the mousePressed variable. To use event, we need to write a function by the same name in our sketch, and it will be called automatically every time the event occurs. In the following code, mousePressed() will be called every time the mouse is pressed.
int brushSize = 20; void setup() { size(400, 400); smooth(); noStroke(); background(255); } void draw() { // keep the motor running void mousePressed() { brush(mouseX, mouseY); void brush(int x, int y) { fill(#CC3300); arc(x, y, brushSize, brushSize, 0, PI); fill(#003333); arc(x, y, brushSize, brushSize, PI, TWO_PI);
mousePressed vs. mousePressed() You'll notice that the program does not behave the same way. Although the mousePressed variable and the mousePressed() event have the same name, they represent different things mousePressed stores true for as long as the mouse button is down, whereas mousePressed() gets called once whenever the mouse becomes pressed, i.e. once per click.
In class exercise Create a recursive program that creates an aesthetically pleasing design. You can begin with code from the recursion powerpoint. Make the graphics appears in response to a mouse click or in some creative way use mouse input and recursion.