-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
326 lines (277 loc) · 8.27 KB
/
script.js
File metadata and controls
326 lines (277 loc) · 8.27 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
document.addEventListener("DOMContentLoaded", function () {
const sliderContainer = document.querySelector(".slider");
const indicators = document.querySelectorAll(".index");
const path = document.querySelector("#link-svg");
const line1 = document.querySelector(".line-1");
const line2 = document.querySelector(".line-2");
const link = document.querySelector(".link");
const linkWrapper = document.querySelector(".link-wrapper");
const totalSlides = 5;
const linkUrls = [
"https://www.thounny.com/",
"https://www.youtube.com/channel/UCTPSmsWGmrVXjVXD2DPXoUQ",
"https://www.linkedin.com/in/thounny/",
"https://github.com/thounny",
"https://thounny.beehiiv.com/subscribe",
];
let currentSlideIndex = 1;
let isAnimating = false;
let currentTopValue = 0;
window.addEventListener("wheel", (e) => {
if (isAnimating) return;
if (e.deltaY > 0) showNextSlide();
else if (e.deltaY < 0) showPrevSlide();
});
const slides = document.querySelectorAll(".slide");
slides.forEach((slide, idx) => {
const img = slide.querySelector("img");
if (idx === 0) {
gsap.set(slide, { zIndex: 1 });
gsap.set(img, { scale: 1, top: "0" });
} else {
gsap.set(slide, {
clipPath: "polygon(0 100%, 100% 100%, 100% 100%, 0 100%)",
zIndex: 0,
});
gsap.set(img, { scale: 2, top: "4em" });
}
});
function showNextSlide() {
const currentSlide = document.querySelector(`#slide-${currentSlideIndex}`);
currentSlideIndex =
currentSlideIndex < totalSlides ? currentSlideIndex + 1 : 1; // Reset to 1 after last slide
let nextSlide = document.querySelector(`#slide-${currentSlideIndex}`);
if (!nextSlide) {
nextSlide = createSlide(currentSlideIndex);
sliderContainer.appendChild(nextSlide);
}
animateSlideTransition(currentSlide, nextSlide, "down");
}
function showPrevSlide() {
const currentSlide = document.querySelector(`#slide-${currentSlideIndex}`);
currentSlideIndex =
currentSlideIndex > 1 ? currentSlideIndex - 1 : totalSlides; // Reset to last slide if at start
let prevSlide = document.querySelector(`#slide-${currentSlideIndex}`);
if (!prevSlide) {
prevSlide = createSlide(currentSlideIndex);
sliderContainer.insertBefore(prevSlide, currentSlide);
}
animateSlideTransition(currentSlide, prevSlide, "up");
}
function createSlide(slideNumber) {
const slide = document.createElement("div");
slide.className = "slide";
slide.id = `slide-${slideNumber}`;
const img = document.createElement("img");
img.src = getImageSource(slideNumber);
img.alt = "";
slide.appendChild(img);
gsap.set(slide, {
clipPath: "polygon(0 100%, 100% 100%, 100% 100%, 0 100%)",
zIndex: 0,
});
gsap.set(img, { scale: 2, top: "4em" });
return slide;
}
function animateSlideTransition(currentSlide, nextSlide, direction) {
if (isAnimating) return;
isAnimating = true;
const currentImg = currentSlide.querySelector("img");
const nextImg = nextSlide.querySelector("img");
gsap.set(nextSlide, {
clipPath:
direction === "up"
? "polygon(0 0, 100% 0, 100% 0, 0 0)"
: "polygon(0 100%, 100% 100%, 100% 100%, 0 100%)",
});
gsap.set(nextImg, { scale: 2, top: "4em" });
gsap.set(currentSlide, { zIndex: 1 });
gsap.set(nextSlide, { zIndex: 2 });
updateSlideTitle(
direction === "up" ? currentSlideIndex - 1 : currentSlideIndex
);
updateIndicators(
direction === "up" ? currentSlideIndex - 1 : currentSlideIndex
);
updateLink(direction === "up" ? currentSlideIndex - 1 : currentSlideIndex);
const timeline = gsap.timeline({
onComplete: () => {
gsap.set(currentSlide, { zIndex: 0 });
gsap.set(nextSlide, { zIndex: 1 });
cleanupSlides();
isAnimating = false;
},
});
timeline.add(animateCircle(), 0);
timeline.add(animateText(), 0);
timeline
.to(
currentImg,
{
scale: 2,
top: "4em",
duration: 2,
ease: "power3.inOut",
},
0
)
.to(
nextSlide,
{
clipPath: "polygon(0 0%, 100% 0%, 100% 100%, 0 100%)",
duration: 2,
ease: "power4.inOut",
},
0
)
.to(
nextImg,
{
scale: 1,
top: "0",
duration: 2,
ease: "power3.inOut",
},
0
);
}
function updateSlideTitle(index) {
const displayNumber = normalizeSlideTitle(index);
const multiplier = window.innerWidth < 900 ? 42 : 150;
currentTopValue = -(displayNumber - 1) * multiplier;
gsap.to(document.querySelector(".postfix"), {
y: `${currentTopValue}px`,
duration: 2,
ease: "power4.inOut",
});
}
function cleanupSlides() {
const slides = document.querySelectorAll(".slide");
slides.forEach((slide) => {
const slideNumber = parseInt(slide.id.split("-")[1]);
if (Math.abs(slideNumber - currentSlideIndex) > 2) {
slide.remove();
}
});
}
function normalizeSlideTitle(number) {
let normalized = number;
while (normalized <= 0) normalized += totalSlides;
return ((normalized - 1) % totalSlides) + 1;
}
function getImageSource(slideNumber) {
const source = `./images/img-${normalizeSlideTitle(slideNumber)}.gif`;
console.log("Image source for slide:", slideNumber, source); // Debugging line
return source;
}
function updateIndicators(index) {
const normalizedIndex = normalizeSlideTitle(index);
indicators.forEach((indicator) => {
gsap.to(indicator, {
scaleX: 0.5,
duration: 2,
ease: "power4.inOut",
});
});
gsap.to(indicators[normalizedIndex - 1], {
scaleX: 1,
duration: 2,
ease: "power4.inOut",
});
}
function animateText() {
const tl = gsap.timeline();
const currentLine1 = line1.querySelector("p");
const currentLine2 = line2.querySelector("p");
const { line1Text, line2Text } = createNewText();
tl.to([currentLine1, currentLine2], {
y: -30,
stagger: 0.1,
delay: 0.25,
duration: 1,
ease: "power3.inOut",
onComplete: () => {
currentLine1.remove();
currentLine2.remove();
},
});
line1.appendChild(line1Text);
line2.appendChild(line2Text);
tl.to(
[line1Text, line2Text],
{
y: 0,
stagger: 0.1,
delay: 0.75,
duration: 1,
ease: "power3.inOut",
},
"<"
);
return tl;
}
function createNewText() {
const line1Text = document.createElement("p");
const line2Text = document.createElement("p");
line1Text.textContent = "View";
line2Text.textContent = "Project";
gsap.set([line1Text, line2Text], {
y: 30,
});
return { line1Text, line2Text };
}
function updateLink(index) {
const normalizedIndex = normalizeSlideTitle(index) - 1;
const linkElement = document.querySelector(".link-wrapper a");
linkElement.href = linkUrls[normalizedIndex];
}
const length = path.getTotalLength();
gsap.set(path, {
strokeDasharray: length,
strokeDashoffset: 0,
rotation: -90,
transformOrigin: "center center",
});
function animateCircle() {
const tl = gsap.timeline();
tl.set(path, {
strokeDashoffset: 0,
strokeDasharray: length,
scale: 1,
})
.to(path, {
strokeDashoffset: -length,
duration: 1,
ease: "power2.inOut",
})
.set(path, {
strokeDashoffset: length,
})
.to(path, {
strokeDashoffset: 0,
duration: 1,
ease: "power2.inOut",
});
return tl;
}
let xTo = gsap.quickTo(linkWrapper, "x", { duration: 0.4, ease: "power3" }),
yTo = gsap.quickTo(linkWrapper, "y", { duration: 0.4, ease: "power3" });
link.addEventListener("mousemove", (e) => {
const rect = link.getBoundingClientRect();
const relX = e.clientX - rect.left - rect.width / 2;
const relY = e.clientY - rect.top - rect.height / 2;
xTo(relX * 0.5);
yTo(relY * 0.5);
});
link.addEventListener("mouseleave", () => {
xTo(0);
yTo(0);
});
gsap.set(linkWrapper, {
x: 0,
y: 0,
xPercent: -50,
yPercent: -50,
});
updateLink(1);
});