'Let points move away a certain amount from cursor
I want to make an SVG animation with a grid of points, which move away a certain amount from their origin, away from the cursor when they are near the cursor, but return to their origin when they are further away from the cursor. I currently update the position using this:
const v = 0.5, moveradius = 80;
points.forEach(function(p){
let dx = 0, dy = 0;
if(getDistance(mx, my, p.position.x, p.position.y) <= moveradius && getDistance(p.origin.x, p.origin.y, p.position.x, p.position.y) <= 30){
dx += p.position.x - mx
dy += p.position.y - my
}else{
dx += p.origin.x - p.position.x
dy += p.origin.y - p.position.y
}
p.position.x += dx*v;
p.position.y += dy*v;
})
function getDistance(x1, y1, x2, y2){
x_n = x2 - x1
y_n = y2 - y1
dist = Math.sqrt((x_n)**2+ (y_n)**2)
return dist;
}
where mx and my are the mouse coordinates. Unfortunately, the points wiggle when they are near their maximum move radius (30). How can I avoid the wiggling?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
