Skip to content

Instantly share code, notes, and snippets.

@manuelborst
Created August 2, 2021 16:03
Show Gist options
  • Save manuelborst/1025479ab37dc619e7b1d3c5e9fcb059 to your computer and use it in GitHub Desktop.
Save manuelborst/1025479ab37dc619e7b1d3c5e9fcb059 to your computer and use it in GitHub Desktop.
Drag and Drop Processing
/**
* Mouse Functions.
*
* Click on the box and drag it across the screen.
*/
float bx;
float by;
int boxSize = 75;
boolean overBox = false;
boolean locked = false;
float xOffset = 0.0;
float yOffset = 0.0;
void setup() {
size(640, 360);
bx = width/2.0;
by = height/2.0;
rectMode(RADIUS);
}
void draw() {
background(0);
// Test if the cursor is over the box
if (mouseX > bx-boxSize && mouseX < bx+boxSize &&
mouseY > by-boxSize && mouseY < by+boxSize) {
overBox = true;
if(!locked) {
stroke(255);
fill(153);
}
} else {
stroke(153);
fill(153);
overBox = false;
}
// Draw the box
rect(bx, by, boxSize, boxSize);
}
void mousePressed() {
if(overBox) {
locked = true;
fill(255, 255, 255);
} else {
locked = false;
}
xOffset = mouseX-bx;
yOffset = mouseY-by;
}
void mouseDragged() {
if(locked) {
bx = mouseX-xOffset;
by = mouseY-yOffset;
}
}
void mouseReleased() {
locked = false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment