-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathscript.js
More file actions
281 lines (228 loc) · 9.34 KB
/
script.js
File metadata and controls
281 lines (228 loc) · 9.34 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
// Menemukan script.js yang diperbaiki untuk menampilkan lirik
let userData = null;
let currentLyricIndex = -1;
let songDuration = 60;
let isPlaying = false;
let isFirstInteraction = true;
const profilePic = document.getElementById('profilePic');
const profileName = document.getElementById('profileName');
const profileBio = document.getElementById('profileBio');
const linksContainer = document.getElementById('linksContainer');
const currentYear = document.getElementById('currentYear');
const albumArt = document.getElementById('albumArt');
const songTitle = document.getElementById('songTitle');
const artist = document.getElementById('artist');
const lyrics = document.getElementById('lyrics');
const playPauseBtn = document.getElementById('playPauseBtn');
const playPauseIcon = document.getElementById('playPauseIcon');
const musicPlayer = document.getElementById('musicPlayer');
const musicToggle = document.getElementById('musicToggle');
const progressBar = document.getElementById('progressBar');
const currentTimeDisplay = document.getElementById('currentTime');
const totalTimeDisplay = document.getElementById('totalTime');
const welcomePanel = document.getElementById('welcomePanel');
const welcomeCloseBtn = document.getElementById('welcomeCloseBtn');
const audioPlayer = document.getElementById('audioPlayer');
function checkWelcomePanel() {
const lastVisit = localStorage.getItem('lastVisit');
const currentTime = new Date().getTime();
if (!lastVisit || (currentTime - lastVisit) > 60000) {
welcomePanel.classList.add('visible');
localStorage.setItem('lastVisit', currentTime);
}
}
welcomeCloseBtn.addEventListener('click', () => {
welcomePanel.classList.remove('visible');
});
function getAverageColor(imageElement, callback) {
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
if (!imageElement.complete) {
imageElement.onload = function() {
extractColor();
};
} else {
extractColor();
}
function extractColor() {
canvas.width = imageElement.width;
canvas.height = imageElement.height;
context.drawImage(imageElement, 0, 0, canvas.width, canvas.height);
const imageData = context.getImageData(0, 0, canvas.width, canvas.height).data;
let r = 0, g = 0, b = 0;
let pixelCount = 0;
for (let i = 0; i < imageData.length; i += 4) {
r += imageData[i];
g += imageData[i + 1];
b += imageData[i + 2];
pixelCount++;
}
r = Math.floor(r / pixelCount);
g = Math.floor(g / pixelCount);
b = Math.floor(b / pixelCount);
const brightness = (r * 299 + g * 587 + b * 114) / 1000;
const textColor = brightness > 128 ? '33, 33, 33' : '255, 255, 255';
const bgColor = `rgba(${r}, ${g}, ${b}, 0.15)`;
const hue1 = Math.floor(Math.random() * 360);
const hue2 = (hue1 + 180) % 360;
const primaryColor = `hsl(${hue1}, 70%, 50%)`;
const secondaryColor = `hsl(${hue2}, 70%, 50%)`;
callback(bgColor, `rgb(${textColor})`, textColor, primaryColor, secondaryColor);
}
}
function initializeFromJSON(data) {
userData = data;
profileName.textContent = data.profile.name;
profileBio.textContent = data.profile.bio;
profilePic.src = data.profile.image;
profilePic.onload = function() {
getAverageColor(profilePic, (bgColor, textColor, textColorRgb, primaryColor, secondaryColor) => {
document.body.style.backgroundColor = bgColor;
document.documentElement.style.setProperty('--bg-color', bgColor);
document.documentElement.style.setProperty('--text-color', textColor);
document.documentElement.style.setProperty('--text-color-rgb', textColorRgb);
document.documentElement.style.setProperty('--primary-color', primaryColor);
document.documentElement.style.setProperty('--secondary-color', secondaryColor);
});
};
linksContainer.innerHTML = '';
data.links.forEach((link, index) => {
const linkElement = document.createElement('a');
linkElement.href = link.url;
linkElement.className = 'link-item animate-link';
linkElement.style.animationDelay = `${0.1 * (index + 1)}s`;
linkElement.target = '_blank';
linkElement.rel = 'noopener noreferrer';
const icon = document.createElement('i');
icon.className = link.icon;
const text = document.createTextNode(link.title);
linkElement.appendChild(icon);
linkElement.appendChild(text);
linksContainer.appendChild(linkElement);
});
songTitle.textContent = data.music.title;
artist.textContent = data.music.artist;
albumArt.src = data.music.albumArt;
if (data.music.audioFile) {
audioPlayer.src = data.music.audioFile;
audioPlayer.addEventListener('loadedmetadata', function() {
songDuration = audioPlayer.duration;
totalTimeDisplay.textContent = formatTime(songDuration);
});
audioPlayer.addEventListener('timeupdate', function() {
currentTimeDisplay.textContent = formatTime(audioPlayer.currentTime);
const percent = (audioPlayer.currentTime / songDuration) * 100;
progressBar.style.width = `${percent}%`;
updateLyricsDisplay(audioPlayer.currentTime);
});
audioPlayer.addEventListener('ended', function() {
audioPlayer.currentTime = 0;
if (isPlaying) {
audioPlayer.play();
} else {
isPlaying = false;
playPauseIcon.className = 'fas fa-play';
}
});
} else {
songDuration = data.music.duration || 60;
totalTimeDisplay.textContent = formatTime(songDuration);
}
// PERBAIKAN: Periksa format timeSync dan pastikan kita mengakses elemen dengan benar
if (data.music.timeSync) {
console.log("Lyrics data loaded:", data.music.timeSync);
updateLyricsDisplay(0);
} else {
lyrics.textContent = data.music.lyrics || "No lyrics available";
}
currentYear.textContent = new Date().getFullYear();
}
function updateLyricsDisplay(time) {
if (!userData || !userData.music.timeSync) return;
// PERBAIKAN: Deteksi dan tangani format yang ada di data.json
// Jika timeSync adalah array dalam array, ambil array pertama
let lyricArray = userData.music.timeSync;
if (Array.isArray(lyricArray) && lyricArray.length > 0 && Array.isArray(lyricArray[0])) {
lyricArray = lyricArray[0];
console.log("Detected nested array format, using first array:", lyricArray.length, "items");
}
let currentLyric = null;
let newLyricIndex = -1;
for (let i = 0; i < lyricArray.length; i++) {
if (lyricArray[i].time <= time) {
currentLyric = lyricArray[i];
newLyricIndex = i;
} else {
break;
}
}
if (currentLyric && newLyricIndex !== currentLyricIndex) {
currentLyricIndex = newLyricIndex;
lyrics.innerHTML = '';
const lyricLine = document.createElement('div');
lyricLine.className = 'lyrics-line active';
lyricLine.textContent = currentLyric.text;
lyrics.appendChild(lyricLine);
lyricLine.classList.add('lyrics-fade-in');
setTimeout(() => {
lyricLine.classList.remove('lyrics-fade-in');
}, 500);
}
}
function formatTime(seconds) {
const mins = Math.floor(seconds / 60);
const secs = Math.floor(seconds % 60);
return `${mins}:${secs.toString().padStart(2, '0')}`;
}
function togglePlayPause() {
isPlaying = !isPlaying;
if (isPlaying) {
playPauseIcon.className = 'fas fa-pause';
if (audioPlayer.src) {
audioPlayer.play();
}
} else {
playPauseIcon.className = 'fas fa-play';
if (audioPlayer.src) {
audioPlayer.pause();
}
}
}
musicToggle.addEventListener('click', () => {
musicPlayer.classList.toggle('hidden');
});
playPauseBtn.addEventListener('click', (event) => {
event.stopPropagation();
togglePlayPause();
});
document.querySelector('.progress-container').addEventListener('click', function(e) {
if (audioPlayer.src) {
const progressContainer = this;
const rect = progressContainer.getBoundingClientRect();
const percent = (e.clientX - rect.left) / rect.width;
audioPlayer.currentTime = percent * songDuration;
}
});
document.addEventListener('click', function playOnFirstInteraction() {
if (isFirstInteraction) {
isFirstInteraction = false;
// Start playing music
if (!isPlaying) {
togglePlayPause();
}
document.removeEventListener('click', playOnFirstInteraction);
}
}, true);
document.addEventListener('DOMContentLoaded', function() {
checkWelcomePanel();
currentLyricIndex = -1;
fetch('data.json')
.then(response => response.json())
.then(data => {
console.log("Data loaded successfully:", data);
initializeFromJSON(data);
})
.catch(error => {
console.error('Error loading data:', error);
});
});