You want the element to move in the direction of the mouse movement rather than following the mouse position directly. Here's how you can achieve this:
javascript
let lastMouseX = window.innerWidth / 2;
let lastMouseY = window.innerHeight / 2;
document.addEventListener('mousemove', (event) => {
// Calculate the direction of mouse movement
const deltaX = event.pageX - lastMouseX;
const deltaY = event.pageY - lastMouseY;
// Get current position of the element
const currentX = gsap.getProperty('#walkingPeeps', 'x');
const currentY = gsap.getProperty('#walkingPeeps', 'y');
// Apply movement based on direction (you can adjust the multiplier for speed)
gsap.to('#walkingPeeps', {
x: currentX + deltaX * 0.5, // 0.5 is the speed multiplier
y: currentY + deltaY * 0.5,
duration: 0.3,
ease: "power2.out"
});
// Update last mouse position
lastMouseX = event.pageX;
lastMouseY = event.pageY;
});
Alternatively, if you want the element to continuously move in the direction the mouse is pointing relative to the center of the screen:
javascript
document.addEventListener('mousemove', (event) => {
// Calculate direction from center of screen to mouse
const centerX = window.innerWidth / 2;
const centerY = window.innerHeight / 2;
const directionX = event.pageX - centerX;
const directionY = event.pageY - centerY;
// Normalize the direction
const magnitude = Math.sqrt(directionX * directionX + directionY * directionY);
const normalizedX = directionX / magnitude;
const normalizedY = directionY / magnitude;
// Apply continuous movement in that direction
gsap.to('#walkingPeeps', {
x: `+=${normalizedX * 10}`, // 10 is the speed
y: `+=${normalizedY * 10}`,
duration: 0.5,
ease: "none",
overwrite: "auto"
});
});
The first approach tracks mouse movement deltas and moves the element based on how the mouse moves. The second approach makes the element move in the direction from the screen center to the mouse cursor. You can adjust the speed multipliers and duration to get the feel you want.