class Particle {
constructor(x, y, r = 3, c = "rgb(200, 200, 200)") {
this.pos = [x, y];
this.r = r;
this.color = c;
this.step = 6;
this.con = false;
}
update() {
const [x, y] = this.pos;
const { step } = this;
let [nextX, nextY] = [
x + Math.floor(-step + Math.random() * (2 * step + 1)),
y + Math.floor(-step + Math.random() * (2 * step + 1))
];
this.pos = [nextX, nextY];
}
stick(struc, context) {
for (let other of struc) {
const d = dist2D(other.pos, this.pos);
const target = other.r + this.r + 10;
if (d <= target) {
this.color = "rgb(100, 100, 100)";
this.con = other.pos;
return true;
}
}
}
draw(context) {
const [x, y] = this.pos;
const { color, r } = this;
context.beginPath();
context.fillStyle = color;
context.fillRect(x, y, r, r);
if (this.con) {
const pos = [x + r / 2, y + r / 2];
const [oX, oY] = this.con;
const otherPos = [oX + r / 2, oY + r / 2];
line(pos, otherPos, context, {
lineWidth: r * 0.5
});
}
}
}