-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdigirain.js
More file actions
153 lines (134 loc) · 3.79 KB
/
digirain.js
File metadata and controls
153 lines (134 loc) · 3.79 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//declare variables
// var symbol;
var symbolSize = 20;
var creditSize = 10;
var streams = [];
var sentences = [];
// var myFont;
var credits = 'excerpts from "Cybernetics and the Pioneers of Computer Art" by Thomas Dreher';
var credits2 = 'code by amilo';
function preload() {
// uncomment below to change text
// poems = loadStrings('mypoetry.txt');
poems = loadStrings('cybernetics.txt');
// uncomment to change the font
// myFont = loadFont("Chunk.otf");
}
function setup(){
createCanvas(
window.innerWidth,
window.innerHeight
);
background(0);
var y = symbolSize;
for (var i = 0; i < poems.length; i++) {
var newSentences = [];
newSentences = poems[i].split(". ");
for (var j = 0; j < newSentences.length; j++) {
sentences.push(newSentences[j]);
}
}
for (var i = 1; i < height / symbolSize; i++) {
var stream = new Stream();
stream.generateSymbols(random(-innerWidth, 0), y);
streams.push(stream);
y += symbolSize;
}
//change this to your font if necessary
//textFont(myFont);
textFont('Courier');
// textSize(symbolSize);
textSize(symbolSize);
// below example from the beginning of tutorial
// symbol = new Symbol(
// 0,
// height /2,
// random(1,3)
// );
// symbol.setToRandomSymbol();
}
function draw(){
background(0,255);
//draw the credits
showCredits();
//draw the streams
streams.forEach(function(stream) {
stream.render();
});
}
function Symbol(x, y, speed){
this.x = x;
this.y = y;
this.value;
this.speed = speed;
this.switchInterval = round(random(40, 80));
this.setToRandomSymbol = function() {
if (frameCount % this.switchInterval == 0) {
var myLine = sentences[round(random(0,sentences.length))];
if (myLine){
this.value = myLine;
// below was an attempt to break the string if too long
// if (myLine.length >= width/symbolSize) {
// console.log(myLine.substring(round(0,width/symbolSize)));
// this.value = myLine.substring(round(0,width/symbolSize));
// }else {this.value = myLine;}
}else{this.value = "Cybernetics and the Pioneers of Computer Art";}
}
// example from the tutorial
// this.value = String.fromCharCode(
// 0X30A0 + round(random(0, 96))
// );
}
this.rain = function() {
// original line as in the tutorial
// if (this.x >= width) {
// this.x =0;
// } else {
// this.x += this.speed;
// }
this.x = (this.x >= width) ? 0 : this.x += this.speed;
}
}
function Stream() {
this.symbols = [];
// commented since we don't want multiple lines on the same line
// this.totalSymbols = round(random(1,5));
this.totalSymbols = 1;
this.speed = random(1,3);
this.generateSymbols = function(x, y) {
for (var i = 0; i< this.totalSymbols; i++){
symbol = new Symbol(x,y, this.speed);
symbol.setToRandomSymbol();
this.symbols.push(symbol);
var myString = symbol.value;
var leng = myString.length;
// console.log(leng);
// this below is not actually necessary since we have only one line of text
x -= symbolSize*leng;
}
}
this.render = function() {
this.symbols.forEach(function(symbol) {
fill(255, 255, 255,200);
textSize(symbolSize);
text(symbol.value, symbol.x, symbol.y);
symbol.rain();
symbol.setToRandomSymbol();
});
}
}
function showCredits(){
fill(255,0,200);
textSize(creditSize);
text(credits , 10, height-symbolSize);
text(credits2 , width - credits2.length*creditSize, height-symbolSize);
}
function mousePressed() {
//this will check if the mouse is on the links and if pressed open them in a new tab
if (mouseY > (height - creditSize*2) && mouseX < (credits.length * creditSize)){
window.open('http://dreher.netzliteratur.net/4_Medienkunst_Kybernetike.html', "_blank");
}
if (mouseY > (height - creditSize*2) && mouseX > (width - credits2.length * creditSize)){
window.open('https://github.com/amilo/stringRain', "_blank");
}
}