forked from littledivy/drawille
-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualizer.ts
38 lines (30 loc) · 896 Bytes
/
visualizer.ts
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
// Random noise visualizer. Works on Deno and the browser.
import Canvas from "../canvas.ts";
let canvas = new Canvas(120, 120);
let ctx = canvas.getContext("2d");
let bufferLength = 128;
let dataArray = [];
let WIDTH = canvas.width;
let HEIGHT = canvas.height;
let barWidth = (WIDTH / bufferLength) * 2.5;
let barHeight;
let x = 0;
function renderFrame() {
// Clear canvas for next frame.
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.save();
x = 0;
// Generate random data
dataArray = [...Array(bufferLength)].map(() =>
Math.floor(Math.random() * bufferLength)
);
for (var i = 0; i < bufferLength; i++) {
barHeight = dataArray[i];
ctx.fillRect(x, HEIGHT - barHeight, barWidth, barHeight);
x += barWidth + 1;
}
ctx.restore();
ctx.strokeRect(0, 0, canvas.width, canvas.height);
console.log(ctx.toString());
}
setInterval(renderFrame, 140);