1 / 11

Mouse Inputs in Processing

Mouse Inputs in Processing. 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

Download Presentation

Mouse Inputs in Processing

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Mouse Inputs in Processing

  2. 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 • Function that runs when the mouse is pressed

  3. 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 // of the mouse. line(150, 25, mouseX, mouseY); }

  4. Moving with the Mouse // Circle follows the cursor void setup() { size(100, 100); smooth(); noStroke(); } void draw() { background(126); ellipse(mouseX, mouseY, 33, 33); }

  5. 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 } }

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

  7. 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

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

  9. 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.

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

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

More Related