-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandomwalk_index.js
193 lines (171 loc) · 3.49 KB
/
randomwalk_index.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
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
const NUM_PARTICLES = 10000;
const PARTICLE_SPEED = 2;
const UPDATE_RATE = 10; // milliseconds
const COLOR_SHIFT_RATE = 3;
var canvas;
const adjacencyMap = {};
let isGrowing = true;
const color = [123, 123, 123];
function init() {
initCanvas();
initParticles();
}
function initCanvas() {
canvas = new Cathanvas('root', {
width: 1000,
height: 800,
});
}
function initParticles() {
createFixedSegment({
x: canvas.width / 2,
y: canvas.height / 2,
});
for (let i = 0; i < NUM_PARTICLES; i++) {
createWalker();
}
}
function createFixedSegment(coords) {
addToAdjacencyMap(Math.floor(coords.x), Math.floor(coords.y));
canvas.drawDot(coords, `rgb(${color[0]},${color[1]},${color[2]})`);
mutateAllColors();
}
/**
* Mutates the color of the next pixel, one value (r, g, or b) at a time
*/
function mutateColor() {
// pick between r g and b
let val;
let roll = Math.random() * 3;
if (roll < 1) {
val = 0;
} else if (roll < 2) {
val = 1;
} else {
val = 2;
}
// increase value up or down an amount
roll = Math.random() * 2;
if (roll < 1) {
color[val] = stayInsideColor(color[val] + COLOR_SHIFT_RATE);
} else {
color[val] = stayInsideColor(color[val] - COLOR_SHIFT_RATE);
}
}
/**
* Mutates the color of the next pixel, all values (r, g, and b) at a time
*/
function mutateAllColors() {
for (let i = 0; i < color.length; i++) {
let roll = Math.random() * 2;
if (roll < 1) {
color[i] = stayInsideColor(color[i] + COLOR_SHIFT_RATE);
} else {
color[i] = stayInsideColor(color[i] - COLOR_SHIFT_RATE);
}
}
}
function stayInsideColor(value) {
if (value < 0) { return 0; }
if (value > 255) { return 255; }
return value;
}
function addToAdjacencyMap(x, y) {
const adj = [
{
x: x + 1,
y: y,
},
{
x: x - 1,
y: y,
},
{
x: x,
y: y + 1,
},
{
x: x,
y: y - 1,
},
{
x: x + 1,
y: y + 1,
},
{
x: x + 1,
y: y - 1,
},
{
x: x - 1,
y: y + 1,
},
{
x: x - 1,
y: y - 1,
}
];
adj.forEach(point => {
if (adjacencyMap[point.x]) {
adjacencyMap[point.x][point.y] = true;
} else {
adjacencyMap[point.x] = {[point.y]: true}
}
});
}
function createWalker() {
const walker = {
x: Math.random() * canvas.width,
y: Math.random() * canvas.height,
};
startWalk(walker);
}
function startWalk(walker) {
let loopId;
function walk() {
if (!isGrowing) {
clearInterval(loopId);
return;
}
if (isFixedAdjacent(Math.floor(walker.x), Math.floor(walker.y))) {
createFixedSegment(walker);
createWalker(); // Experimental - more walkers per surface area
restartWalk(walker);
return;
}
const xrand = Math.random();
const yrand = Math.random();
if (xrand < 0.5) {
walker.x = stayInsideCanvas(walker.x + PARTICLE_SPEED, 'x')
} else {
walker.x = stayInsideCanvas(walker.x - PARTICLE_SPEED, 'x')
}
if (yrand < 0.5) {
walker.y = stayInsideCanvas(walker.y + PARTICLE_SPEED, 'y')
} else {
walker.y = stayInsideCanvas(walker.y - PARTICLE_SPEED, 'y')
}
}
loopId = setInterval(walk, UPDATE_RATE);
}
function restartWalk(walker) {
walker.x = Math.random() * canvas.width;
walker.y = Math.random() * canvas.height;
}
function isFixedAdjacent(x, y) {
return adjacencyMap[x] && adjacencyMap[x][y];
}
function stayInsideCanvas(n, axis) {
if (axis === 'x') {
if (n < 0) { return 0; }
if (n > canvas.width) { return canvas.width; }
}
if (axis === 'y') {
if (n < 0) { return 0; }
if (n > canvas.height) { return canvas.height; }
}
return n;
}
function stopGrowing() {
isGrowing = false;
}