-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathindex.js
464 lines (431 loc) · 12.4 KB
/
index.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
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
import { preloadImages } from './utils.js';
// Function to animate the header (frame)
const animateFrame = () => {
const frame = document.querySelector('.frame');
const frameTitle = frame.querySelector('.frame__title');
gsap.timeline({
defaults: {
ease: 'none'
},
scrollTrigger: {
trigger: frame,
start: 'clamp(top bottom)',
end: 'bottom top',
scrub: true
}
})
.to(frame, {
yPercent: 35,
scale: 0.95,
startAt: { filter: 'brightness(100%)' },
filter: 'brightness(30%)'
})
.to(frameTitle, {
xPercent: -80
}, 0);
};
// Function to animate the first grid
const animateFirstGrid = () => {
const grid = document.querySelector('[data-grid-first]');
const gridImages = grid.querySelectorAll('.grid__img');
gsap.timeline({
defaults: {
ease: 'sine'
},
scrollTrigger: {
trigger: grid,
start: 'center center',
end: '+=250%',
pin: grid.parentNode,
scrub: 0.5,
}
})
.from(gridImages, {
stagger: 0.07,
y: () => gsap.utils.random(window.innerHeight, window.innerHeight * 1.8)
})
// text content
.from(grid.parentNode.querySelector('.content__title'), {
duration: 1.2,
ease: 'power4',
yPercent: 180,
autoAlpha: 0
}, 0.8);
};
// Function to animate the second grid
const animateSecondGrid = () => {
const grid = document.querySelector('[data-grid-second]');
const gridImages = grid.querySelectorAll('.grid__img');
const middleIndex = Math.floor(gridImages.length / 2);
gsap.timeline({
defaults: {
ease: 'power3'
},
scrollTrigger: {
trigger: grid,
start: 'center center',
end: '+=250%',
pin: grid.parentNode,
scrub: 0.5,
}
})
.from(gridImages, {
stagger: {
amount: 0.3,
from: 'center'
},
y: window.innerHeight,
transformOrigin: '50% 0%',
rotation: pos => {
const distanceFromCenter = Math.abs(pos - middleIndex);
return pos < middleIndex ? distanceFromCenter * 3 : distanceFromCenter * -3;
},
})
// text content
.from(grid.querySelectorAll('.grid__item'), {
stagger: {
amount: 0.3,
from: 'center'
},
yPercent: 100,
autoAlpha: 0
}, 0);
};
// Function to animate the third grid
const animateThirdGrid = () => {
const grid = document.querySelector('[data-grid-third]');
const gridImages = grid.querySelectorAll('.grid__img');
gsap.timeline({
defaults: {
ease: 'power3'
},
scrollTrigger: {
trigger: grid,
start: 'center center',
end: '+=200%',
pin: grid.parentNode,
scrub: 0.2,
}
})
.from(gridImages, {
stagger: 0.06,
y: window.innerHeight,
rotation: () => gsap.utils.random(-15,15),
transformOrigin: '50% 0%'
})
.fromTo(gridImages, {
filter: 'brightness(100%)'
}, {
ease: 'none',
stagger: 0.06,
filter: pos => pos < gridImages.length-1 ? 'brightness(20%)' : 'brightness(100%)'
}, 0)
// text content
.from(grid.querySelectorAll('.grid__item'), {
xPercent: pos => pos%2 ? 100 : -100,
autoAlpha: 0
}, 0.06*gridImages.length);
};
/**
* Calculates the initial translation and 3D rotation of an element, moving and rotating it further away from the center of the screen.
* The rotation and Z-axis translation are proportional to the distance from the center, with elements near the center rotating less and moving less in Z.
*
* @param {Element} element - The DOM element to calculate the translation and rotation for
* @param {Number} offsetDistance - The distance by which the element will be moved away from the center (default: 250px)
* @param {Number} maxRotation - The maximum rotation in degrees for farthest elements (default: 300 degrees)
* @param {Number} maxZTranslation - The maximum Z-axis translation in pixels for farthest elements (default: 2000px)
* @returns {Object} The x, y, z translation and rotateX, rotateY values as {x, y, z, rotateX, rotateY}
*/
const calculateInitialTransform = (element, offsetDistance = 250, maxRotation = 300, maxZTranslation = 2000) => {
const viewportCenter = { width: window.innerWidth / 2, height: window.innerHeight / 2 };
const elementCenter = {
x: element.offsetLeft + element.offsetWidth / 2,
y: element.offsetTop + element.offsetHeight / 2
};
// Calculate the angle between the center of the element and the center of the viewport
const angle = Math.atan2(Math.abs(viewportCenter.height - elementCenter.y), Math.abs(viewportCenter.width - elementCenter.x));
// Calculate the x and y translation based on the angle and distance
const translateX = Math.abs(Math.cos(angle) * offsetDistance);
const translateY = Math.abs(Math.sin(angle) * offsetDistance);
// Calculate the maximum possible distance from the center (diagonal of the viewport)
const maxDistance = Math.sqrt(Math.pow(viewportCenter.width, 2) + Math.pow(viewportCenter.height, 2));
// Calculate the current distance from the center
const currentDistance = Math.sqrt(Math.pow(viewportCenter.width - elementCenter.x, 2) + Math.pow(viewportCenter.height - elementCenter.y, 2));
// Scale rotation and Z-translation based on distance from the center (closer elements rotate/translate less, farther ones rotate/translate more)
const distanceFactor = currentDistance / maxDistance;
// Calculate the rotation values based on the position relative to the center
const rotationX = ((elementCenter.y < viewportCenter.height ? -1 : 1) * (translateY / offsetDistance) * maxRotation * distanceFactor);
const rotationY = ((elementCenter.x < viewportCenter.width ? 1 : -1) * (translateX / offsetDistance) * maxRotation * distanceFactor);
// Calculate the Z-axis translation (depth) based on the distance from the center
const translateZ = maxZTranslation * distanceFactor;
// Determine direction based on position relative to the viewport center
return {
x: elementCenter.x < viewportCenter.width ? -translateX : translateX,
y: elementCenter.y < viewportCenter.height ? -translateY : translateY,
z: translateZ,
rotateX: rotationX,
rotateY: rotationY
};
};
// Function to animate the fourth grid
const animateFourthGrid = () => {
const grid = document.querySelector('[data-grid-fourth]');
const gridImages = grid.querySelectorAll('.grid__img');
gsap.timeline({
defaults: {
ease: 'expo'
},
scrollTrigger: {
trigger: grid,
start: 'center center',
end: '+=200%',
pin: grid.parentNode,
scrub: 0.2,
}
})
.set(grid, {perspective: 1000}) // Add perspective for 3D effect
.fromTo(gridImages, {
// Define the starting position based on the pre-calculated translation, rotation, and Z-axis translation values
x: (_, el) => calculateInitialTransform(el).x,
y: (_, el) => calculateInitialTransform(el).y,
z: (_, el) => calculateInitialTransform(el).z, // Z-axis translation
rotateX: (_, el) => calculateInitialTransform(el).rotateX*.5,
rotateY: (_, el) => calculateInitialTransform(el).rotateY,
autoAlpha: 0,
scale: 0.7,
}, {
// Animate the images to their original position and remove transform
x: 0,
y: 0,
z: 0,
rotateX: 0,
rotateY: 0,
autoAlpha: 1,
scale: 1,
stagger: {
amount: 0.2,
from: 'center',
grid: [4, 9]
}
});
};
// Function to animate the fourth (v2) grid
const animateFourthV2Grid = () => {
const grid = document.querySelector('[data-grid-fourth-v2]');
const gridImages = grid.querySelectorAll('.grid__img');
gsap.timeline({
defaults: {
ease: 'power4',
},
scrollTrigger: {
trigger: grid,
start: 'center center',
end: '+=200%',
pin: grid.parentNode,
scrub: 0.2,
}
})
.set(grid, {perspective: 1200}) // Add perspective for 3D effect
.fromTo(gridImages, {
// Define the starting position based on the pre-calculated translation, rotation, and Z-axis translation values
x: (_, el) => calculateInitialTransform(el, 900).x,
y: (_, el) => calculateInitialTransform(el, 600).y,
z: (_, el) => calculateInitialTransform(el, _, _, -3000).z, // Z-axis translation
rotateX: (_, el) => calculateInitialTransform(el, 250, -160, -3000).rotateX,
rotateY: (_, el) => calculateInitialTransform(el, 250, -160, -3000).rotateY,
autoAlpha: 0,
scale: 0.4,
}, {
x: 0,
y: 0,
z: 0,
rotateX: 0,
rotateY: 0,
autoAlpha: 1,
scale: 1,
stagger: {
amount: 0.15,
from: 'center',
grid: [4, 9]
}
})
};
// Function to animate the fourth grid
const animateFifthGrid = () => {
const grid = document.querySelector('[data-grid-fifth]');
const gridImages = grid.querySelectorAll('.grid__img');
gsap.timeline({
defaults: {
ease: 'sine'
},
scrollTrigger: {
trigger: grid,
start: 'center center',
end: '+=250%',
pin: grid.parentNode,
scrub: 0.3,
}
})
.set(grid, {perspective: 1000})
.from(gridImages, {
stagger: {
amount: 0.4,
from: 'random',
grid: [4,9]
},
y: window.innerHeight,
rotationX: -70,
transformOrigin: '50% 0%',
z: -900,
autoAlpha: 0
});
};
// Function to animate the sixth grid
const animateSixthGrid = () => {
const grid = document.querySelector('[data-grid-sixth]');
const gridImages = grid.querySelectorAll('.grid__img');
gsap.timeline({
defaults: {
ease: 'none'
},
scrollTrigger: {
trigger: grid,
start: 'center center',
end: '+=200%',
pin: grid.parentNode,
scrub: 0.5,
}
})
.from(gridImages, {
stagger: {
amount: 0.03,
from: 'edges',
grid: [3,3]
},
scale: 0.7,
autoAlpha: 0
})
.from(grid, {
scale: .7,
skewY: 5,
}, 0);
};
// Function to animate the seventh grid
const animateSeventhGrid = () => {
const grid = document.querySelector('[data-grid-seventh]');
const gridImages = grid.querySelectorAll('.grid__img');
gsap.timeline({
defaults: {
ease: 'power1'
},
scrollTrigger: {
trigger: grid,
start: 'center center',
end: '+=150%',
pin: grid.parentNode,
scrub: 0.5,
}
})
.fromTo(gridImages, {
yPercent: -102,
//filter: 'brightness(300%) contrast(480%)'
}, {
stagger: 0.08,
yPercent: 0,
//filter: 'brightness(100%) contrast(100%)'
})
.from([...gridImages].map(img => img.querySelector('.grid__img-inner')), {
stagger: 0.08,
yPercent: 102,
}, 0)
// text content
.from(grid.querySelectorAll('.grid__item'), {
yPercent: 20,
stagger: gridImages.length/2*0.08,
autoAlpha: 0,
}, 0);
};
// Function to animate the eighth grid
const animateEighthGrid = () => {
const grid = document.querySelector('[data-grid-eighth]');
const gridImages = grid.querySelectorAll('.grid__img');
gsap.timeline({
defaults: {
ease: 'expo'
},
scrollTrigger: {
trigger: grid,
start: 'center center',
end: '+=250%',
pin: grid.parentNode,
scrub: true,
}
})
.set(grid, {perspective: 2000})
.from(gridImages, {
stagger: {
amount: 0.8,
from: 'start'
},
rotationY: 65,
transformOrigin: '0% 50%',
z: -200,
yPercent: 10
})
.from(gridImages, {
stagger: {
amount: 0.8,
from: 'start'
},
duration: 0.2,
autoAlpha: 0
}, 0);
};
// Function to animate the ninth grid
const animateNinthGrid = () => {
const grid = document.querySelector('[data-grid-ninth]');
const gridImages = grid.querySelectorAll('.grid__img');
gsap.timeline({
defaults: {
ease: 'power3'
},
scrollTrigger: {
trigger: grid,
start: 'center center',
end: '+=200%',
pin: grid.parentNode,
scrub: true,
}
})
.from(gridImages, {
transformOrigin: '100% -450%',
stagger: 0.07,
scaleX: 1.05,
skewX: 15,
xPercent: 50,
rotation: -10,
autoAlpha: 0
});
};
// Main initialization function
const init = () => {
// Animate the header (frame)
animateFrame();
// Call animations for each grid based on their data attributes
animateFirstGrid();
animateSecondGrid();
animateThirdGrid();
animateFourthGrid();
animateFourthV2Grid();
animateFifthGrid();
animateSixthGrid();
animateSeventhGrid();
animateEighthGrid();
animateNinthGrid();
};
// Preload images and initialize animations
preloadImages('.grid__img').then(() => {
document.body.classList.remove('loading'); // Remove the loading class from the body
init();
window.scrollTo(0, 0);
});