We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › scaling with mouseDrag
Page Index Toggle Pages: 1
scaling with mouseDrag? (Read 782 times)
scaling with mouseDrag?
Dec 6th, 2009, 3:05pm
 
I want to be able to click and drag my mouse and have an image load to the size and place I dragged. Any ideas? Can't quite get my head around this. I'm thinking I need to compare x and y before and after and figure the scale and placement from that. But can't quite get there.  Shocked
Re: scaling with mouseDrag?
Reply #1 - Dec 7th, 2009, 1:08am
 
Assuming you want to draw a rectangle:

Find the differences between mouseX and mouseY from mousePressed() to mouseReleased() to get the width and height.

Use the lower of each (x/y) to find the upper left corner and draw it with image().
Re: scaling with mouseDrag?
Reply #2 - Dec 7th, 2009, 4:19am
 
For convenience, I would use PVector and mouseDragged() :

Quote:
PVector click, drag;

void setup() {
  click = new PVector(0, 0);
  drag = new PVector(0, 0);
}

void draw() {
  background(255);
  rect(click.x, click.y, drag.x - click.x, drag.y - click.y);
}

void mousePressed() {
  click.set(mouseX, mouseY, 0);
  drag.set(mouseX, mouseY, 0);
}

void mouseDragged() {
  drag.set(mouseX, mouseY, 0);
}

Re: scaling with mouseDrag?
Reply #3 - Dec 7th, 2009, 7:24am
 
One slight improvement: now the image keeps its aspect ratio and rotates with the mouse. See if this is useful.

PVector click, drag;
PImage img1;
void setup() {
 size(640,480);
 click = new PVector(0, 0);
 drag = new PVector(0, 0);
 img1=loadImage("test.jpg");
 imageMode(CENTER);
 rectMode(CENTER);
}

void draw() {
 background(255);
 float w, h, imgDia, drawDia, imgAng, drawAng;
 imgDia=sqrt(pow(img1.width,2)+pow(img1.height,2));
 drawDia=sqrt(pow(drag.x-click.x,2)+pow(drag.y-click.y,2));
 imgAng=atan((float)img1.height/(float)img1.width);
 drawAng=((drag.x-click.x>0)?atan((drag.y-click.y)/(drag.x-click.x)):PI+atan((dr
ag.y-click.y)/(drag.x-click.x)))-(imgAng);
 println(imgAng*57.1+" "+drawAng*57.1);
 w=img1.width*drawDia/imgDia;
 h=img1.height*drawDia/imgDia;
 pushMatrix();
 translate((drag.x+click.x)/2, (drag.y+click.y)/2);
 rotate(drawAng);
 image(img1, 0, 0, w, h);
 noFill();
 stroke(255,0,0);
 rect(0, 0, w, h);
 popMatrix();
}

void mousePressed() {
 click.set(mouseX, mouseY, 0);
 drag.set(mouseX, mouseY, 0);
}

void mouseDragged() {
 drag.set(mouseX, mouseY, 0);
}
Re: scaling with mouseDrag?
Reply #4 - Dec 8th, 2009, 10:26am
 
Thanks to all. THis is the best forum ever! Smiley
Page Index Toggle Pages: 1