-
Notifications
You must be signed in to change notification settings - Fork 1
/
pixi-0101.html
176 lines (151 loc) · 5.9 KB
/
pixi-0101.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
<!doctype html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href='//fonts.googleapis.com/css?family=Share+Tech+Mono' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="css/0101-matrix.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/pixi.js/3.0.6/pixi.min.js"></script>
</head>
<body>
<div id="pixi-canvas"></div>
<script>
// TODO: there's a memory issue with the object deletion
// the app slows over time due to this. Likely line 98.
// http://stackoverflow.com/questions/11582512/how-to-get-url-parameters-with-javascript/11582513#11582513
function getURLParameter(name) {
return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search) || [, ""])[1].replace(/\+/g, '%20')) || null
}
var maxChars = getURLParameter('maxchars');
if (!maxChars) maxChars = 120;
var maxShadowLength = getURLParameter('maxshadowlength');
if (!maxShadowLength) maxShadowLength = 16;
var percentAlpha = 1/maxShadowLength;
var maxShadows = maxChars * maxShadowLength;
// get window dimensions
var displayWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
var displayHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
function rand (low, high) {
return Math.floor((Math.random() * high) + low);
};
var chars = [];
var shadows = [];
var characters = "01";
var renderer = PIXI.autoDetectRenderer(displayWidth, displayHeight, {
backgroundColor: 0x000000
});
document.body.appendChild(renderer.view);
var stage = new PIXI.Container();
var styleLTR = {
font: '40px Share Tech Mono, Courier, monospace',
fill: '#0000ff'
};
var styleRTL = {
font: '40px Share Tech Mono, Courier, monospace',
fill: '#00ff00'
};
var styleHit = {
font: '40px Share Tech Mono, Courier, monospace',
fill: '#ff0000'
};
// randomize chars when they hit screen edges
function randomize_char(c) {
// < 0.5 = LTR, > 0.5 = LTR
var dir = Math.random();
if(dir < 0.5) {
c.x = 0;
c.style = styleLTR;
} else {
c.x = displayWidth;
c.style = styleRTL;
}
c.y = rand(0, displayHeight);
c.text = characters[rand(0, characters.length)];
// vary the size
var scale = 1 * Math.random();
if (scale > 0.25) {
c.scale.x = scale;
c.scale.y = scale;
}
return;
};
// create chars
for (var i = 0; i < maxChars; i++) {
var c = new PIXI.Text('0', styleLTR);
randomize_char(c);
stage.addChild(c);
chars.push(c);
}
// populate shadow array
for (var i = 0; i < maxShadows; i++) {
var shadow = new PIXI.Text('0', styleRTL);
// keep shadows unrendered initially
shadow.renderable = false;
stage.addChild(shadow);
shadows.push(shadow);
}
function findAvailableShadow() {
for (var i = 0; i < maxShadows; i++) {
var shadow = shadows[i];
if (!shadow.renderable) {
return i;
}
}
// no shadow available, return -1
return -1;
}
// start animating
animate();
function animate() {
requestAnimationFrame(animate);
// animate shadows
for (var i = 0; i < shadows.length; i++) {
var shadow = shadows[i];
// don't render off-screen shadows, leave them open for future
if (shadow.alpha <= 0) {
shadow.renderable = false;
} else {
shadow.alpha -= percentAlpha;
}
}
// animate chars
for (var i = 0; i < chars.length; i++) {
var c = chars[i];
// first render shadow for this char
var s = findAvailableShadow();
if (s >= 0) {
var shadow = shadows[s];
shadow.text = c.text;
shadow.style = c.style;
shadow.x = c.x;
shadow.y = c.y;
shadow.scale.x = c.scale.x;
shadow.scale.y = c.scale.y;
// decrement alpha a bit
shadow.alpha = c.alpha - percentAlpha;
shadow.renderable = true;
}
//move chars depending on direction
if(c.style == styleLTR) c.x += c.width
else c.x -= c.width;
if (c.x > displayWidth || c.x < 0) {
randomize_char(c);
}
// change the character
c.text = characters[rand(0, characters.length)];
// run hit test to change char if collision
for (var j = 0; j < chars.length; j++) {
var b = chars[j];
var point = new PIXI.Point(b.x, b.y);
if (c.containsPoint(point)) {
c.style = styleHit;
}
// must delete this so it doesn't eat memory!
//point.destory();
}
}
// render the root container
renderer.render(stage);
};
</script>
</body>
</html>