-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparticles.js
67 lines (56 loc) · 1.54 KB
/
particles.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
const particles = [];
function setup() {
createCanvas(window.innerWidth, window.innerHeight/1.5);
const particlesLength = Math.floor(window.innerWidth / 10);
for(let i = 0; i < particlesLength; i++) {
particles.push(new Particle());
}
}
function draw(){
background(55, 100, 144);
particles.forEach((p, index) => {
p.update();
p.draw();
p.checkParticles(particles.slice(index));
});
}
class Particle {
constructor() {
// Position
this.pos = createVector(random(width), random(height));
// Velocity
this.vel = createVector(random(-2, 2), random(-2, 2));
// Size
this.size = 10;
}
// Update movement by adding velocity
update() {
this.pos.add(this.vel);
this.edges();
}
// Draw single particle
draw() {
noStroke();
fill('rgba(255,255,255,0.5)');
circle(this.pos.x, this.pos.y, this.size);
}
// Detect edges
edges() {
if(this.pos.x < 0 || this.pos.x > width) {
this.vel.x *= -1;
}
if(this.pos.y < 0 || this.pos.y > height) {
this.vel.y *= -1;
}
}
// Connect particles
checkParticles(particles) {
particles.forEach(particle => {
const d = dist(this.pos.x, this.pos.y, particle.pos.x, particle.pos.y);
if(d < 120) {
stroke('rgba(255,255,255,0.1)');
line(this.pos.x, this.pos.y, particle.pos.x, particle.pos.y);
}
});
}
}