-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
140 lines (122 loc) · 3.45 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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
const overlay = document.querySelector(".overlay");
const on = document.querySelector(".on");
const off = document.querySelector(".off");
const icon = document.querySelector(".icon");
const timeline = gsap.timeline();
let pulseTimeout;
lucide.createIcons();
overlay.addEventListener("mousemove", pointerMove);
overlay.addEventListener("touchmove", pointerMove);
const duration = 0.75;
const ease = "slow(0.2, 0.2, false)";
function initialAnimation() {
timeline.to(".overlay", {
"--mouse-x": "33%",
"--mouse-y": "33%",
duration,
ease: ease,
});
timeline.to(".overlay", {
"--mouse-x": "75%",
"--mouse-y": "50%",
duration,
ease: ease,
});
timeline.to(".overlay", {
"--mouse-x": "33%",
"--mouse-y": "75%",
duration,
ease: ease,
});
}
initialAnimation();
function pulse() {
timeline.to(".overlay", {
"--spotlight-width": "6em",
"--spotlight-height": "6em",
duration: 0.25,
ease: ease,
});
timeline.to(".overlay", {
"--spotlight-width": "5em",
"--spotlight-height": "5em",
duration: 0.25,
ease: ease,
});
timeline.to(".overlay", {
"--spotlight-width": "6em",
"--spotlight-height": "6em",
duration: 0.25,
ease: ease,
});
timeline.to(".overlay", {
"--spotlight-width": "5em",
"--spotlight-height": "5em",
duration: 0.25,
ease: ease,
});
}
pulse();
const overlayRect = overlay.getBoundingClientRect();
let userInteraction;
function pointerMove(e) {
e.preventDefault();
clearInterval(userInteraction);
let x, y;
if (e.touches && e.touches.length > 0) {
// For mobile devices, use touch event coordinates
x = e.touches[0].clientX;
y = e.touches[0].clientY;
} else {
// For desktop, use mouse event coordinates
x = e.offsetX;
y = e.offsetY;
}
const edges = gsap.timeline();
//const edge = 4
if (x <= 4 || y <= 100 || x >= overlayRect.width - 4 || y >= overlayRect.height - 4) {
edges.to(".overlay", {
"--spotlight-width": 0,
"--spotlight-height": 0,
duration: 0.25,
ease: "low(0.5, 0.5, false)",
});
} else {
edges.to(".overlay", {
"--spotlight-width": "5em",
"--spotlight-height": "5em",
duration: 0.25,
ease: ease,
});
}
overlay.style.setProperty("--mouse-x", `${x}px`); //setting the x property to the mouse position
overlay.style.setProperty("--mouse-y", `${y - 80}px`);
userInteraction = setInterval(() => {
clearInterval(userInteraction);
pulse();
}, 3000); // start a timer for 3 seconds
}
let isMoved = false;
on.addEventListener("click", () => {
if (isMoved === true) {
overlay.style.display = "inherit"; // Show overlay
on.style.marginLeft = 0; // Move left
on.innerHTML = `<i class="icon" data-lucide="flashlight"></i>`;
} else {
overlay.style.display = "none"; // Hide overlay
on.style.marginLeft = "55%";
/* Adjust margin based on viewport width
if (window.innerWidth > 768) {
// For viewport width larger than 768px, move the icon to the right by 3.2em
on.style.marginLeft = "calc(100% - 3.2em)";
} else {
// For viewport width smaller than or equal to 768px, move the icon to the right by 2em
on.style.marginLeft = "calc(100% - 4em)";
}*/
//on.style.marginLeft = "calc(100% - 3.2em)"; // Move right
on.innerHTML = `<i class="icon" data-lucide="flashlight-off"></i>`;
}
isMoved = !isMoved;
lucide.createIcons();
});
//lucide icon goes too far to the right on mobile