forked from sandrofigo/p5-Connected-Dots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
81 lines (64 loc) · 2.38 KB
/
sketch.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
let particles = [];
const particleCount = 150;
const maxDistance = 50;
const maxConnections = 3;
const minSpeed = 0.05;
const maxSpeed = 0.15;
function setup() {
createCanvas(window.innerWidth, window.innerHeight);
background(41, 47, 54);
for (let i = 0; i < particleCount; i++) {
let p = new Particle();
particles.push(p);
}
}
function draw() {
background(41, 47, 54);
//orange
//const particleColor = color(242, 147, 24);
const particleColor = color(127);
const lineColor = color(255, 255, 255);
for (let i = 0; i < particles.length; i++) {
noStroke();
fill(particleColor);
particles[i].draw();
particles[i].update();
}
for (let i = 0; i < particles.length - 1; i++) {
let current = particles[i];
let affected = [];
for (let j = 0; j < particles.length; j++) {
if (particles[j] != current) {
let distance = current.position.dist(particles[j].position);
if (distance <= maxDistance && affected.length <= maxConnections) {
affected.push({
'particle': particles[j],
'distance': distance
});
}
}
}
for (let a = 0; a < affected.length; a++) {
stroke(red(lineColor), green(lineColor), blue(lineColor), map(affected[a].distance, 0, maxDistance, 255, 0));
//smooth();
line(current.position.x, current.position.y, affected[a].particle.position.x, affected[a].particle.position.y);
}
}
}
function Particle() {
this.position = createVector(random(0, window.innerWidth), random(0, window.innerHeight));
this.velocity = createVector(random(-1, 1), random(-1, 1));
this.velocity.normalize();
this.velocity.mult(random(minSpeed, maxSpeed));
this.update = function () {
this.position.add(this.velocity);
if (this.position.x < 0) this.position.x = window.innerWidth;
if (this.position.y < 0) this.position.y = window.innerHeight;
if (this.position.x > window.innerWidth) this.position.x = 0;
if (this.position.y > window.innerHeight) this.position.y = 0;
};
this.draw = function () {
//point(this.position.x, this.position.y);
ellipse(this.position.x, this.position.y, 2, 2);
};
}