-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
224 lines (190 loc) · 7.96 KB
/
index.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
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html" manifest="/colorGrid/manifest.appcache">
<head>
<title>Experiment with Color</title>
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta name="description" content="A small experiment with color" />
<meta name="keywords" content="color, animation" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<style type="text/css">
body {
margin: 0;
}
table {
height: 100%;
position: fixed;
width: 100%;
}
td {
background: rgb(127,127,127);
height: 4%;
padding: 0;
-moz-transition: all .15s linear;
-o-transition: all .15s linear;
-webkit-transition: all .15s linear;
transition: all .15s linear;
width: 4%;
}
</style>
</head>
<body>
<script type="text/javascript">
/*jslint browser: true, white: true, for: true */
/*global document, setInterval */
(function () {
'use strict';
var cells = [],
// Max random change per iteration for any cell's r, g or b value.
variance = 5,
// Each cell will consider the color of cells adjacent in these
// directions.
sides = ['up', 'left', 'right'],
// Cell size of the table. Must match the CSS declaration of
// height / width.
size = {x: 20, y: 20},
// Element to append the table to.
parent = document.body,
// Percent chance of a random event happening on any one cell.
randomEvent = 0,
// When a randomEvent occurs, it's impact will increase the color
// variance to this value.
randomVariance = 200,
// Timeout (measured in ms) between iterations. Must match the CSS
// declaration of transition timeout.
speed = 150;
var varianceCache = ((variance * 2) + 1),
randomVarianceCache = ((randomVariance * 2) + 1);
var addEvent = function (elm, event, action, capture) {
capture = capture || false;
if (elm.addEventListener) {
elm.addEventListener(event, action, capture);
}
else if (elm.attachEvent) {
elm.attachEvent('on' + event, action);
}
else {
elm['on' + event] = action;
}
};
var clickCell = function (e) {
var cell,
colors = {},
x,
y;
e = e || window.event;
if (e.target) {
cell = e.target;
}
else {
cell = e.srcElement;
}
x = cell.dataset.x;
y = cell.dataset.y;
cells[y][x].r = cells[y][x].r + Math.floor(Math.random() * randomVarianceCache) - randomVariance;
cells[y][x].g = cells[y][x].g + Math.floor(Math.random() * randomVarianceCache) - randomVariance;
cells[y][x].b = cells[y][x].b + Math.floor(Math.random() * randomVarianceCache) - randomVariance;
cell.style.backgroundColor = 'rgb(' + cells[y][x].r + ',' + cells[y][x].g + ',' + cells[y][x].b + ')';
};
var setup = function () {
var table = document.createElement('table'),
tbody = document.createElement('tbody'),
row,
y = 0,
x = 0,
loopCell = function (x, row) {
var cell;
for(x; x < size.x; x += 1) {
cell = document.createElement('td');
cell.dataset.x = x;
cell.dataset.y = y;
row.appendChild(cell);
// We'll default the table color to a neutral gray.
cells[y][x] = {node: cell, r: 127, g: 127, b: 127};
}
};
table.cellSpacing = '0';
table.appendChild(tbody);
// Loop through the number of rows we want and create a <tr> for each
// one.
for(y; y < size.y; y += 1) {
row = document.createElement('tr');
cells[y] = [];
// For each row, loop through and add in the cells that match the
// number of columns we want.
loopCell(x, row);
tbody.appendChild(row);
}
// When we're all done manipulating the new nodes, we'll insert them
// into the DOM.
parent.appendChild(table);
addEvent(table, 'click', clickCell);
};
var iterate = function () {
var y = 0,
x = 0,
loopCell = function (cell, directions) {
var colors = {r: cell.r,
g: cell.g,
b: cell.b},
total = 1,
direction;
// Take influence from adjacent cells.
for(direction in directions) {
if(directions[direction] && (sides.indexOf(direction) !== -1)) {
total += 1;
colors.r += directions[direction].r;
colors.g += directions[direction].g;
colors.b += directions[direction].b;
}
}
// Now average out the colors for the current cell.
colors.r = colors.r / total;
colors.g = colors.g / total;
colors.b = colors.b / total;
// A rare random event will greatly increase the color variance
// on given cell.
if((randomEvent) && (Math.random() * 100) < randomEvent) {
colors.r += Math.floor(Math.random() * randomVarianceCache) - randomVariance;
colors.g += Math.floor(Math.random() * randomVarianceCache) - randomVariance;
colors.b += Math.floor(Math.random() * randomVarianceCache) - randomVariance;
}
// Otherwise, we introduce a relatively small color variance.
else {
colors.r += Math.floor(Math.random() * varianceCache) - variance;
colors.g += Math.floor(Math.random() * varianceCache) - variance;
colors.b += Math.floor(Math.random() * varianceCache) - variance;
}
// Keep the colors between 0 and 255 - and only accept whole
// numbers.
cell.r = Math.max(Math.min(Math.round(colors.r), 255), 0);
cell.g = Math.max(Math.min(Math.round(colors.g), 255), 0);
cell.b = Math.max(Math.min(Math.round(colors.b), 255), 0);
cell.node.style.backgroundColor = 'rgb(' + cell.r + ',' + cell.g + ',' + cell.b + ')';
},
loopRows = function (y, x) {
var directions = {};
for(x; x < cells[y].length; x += 1) {
directions.up = y ? cells[(y - 1)][x] : null;
directions.down = y < cells.length - 1 ? cells[(y + 1)][x] : null;
directions.left = x ? cells[y][(x - 1)] : null;
directions.right = x < cells[y].length - 1 ? cells[y][(x + 1)] : null;
loopCell(cells[y][x], directions);
}
};
for(y; y < cells.length; y += 1) {
loopRows(y, x);
}
};
setup();
setInterval(function () {
iterate();
}, speed);
}());
</script>
</body>
</html>