Skip to content

Instantly share code, notes, and snippets.

@muhibbudins
Created November 11, 2017 02:19
Show Gist options
  • Save muhibbudins/585e8122d9ebe1d3018a8ef39bf560e0 to your computer and use it in GitHub Desktop.
Save muhibbudins/585e8122d9ebe1d3018a8ef39bf560e0 to your computer and use it in GitHub Desktop.
Script for Audio Context Tutorial at my medium account (https://medium.com/@muhibbudins)
// 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