-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.js
112 lines (89 loc) · 3.29 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
document.addEventListener('DOMContentLoaded', function () {
const greeting1 = document.getElementById('greeting1');
const greeting2 = document.getElementById('greeting2');
const container = document.querySelector('.container');
const socials = document.getElementById('socials');
socials.style.opacity = 0;
socials.classList.add('hidden');
fadeIn(greeting1, 1400, function () {
fadeOut(greeting1, 1400, function () {
fadeIn(greeting2, 1400, function () {
fadeOut(greeting2, 1400, function () {
animateBackgroundColorChange(container, 'black', '#121212', 800);
});
});
});
});
function fadeIn(element, duration, callback) {
element.style.display = 'block';
element.style.opacity = 0;
let startTime = null;
function fade() {
if (startTime === null) {
startTime = performance.now();
}
const progress = (performance.now() - startTime) / duration;
element.style.opacity = Math.min(progress, 1);
if (progress < 1) {
requestAnimationFrame(fade);
} else {
callback();
}
}
requestAnimationFrame(fade);
}
function fadeOut(element, duration, callback) {
let startTime = null;
function fade() {
if (startTime === null) {
startTime = performance.now();
}
const progress = (performance.now() - startTime) / duration;
element.style.opacity = 1 - Math.min(progress, 1);
if (progress < 1) {
requestAnimationFrame(fade);
} else {
element.style.display = 'none';
callback();
}
}
requestAnimationFrame(fade);
}
function animateBackgroundColorChange(element, fromColor, toColor, duration) {
let startTime = null;
function animate() {
if (startTime === null) {
startTime = performance.now();
}
const progress = (performance.now() - startTime) / duration;
element.style.backgroundColor = interpolateColor(fromColor, toColor, progress);
if (progress < 1) {
requestAnimationFrame(animate);
} else {
showSocials();
}
}
requestAnimationFrame(animate);
}
function showSocials() {
fadeIn(socials, 1000);
socials.classList.remove('hidden');
}
function interpolateColor(fromColor, toColor, progress) {
const fromRGB = hexToRGB(fromColor);
const toRGB = hexToRGB(toColor);
const resultRGB = [];
for (let i = 0; i < 3; i++) {
resultRGB[i] = Math.round(fromRGB[i] + (toRGB[i] - fromRGB[i]) * progress);
}
return `rgb(${resultRGB.join(',')})`;
}
function hexToRGB(hexColor) {
const hex = hexColor.slice(1);
const bigint = parseInt(hex, 16);
const r = (bigint >> 16) & 255;
const g = (bigint >> 8) & 255;
const b = bigint & 255;
return [r, g, b];
}
});