We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello. I've created an easing function that allows mouse cursor to behave elastically. But the problem is, I can't get anything drawn on the canvas. The elastic string line (green) should appear when mouse is clicked and should disappear once mouse is released; but to achieve that I set 'background' to a certain color, which doesn't allow me to draw (draw through continuous lines like this) on the canvas. All I want is to draw shapes not with my mouse coordinates but with the scattering point of the line (the orange dot), which will make the strokes less jagged. Here's the code I'm working with:
Spring2D s1;
float gravity = 0;
float mass = 4.0;
void setup() {
frameRate(60);
size(640, 360);
fill(255, 126);
// Inputs: x, y, mass, gravity
s1 = new Spring2D(0.0, width/2, mass, gravity);
}
void draw() {
s1.display(mouseX, mouseY);
s1.update(mouseX, mouseY);
}
class Spring2D {
float vx, vy; // The x- and y-axis velocities
float x, y; // The x- and y-coordinates
float gravity;
float mass;
float radius = 10;
float stiffness = 0.7;
float damping = 0.5;
Spring2D(float xpos, float ypos, float m, float g) {
x = xpos;
y = ypos;
mass = m;
gravity = g;
}
void update(float targetX, float targetY) {
float forceX = (targetX - x) * stiffness;
float ax = forceX / mass;
vx = damping * (vx + ax);
x += vx;
float forceY = (targetY - y) * stiffness;
forceY += gravity;
float ay = forceY / mass;
vy = damping * (vy + ay);
y += vy;
}
void display(float nx, float ny) {
if (mousePressed == true) {
background(0);
stroke(40, 255, 150);
line(x, y, nx, ny);
noStroke();
fill(255, 130, 40);
ellipse(x, y, 5, 5);
} else {
background(0);
}
}
}
Thanks.
Answers
this is surely not ideal, but it is an idea, maybe it could lead to something better. You can erase the old green lines by drawing over them with a black line. The problem is that the black lines will be drawn over parts of the orange line, especially when the mouse is moving quickly.
Yeah, thanks. If black lines wasn't drawn on the orange one, it would be perfect. ^^
Is it that hard guys?
Solved?
No Chrisir, still looking for the solution.
Oh Chrisir, that's awesome, exactly what I want. Thank you so much.
The only little thing is that when mouse is released and and clicked somewhere else, it connects the line. And likewise; the first stroke, when mouse is clicked on a point and stay still it draws a short line towards the mouse cursor. ^^ Can you fix that too?
I can't . I don't see how
Thanks :)
Just kidding
Its dead easy
I just don't have time
Ah ok, then I'll play a bit with it. But still open to your edit ^^
first in line 90 check if one of the values is different from the previous
line 20&21 at end of
draw()
betterfor the line - that's true. you could
make an ArrayList
segments
of our current ArrayList with PVector (solution A) ORmake a class with PVector & a flag hasNextLine or so (solution B)
following solution B above :
Have you tried this code? This weirdly draws by mouse co-ordinates and not by the orange line.
Ah, ok
Line 117
replace nx, ny
with x,y
Aww marry me!