-
Notifications
You must be signed in to change notification settings - Fork 44
/
messages.html
260 lines (218 loc) · 6.04 KB
/
messages.html
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<script src="https://cdn.pubnub.com/pubnub.min.js"></script>
<script src="unistring.js"></script>
<script src="symbols.js"></script>
<title>Twitter Matrix</title>
<style>
@font-face {
font-family: 'Unifont';
src: url('unifont.woff') format('woff'), url('unifont.ttf') format('truetype');
font-weight: normal;
font-style: normal;
}
html, body, canvas, script { margin: 0; padding: 0; overflow: hidden; }
body { background: black; }
canvas { cursor: pointer; }
</style>
</head>
<body>
<canvas id="c"></canvas>
<audio id="a" loop="loop" preload="auto">
<source src="DramaticEndingShortVersion.mp3" type="audio/mpeg">
</audio>
<script>
onload = function() {
// based on
// https://codepen.io/P3R0/pen/MwgoKv
// https://www.pubnub.com/developers/realtime-data-streams/twitter-stream/
// visualization settings
var maxMessages = 10240;
var font_size = 16;
var minSpeed = 22;
var maxSpeed = 99;
var speedStep = 11;
var currentSpeed = 88;
var maxStepsToResetColumn = 200;
var twitterMessages = []; // data stack (queue)
var messages = []; // current printed data
var a = document.getElementById("a");
var c = document.getElementById("c");
var ctx = c.getContext("2d");
//making the canvas full screen
c.height = window.innerHeight;
c.width = window.innerWidth;
// limits
var columns = Math.floor(c.width / font_size);
var rows = Math.floor(c.height / font_size);
var middle = Math.floor(columns / 2);
// x is the x coordinate or column number
for (var x = 0; x < columns; x++)
messages.push({ y: Math.floor(Math.random() * rows), offset: 0, symbols: [] });
var timerId = setInterval(draw, currentSpeed);
mouseState = { x: -1, y: -1 };
currentColumnState = { };
function setColumnState(column)
{
currentColumnState.column = column;
currentColumnState.messageEnd = false;
currentColumnState.steps = 0;
}
function resetColumnState()
{
setColumnState(-1);
}
resetColumnState();
function updateColumnState(e)
{
var column = Math.floor(e.clientX / font_size);
if (currentColumnState.column != column)
{
setColumnState(column);
}
}
function onClick(e)
{
updateColumnState(e);
var message = messages[currentColumnState.column];
if (message.url)
{
window.open(message.url, '_blank');
}
}
function toggleAudio()
{
if (a.paused)
{
a.play();
}
else
{
a.pause();
}
}
onmousemove = updateColumnState;
onclick = onClick;
onkeyup = function(e)
{
if (e.keyCode == 32)
{
toggleAudio();
}
}
//drawing the characters
function draw()
{
// Update column state
var currentColumn = currentColumnState.column;
if (currentColumn > 0)
{
currentColumnState.steps++;
if (currentColumnState.steps > maxStepsToResetColumn)
{
resetColumnState();
currentColumn = -1;
}
}
// Fade old symbols away one step
if (currentColumn > 0)
{
//Black BG for the canvas
//translucent BG to show trail
ctx.fillStyle = "rgba(0, 0, 0, 0.10)";
ctx.fillRect(0, 0, currentColumn * font_size, c.height);
ctx.fillRect((currentColumn + 1) * font_size, 0, c.width, c.height);
}
else
{
//Black BG for the canvas
//translucent BG to show trail
ctx.fillStyle = "rgba(0, 0, 0, 0.10)";
ctx.fillRect(0, 0, c.width, c.height);
}
ctx.fillStyle = "#4F4"; //green text
ctx.font = font_size + 'px "Unifont", "Courier New", "Courier"';
ctx.textAlign = "center";
// draw new letters
for (var i = 0; i < columns; i++)
{
// iterate from middle of the screen
var x = i % 2 == 1 ? middle + (Math.floor(i / 2) + 1) : middle - Math.floor(i / 2);
if (x >= columns || x == currentColumn)
continue;
var message = messages[x];
if (message.offset < message.symbols.length)
{
var symbol = message.symbols[message.offset];
if (symbol)
ctx.fillText(symbol, (x * font_size) + (font_size / 2), message.y * font_size);
message.offset++;
}
else
{
var startNewColumn = Math.random() > 0.95;
if (twitterMessages.length > 0 && startNewColumn)
{
var twitterMessage = twitterMessages.pop();
var symbols = getSymbols(twitterMessage.text);
message.url = twitterMessage.url;
message.offset = 0;
message.symbols = symbols;
}
}
//incrementing Y coordinate
if (message.y >= rows)
message.y = 1;
else
message.y++;
}
// adjust speed
if (twitterMessages.length > (columns * 2))
{
if (currentSpeed > minSpeed)
{
currentSpeed -= speedStep; // speed up
clearInterval(timerId);
timerId = setInterval(draw, currentSpeed);
}
}
else if (twitterMessages.length < (columns / 2))
{
if (currentSpeed < maxSpeed)
{
currentSpeed += speedStep; // slow down
clearInterval(timerId);
timerId = setInterval(draw, currentSpeed);
}
}
}
// Take language from url hash parameter
language = window.location.hash.substr(1).toLowerCase();
function addTwitterMessage(msg) {
var messageUrl = "https://twitter.com/" + msg.user.screen_name + "/status/" + msg.id_str;
twitterMessages.push({ text: msg.text, url: messageUrl });
}
PUBNUB.init({
subscribe_key: 'sub-c-78806dd4-42a6-11e4-aed8-02ee2ddab7fe',
ssl: true
}).subscribe({
channel: 'pubnub-twitter',
message: function(msg) {
// skip if there are too many messages (events)
if (twitterMessages.length < maxMessages)
{
if (!language || msg.lang == language)
{
addTwitterMessage(msg);
}
}
}
});
}
</script>
</body>
</html>