-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathupdateDaemon.js
More file actions
68 lines (55 loc) · 2.11 KB
/
updateDaemon.js
File metadata and controls
68 lines (55 loc) · 2.11 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
const createNode = options => {
const {
minNodeSize,
maxNodeSize,
minVelocity,
maxVelocity,
width,
height
} = options;
const nodeX = Math.floor(Math.random() * (width + 100) - 50);
const nodeY = Math.floor(Math.random() * (height + 100) - 50);
const nodeVelocityX = (Math.random() * maxVelocity + minVelocity) * (Math.round(Math.random()) ? 1 : -1);
const nodeVelocityY = (Math.random() * maxVelocity + minVelocity) * (Math.round(Math.random()) ? 1 : -1);
const nodeRadius = Math.floor(Math.random() * maxNodeSize + minNodeSize);
return { x: nodeX, y: nodeY, r: nodeRadius, vx: nodeVelocityX, vy: nodeVelocityY };
};
self.addEventListener('message', e => {
const { command, nodes, options } = e.data;
switch (command) {
case 'update':
const { nodesAmount, maxVelocity, minVelocity, width, height } = options;
while (nodes.length < nodesAmount) {
nodes.push(createNode(options));
}
if (nodes.length > nodesAmount) {
nodes.length = nodesAmount;
}
for (let i = 0; i < nodes.length; i++) {
nodes[i].x += nodes[i].vx;
nodes[i].y += nodes[i].vy;
if ((nodes[i].x < -25 || nodes[i].x > width + 25) || (nodes[i].y < -25 || nodes[i].y > height + 25)) {
nodes[i].vx = (Math.random() * maxVelocity + minVelocity) * (nodes[i].vx < 0 ? 1 : -1);
nodes[i].vy = (Math.random() * maxVelocity + minVelocity) * (nodes[i].vy < 0 ? 1 : -1);
}
}
const buckets = [];
const numberOfBuckets = 5;
const bucketWidth = Math.ceil(width / numberOfBuckets);
for (let i = 0; i < numberOfBuckets; i++) {
buckets.push({ x: i * bucketWidth - 50, y: -25, w: bucketWidth + 100, h: height + 50, nodes: [] });
}
nodes.forEach((node) => {
buckets.forEach((bucket) => {
if (node.x >= bucket.x && node.x <= bucket.x + bucket.w && node.y >= bucket.y && node.y <= bucket.y + bucket.h) {
bucket.nodes.push(node);
}
});
});
self.postMessage({ nodes, buckets });
break;
case 'stop':
self.close();
break;
};
});