-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathperParticles.js
More file actions
126 lines (108 loc) · 2.93 KB
/
perParticles.js
File metadata and controls
126 lines (108 loc) · 2.93 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
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
let minNodeSize = 1;
let maxNodeSize = 3;
let minVelocity = 0.25;
let maxVelocity = 2;
let nodesAmount = 1000;
let backgroundColor = '#222222';
let nodeColor = '#D1C4E9';
let lineColor = '#CE93D8';
const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');
ctx.canvas.width = document.body.clientWidth;
ctx.canvas.height = window.innerHeight;
if (ctx.canvas.width > 5120) {
ctx.canvas.width = 5120;
}
let nodes = [];
let lines = [];
let daemonsWorking = 0;
let timestamp = Date.now();
const updateDaemon = new Worker('updateDaemon.js');
const bucketDaemons = [
new Worker('bucketDaemon.js'),
new Worker('bucketDaemon.js'),
new Worker('bucketDaemon.js'),
new Worker('bucketDaemon.js'),
new Worker('bucketDaemon.js')
];
const update = () => {
document.querySelector('.nodeCount').textContent = `${nodes.length} nodes`;
lines = [];
updateDaemon.postMessage({
command: 'update',
nodes,
options: {
minNodeSize,
maxNodeSize,
minVelocity,
maxVelocity,
nodesAmount,
width: ctx.canvas.width,
height: ctx.canvas.height
}
});
};
const processBuckets = e => {
nodes = e.data.nodes;
e.data.buckets.forEach((bucket, index) => {
daemonsWorking += 1;
bucketDaemons[index].postMessage({ command: 'process', bucket });
});
};
const gatherLines = e => {
lines = [...lines, ...e.data.lines];
daemonsWorking -= 1;
if (daemonsWorking === 0) {
draw();
}
};
const draw = e => {
ctx.fillStyle = backgroundColor;
ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
lines.forEach(line => {
const { n1, n2, distance } = line;
ctx.strokeStyle = lineColor;
ctx.lineWidth = 50 / distance < 0.2 ? 50 / distance : 0.2;
ctx.beginPath();
ctx.moveTo(n1.x, n1.y);
ctx.lineTo(n2.x, n2.y);
ctx.stroke();
ctx.lineWidth = 0.1;
});
nodes.forEach(node => {
ctx.strokeStyle = nodeColor;
ctx.fillStyle = nodeColor;
ctx.beginPath();
ctx.arc(node.x, node.y, node.r, 0, 2 * Math.PI);
ctx.stroke();
ctx.fill();
});
requestAnimationFrame(update);
};
updateDaemon.addEventListener('message', processBuckets);
bucketDaemons.forEach(daemon => {
daemon.addEventListener('message', gatherLines);
});
window.addEventListener('resize', () => {
ctx.canvas.width = document.body.clientWidth;
ctx.canvas.height = window.innerHeight;
if (ctx.canvas.width > 5120) {
ctx.canvas.width = 5120;
}
});
window.addEventListener('unload', () => {
updateDaemon.postMessage({ command: 'stop' });
bucketDaemons.forEach(daemon => daemon.postMessage({ command: 'stop' }));
});
const stats = new Stats();
const nodeCount = document.createElement('div');
nodeCount.classList.add('nodeCount');
stats.dom.appendChild(nodeCount);
stats.dom.style.margin = '5px';
stats.dom.style.pointerEvents = 'none';
document.body.appendChild(stats.dom);
requestAnimationFrame(function loop() {
stats.update();
requestAnimationFrame(loop);
});
update();