Files
2026-11-02 10:26:32 -06:00

75 lines
2.0 KiB
JavaScript

import { animate } from '../../assets/vendor/animejs/modules/animation/index.js';
import { remove, set } from '../../assets/vendor/animejs/modules/utils/index.js';
const cards = document.querySelectorAll('#tecnologias .platform-marquee__card');
const canTilt = window.matchMedia('(hover: hover) and (pointer: fine)').matches;
const reduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
if (cards.length && canTilt && !reduceMotion) {
cards.forEach((card) => {
const maxRotation = 7;
let bounds = null;
let frame = 0;
let pointerX = 0;
let pointerY = 0;
set(card, { rotateX: 0, rotateY: 0, scale: 1 });
card.classList.add('platform-card-tilt-ready');
const renderTilt = () => {
frame = 0;
if (!bounds) {
return;
}
const normalizedX = Math.max(-1, Math.min(1, ((pointerX - bounds.left) / bounds.width) * 2 - 1));
const normalizedY = Math.max(-1, Math.min(1, ((pointerY - bounds.top) / bounds.height) * 2 - 1));
remove(card);
animate(card, {
rotateX: normalizedY * -maxRotation,
rotateY: normalizedX * maxRotation,
scale: 1.055,
duration: 320,
ease: 'out(3)',
});
};
card.addEventListener('pointerenter', (event) => {
bounds = card.getBoundingClientRect();
pointerX = event.clientX;
pointerY = event.clientY;
card.classList.add('is-platform-tilting');
renderTilt();
});
card.addEventListener('pointermove', (event) => {
pointerX = event.clientX;
pointerY = event.clientY;
if (!frame) {
frame = window.requestAnimationFrame(renderTilt);
}
});
card.addEventListener('pointerleave', () => {
if (frame) {
window.cancelAnimationFrame(frame);
frame = 0;
}
bounds = null;
card.classList.remove('is-platform-tilting');
remove(card);
animate(card, {
rotateX: 0,
rotateY: 0,
scale: 1,
duration: 620,
ease: 'out(4)',
});
});
});
}