11
Mouse Inputs in Processing Spring 2010

Mouse Inputs in Processing Spring 2010. Interacting with the Mouse mouseX and mouseY: pg 205-211mouseXmouseY –The position of the mouse in the canvas

Embed Size (px)

Citation preview

Page 1: Mouse Inputs in Processing Spring 2010. Interacting with the Mouse mouseX and mouseY: pg 205-211mouseXmouseY –The position of the mouse in the canvas

Mouse Inputs in Processing

Spring 2010

Page 2: Mouse Inputs in Processing Spring 2010. Interacting with the Mouse mouseX and mouseY: pg 205-211mouseXmouseY –The position of the mouse in the canvas

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

Page 3: Mouse Inputs in Processing Spring 2010. Interacting with the Mouse mouseX and mouseY: pg 205-211mouseXmouseY –The position of the mouse in the canvas

Hello Mousevoid 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 // of the mouse.

line(150, 25, mouseX, mouseY); }

Page 4: Mouse Inputs in Processing Spring 2010. Interacting with the Mouse mouseX and mouseY: pg 205-211mouseXmouseY –The position of the mouse in the canvas

Moving with the Mouse

// Circle follows the cursorvoid setup() { size(100, 100); smooth(); noStroke();}

void draw() { background(126); ellipse(mouseX, mouseY, 33, 33);}

Page 5: Mouse Inputs in Processing Spring 2010. Interacting with the Mouse mouseX and mouseY: pg 205-211mouseXmouseY –The position of the mouse in the canvas

Highlighting with the Mouse// Cursor position selects a quadrant of the display windowvoid 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 }}

Page 6: Mouse Inputs in Processing Spring 2010. Interacting with the Mouse mouseX and mouseY: pg 205-211mouseXmouseY –The position of the mouse in the canvas

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);}

Page 7: Mouse Inputs in Processing Spring 2010. Interacting with the Mouse mouseX and mouseY: pg 205-211mouseXmouseY –The position of the mouse in the canvas

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

Page 8: Mouse Inputs in Processing Spring 2010. Interacting with the Mouse mouseX and mouseY: pg 205-211mouseXmouseY –The position of the mouse in the canvas

A Simple Paint Example int brushSize = 20; void setup() { size(400, 400); 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); }

Page 9: Mouse Inputs in Processing Spring 2010. Interacting with the Mouse mouseX and mouseY: pg 205-211mouseXmouseY –The position of the mouse in the canvas

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.

Page 10: Mouse Inputs in Processing Spring 2010. Interacting with the Mouse mouseX and mouseY: pg 205-211mouseXmouseY –The position of the mouse in the canvas

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); }

Page 11: Mouse Inputs in Processing Spring 2010. Interacting with the Mouse mouseX and mouseY: pg 205-211mouseXmouseY –The position of the mouse in the canvas

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 is true for as long as the mouse button is down, whereas mousePressed() is called once whenever the mouse becomes pressed, i.e. once per click.