Skip to content

Commit 763be7f

Browse files
authored
Merge pull request #552 from zoyron/particle
Added Particle.js to Simulate folder
2 parents e30d78b + e574e61 commit 763be7f

File tree

3 files changed

+207
-0
lines changed

3 files changed

+207
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* @name Particles
3+
* @description There is a light-weight JavaScript library named
4+
* particle.js which creates a very pleasing particle system.
5+
* This is an attempt to recreate that particle system using p5.js.
6+
* Inspired by Particle.js, contributed by Sagar Arora.
7+
*/
8+
9+
10+
// this class describes the properties of a single particle.
11+
class Particle {
12+
// setting the co-ordinates, radius and the
13+
// speed of a particle in both the co-ordinates axes.
14+
constructor(){
15+
this.x = random(0,width);
16+
this.y = random(0,height);
17+
this.r = random(1,8);
18+
this.xSpeed = random(-2,2);
19+
this.ySpeed = random(-1,1.5);
20+
}
21+
22+
// creation of a particle.
23+
createParticle() {
24+
noStroke();
25+
fill('rgba(200,169,169,0.5)');
26+
circle(this.x,this.y,this.r);
27+
}
28+
29+
// setting the particle in motion.
30+
moveParticle() {
31+
if(this.x < 0 || this.x > width)
32+
this.xSpeed*=-1;
33+
if(this.y < 0 || this.y > height)
34+
this.ySpeed*=-1;
35+
this.x+=this.xSpeed;
36+
this.y+=this.ySpeed;
37+
}
38+
39+
// this function creates the connections(lines)
40+
// between particles which are less than a certain distance apart
41+
joinParticles(paraticles) {
42+
particles.forEach(element =>{
43+
let dis = dist(this.x,this.y,element.x,element.y);
44+
if(dis<85) {
45+
stroke('rgba(255,255,255,0.04)');
46+
line(this.x,this.y,element.x,element.y);
47+
}
48+
});
49+
}
50+
}
51+
52+
// an array to add multiple particles
53+
let particles = [];
54+
55+
function setup() {
56+
createCanvas(720, 400);
57+
for(let i = 0;i<width/10;i++){
58+
particles.push(new Particle());
59+
}
60+
}
61+
62+
function draw() {
63+
background('#0f0f0f');
64+
for(let i = 0;i<particles.length;i++) {
65+
particles[i].createParticle();
66+
particles[i].moveParticle();
67+
particles[i].joinParticles(particles.slice(i));
68+
}
69+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* @name Particles
3+
* @description There is a light-weight JavaScript library named
4+
* particle.js which creates a very pleasing particle system.
5+
* This is an attempt to recreate that particle system using p5.js.
6+
* Inspired by Particle.js, contributed by Sagar Arora.
7+
*/
8+
9+
10+
// this class describes the properties of a single particle.
11+
class Particle {
12+
// setting the co-ordinates, radius and the
13+
// speed of a particle in both the co-ordinates axes.
14+
constructor(){
15+
this.x = random(0,width);
16+
this.y = random(0,height);
17+
this.r = random(1,8);
18+
this.xSpeed = random(-2,2);
19+
this.ySpeed = random(-1,1.5);
20+
}
21+
22+
// creation of a particle.
23+
createParticle() {
24+
noStroke();
25+
fill('rgba(200,169,169,0.5)');
26+
circle(this.x,this.y,this.r);
27+
}
28+
29+
// setting the particle in motion.
30+
moveParticle() {
31+
if(this.x < 0 || this.x > width)
32+
this.xSpeed*=-1;
33+
if(this.y < 0 || this.y > height)
34+
this.ySpeed*=-1;
35+
this.x+=this.xSpeed;
36+
this.y+=this.ySpeed;
37+
}
38+
39+
// this function creates the connections(lines)
40+
// between particles which are less than a certain distance apart
41+
joinParticles(paraticles) {
42+
particles.forEach(element =>{
43+
let dis = dist(this.x,this.y,element.x,element.y);
44+
if(dis<85) {
45+
stroke('rgba(255,255,255,0.04)');
46+
line(this.x,this.y,element.x,element.y);
47+
}
48+
});
49+
}
50+
}
51+
52+
// an array to add multiple particles
53+
let particles = [];
54+
55+
function setup() {
56+
createCanvas(720, 400);
57+
for(let i = 0;i<width/10;i++){
58+
particles.push(new Particle());
59+
}
60+
}
61+
62+
function draw() {
63+
background('#0f0f0f');
64+
for(let i = 0;i<particles.length;i++) {
65+
particles[i].createParticle();
66+
particles[i].moveParticle();
67+
particles[i].joinParticles(particles.slice(i));
68+
}
69+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* @name Particles
3+
* @description There is a light-weight JavaScript library named
4+
* particle.js which creates a very pleasing particle system.
5+
* This is an attempt to recreate that particle system using p5.js.
6+
* Inspired by Particle.js, contributed by Sagar Arora.
7+
*/
8+
9+
10+
// this class describes the properties of a single particle.
11+
class Particle {
12+
// setting the co-ordinates, radius and the
13+
// speed of a particle in both the co-ordinates axes.
14+
constructor(){
15+
this.x = random(0,width);
16+
this.y = random(0,height);
17+
this.r = random(1,8);
18+
this.xSpeed = random(-2,2);
19+
this.ySpeed = random(-1,1.5);
20+
}
21+
22+
// creation of a particle.
23+
createParticle() {
24+
noStroke();
25+
fill('rgba(200,169,169,0.5)');
26+
circle(this.x,this.y,this.r);
27+
}
28+
29+
// setting the particle in motion.
30+
moveParticle() {
31+
if(this.x < 0 || this.x > width)
32+
this.xSpeed*=-1;
33+
if(this.y < 0 || this.y > height)
34+
this.ySpeed*=-1;
35+
this.x+=this.xSpeed;
36+
this.y+=this.ySpeed;
37+
}
38+
39+
// this function creates the connections(lines)
40+
// between particles which are less than a certain distance apart
41+
joinParticles(paraticles) {
42+
particles.forEach(element =>{
43+
let dis = dist(this.x,this.y,element.x,element.y);
44+
if(dis<85) {
45+
stroke('rgba(255,255,255,0.04)');
46+
line(this.x,this.y,element.x,element.y);
47+
}
48+
});
49+
}
50+
}
51+
52+
// an array to add multiple particles
53+
let particles = [];
54+
55+
function setup() {
56+
createCanvas(720, 400);
57+
for(let i = 0;i<width/10;i++){
58+
particles.push(new Particle());
59+
}
60+
}
61+
62+
function draw() {
63+
background('#0f0f0f');
64+
for(let i = 0;i<particles.length;i++) {
65+
particles[i].createParticle();
66+
particles[i].moveParticle();
67+
particles[i].joinParticles(particles.slice(i));
68+
}
69+
}

0 commit comments

Comments
 (0)