-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbg.js
More file actions
71 lines (58 loc) · 2.21 KB
/
bg.js
File metadata and controls
71 lines (58 loc) · 2.21 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
const canvas = document.getElementById('backgroundCanvas');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
const points = [];
const pointCount = 0; // Количество точек
const maxDistance = 30; // Максимальное расстояние для соединения точек
// Создаем точки
for (let i = 0; i < pointCount; i++) {
points.push({
x: Math.random() * canvas.width,
y: Math.random() * canvas.height,
dx: (Math.random() - 0.5) * 1, // Скорость по x
dy: (Math.random() - 0.5) * 1 // Скорость по y
});
}
// Рисуем точки и линии
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = '#555';
ctx.strokeStyle = '#555';
// Рисуем точки
points.forEach(point => {
ctx.beginPath();
ctx.arc(point.x, point.y, 2, 0, Math.PI * 2);
ctx.fill();
});
// Рисуем линии между точками
for (let i = 0; i < points.length; i++) {
for (let j = i + 1; j < points.length; j++) {
const dx = points[i].x - points[j].x;
const dy = points[i].y - points[j].y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance < maxDistance) {
ctx.beginPath();
ctx.moveTo(points[i].x, points[i].y);
ctx.lineTo(points[j].x, points[j].y);
ctx.stroke();
}
}
}
// Обновляем позиции точек
points.forEach(point => {
point.x += point.dx;
point.y += point.dy;
// Отражаем точки, если они выходят за границы
if (point.x < 0 || point.x > canvas.width) point.dx = -point.dx;
if (point.y < 0 || point.y > canvas.height) point.dy = -point.dy;
});
requestAnimationFrame(draw);
}
// Запускаем анимацию
draw();
// Изменяем размер canvas при изменении размера окна
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
});