I simplified a bit your sketch (no need for arrays of 1 entry, unless you plan to expand them later) and well, as a result it seems to work as you wish... Note: avoid putting sketch size in variables.
Code:int rect_x1; // catch the start dragging point x
int rect_y1; // catch the start dragging point y
int rect_x2; // record moving mouseX
int rect_y2; // record moving mouseY
int rect_x3; // record mouseX releasing point
int rect_y3; // record mouseY releasing point.
void setup() {
size(800, 600);
smooth();
}
void draw() {
background(255);
draw_rect();
}
void draw_rect() {
if (mousePressed) {
float sizex = rect_x2 - rect_x1;
float sizey = rect_y2 - rect_y1;
strokeWeight(.4);
rect(rect_x1, rect_y1, sizex,sizey);
fill(100,30);
} else{
float sizex1 = rect_x3 - rect_x1;
float sizey1 = rect_y3 - rect_y1;
rect(rect_x1, rect_y1, sizex1, sizey1);
fill(100, 30);
strokeWeight(1.5);
}
}
void mousePressed() {
rect_x1 = mouseX;
rect_y1 = mouseY;
}
void mouseDragged() {
rect_x2 = mouseX;
rect_y2 = mouseY;
}
void mouseReleased() {
rect_x3 = mouseX;
rect_y3 = mouseY;
}
Looking more attentively at the code, I see it can be simplified even more:
Code:// Same code above, but get rid of x3/y3
void draw_rect() {
float sizex = rect_x2 - rect_x1;
float sizey = rect_y2 - rect_y1;
if (mousePressed) {
strokeWeight(.4);
} else{
strokeWeight(1.5);
}
fill(100, 30);
rect(rect_x1, rect_y1, sizex,sizey);
}
void mousePressed() {
rect_x1 = mouseX;
rect_y1 = mouseY;
mouseDragged(); // Reset vars
}
void mouseDragged() {
rect_x2 = mouseX;
rect_y2 = mouseY;
}