-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnected.html
456 lines (386 loc) · 15.8 KB
/
connected.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SpotiTwitch - Connected</title>
<style>
:root {
--display-width: 1920px;
--display-height: 1080px;
--vinyle-width: 500px;
--vinyle-height: 500px;
--box-height: calc(var(--vinyle-height) / 2);
--box-width: 600px;
--center-box-top: calc(var(--vinyle-height) / 2 - var(--box-height) / 2);
--half-display-width: calc(var(--display-width) / 2);
--half-box-width: calc(var(--box-width) / 4);
--end-box: calc(var(--half-display-width) + var(--half-box-width));
--color-box: rgba(0, 0, 0, 0.79);
}
body {
background-color: grey;
}
* {
min-width: 0;
min-height: 0;
}
.container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.vinyl {
width: var(--vinyle-width);
height: var(--vinyle-height);
background-image: url("https://i.pinimg.com/originals/0f/6e/9e/0f6e9e2b6b5b6b6b6b6b6b6b6b6b6b6b.jpg");
background-size: cover;
background-position: center;
border-radius: 50%;
margin: 0 auto;
position: relative;
overflow: hidden;
animation: rot 10s linear infinite;
z-index: 5;
}
/*!* Ajout des reflets de style cartoon *!*/
/*.vinyl::before,*/
/*.vinyl::after {*/
/* content: '';*/
/* position: absolute;*/
/* width: 50%;*/
/* height: 10%;*/
/* background: rgba(255, 255, 255, 0.2); !* Couleur légèrement transparente pour un effet de reflet *!*/
/* border-radius: 50px;*/
/* transform: rotate(-30deg);*/
/* pointer-events: none;*/
/* z-index: 6;*/
/*}*/
/*!* Premier reflet courbé *!*/
/*.vinyl::before {*/
/* top: 10%;*/
/* left: -15%;*/
/* transform: rotate(-25deg);*/
/*}*/
/*!* Deuxième reflet courbé *!*/
/*.vinyl::after {*/
/* top: 55%;*/
/* left: 30%;*/
/* transform: rotate(35deg);*/
/*}*/
.rond {
width: 125px;
height: 125px;
background-color: #000;
border-radius: 50%;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.rond::before {
content: '';
width: 25%;
height: 25%;
background-color: #fff;
border-radius: 50%;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
@keyframes rot {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
.small-box {
width: var(--box-width);
height: var(--box-height);
background-color: var(--color-box);
margin: 0 auto;
position: absolute;
top: var(--center-box-top);
left: var(--end-box);
overflow: hidden;
border-radius: 10px;
}
.small-box-content-text {
display: grid;
grid-template-columns: 1fr;
grid-template-rows: repeat(2, 1fr);
grid-column-gap: 0;
grid-row-gap: 0;
}
.artist-name { grid-area: 1 / 1 / 2 / 2; }
.marquee-artist { grid-area: 1 / 1 / 2 / 2; }
.marquee-song { grid-area: 2 / 1 / 3 / 2; }
.song-name { grid-area: 2 / 1 / 3 / 2; }
.small-box-content-text {
color: #fff;
height: 100%;
font-family: 'Montserrat', sans-serif;
font-size: 40px;
font-weight: bold;
text-align: center;
overflow: hidden;
white-space: nowrap;
}
.artist-name, .song-name {
/*Center the elements inside*/
display: flex;
justify-content: center;
align-items: center;
}
.loading-bar {
width: 100%;
height: 10px;
background-color: var(--color-box);
position: absolute;
bottom: 0;
left: 0;
border: 1px solid #000;
}
.loading-bar-progress {
width: 0;
height: 100%;
background-color: rgba(255, 255, 255, 0.8);
transition: width 0.5s ease-in-out;
}
.marquee-artist, .marquee-song {
position: relative;
overflow: hidden;
--offset: 20vw;
--move-initial: calc(-25% + var(--offset));
--move-final: calc(-50% + var(--offset));
}
.marquee__inner__artist, .marquee__inner__song {
display: inline-block;
padding-left: 100%;
width: fit-content;
animation: marquee 15s linear infinite;
white-space: nowrap;
line-height: calc(var(--box-height) / 2); /* Center vertically */
}
@keyframes marquee {
0% {
transform: translateX(0%);
}
100% {
transform: translateX(-100%);
}
}
/* Assurez-vous que les textes défilants utilisent les mêmes styles */
.marquee__inner__artist span, .marquee__inner__song span,
.artist-name, .song-name {
font-family: 'Montserrat', sans-serif; /* Choix de la police */
font-size: 100px; /* Taille de la police */
}
/* Ajuster la taille des éléments en mode défilement */
.marquee__inner__artist span, .marquee__inner__song span {
font-size: 100px; /* Vous pouvez ajuster pour les rendre plus petits ou plus grands */
font-weight: bold; /* Ajuster le poids de la police si nécessaire */
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/color-thief/2.3.0/color-thief.umd.js"></script>
</head>
<body>
<div class="main">
<div class="container">
<div class="vinyl">
<div class="rond"></div>
<img id="album-cover" src="" alt="Pochette d'album" crossorigin="anonymous">
<!-- <img class="overlay-image" src="transparent-image.png">-->
</div>
<div class="small-box">
<div class="small-box-content-text">
<div class="artist-name">
<span id="artist-name">Chargement...</span>
</div>
<div class="marquee-artist" style="display: none">
<div class="marquee__inner__artist" aria-hidden="true">
<span id="artist-name-1">Chargement...</span>
<!-- <span id="artist-name-2">Chargement...</span>-->
<!-- <span id="artist-name-3">Chargement...</span>-->
<!-- <span id="artist-name-4">Chargement...</span>-->
</div>
</div>
<div class="song-name">
<span id="song-name">Chargement...</span>
</div>
<div class="marquee-song" style="display: none">
<div class="marquee__inner__song" aria-hidden="true">
<span id="song-name-1">Chargement...</span>
<!-- <span id="song-name-2">Chargement...</span>-->
<!-- <span id="song-name-3">Chargement...</span>-->
<!-- <span id="song-name-4">Chargement...</span>-->
</div>
</div>
</div>
<div class="loading-bar">
<div class="loading-bar-progress"></div>
</div>
</div>
<div style="display: none">
<p id="song-duration"></p>
<p id="song-progress"></p>
<p id="song-progress-percent"></p>
</div>
</div>
</div>
</body>
<script>
let isPaused = false;
document.addEventListener('DOMContentLoaded', () => {
const accessToken = getAccessTokenFromUrl();
if (accessToken) {
initSpotifyPlayer(accessToken);
setInterval(() => {
if (!isPaused) updateSongProgress(); // Only update if not paused
}, 1000);
setInterval(() => refreshIfSongFinished(), 1000);
setInterval(() => refreshData(accessToken), 5000); // Check every 5 seconds
} else {
redirectToSpotifyAuth();
}
const albumCover = document.getElementById('album-cover');
albumCover.addEventListener('load', () => {
applyDominantColor(albumCover);
});
});
function applyDominantColor(image) {
const colorThief = new ColorThief();
if (!image.complete) return;
const dominantColor = colorThief.getColor(image); // Récupère la couleur dominante [R, G, B]
const palette = colorThief.getPalette(image, 5); // Récupère une palette de couleurs
// Convertir la couleur dominante en format CSS
const dominantColorCss = rgbToCss(dominantColor);
// Appliquer la couleur dominante à l'arrière-plan de la boîte
setElementBackgroundColor('.small-box', dominantColorCss);
// Assigner une couleur de texte contrastée
const textColor = getContrastingTextColor(dominantColor);
setTextColor('.artist-name, .song-name, .marquee-artist, .marquee-song', textColor);
}
// Fonction pour convertir un tableau RGB en chaîne CSS
function rgbToCss(rgbArray) {
return `rgb(${rgbArray.join(',')})`;
}
// Fonction pour appliquer la couleur d'arrière-plan à un élément
function setElementBackgroundColor(selector, color) {
const element = document.querySelector(selector);
if (element) {
element.style.backgroundColor = color;
element.style.transition = 'background-color 0.5s ease'; // Transition fluide pour l'esthétique
}
}
// Fonction pour définir la couleur de texte sur plusieurs éléments
function setTextColor(selector, color) {
const elements = document.querySelectorAll(selector);
elements.forEach(el => {
el.style.color = color;
el.style.transition = 'color 0.5s ease'; // Transition fluide pour un changement en douceur
});
}
// Fonction utilitaire pour choisir une couleur de texte contrastée (noir ou blanc)
function getContrastingTextColor(rgbArray) {
const [r, g, b] = rgbArray;
const brightness = calculateBrightness(r, g, b);
return brightness > 128 ? '#000' : '#fff';
}
// Calculer la luminosité pour déterminer la couleur contrastée
function calculateBrightness(r, g, b) {
return (r * 299 + g * 587 + b * 114) / 1000;
}
function getAccessTokenFromUrl() {
const hash = window.location.hash.substring(1);
const params = new URLSearchParams(hash);
return params.get('access_token');
}
function redirectToSpotifyAuth() {
const client_id = '324a24a6a90c4cb396505d1b0def7c07';
const redirect_uri = 'http://localhost/SpotiTwitch/connected.html';
const scopes = 'user-read-playback-state';
window.location.href = `https://accounts.spotify.com/authorize?response_type=token&client_id=${client_id}&scope=${encodeURIComponent(scopes)}&redirect_uri=${encodeURIComponent(redirect_uri)}`;
}
function initSpotifyPlayer(token) {
fetchCurrentlyPlaying(token);
}
function fetchCurrentlyPlaying(token) {
fetch('https://api.spotify.com/v1/me/player/currently-playing', {
headers: {'Authorization': `Bearer ${token}`}
})
.then(response => {
if (!response.ok) throw new Error('Erreur réseau');
return response.json();
})
.then(data => {
if (data && data.item) {
console.log('Données récupérées:', data);
updateTrackDisplay(data);
isPaused = !data.is_playing; // Update isPaused based on is_playing
} else {
console.log('Aucune musique en cours de lecture.');
isPaused = true; // Assume paused if no music is playing
}
})
.catch(error => console.error('Erreur lors de la récupération des données:', error));
}
function updateTrackDisplay(data) {
const artistNames = data.item.artists.map(artist => artist.name).join(', ');
const songName = data.item.name;
setTextContentByIds(['artist-name', 'artist-name-1', 'artist-name-2', 'artist-name-3', 'artist-name-4'], artistNames);
setTextContentByIds(['song-name', 'song-name-1', 'song-name-2', 'song-name-3', 'song-name-4'], songName);
document.getElementById('album-cover').src = data.item.album.images[0].url;
document.getElementById('song-duration').innerText = formatDuration(data.item.duration_ms);
document.getElementById('song-progress').innerText = formatDuration(data.progress_ms);
toggleMarquee('artist-name', 'marquee-artist', artistNames.length > 10);
toggleMarquee('song-name', 'marquee-song', songName.length > 8);
}
function setTextContentByIds(ids, text) {
ids.forEach(id => {
const element = document.getElementById(id);
if (element) element.innerText = text;
});
}
function toggleMarquee(staticId, marqueeClass, shouldMarquee) {
document.getElementById(staticId).style.display = shouldMarquee ? 'none' : 'block';
document.getElementsByClassName(marqueeClass)[0].style.display = shouldMarquee ? 'block' : 'none';
}
function formatDuration(ms) {
const minutes = Math.floor(ms / 60000);
const seconds = ((ms % 60000) / 1000).toFixed(0);
return `${minutes}:${seconds < 10 ? '0' : ''}${seconds}`;
}
function updateSongProgress() {
const songProgressElem = document.getElementById('song-progress');
const songDurationElem = document.getElementById('song-duration');
let [progressSeconds, durationSeconds] = [songProgressElem, songDurationElem].map(elem => {
const [minutes, seconds] = elem.innerText.split(':').map(Number);
return minutes * 60 + seconds;
});
progressSeconds = Math.min(progressSeconds + 1, durationSeconds);
const minutes = Math.floor(progressSeconds / 60);
const seconds = progressSeconds % 60;
songProgressElem.innerText = `${minutes}:${seconds < 10 ? '0' : ''}${seconds}`;
const progressPercent = (progressSeconds / durationSeconds) * 100;
document.querySelector('.loading-bar-progress').style.width = `${progressPercent}%`;
}
function refreshData(token) {
fetchCurrentlyPlaying(token); // This call also updates isPaused status
}
function refreshIfSongFinished() {
const songProgressElem = document.getElementById('song-progress');
const songDurationElem = document.getElementById('song-duration');
const [progressSeconds, durationSeconds] = [songProgressElem, songDurationElem].map(elem => {
const [minutes, seconds] = elem.innerText.split(':').map(Number);
return minutes * 60 + seconds;
});
if (progressSeconds >= durationSeconds) {
refreshData(getAccessTokenFromUrl());
}
}
</script>
</html>