Ishaant Nandu

How to make a cool cursor

Some of my friends were surprised to see my website having a special cursor. Basically, it is just a trail of dots moving to my cursor. I originally saw it on the Rubix Records website, but now they have removed it. So here is the code/demo for it! Also check out this website for more playful effects.
Note- this is my first time using Codepen. Now I will try to use codepen more often.

Code explanation.

HTML

Let us start with the HTML, which is just 60 <div class="circle"></div> You can increase or decrease the number of <div class="circle"></div> for different amounts of reactivity and sizing. If you are working in VS code, Zed (My favourite) or WebStorm or any modern editor including NVIM (Sorry to the folks who use Notepad++), then you can just type div.circle*60 and then press .
Just check out this.

CSS

The css is

.circle {
    height: 24px;
    width: 24px;
    border-radius: 50%;
    background-color: black;
    position: fixed;
    pointer-events: none;
    z-index: 9999;
}

So every .circle has some properties that can be changed and some which are necessary. The necessary properties are position: fixed; pointer-events: none; z-index: 9999;. The position makes sure that it can move. Setting the pointer-events to none makes sure that the balls do not react to selecting text. The Z-index makes sure that the balls aren’t hidden. The properties that can be changed are height:24px; width:24px; border-radius:50%; background-color:black;. The height and width change the size of the balls. The background color obviously changes the color of the balls. If the background is set to hsl(calc((sibling-index() - 1) * 6), 80%, 55%);, then each ball will be a color of a rainbow. But that creates another problem. The tiny balls are the ones ahead, instead of being behind. That can be fixed by changing the Z-index to calc(10000 - sibling-index()). Having a border radius makes it so that you can make many shapes. You can use a Border radius generator, but with some tweaking with a ::before and a ::after we can get a heart.

If we remove the content:"";, then we can get a diamond. And also by changing the -1 in the hsl(calc((sibling-index() - 1) * 6), 80%, 55%); to 512, 256, 384 or any other power of 2, we can offset the hue color.

JS

The JavaScript is this-

const coords = { x: 0, y: 0 };
const circles = document.querySelectorAll(".circle");

circles.forEach(function (circle, index) {
    circle.x = 0;
    circle.y = 0;
});

window.addEventListener("mousemove", function (e) {
    coords.x = e.clientX;
    coords.y = e.clientY;
});

function animateCircles() {
    let x = coords.x;
    let y = coords.y;

    circles.forEach(function (circle, index) {
        circle.style.left = x - 10 + "px";
        circle.style.top = y - 10 + "px";

        // Use transform for scaling
        circle.style.transform = `scale(${(circles.length - index) / circles.length * 2})`;

        circle.x = x;
        circle.y = y;
        let speed =0.1;
        const nextCircle = circles[index + 1] || circles[0];
        x += (nextCircle.x - x) * speed;
        y += (nextCircle.y - y) * speed;
    });

    requestAnimationFrame(animateCircles);
}

animateCircles();

Now, by changing the circle.style.top = y - 10 + "px"; and the circle.style.left = x - 10 + "px"; we can offset the circles from the cursor. By changing let speed =0.5 from values 0.1 to 0.999, we can change the speed at which the balls react. At let speed = 1, the balls are unreactive.
Note- Speed 0.9 and speed 0.99 are very different. In speed 0.9 the balls are still slow but are usable. At speed 0.99 every single ball will take a second to reach your cursor.

Thank you! I hope you enjoyed this blog!