# Slowing down mouse

Hello,
I wanted to make an ellipse that would follow the mouse but slowed down. Can anyone tell me why my code doesn’t work?

``````float x, y;
float speed = 7;

void setup() {
size(1000,1000);
smooth();
frameRate(30);
}

void draw() {
background(255);
float distance = dist(x, y, mouseX, mouseY);
float speedx = (mouseX - x) / distance * speed;
float speedy = (mouseY - y) / distance * speed;
x += speedx;
y += speedy;
fill(0);
ellipse(x, y, 20, 20);
}
``````
2 Likes

Remove that ` / distance` and set `speed` to some value between `0 and 1` (0.2 for example). Then your code should work perfectly!

1 Like

Another option: it is common to use linear interpolation – lerp – to move a value a percentage of the way towards another, as with a following / chasing object.

`x = lerp(x, mouseX, 0.25);`
`y = lerp(y, mouseY, 0.25);`

https://processing.org/reference/lerp_.html

or

``````PVector pv;
// ...
pv.lerp(new PVector(mouseX,mouseY), 0.25);
ellipse(pv.x, pv.y, 20, 20);
``````

https://processing.org/reference/PVector_lerp_.html

2 Likes