Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
125 lines (113 loc) · 3.39 KB
/
Copy pathscript.js
File metadata and controls
125 lines (113 loc) · 3.39 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
const canvas = document.querySelector(".particle-canvas");
const ctx = canvas.getContext("2d");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
const lineDistanceX = canvas.width / 7;
const lineDistanceY = canvas.height / 7;
let particlesArray;
const mouse = {
x: null,
y: null,
radius: (canvas.width / 100) * (canvas.height / 100),
};
window.addEventListener("mousemove", (event) => {
mouse.x = event.x;
mouse.y = event.y;
});
window.addEventListener("mouseout", () => {
mouse.x = null;
mouse.y = null;
});
window.addEventListener("resize", () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
mouse.radius = (canvas.width / 100) * (canvas.height / 100);
init();
});
class Particle {
constructor(x, y, directionX, directionY, size, color) {
(this.x = x), (this.y = y);
(this.directionX = directionX),
(this.directionY = directionY),
(this.size = size),
(this.color = color);
}
draw() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2, false);
ctx.fillStyle = "#16222A";
ctx.fill();
}
update() {
if (this.x > canvas.width || this.x < 0) {
this.directionX = -this.directionX;
}
if (this.y > canvas.height || this.y < 0) {
this.directionY = -this.directionY;
}
const distanceX = mouse.x - this.x;
const distanceY = mouse.y - this.y;
const distance = Math.sqrt(distanceX * distanceX + distanceY * distanceY);
if (distance < mouse.radius + this.size) {
if (mouse.x < this.x && this.x < canvas.width - this.size * 10) {
this.x += 10;
}
if (mouse.x > this.x && this.x > this.size * 10) {
this.x -= 10;
}
if (mouse.y < this.y && this.y < canvas.height - this.size * 10) {
this.y += 10;
}
if (mouse.y > this.y && this.y > this.size * 10) {
this.y -= 10;
}
}
this.x += this.directionX;
this.y += this.directionY;
this.draw();
}
}
function init() {
particlesArray = [];
const numberOfParticles = (canvas.height * canvas.width) / 9000;
for (let i = 0; i < numberOfParticles; i++) {
const size = Math.random() * 3 + 1;
const x = Math.random() * (innerWidth - size * 2 - size * 2) + size * 2;
const y = Math.random() * (innerHeight - size * 2 - size * 2) + size * 2;
const directionX = Math.random() * 5 - 2.5;
const directionY = Math.random() * 5 - 2.5;
const color = "#16222A";
particlesArray.push(
new Particle(x, y, directionX, directionY, size, color)
);
}
}
function connect() {
for (const mainParticle of particlesArray) {
for (const otherParticle of particlesArray) {
const distance =
(mainParticle.x - otherParticle.x) *
(mainParticle.x - otherParticle.x) +
(mainParticle.y - otherParticle.y) * (mainParticle.y - otherParticle.y);
if (distance < lineDistanceX * lineDistanceY) {
const opacity = 1 - distance / 10000;
ctx.strokeStyle = `rgba(22, 34, 42, ${opacity})`;
ctx.lineWidth = 0.5;
ctx.beginPath();
ctx.moveTo(mainParticle.x, mainParticle.y);
ctx.lineTo(otherParticle.x, otherParticle.y);
ctx.stroke();
}
}
}
}
function animate() {
requestAnimationFrame(animate);
ctx.clearRect(0, 0, innerWidth, innerHeight);
for (const particle of particlesArray) {
particle.update();
}
connect();
}
init();
animate();