-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrixSketch.js
90 lines (78 loc) · 1.84 KB
/
matrixSketch.js
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
p5.disableFriendlyErrors = true;
var pScale = 10;
var symbols = [];
var video;
var r;
var g;
var b;
var brightness;
var gBrightness;
function setup() {
createCanvas(window.innerWidth, window.innerHeight);
pixelDensity(1);
video = createCapture(VIDEO);
video.size(width / pScale, height / pScale);
video.hide();
var x = 0;
for (var i = 0; i <= width / pScale; i++) {
var streamSize = random(40, 90);
var y = random(0, -100);
var speed = random(0.1, 1);
for (var j = 0; j <= streamSize; j++) {
var symbol = new Symbol(x, y, speed);
symbol.setToRandomSymbol();
symbols.push(symbol);
y--;
}
x++;
}
textSize(pScale);
}
function draw() {
background(0, 230);
video.loadPixels();
symbols.forEach(function(symbol) {
if (
symbol.x * pScale > width ||
symbol.x * pScale < 0 ||
symbol.y * pScale > height ||
symbol.y * pScale < 0
) {
} else {
var index =
(video.width -
Math.ceil(symbol.x) +
1 +
Math.ceil(symbol.y) * video.width) *
4;
r = video.pixels[index];
g = video.pixels[index + 1];
b = video.pixels[index + 2];
brightness = (r + g + b) / 3;
gBrightness = map(g, 0, 255, 20, 260);
if (!r || !g || !b) {
fill(0, 0, 0);
} else {
fill(30, g, 30, gBrightness);
}
text(symbol.value, symbol.x * pScale, symbol.y * pScale);
}
symbol.rain();
symbol.setToRandomSymbol();
});
}
function Symbol(x, y, speed) {
this.x = x;
this.y = y;
this.value;
this.speed = speed;
this.switchInterval = round(random(2, 25));
this.setToRandomSymbol = function() {
if (frameCount % this.switchInterval == 0) {
this.value = String.fromCharCode(0x30a0 + round(random(0, 96)));
}
};
this.rain = function() {
this.y = this.y * pScale >= height ? 0 : (this.y += this.speed);
};
}