forked from Shubham56-droid/Web-Components-Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualizer.js
More file actions
71 lines (64 loc) · 2.17 KB
/
visualizer.js
File metadata and controls
71 lines (64 loc) · 2.17 KB
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
function main(){
const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
class Bar{
constructor(x,y,width,height,color,index){
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.color = color;
this.index = index;
}
update(micInput){
const sound = micInput * 1000;
if(sound > this.height){
this.height = sound;
}else{
this.height -= this.height * 0.03;
}
}
draw(context){
// context.fillStyle = this.color;
// context.fillRect(this.x,this.y,this.width,this.height);
context.strokeStyle = this.color;
context.save();
context.translate(canvas.width/2, canvas.height/2);
context.rotate(this.index * 0.05);
context.beginPath();
context.moveTo(this.x,this.y);
context.lineTo(this.y,this.height);
context.stroke();
context.strokeRect(this.x,this.y,this.x,this.height)
context.restore();
}
}
const microphone = new Microphone();
let bars = [];
let barWidth = canvas.width / 256;
function createBars(){
for(let i = 0; i < 256; i++){
let color = 'hsl('+ i * 2 +',100%,50%)';
bars.push(new Bar(0,i * 0.9,1,50,color,i));
}
}
createBars();
console.log(bars);
function animate(){
if(microphone.initialized){
ctx.clearRect(0,0,canvas.width,canvas.height);
const samples = microphone.getSamples();
console.log(samples);
// generated audio samples from microphone
// animate bars based on microphone data
bars.forEach(function(bar, i){
bar.update(samples[i]);
bar.draw(ctx);
});
}
requestAnimationFrame(animate);
}
animate();
}