Created
January 26, 2021 04:33
-
-
Save ngochuy13/7a56c956352f27eec55b207b0f61a62c to your computer and use it in GitHub Desktop.
audio-bg-animation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<body style=" | |
background-color: #bfbfbf; | |
display: flex; | |
align-items: center; | |
justify-content: center;"> | |
<img id="img" src="laptrinhvientv.jpg" style=" | |
width: 200px; | |
border-radius: 50%; | |
" /> | |
</body> | |
</html> | |
<script> | |
audio = new Audio(); | |
audio.src = "xedangdithichay.mp3"; | |
analyser = null; | |
document.onclick = () => { | |
context = new AudioContext(); | |
analyser = context.createAnalyser(); | |
context.createMediaElementSource(audio) | |
.connect(analyser); | |
analyser.connect(context.destination); | |
audio.play(); | |
loop(); | |
} | |
function loop() { | |
window.requestAnimationFrame(loop) | |
fbc = new Uint8Array(analyser.frequencyBinCount); | |
analyser.getByteFrequencyData(fbc); | |
avg = fbc.reduce((a, b) => a + b, 0) / fbc.length; | |
document.getElementById('img').style.width = avg * 5; | |
document.body.style.backgroundColor = 'rgb(' + avg + ',' + avg + ',' + avg + ')'; | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment