-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackground.js
More file actions
139 lines (115 loc) · 3.63 KB
/
background.js
File metadata and controls
139 lines (115 loc) · 3.63 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
127
128
129
130
131
132
133
134
135
136
137
138
139
const canvas = document.getElementById("background");
const context = canvas.getContext("2d");
mousex = 0;
mousey = 0;
onmousemove = function (e) { mousex = e.clientX, mousey = e.clientY }
let width, height;
const particles = [];
function resize() {
width = window.innerWidth;
height = window.innerHeight;
canvas.width = width;
canvas.height = height;
}
function getColor() {
let elem = document.getElementsByClassName("header")[0];
rgb = window.getComputedStyle(elem).color;
return rgb
}
class Particle {
constructor() {
this.posX = Math.random() * width;
this.posY = Math.random() * height;
this.radius = 3;
this.speedX = (Math.random() - 0.5) * 2;
this.speedY = (Math.random() - 0.5) * 2;
this.color = "#000000";
}
update() {
try {
this.color = getColor(false);
}
catch (e) {
console.warn(e);
}
const dx = mousex - this.posX;
const dy = mousey - this.posY;
const ds = Math.sqrt(dx * dx + dy * dy);
if (ds < 100) {
this.speedX = - (dx) * 0.125;
this.speedY = - (dy) * 0.125;
}
if (this.posX + this.radius > width) {
this.speedX = -this.speedX + (Math.random() / 2);
this.posX = width - this.radius;
}
if (this.posX - this.radius < 0) {
this.speedX = -this.speedX + (Math.random() / 2);
this.posX = 0 + this.radius;
}
if (this.posY + this.radius > height) {
this.speedY = -this.speedY + (Math.random() / 2);
this.posY = height - this.radius;
}
if (this.posY - this.radius < 0) {
this.speedY = -this.speedY + (Math.random() / 2);
this.posY = 0 + this.radius;
}
if (Math.abs(this.speedX / 1.01) > 0.2) {
this.speedX = this.speedX / 1.01;
}
if (Math.abs(this.speedY / 1.01) > 0.2) {
this.speedY = this.speedY / 1.01;
}
this.posX += this.speedX;
this.posY += this.speedY;
}
draw() {
context.beginPath();
context.arc(this.posX, this.posY, this.radius, 0, Math.PI * 2);
context.fillStyle = this.color;
context.fill();
context.closePath();
}
}
function createDots(num) {
for (let i = 0; i < num; i++) {
particles.push(new Particle());
}
}
function distance(dot1, dot2) {
const dx = dot1.posX - dot2.posX;
const dy = dot1.posY - dot2.posY;
return Math.sqrt(dx * dx + dy * dy);
}
function drawLines() {
for (let i = 0; i < particles.length; i++) {
for (let j = i + 1; j < particles.length; j++) {
const dist = distance(particles[i], particles[j]);
if (dist < 200) {
const opacity = 1 - dist / 200;
context.beginPath();
context.moveTo(particles[i].posX, particles[i].posY);
context.lineTo(particles[j].posX, particles[j].posY);
rgb = getColor().replace(/[^\d,]/g, '').split(',');
context.strokeStyle = 'rgba(' + rgb[0] + ',' + rgb[1] + ',' + rgb[2] + `, ${opacity})`;
context.lineWidth = particles[i].radius / 1;
context.stroke();
context.closePath();
}
}
}
}
function animate() {
context.clearRect(0, 0, width, height);
for (let i = 0; i < particles.length; i++) {
particles[i].update();
particles[i].draw();
}
drawLines();
requestAnimationFrame(animate);
}
resize();
createDots(100);
animate();
window.addEventListener("resize", resize);