-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs.js
109 lines (94 loc) · 3 KB
/
js.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
window.addEventListener("load", () => {
function preloadImage(url) {
var img = new Image();
img.src = url;
}
preloadImage("/cursor/iggyright.png");
preloadImage("/cursor/iggyleft.png");
preloadImage("/cursor/iggyeat.png");
preloadImage("/cursor/iggytongue.png");
});
gsap.set(".follower", { xPercent: -50, yPercent: -50 });
gsap.set(".cursor", { xPercent: -50, yPercent: -50 });
const follow = document.querySelector(".follower");
const cur = document.querySelector(".cursor");
window.addEventListener("mousemove", (e) => {
gsap.to(cur, 0.2, { x: e.clientX, y: e.clientY });
gsap.to(follow, 0.9, { x: e.clientX, y: e.clientY });
});
gsap.set(".follower", { xPercent: -50, yPercent: -50 });
gsap.set(".cursor", { xPercent: -50, yPercent: -50 });
function getPositionAtCenter(element) {
const { top, left, width, height } = element.getBoundingClientRect();
return {
x: left + width / 2,
y: top + height / 2,
};
}
function getDistanceBetweenElements(a, b) {
const aPosition = getPositionAtCenter(a);
const bPosition = getPositionAtCenter(b);
return { a: aPosition.x, b: bPosition.x, c: aPosition.y, d: bPosition.y };
}
const distance = getDistanceBetweenElements(follow, cur);
window.addEventListener("mousemove", (event) => {
cur.style.opacity = "1";
const dist = getDistanceBetweenElements(cur, follow);
const isOverlapping = dist.a == dist.b || dist.c == dist.d;
let diff = Math.abs(dist.a - dist.b);
let diff2 = Math.abs(dist.c - dist.d);
if (dist.a > dist.b) {
follow.style.backgroundImage = "url(/cursor/iggyright.png)";
} else if (dist.a < dist.b) {
follow.style.backgroundImage = "url(/cursor/iggyleft.png)";
}
});
// eat
(function (mouseStopDelay) {
let timeout;
document.addEventListener("mousemove", function (e) {
clearTimeout(timeout);
timeout = setTimeout(function () {
let event = new CustomEvent("mousestop", {
detail: {
clientX: e.clientX,
clientY: e.clientY,
},
bubbles: true,
cancelable: true,
});
e.target.dispatchEvent(event);
}, mouseStopDelay);
});
})(1000);
window.addEventListener("mousestop", (event) => {
const dist = getDistanceBetweenElements(cur, follow);
const isOverlapping = dist.a == dist.b;
let diff = Math.abs(dist.a - dist.b);
let diff2 = Math.abs(dist.c - dist.d);
if (isOverlapping) {
follow.style.backgroundImage = "url(/cursor/iggyeat.png)";
cur.style.opacity = "0";
}
});
// tongue
(function (mouseStopDelay2) {
let timeout;
document.addEventListener("mousemove", function (e) {
clearTimeout(timeout);
timeout = setTimeout(function () {
let event = new CustomEvent("mousestop2", {
detail: {
clientX: e.clientX,
clientY: e.clientY,
},
bubbles: true,
cancelable: true,
});
e.target.dispatchEvent(event);
}, mouseStopDelay2);
});
})(400);
window.addEventListener("mousestop2", (event) => {
follow.style.backgroundImage = "url(/cursor/iggytongue.png)";
});