-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteractive-star.js
More file actions
43 lines (35 loc) · 1.07 KB
/
interactive-star.js
File metadata and controls
43 lines (35 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
document.addEventListener('DOMContentLoaded', () => {
const star = document.getElementById('interactive-star');
const starContainer = star.closest('.star-container');
function rotateStar(e) {
const rect = starContainer.getBoundingClientRect();
const centerX = rect.left + rect.width / 2;
const centerY = rect.top + rect.height / 2;
const mouseX = e.clientX;
const mouseY = e.clientY;
const angleX = (mouseY - centerY) / 50;
const angleY = -(mouseX - centerX) / 50;
gsap.to(star, {
rotationX: angleX,
rotationY: angleY,
ease: 'power1.out',
duration: 0.5
});
}
function resetStarRotation() {
gsap.to(star, {
rotationX: 0,
rotationY: 0,
ease: 'power1.out',
duration: 0.5
});
}
window.addEventListener('mousemove', rotateStar);
window.addEventListener('mouseleave', resetStarRotation);
// Mobile touch events
window.addEventListener('touchmove', (e) => {
const touch = e.touches[0];
rotateStar(touch);
});
window.addEventListener('touchend', resetStarRotation);
});