Skip to content
*
* Tuning options are available in the CONFIG object below.
*/
(() => {
"use strict";
const CONFIG = {
particleEveryMs: 42, // Lower = more leaves
maxParticles: 34, // Maximum leaves alive at once
minSize: 10,
maxSize: 23,
lifeMs: 1050, // How long each leaf floats
followEase: 0.19, // Cursor smoothing: 0.1–0.3 feels soft
drift: 34, // Sideways/upward drift distance
enabledOnTouch: false
};
const reducedMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
const finePointer = window.matchMedia("(hover: hover) and (pointer: fine)").matches;
if (reducedMotion || (!finePointer && !CONFIG.enabledOnTouch)) return;
const layer = document.getElementById("leaf-cursor-layer") || createLayer();
const cursor = document.getElementById("leaf-cursor") || createCursor(layer);
document.documentElement.classList.add("leaf-cursor-enabled");
const state = {
x: -100, y: -100, targetX: -100, targetY: -100,
lastParticleAt: 0, active: false, raf: 0, particles: []
};
const leafSvg = `
`;
function createLayer() {
const el = document.createElement("div");
el.id = "leaf-cursor-layer";
el.setAttribute("aria-hidden", "true");
document.body.appendChild(el);
return el;
}
function createCursor(parent) {
const el = document.createElement("div");
el.id = "leaf-cursor";
el.className = "leaf-cursor";
el.innerHTML = leafSvg;
parent.appendChild(el);
return el;
}
function random(min, max) { return Math.random() * (max - min) + min; }
function spawnLeaf(x, y, now) {
if (state.particles.length >= CONFIG.maxParticles) return;
const el = document.createElement("div");
el.className = "leaf-particle";
const size = random(CONFIG.minSize, CONFIG.maxSize);
const angle = random(-Math.PI * 0.9, -Math.PI * 0.1);
const distance = random(CONFIG.drift * 0.45, CONFIG.drift);
const p = {
el, x: x + random(-5, 5), y: y + random(-5, 5),
dx: Math.cos(angle) * distance + random(-12, 12),
dy: Math.sin(angle) * distance - random(4, 18),
rotation: random(-150, 150),
spin: random(-150, 150),
born: now, life: random(CONFIG.lifeMs * .75, CONFIG.lifeMs * 1.25)
};
el.style.setProperty("--size", `${size}px`);
el.style.setProperty("--leaf-color", ["#68b957", "#8bcf68", "#4e9e50", "#a5d879"][Math.floor(random(0, 4))]);
el.innerHTML = leafSvg;
layer.appendChild(el);
state.particles.push(p);
}
function onPointerMove(event) {
if (event.pointerType === "touch" && !CONFIG.enabledOnTouch) return;
state.targetX = event.clientX;
state.targetY = event.clientY;
state.active = true;
cursor.classList.add("is-visible");
if (!state.raf) state.raf = requestAnimationFrame(animate);
}
function onPointerLeave() {
state.active = false;
cursor.classList.remove("is-visible");
}
function animate(now) {
state.raf = 0;
state.x += (state.targetX - state.x) * CONFIG.followEase;
state.y += (state.targetY - state.y) * CONFIG.followEase;
const dx = state.targetX - state.x;
const dy = state.targetY - state.y;
const tilt = Math.max(-35, Math.min(35, dx * 1.8));
cursor.style.transform = `translate3d(${state.x - 15}px, ${state.y - 15}px, 0) rotate(${tilt - 25}deg)`;
if (state.active && now - state.lastParticleAt > CONFIG.particleEveryMs) {
spawnLeaf(state.x, state.y, now);
state.lastParticleAt = now;
}
state.particles = state.particles.filter((p) => {
const age = now - p.born;
const progress = Math.min(age / p.life, 1);
if (progress >= 1) {
p.el.remove();
return false;
}
const ease = 1 - Math.pow(1 - progress, 2);
const floatX = p.x + p.dx * ease + Math.sin(progress * Math.PI * 2) * 5;
const floatY = p.y + p.dy * ease + progress * 18;
const opacity = Math.sin(progress * Math.PI) * .9;
const scale = .65 + Math.sin(progress * Math.PI) * .35;
p.el.style.opacity = opacity.toFixed(3);
p.el.style.transform = `translate3d(${floatX}px, ${floatY}px, 0) rotate(${p.rotation + p.spin * progress}deg) scale(${scale})`;
return true;
});
if (state.active || state.particles.length) {
state.raf = requestAnimationFrame(animate);
}
}
window.addEventListener("pointermove", onPointerMove, { passive: true });
document.documentElement.addEventListener("pointerleave", onPointerLeave);
window.addEventListener("blur", onPointerLeave);
window.addEventListener("focus", () => {
if (state.active && !state.raf) state.raf = requestAnimationFrame(animate);
});
// Remove the custom cursor effect cleanly when the page is hidden.
document.addEventListener("visibilitychange", () => {
if (document.hidden) {
state.active = false;
cursor.classList.remove("is-visible");
if (state.raf) cancelAnimationFrame(state.raf);
state.raf = 0;
}
});
})();