-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
73 lines (70 loc) · 2.18 KB
/
script.js
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const cursor = document.querySelector('#cursor');
const stickyElement = document.querySelector('#stickyElement');
const lerp = (x, y, a) => x * (1 - a) + y * a;
const windowMouseMove = (e) => {
gsap.to(cursor, {
x: e.clientX,
y: e.clientY,
ease: 'expo',
duration: 0.5
});
};
window.addEventListener('mousemove', windowMouseMove)
const rotate = (e) => {
const angle = Math.atan2(e.y, e.x)
gsap.to(cursor, {
rotate: `${angle}rad`,
duration: 0
});
}
stickyElement.addEventListener('mousemove', (dets) => {
window.removeEventListener('mousemove', windowMouseMove);
const stickyElemDims = stickyElement.getBoundingClientRect();
const xMap = gsap.utils.mapRange(stickyElemDims.left, stickyElemDims.width + stickyElemDims.left, 0, 1, dets.clientX);
const yMap = gsap.utils.mapRange(stickyElemDims.top, stickyElemDims.height + stickyElemDims.top, 0, 1, dets.clientY);
const center = {
x: stickyElemDims.left + stickyElemDims.width / 2,
y: stickyElemDims.top + stickyElemDims.height / 2
};
const cursorDims = {
height: cursor.offsetHeight,
width: cursor.offsetWidth
}
const distance = {
x: dets.clientX - center.x,
y: dets.clientY - center.y
}
rotate(distance)
const absDistance = Math.max(Math.abs(distance.x), Math.abs(distance.y))
const xMapRange = gsap.utils.mapRange(0, cursorDims.width / 2, 1, 1.1, absDistance)
const yMapRange = gsap.utils.mapRange(0, cursorDims.height / 2, 1, .9, absDistance)
gsap.to(cursor, {
x: center.x,
y: center.y,
scaleX: xMapRange,
scaleY: yMapRange,
height: "80px",
width: "80px",
ease: 'expo',
duration: 0.5
});
gsap.to(stickyElement, {
x: lerp(-50, 50, xMap),
y: lerp(-50, 50, yMap),
});
});
stickyElement.addEventListener('mouseleave', () => {
window.addEventListener('mousemove', windowMouseMove);
gsap.to(cursor, {
scaleX: 1,
scaleY: 1,
height: "20px",
width: "20px",
ease: 'expo',
duration: 0.5
});
gsap.to(stickyElement, {
x: 0,
y: 0,
});
});