-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
72 lines (60 loc) · 1.84 KB
/
background.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
const canvas = document.getElementById("bg");
const display = canvas.getContext('2d');
let nodes = [];
// for display resize and stuff
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
window.addEventListener("resize", function() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
});
function randomNumber(min, max) { // random number function
return Math.round(Math.random() * (max - min) + min);
}
function rect(x, y, width, height, color) { // rect function
display.fillStyle = color;
display.fillRect(x, y, width, height);
}
class Node {
constructor(x, y) {
this.x = x;
this.y = y;
this.vx = randomNumber(-1, 1);
this.vy = randomNumber(-1, 1);
nodes.push(this);
}
}
for (let i = 0; i < Math.round(canvas.width/25); i++) {
new Node(randomNumber(0, canvas.width), randomNumber(0, canvas.height));
}
function dist(x1, y1, x2, y2){
let y = x2 - x1;
let x = y2 - y1;
return Math.sqrt(x * x + y * y);
}
function line(x1, y1, x2, y2) {
display.strokeStyle = "rgba(64, 118, 255, 0.9)";
display.lineWidth = 3;
display.beginPath();
display.moveTo(x1, y1);
display.lineTo(x2, y2);
display.stroke();
}
function draw() {
display.clearRect(0, 0, canvas.width, canvas.height);
nodes.forEach((node) => {
rect(node.x, node.y, 10, 10, "rgba(255, 255, 255)");
node.x += node.vx;
node.y += node.vy;
if (node.x > canvas.width || node.x < 0 || node.y > canvas.height || node.y < 0) {
node.vx *= -1;
node.vy *= -1;
}
nodes.forEach((childNode) => {
if (dist(node.x, node.y, childNode.x, childNode.y) < 100) {
line(node.x, node.y, childNode.x, childNode.y);
}
})
})
}
setInterval(draw, 1000/60); // 60 fps