Created
November 11, 2017 02:19
-
-
Save hibuno/585e8122d9ebe1d3018a8ef39bf560e0 to your computer and use it in GitHub Desktop.
Script for Audio Context Tutorial at my medium account (https://medium.com/@muhibbudins)
This file contains hidden or 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
// Prepare Canvas | |
const context = canvas.getContext('2d'); | |
// Draw wave | |
function draw() { | |
requestAnimationFrame(draw); | |
analyser.getByteFrequencyData(frequency); | |
context.fillStyle = '#101010'; | |
context.fillRect(0, 0, canvas.width, canvas.height); | |
context.lineWidth = 1.5; | |
context.strokeStyle = colorize(); | |
context.beginPath(); | |
var sliceWidth = canvas.width * 1.0 / frequencyLength; | |
var x = 0; | |
for (var i = 0; i < frequencyLength; i++) { | |
var v = frequency[i] / 256.0; | |
var y = v * canvas.height / 2; | |
if (i === 0) { | |
context.moveTo(x, y); | |
} else { | |
context.lineTo(x, y); | |
} | |
x += sliceWidth; | |
} | |
context.lineTo(canvas.width, canvas.height / 2); | |
context.stroke(); | |
}; | |
draw(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment