We are about to switch to a new forum software. Until then we have removed the registration on this forum.
My image just keeps snapping back to its original position after I drag it a little. How can I be able to drag the image freely around the canvas?
float[] sx = new float[100];
float[] sy = new float[100];
float[] speed = new float[100];
PImage[] img = new PImage[10];
int imgIndex = 0;
float x = 0;
float y = 0;
float sz = 100;
void setup() {
size(500, 500);
background(0, 60, 150);
frameRate(25);
// Load images here
for ( int i = 0; i< img.length; i++ ) {
img[i] = loadImage( i + ".jpg" );
}
//snow
for (int i = 0; i < 100; i++) {
sx[i] = random(0, width);
sy[i] = random(0, height);
speed[i] = random(1, 5);
}
}
//end snow
void draw() {
background(0, 60, 150);
gradient();
//snow
fill(255);
for (int i = 0; i < 100; i++) {
ellipse(sx[i], sy[i], speed[i], speed[i]);
sy[i] = sy[i] + speed[i];
if (sy[i] > height) {
sy[i] = 0;
}
}
//end snow
monument(img[imgIndex], x, y, 200);
}
void gradient() {
int g = 0;
noStroke();
while (g<100) {
fill(g*2.55, 100);
rect(0, g*5, height, 5);
g++;
}
}
void monument(PImage pimg, float x, float y, float sz) {
if (dist(x, y, mouseX, mouseY) < sz) {
if (mousePressed) {
cursor(MOVE);
x = lerp(x, mouseX, 0.2);
y = lerp(y, mouseY, 0.2);
}
else {
cursor(HAND);
}
}
else {
cursor(ARROW);
}
image(pimg, x, y, sz, sz);
print(x);
}
Answers
Edit your post. Select your code. Hit Ctrl + k or click the C button in the formatting options.
Why do you use lerp()? Just put the shape where mouseX / mouseY are, once you found out you are dragging.
There is a Processing example showing how to handle drag, although it uses classes (might be a good idea, anyway), but you can get some ideas.
Can this code be simplified? Like by using array? If so how? float[] sx = new float[100]; float[] sy = new float[100]; float[] speed = new float[100];
Instead of
PImage img1, img2, img3, img4, img5;
, use something like this:Also, take a look at the tutorial below: :(|)
http://wiki.processing.org/w/From_several_variables_to_arrays