# Slowing down mouse

**URL:** <https://discourse.processing.org/t/slowing-down-mouse/20357>\
**Category:** Coding Questions\
**Created:** [May 1, 2020, 4:59pm UTC](https://discourse.processing.org/t/slowing-down-mouse/20357 "2020-05-01T16:59:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![14mmss](https://avatars.discourse-cdn.com/v4/letter/1/d07c76/32.png) [@14mmss](https://discourse.processing.org/u/14mmss)\
**Post date:** [May 1, 2020, 4:59pm UTC](https://discourse.processing.org/t/slowing-down-mouse/20357/1 "2020-05-01T16:59:43Z")

</div>

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?

```auto
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);
}

```

---

<div class="post-metadata">

**Author:** ![Schred](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/schred/32/13146_2.png) [@Schred](https://discourse.processing.org/u/Schred)\
**Post date:** [May 1, 2020, 5:29pm UTC](https://discourse.processing.org/t/slowing-down-mouse/20357/2 "2020-05-01T17:29:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [May 2, 2020, 3:34pm UTC](https://discourse.processing.org/t/slowing-down-mouse/20357/3 "2020-05-02T15:34:41Z")

</div>

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](https://processing.org/reference/lerp_.html)

or

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

```

[https://processing.org/reference/PVector\_lerp\_.html](https://processing.org/reference/PVector_lerp_.html)
