93 lines
2.9 KiB
JavaScript
93 lines
2.9 KiB
JavaScript
/* Spotlight cursor from Cursify.
|
|
https://cursify.ui-layouts.com/components/spotlight-cursor */
|
|
(function () {
|
|
if (window.matchMedia('(prefers-reduced-motion: reduce)').matches) {
|
|
return;
|
|
}
|
|
|
|
var spotlightSize = 250;
|
|
var fadeSpeed = 0.95;
|
|
var pulseSpeed = 3200;
|
|
|
|
var root = document.createElement('div');
|
|
root.className = 'spotlight-cursor';
|
|
root.setAttribute('aria-hidden', 'true');
|
|
|
|
var canvas = document.createElement('canvas');
|
|
root.appendChild(canvas);
|
|
document.body.appendChild(root);
|
|
|
|
var ctx = canvas.getContext('2d');
|
|
var spotlightPos = { x: window.innerWidth / 2, y: window.innerHeight / 2 };
|
|
var targetPos = { x: spotlightPos.x, y: spotlightPos.y };
|
|
var lastVisibilityCheck = 0;
|
|
|
|
function resizeCanvas() {
|
|
canvas.width = window.innerWidth;
|
|
canvas.height = window.innerHeight;
|
|
}
|
|
|
|
function lerp(start, end, factor) {
|
|
return start + (end - start) * factor;
|
|
}
|
|
|
|
function handleMouseMove(event) {
|
|
targetPos.x = event.clientX;
|
|
targetPos.y = event.clientY;
|
|
updateSpotlightVisibility(event.target);
|
|
}
|
|
|
|
function updateSpotlightVisibility(target) {
|
|
var excludedSelector = '#tecnologias, .consulting-why, .bg-white, [data-spotlight-exclude]';
|
|
var isOverExcludedSection = target instanceof Element && Boolean(target.closest(excludedSelector));
|
|
|
|
root.classList.toggle('is-hidden', isOverExcludedSection);
|
|
}
|
|
|
|
function updateSpotlightVisibilityAtCursor() {
|
|
updateSpotlightVisibility(document.elementFromPoint(targetPos.x, targetPos.y));
|
|
}
|
|
|
|
function render(timestamp) {
|
|
if (timestamp - lastVisibilityCheck >= 120) {
|
|
updateSpotlightVisibilityAtCursor();
|
|
lastVisibilityCheck = timestamp;
|
|
}
|
|
|
|
spotlightPos.x = lerp(spotlightPos.x, targetPos.x, fadeSpeed);
|
|
spotlightPos.y = lerp(spotlightPos.y, targetPos.y, fadeSpeed);
|
|
|
|
ctx.globalCompositeOperation = 'source-over';
|
|
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
|
|
var pulseScale = 1 + 0.04 * Math.sin((Date.now() / pulseSpeed) * Math.PI * 2);
|
|
var currentSpotlightSize = spotlightSize * pulseScale;
|
|
var glowGradient = ctx.createRadialGradient(
|
|
spotlightPos.x,
|
|
spotlightPos.y,
|
|
0,
|
|
spotlightPos.x,
|
|
spotlightPos.y,
|
|
currentSpotlightSize * 1.25
|
|
);
|
|
|
|
glowGradient.addColorStop(0, 'rgba(255, 205, 17, 0.62)');
|
|
glowGradient.addColorStop(0.24, 'rgba(255, 198, 0, 0.5)');
|
|
glowGradient.addColorStop(0.5, 'rgba(220, 163, 0, 0.34)');
|
|
glowGradient.addColorStop(0.74, 'rgba(154, 113, 0, 0.17)');
|
|
glowGradient.addColorStop(1, 'rgba(110, 80, 0, 0)');
|
|
ctx.fillStyle = glowGradient;
|
|
ctx.beginPath();
|
|
ctx.arc(spotlightPos.x, spotlightPos.y, currentSpotlightSize * 1.25, 0, Math.PI * 2);
|
|
ctx.fill();
|
|
|
|
window.requestAnimationFrame(render);
|
|
}
|
|
|
|
resizeCanvas();
|
|
window.addEventListener('resize', resizeCanvas);
|
|
window.addEventListener('scroll', updateSpotlightVisibilityAtCursor, { passive: true });
|
|
document.addEventListener('mousemove', handleMouseMove);
|
|
window.requestAnimationFrame(render);
|
|
}());
|