-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.html
372 lines (334 loc) · 16.3 KB
/
player.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
<!DOCTYPE html>
<html>
<head>
<title>Cast to 'Record Player' to Play</title>
<link rel="icon" type="image/png" href="/images/favicon.png">
<link rel="stylesheet" href="style.css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap" rel="stylesheet">
</head>
<body>
<div class="currentSong">
<div class="track">Track Name</div>
<div class="artist">Artist</div>
<button id="togglePlay" class="button">Play</button>
</div>
<div class="options">
<button id="toggleFS" class="button">Fullscreen</button>
<button id="toggleOptions" class="button">Colour</button>
<div class="volume">
<input type="range" min="0" max="100" value="50" id="volumeSlider">
</div>
</div>
<div class="prevTrack">
<img class="image" id="prevImage" draggable="false">
<div class="captionT" id="prevCaptionT">Track</div>
<div class="captionA" id="prevCaptionA">Artist</div>
</div>
<div class="nextTrack">
<img class="image" id="nextImage" draggable="false">
<div class="captionT" id="nextCaptionT">Track</div>
<div class="captionA" id="nextCaptionA">Artist</div>
</div>
<div class="container">
<div id="vinyl-record">
<div class="album-art"></div>
<div class="pin"></div>
<div class="needle"></div>
</div>
</div>
<div class="menu">
<div class="menu-items">
<div class="menu-item" id="classic"></div>
<div class="menu-item" id="red" onclick="setColor('red');"></div>
<div class="menu-item" id="yellow" onclick="setColor('yellow');"></div>
<div class="menu-item" id="orange" onclick="setColor('orange');"></div>
<div class="menu-item" id="blue" onclick="setColor('blue');"></div>
<div class="menu-item" id="navy" onclick="setColor('navy');"></div>
<div class="menu-item" id="green" onclick="setColor('green');"></div>
<div class="menu-item" id="pink" onclick="setColor('pink');"></div>
<script>
var previouslySelected
const currentCol = localStorage.getItem('color');
if (currentCol) {
previouslySelected = currentCol;
const colElement = document.getElementById(currentCol);
document.getElementById('vinyl-record').style.backgroundColor = currentCol;
colElement.style.borderRadius = "50%";
}
window.setColor = function(col) {
if (col != previouslySelected) {
localStorage.setItem('color',col);
const colElement = document.getElementById(col);
if (col != "classic") {
document.getElementById('vinyl-record').style.backgroundColor = col;
}
colElement.style.borderRadius = "50%";
if (previouslySelected) {
const prevElement = document.getElementById(previouslySelected);
prevElement.style.borderRadius = "5px";
}
previouslySelected = col;
}
}
</script>
</div>
</div>
<script src="https://sdk.scdn.co/spotify-player.js"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
window.onSpotifyWebPlaybackSDKReady = () => {
const token = localStorage.getItem('spotifyAccessToken');
if (!token) {
window.location = "https://recordplayer.vercel.app";
}
const player = new Spotify.Player({
name: 'Record Player',
getOAuthToken: cb => { cb(token); },
volume: 0.5
});
var dragging = false;
// Ready
player.addListener('ready', ({ device_id }) => {
console.log('Ready with Device ID', device_id);
});
// Not Ready
player.addListener('not_ready', ({ device_id }) => {
console.log('Device ID has gone offline', device_id);
});
player.addListener('initialization_error', ({ message }) => {
console.error(message);
});
player.addListener('authentication_error', ({ message }) => {
console.error(message);
});
player.addListener('account_error', ({ message }) => {
console.error(message);
});
document.getElementById('togglePlay').onclick = function() {
player.togglePlay();
};
document.getElementById('toggleFS').onclick = function() {
if (!document.fullscreenElement) {
document.documentElement.requestFullscreen();
document.getElementById('toggleFS').textContent="Exit Fullscreen";
} else {
document.exitFullscreen();
document.getElementById('toggleFS').textContent="Fullscreen";
}
};
document.getElementById('toggleOptions').onclick = function() {
const menu = document.querySelector('.menu');
if (menu.style.display == "none") {
document.getElementById('toggleOptions').textContent="Exit Colour";
menu.style.display = "block";
} else {
document.getElementById('toggleOptions').textContent="Colour";
menu.style.display = "none";
}
};
var volumeSlider = document.getElementById("volumeSlider");
volumeSlider.oninput = function() {
player.setVolume(this.value/100).then(() => {
});
}
document.querySelector('.prevTrack').addEventListener('click', function() {
player.previousTrack().then(() => {
});
});
document.querySelector('.nextTrack').addEventListener('click', function() {
player.nextTrack().then(() => {
});
});
player.connect();
player.addListener('player_state_changed', ({
position,
duration,
paused,
track_window: { current_track }
}) => {
dragElement(document.querySelector('.needle'));
function dragElement(elmnt) {
var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
if (document.getElementById(elmnt.id + "header")) {
/* if present, the header is where you move the DIV from:*/
document.getElementById(elmnt.id + "header").onmousedown = dragMouseDown;
} else {
/* otherwise, move the DIV from anywhere inside the DIV:*/
elmnt.onmousedown = dragMouseDown;
}
function dragMouseDown(e) {
e = e || window.event;
e.preventDefault();
// get the mouse cursor position at startup:
elmnt.style.cursor = "grabbing";
dragging = true;
pos3 = e.clientX;
pos4 = e.clientY;
startAngle = getAngle(e.clientX, e.clientY);
currentAngle = getCurrentRotation(elmnt);
document.onmouseup = closeDragElement;
// call a function whenever the cursor moves:
document.onmousemove = elementDrag;
}
function elementDrag(e) {
e = e || window.event;
e.preventDefault();
var angle = getAngle(e.clientX, e.clientY);
var deltaAngle = (angle - startAngle)*0.5; //speed
deltaAngle = deltaAngle < -180 ? deltaAngle + 360 : deltaAngle > 180 ? deltaAngle - 360 : deltaAngle;
currentAngle += deltaAngle
if (currentAngle < 0) currentAngle = 0;
if (currentAngle > 28) currentAngle = 28;
elmnt.style.transform = `rotate(${currentAngle}deg) translateX(-100%)`;
startAngle = angle;
}
function getAngle(x, y) {
var rect = elmnt.getBoundingClientRect();
var elmntX = rect.left + rect.width / 2;
var elmntY = rect.top + rect.height / 2;
var dx = x - elmntX;
var dy = y - elmntY;
return Math.atan2(dy, dx) * (180 / Math.PI);
}
function getCurrentRotation(elmnt) {
var transform = elmnt.style.transform;
var match = transform.match(/rotate\(([-0-9.]+)deg\)/);
if (match) {
return parseFloat(match[1]);
}
return 0;
}
function closeDragElement() {
/* stop moving when mouse button is released:*/
player.seek((currentAngle/28)*duration).then(() => {
});
dragging = false;
elmnt.style.cursor = "grab";
document.onmouseup = null;
document.onmousemove = null;
}
}
document.querySelector('.track').textContent = current_track.name;
document.querySelector('.artist').textContent = current_track.artists.map(artist => artist.name).join(", ");
document.title = `Playing ${current_track.name} by ${current_track.artists[0].name}`;
const albumArtClass = document.querySelector('.album-art');
if (!paused) {
albumArtClass.classList.add('spin');
document.getElementById('togglePlay').textContent="Pause"
} else {
albumArtClass.classList.remove('spin');
document.getElementById('togglePlay').textContent="Play"
}
albumArt = current_track.album.images.find(image => image.width === 300 && image.height === 300).url
if (albumArt) {
albumArtClass.style.backgroundImage = `url(${albumArt})`;
getPredominantColor(albumArt);
}
var needlePosition = position;
// Example to update needle position dynamically
const needleClass = document.querySelector('.needle');
setInterval(() => {
player.getCurrentState().then(state => {
if (!state) {
console.error('User is not playing music through the Web Playback SDK');
} else {
if (!dragging) {
var needlePosition = state.position;
const rotationAngle = (needlePosition / state.duration)*28;
needleClass.style.transform = `rotate(${rotationAngle}deg) translateX(-100%)`;
}
var next_track = state.track_window.next_tracks[0];
const nextAlbumArtClass = document.getElementById('nextImage');
nextAlbumArt = next_track.album.images.find(image => image.width === 300 && image.height === 300).url
if (nextAlbumArt) {
nextAlbumArtClass.src = nextAlbumArt
}
const nextCaptionT = document.getElementById('nextCaptionT');
const nextCaptionA = document.getElementById('nextCaptionA');
nextCaptionT.textContent=next_track.name
nextCaptionA.textContent=next_track.artists.map(artist => artist.name).join(", ");
var prev_track = state.track_window.previous_tracks[1];
const prevAlbumArtClass = document.getElementById('prevImage');
prevAlbumArt = prev_track.album.images.find(image => image.width === 300 && image.height === 300).url
if (prevAlbumArt) {
prevAlbumArtClass.src = prevAlbumArt
}
const prevCaptionT = document.getElementById('prevCaptionT');
const prevCaptionA = document.getElementById('prevCaptionA');
prevCaptionT.textContent=prev_track.name
prevCaptionA.textContent=prev_track.artists.map(artist => artist.name).join(", ");
}
});
}, 1000);
});
}
function getPredominantColor(imageUrl) {
var img = new Image();
img.src = imageUrl;
img.crossOrigin = "Anonymous";
img.onload = function() {
var canvas = document.createElement("canvas");
canvas.width = img.width;
canvas.height = img.height;
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height).data;
var colorCount = {};
var maxCount = 0;
var predominantColor = [0, 0, 0]; // Default color in case of failure to detect
for (var i = 0; i < imageData.length; i += 4) {
var color = [imageData[i], imageData[i + 1], imageData[i + 2]];
var key = color.join(",");
colorCount[key] = (colorCount[key] || 0) + 1;
if (colorCount[key] > maxCount) {
maxCount = colorCount[key];
predominantColor = color;
}
}
// Update the overlay color with the predominant color
var rgbString = "rgb(" + predominantColor.join(",") + ")";
document.body.style.backgroundColor = rgbString;
const pinClass = document.querySelector('.pin');
pinClass.style.backgroundColor = rgbString;
function getLuminance(color) {
const luminance = 0.2126 * color[0] + 0.7152 * color[1] + 0.0722 * color[2];
return luminance;
}
const luminance = getLuminance(predominantColor);
const buttons = document.querySelectorAll('.button');
if (luminance < 128) {
document.body.style.color = 'white';
buttons.forEach(button => {
button.style.color = 'black';
button.style.backgroundColor = 'white';
});
} else {
document.body.style.color = 'black';
buttons.forEach(button => {
button.style.color = 'white';
button.style.backgroundColor = 'black';
});
}
document.getElementById('classic').onclick = function() {
window.setColor('classic');
if (luminance < 25) {
document.getElementById('vinyl-record').style.backgroundColor = 'white';
} else {
document.getElementById('vinyl-record').style.backgroundColor = 'black';
}
};
const currentCol = localStorage.getItem('color');
if (currentCol=='classic') {
if (luminance < 25) {
document.getElementById('vinyl-record').style.backgroundColor = 'white';
} else {
document.getElementById('vinyl-record').style.backgroundColor = 'black';
}
}
};
}
</script>
</body>
</html>