-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
150 lines (128 loc) · 4.85 KB
/
main.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
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
140
141
142
143
144
145
146
147
148
149
150
const checkboxes = document.querySelectorAll('input');
const button = document.querySelector('button');
const canvas = document.querySelector('canvas');
const container = document.querySelector('.canvas-container');
canvas.style.opacity = 1;
const computedStyle = getComputedStyle(container);
canvas.width = parseInt(computedStyle.width);
canvas.height = parseInt(computedStyle.height);
const ctx = canvas.getContext('2d');
let circles = [];
let canvasId;
const radius = window.innerWidth > 600 ? 30 : 15;
const seeker = window.innerWidth > 600 ? radius * 8 : radius * 7;
const colors = ['#800000', '#008000', '#000080', '#808000', '#800080', '#008080', '#2F4F4F', '#696969', '#A52A2A', '#8B4513'];
const draw = () => {
ctx.clearRect(0, 0, canvas.width, canvas.height);
cancelAnimationFrame(canvasId);
for (let i = 0; i < circles.length; i++) {
let circleA = circles[i];
circleA.x += circleA.dx;
circleA.y += circleA.dy;
if (
Math.pow(circleA.x, 2) + Math.pow(circleA.y - canvas.height / 2, 2) < Math.pow(seeker, 2) ||
Math.pow(circleA.x - canvas.width, 2) + Math.pow(circleA.y - canvas.height / 2, 2) < Math.pow(seeker, 2)
) {
circleA.dx = 0;
circleA.dy = 0;
const label = Array.from(checkboxes).find(checkbox => checkbox.parentElement.textContent === circleA.name).parentElement;
if (Math.pow(circleA.x, 2) + Math.pow(circleA.y - canvas.height / 2, 2) < Math.pow(seeker, 2)) {
label.style.color = 'red';
}
if (Math.pow(circleA.x - canvas.width, 2) + Math.pow(circleA.y - canvas.height / 2, 2) < Math.pow(seeker, 2)) {
label.style.color = 'blue';
}
} else {
if (circleA.x - radius < 0 || circleA.x + radius > canvas.width) {
circleA.dx = -circleA.dx;
}
if (circleA.y - radius < 0 || circleA.y + radius > canvas.height) {
circleA.dy = -circleA.dy;
}
}
for (let j = i + 1; j < circles.length; j++) {
let circleB = circles[j];
let dx = circleB.x - circleA.x;
let dy = circleB.y - circleA.y;
let distance = Math.sqrt(dx * dx + dy * dy);
if (distance < radius * 2) {
if (circleA.dx === 0 && circleA.dy === 0) {
circleB.dx = -circleB.dx;
circleB.dy = -circleB.dy;
}
else if (circleB.dx === 0 && circleB.dy === 0) {
circleA.dx = -circleA.dx;
circleA.dy = -circleA.dy;
}
else {
if ((circleA.dx > 0 && circleB.dx < 0) || (circleA.dx < 0 && circleB.dx > 0)) {
circleA.dx = -circleA.dx;
circleB.dx = -circleB.dx;
}
if ((circleA.dy > 0 && circleB.dy < 0) || (circleA.dy < 0 && circleB.dy > 0)) {
circleA.dy = -circleA.dy;
circleB.dy = -circleB.dy;
}
}
}
}
ctx.beginPath();
ctx.arc(circleA.x, circleA.y, radius, 0, 2 * Math.PI);
ctx.fillStyle = circleA.color;
ctx.fill();
ctx.font = '20px Arial';
ctx.fillStyle = 'white';
ctx.fillText(circleA.name, circleA.x, circleA.y);
}
ctx.beginPath();
ctx.arc(0, canvas.height / 2, seeker, 0, 2 * Math.PI);
ctx.strokeStyle = 'red';
ctx.stroke();
ctx.beginPath();
ctx.arc(canvas.width, canvas.height / 2, seeker, 0, 2 * Math.PI);
ctx.strokeStyle = 'blue';
ctx.stroke();
canvasId = requestAnimationFrame(draw);
};
const handleCheckboxes = () => {
const names = [];
checkboxes.forEach(checkbox => {
const label = checkbox.parentElement;
label.style.color = 'black';
if (checkbox.checked) {
names.push(label.textContent);
}
});
circles = names.map((name, i) => {
const dx = (Math.random() - 0.5) * 10;
const dy = (Math.random() - 0.5) * 10;
const x = Math.random() * (canvas.width - 2 * radius) + radius;
const y = Math.random() * (canvas.height - 2 * radius) + radius;
const color = colors[i % colors.length];
return { x, y, dx, dy, name, color };
});
};
button.onclick = () => {
handleCheckboxes();
draw();
};
button.ontouchstart = () => button.classList.add('active');
button.ontouchend = () => button.classList.remove('active');
canvas.addEventListener('click', function(event) {
const rect = canvas.getBoundingClientRect();
const x = event.clientX - rect.left;
const y = event.clientY - rect.top;
const clickRadius = radius * 1.5;
const clickedCircle = circles.find(circle => {
const distance = Math.sqrt(Math.pow(x - circle.x, 2) + Math.pow(y - circle.y, 2));
return distance < clickRadius;
});
if (clickedCircle) {
circles = circles.filter(circle => circle !== clickedCircle);
const dx = (Math.random() - 0.5) * 10;
const dy = (Math.random() - 0.5) * 10;
const x = Math.random() * (canvas.width - 2 * radius) + radius;
const y = Math.random() * (canvas.height - 2 * radius) + radius;
circles.push({ x, y, dx, dy, name: clickedCircle.name, color: clickedCircle.color });
}
});