-
Notifications
You must be signed in to change notification settings - Fork 0
/
particle-collision.js
105 lines (86 loc) · 2.39 KB
/
particle-collision.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
const canvas = document.querySelector("canvas");
const c = canvas.getContext("2d");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// 数值随机方法
function randomIntFromRange(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
// 获取两点之间的距离
function getDistance(x1, y1, x2, y2) {
let d = Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2), 2);
return d;
}
// 对象
function Particle(x, y, radius, color) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;
this.velocity = {
x: Math.random() - 0.5,
y: Math.random() - 0.5
};
this.update = particles => {
this.draw();
if (particles && particles.length > 0) {
for (let i = 0; i < particles.length; i++) {
if (this === particles[i]) continue;
if (
getDistance(this.x, this.y, particles[i].x, particles[i].y) -
this.radius * 2 <
0
) {
console.log(`${i}-collied`);
}
}
}
if (this.x - this.radius <= 0 || this.x + this.radius >= canvas.width)
this.velocity.x = -this.velocity.x;
if (this.y - this.radius <= 0 || this.y + this.radius >= canvas.height)
this.velocity.y = -this.velocity.y;
this.x += this.velocity.x;
this.y += this.velocity.y;
};
this.draw = () => {
c.beginPath();
c.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
// c.fillStyle = this.color;
// c.fill();
c.strokeStyle = this.color;
c.stroke();
c.closePath();
};
}
let particles = [];
let count = 4;
init = () => {
for (let i = 0; i < count; i++) {
let radius = 100,
x = randomIntFromRange(radius, innerWidth - radius),
y = randomIntFromRange(radius, innerHeight - radius),
color = "#333";
if (i !== 0) {
for (let j = 0; j < particles.length; j++) {
if (
getDistance(x, y, particles[j].x, particles[j].y) - radius * 2 <
0
) {
(x = randomIntFromRange(radius, innerWidth - radius)),
(y = randomIntFromRange(radius, innerHeight - radius)),
(j = -1);
}
}
}
particles.push(new Particle(x, y, radius, color));
}
};
animate = () => {
requestAnimationFrame(animate);
c.clearRect(0, 0, canvas.width, canvas.height);
particles.forEach(particle => {
particle.update(particles);
});
};
init();
animate();