Certainly! Below is a JavaScript snippet that creates a mouse cursor trail effect. You can paste this code directly into your browser's developer console, and it will create a visually appealing trail of circles that follow your mouse cursor.
(function () {
// Create a container for the trail elements
const trailContainer = document.createElement('div');
trailContainer.style.position = 'absolute';
trailContainer.style.top = '0';
trailContainer.style.left = '0';
trailContainer.style.pointerEvents = 'none';
trailContainer.style.zIndex = '9999';
document.body.appendChild(trailContainer);
// Configuration for the trail
const numberOfCircles = 10; // Number of trailing circles
const circleSize = 20; // Base size of each circle
const opacityDecay = 0.8; // Opacity decay rate
const sizeDecay = 0.8; // Size decay rate
// Create the trail elements
const circles = [];
for (let i = 0; i < numberOfCircles; i++) {
const circle = document.createElement('div');
circle.style.position = 'absolute';
circle.style.width = `${circleSize}px`;
circle.style.height = `${circleSize}px`;
circle.style.borderRadius = '50%';
circle.style.background = `rgba(255, 255, 255, ${1 - i / numberOfCircles})`;
circle.style.pointerEvents = 'none';
circle.style.transform = 'translate(-50%, -50%)';
trailContainer.appendChild(circle);
circles.push(circle);
}
// Track the mouse position
let mouseX = 0;
let mouseY = 0;
document.addEventListener('mousemove', (event) => {
mouseX = event.clientX;
mouseY = event.clientY;
});
// Animation loop
function animate() {
let lastX = mouseX;
let lastY = mouseY;
circles.forEach((circle, index) => {
const prevCircle = circles[index - 1] || { style: { left: `${lastX}px`, top: `${lastY}px` } };
const prevX = parseFloat(prevCircle.style.left);
const prevY = parseFloat(prevCircle.style.top);
const newX = prevX + (lastX - prevX) * 0.3;
const newY = prevY + (lastY - prevY) * 0.3;
circle.style.left = `${newX}px`;
circle.style.top = `${newY}px`;
lastX = newX;
lastY = newY;
// Adjust size and opacity
const scale = Math.pow(sizeDecay, index);
circle.style.transform = `translate(-50%, -50%) scale(${scale})`;
circle.style.opacity = `${1 - index / numberOfCircles}`;
});
requestAnimationFrame(animate);
}
// Start the animation
animate();
})();
How It Works:
- Trail Elements: The script dynamically creates a series of
div elements styled as circles. These circles form the trail.
- Mouse Tracking: The
mousemove event listener tracks the current position of the mouse.
- Animation Loop: The
animate function continuously updates the position, size, and opacity of the circles to create the trailing effect.
- Customization: You can adjust variables like
numberOfCircles, circleSize, opacityDecay, and sizeDecay to customize the appearance of the trail.
How to Use:
- Open your browser's developer console (
F12 or Ctrl+Shift+I).
- Paste the code into the console and press
Enter.
- Move your mouse around the page to see the cursor trail effect.
Let me know if you need further customization!