By console logging hover() and inRange() of just one tomato, I could see that flickering happens when inRange() is flipping between true and false. So what happens is:
<aside>
in range —> follow mouse —> just out of range —> bounce back —> in range again
</aside>
I still don’t know why that happens. To see it more clearly, I drew just one tomato, and overlaid the range on top of it.
It is stable inside the range, and flickers the moment it leaves the rectangle, until it reaches another “boundary” and bounces back. What is this boundary?
I then printed the distance between x and original x on screen, to see where is the furtherest point it could get to.
Oddly enough, even when my mouse is way beyond the range, the distance value is rarely over 80.
Theoretically, when it goes out of range by 1 pixel, inRange() becomes false; it should bounce back. And when it bounces back by just 1 pixel, it… follows mouse… because now it’s in range and mouse is still hovering on it.
If I make the range larger than the size of the tomato, would it make hover() false when inRange() is false? Let’s test it out.
I realized three things: