|
|
|
@ -4,27 +4,26 @@ tag.src = 'https://www.youtube.com/iframe_api';
|
|
|
|
|
let firstScriptTag = document.getElementsByTagName('script')[0]; |
|
|
|
|
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); |
|
|
|
|
|
|
|
|
|
//THE DJ CROSSFADER!!!
|
|
|
|
|
let crossfader = document.querySelector('#crossfader'); |
|
|
|
|
|
|
|
|
|
class player { |
|
|
|
|
trackID; |
|
|
|
|
// volume;
|
|
|
|
|
// id;
|
|
|
|
|
// iframe;
|
|
|
|
|
player; |
|
|
|
|
// node;
|
|
|
|
|
crossfaderCoef; |
|
|
|
|
|
|
|
|
|
constructor(id) { |
|
|
|
|
this.id = id; |
|
|
|
|
this.iframe = document.querySelector('#'+this.id); |
|
|
|
|
// console.log('#'+this.id);
|
|
|
|
|
// console.log(this.iframe);
|
|
|
|
|
// this.volume = vol;
|
|
|
|
|
|
|
|
|
|
this.node = this.iframe.parentNode; |
|
|
|
|
this.volume = this.node.querySelector('.volume-slider').value; |
|
|
|
|
this.volSliderVal = this.node.querySelector('.volume-slider-value'); |
|
|
|
|
this.volVal = this.node.querySelector('.volume-value') |
|
|
|
|
this.volSliderVal.innerHTML = this.volume + '%'; |
|
|
|
|
this.loadButton = this.node.querySelector('.load-button'); |
|
|
|
|
this.speedSlider = this.node.querySelector('.speed-slider'); |
|
|
|
|
this.speedSlider.previousElementSibling.innerHTML = 'Speed: ' + this.speedSlider.value; |
|
|
|
|
this.speedSlider.nextElementSibling.querySelector('.speed-value').innerHTML = this.speedSlider.value; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
init(trackID) { |
|
|
|
@ -40,17 +39,12 @@ class player {
|
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
onPlayerReady(e) { |
|
|
|
|
// console.log(e.target);
|
|
|
|
|
// this.node.querySelector('.volumeSlider').value = this.player.getVolume();
|
|
|
|
|
// e.target.playVideo();
|
|
|
|
|
// console.log(e.target);
|
|
|
|
|
// e.target.setVolume(this.volume);
|
|
|
|
|
this.player.setVolume(this.volume); |
|
|
|
|
|
|
|
|
|
this.node.querySelector('.volume-slider').addEventListener('input', (e) => { |
|
|
|
|
let volume = e.target.value; |
|
|
|
|
this.volume = volume; |
|
|
|
|
this.player.setVolume(volume); |
|
|
|
|
this.volume = e.target.value; |
|
|
|
|
this.volSliderVal.innerHTML = this.volume + '%'; |
|
|
|
|
this.volumeUpdate(); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
this.loadButton.addEventListener('click', () => { |
|
|
|
@ -72,11 +66,11 @@ class player {
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
this.speedSlider.addEventListener('input', (e) => { |
|
|
|
|
this.speedSlider.previousElementSibling.innerHTML = 'Speed: ' + e.target.value; |
|
|
|
|
this.speedSlider.nextElementSibling.querySelector('.speed-value').innerHTML = e.target.value; |
|
|
|
|
this.player.setPlaybackRate(Number(e.target.value)); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
// updateCrossfader(crossfader.value); //Throws an error at the beginning because the second player is not ready yet.
|
|
|
|
|
this.volumeUpdate(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
onPlayerStateChange(e) { |
|
|
|
@ -103,6 +97,12 @@ class player {
|
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
volumeUpdate() { |
|
|
|
|
let outputVolume = Math.round(this.volume * this.crossfaderCoef); |
|
|
|
|
this.player.setVolume(outputVolume); |
|
|
|
|
this.volVal.innerHTML = outputVolume + '%'; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -116,38 +116,65 @@ function onYouTubeIframeAPIReady() {
|
|
|
|
|
playerB.init('PhEuAuhH418'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// while (bothPlayersReady < 2) {
|
|
|
|
|
//
|
|
|
|
|
// }
|
|
|
|
|
crossfader.addEventListener('input', (e) => { |
|
|
|
|
calcCrossfaderCoefs(crossfader.value); |
|
|
|
|
|
|
|
|
|
crossfader.addEventListener('input', (e) => { |
|
|
|
|
//Makes the slider stick on the middle
|
|
|
|
|
if (Math.abs(crossfader.value - 500) < 15) { |
|
|
|
|
crossfader.value = 500; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
updateCrossfader(crossfader.value); |
|
|
|
|
}); |
|
|
|
|
calcCrossfaderCoefs(crossfader.value); |
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
let boxShadowStyle = '0 0 3em #ddbddd' |
|
|
|
|
playerA.iframe.style.boxShadow = boxShadowStyle + componentToHex(Math.round(playerA.crossfaderCoef*255)); |
|
|
|
|
playerB.iframe.style.boxShadow = boxShadowStyle + componentToHex(Math.round(playerB.crossfaderCoef*255)); |
|
|
|
|
|
|
|
|
|
playerA.volumeUpdate(); |
|
|
|
|
playerB.volumeUpdate(); |
|
|
|
|
|
|
|
|
|
function updateCrossfader(input) { |
|
|
|
|
let normVal = crossfader.value / 1000; |
|
|
|
|
// updateCrossfader(crossfader.value);
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
// let coefB = normVal ** 2;
|
|
|
|
|
// let coefA = (1 - normVal) ** 2;
|
|
|
|
|
function calcCrossfaderCoefs(input) { |
|
|
|
|
let normVal = input / 1000; |
|
|
|
|
|
|
|
|
|
//Classic 'transition' curve
|
|
|
|
|
let coefB = Math.min(normVal*2, 1); |
|
|
|
|
let coefA = Math.min((1-normVal)*2, 1); |
|
|
|
|
|
|
|
|
|
//Cheap smooth curve
|
|
|
|
|
// let coefB = normVal ** 2;
|
|
|
|
|
// let coefA = (1 - normVal) ** 2;
|
|
|
|
|
|
|
|
|
|
// Sets box-shadow transparency depending on crossfader position
|
|
|
|
|
let boxShadowStyle = '0 0 3em #ddbddd' |
|
|
|
|
playerA.iframe.style.boxShadow = boxShadowStyle + componentToHex(Math.round(coefA*255)); |
|
|
|
|
playerB.iframe.style.boxShadow = boxShadowStyle + componentToHex(Math.round(coefB*255)); |
|
|
|
|
//Amplitude-preserving curve (but not power)
|
|
|
|
|
// let coefB = (normVal ** 2) * (3 - 2 * normVal);
|
|
|
|
|
// let coefA = 1 - coefB;
|
|
|
|
|
|
|
|
|
|
playerA.player.setVolume(playerA.volume * coefA); |
|
|
|
|
playerB.player.setVolume(playerB.volume * coefB); |
|
|
|
|
// return [coefA, coefB];
|
|
|
|
|
playerA.crossfaderCoef = coefA; |
|
|
|
|
playerB.crossfaderCoef = coefB; |
|
|
|
|
} |
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// function updateCrossfader(input) {
|
|
|
|
|
// let normVal = crossfader.value / 1000;
|
|
|
|
|
//
|
|
|
|
|
// // let coefB = normVal ** 2;
|
|
|
|
|
// // let coefA = (1 - normVal) ** 2;
|
|
|
|
|
// let coefB = Math.min(normVal*2, 1);
|
|
|
|
|
// let coefA = Math.min((1-normVal)*2, 1);
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
// // Sets box-shadow transparency depending on crossfader position
|
|
|
|
|
// let boxShadowStyle = '0 0 3em #ddbddd'
|
|
|
|
|
// playerA.iframe.style.boxShadow = boxShadowStyle + componentToHex(Math.round(coefA*255));
|
|
|
|
|
// playerB.iframe.style.boxShadow = boxShadowStyle + componentToHex(Math.round(coefB*255));
|
|
|
|
|
//
|
|
|
|
|
// playerA.player.setVolume(playerA.volume * coefA);
|
|
|
|
|
// playerB.player.setVolume(playerB.volume * coefB);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// From https://thomaspark.co/projects/needledrop/ by Thomas Park
|
|
|
|
|
// function getYouTubeID(url){
|
|
|
|
|