Skip to content

Commit 541252b

Browse files
authored
Merge pull request #345 from JithinKS97/master
update p5js.org/examples HelloP5 section to ES6
2 parents ba3bfb7 + a9a8a38 commit 541252b

24 files changed

+695
-681
lines changed

src/data/examples/en/90_Hello_P5/01_shapes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function setup() {
2121
// A design for a simple flower
2222
translate(580, 200);
2323
noStroke();
24-
for (var i = 0; i < 10; i ++) {
24+
for (let i = 0; i < 10; i ++) {
2525
ellipse(0, 30, 20, 80);
2626
rotate(PI/5);
2727
}

src/data/examples/en/90_Hello_P5/02_interactivity.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99

1010
// for red, green, and blue color values
11-
var r, g, b;
11+
let r, g, b;
1212

1313
function setup() {
1414
createCanvas(720, 400);
@@ -30,7 +30,7 @@ function draw() {
3030
// When the user clicks the mouse
3131
function mousePressed() {
3232
// Check if mouse is inside the circle
33-
var d = dist(mouseX, mouseY, 360, 200);
33+
let d = dist(mouseX, mouseY, 360, 200);
3434
if (d < 100) {
3535
// Pick new random color values
3636
r = random(255);

src/data/examples/en/90_Hello_P5/03_interactivity.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99

1010
// A HTML range slider
11-
var slider;
11+
let slider;
1212

1313
function setup() {
1414
createCanvas(720, 400);

src/data/examples/en/90_Hello_P5/04_animate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @description The circle moves.
44
*/
55
// Where is the circle
6-
var x, y;
6+
let x, y;
77

88
function setup() {
99
createCanvas(720, 400);

src/data/examples/en/90_Hello_P5/04_flocking.js

Lines changed: 150 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -4,180 +4,182 @@
44
* (Rules: Cohesion, Separation, Alignment.)<br>
55
* From <a href="http://natureofcode.com">natureofcode.com</a>.
66
*/
7-
var boids = [];
7+
let boids = [];
88

99
function setup() {
1010
createCanvas(720, 400);
1111

1212
// Add an initial set of boids into the system
13-
for (var i = 0; i < 100; i++) {
13+
for (let i = 0; i < 100; i++) {
1414
boids[i] = new Boid(random(width), random(height));
1515
}
1616
}
1717

1818
function draw() {
1919
background(51);
2020
// Run all the boids
21-
for (var i = 0; i < boids.length; i++) {
21+
for (let i = 0; i < boids.length; i++) {
2222
boids[i].run(boids);
2323
}
2424
}
2525

26-
2726
// Boid class
2827
// Methods for Separation, Cohesion, Alignment added
29-
function Boid(x, y) {
30-
this.acceleration = createVector(0, 0);
31-
this.velocity = p5.Vector.random2D();
32-
this.position = createVector(x, y);
33-
this.r = 3.0;
34-
this.maxspeed = 3; // Maximum speed
35-
this.maxforce = 0.05; // Maximum steering force
36-
}
37-
38-
Boid.prototype.run = function(boids) {
39-
this.flock(boids);
40-
this.update();
41-
this.borders();
42-
this.render();
43-
}
44-
45-
// Forces go into acceleration
46-
Boid.prototype.applyForce = function(force) {
47-
this.acceleration.add(force);
48-
}
49-
50-
// We accumulate a new acceleration each time based on three rules
51-
Boid.prototype.flock = function(boids) {
52-
var sep = this.separate(boids); // Separation
53-
var ali = this.align(boids); // Alignment
54-
var coh = this.cohesion(boids); // Cohesion
55-
// Arbitrarily weight these forces
56-
sep.mult(2.5);
57-
ali.mult(1.0);
58-
coh.mult(1.0);
59-
// Add the force vectors to acceleration
60-
this.applyForce(sep);
61-
this.applyForce(ali);
62-
this.applyForce(coh);
63-
}
64-
65-
// Method to update location
66-
Boid.prototype.update = function() {
67-
// Update velocity
68-
this.velocity.add(this.acceleration);
69-
// Limit speed
70-
this.velocity.limit(this.maxspeed);
71-
this.position.add(this.velocity);
72-
// Reset acceleration to 0 each cycle
73-
this.acceleration.mult(0);
74-
}
75-
76-
// A method that calculates and applies a steering force towards a target
77-
// STEER = DESIRED MINUS VELOCITY
78-
Boid.prototype.seek = function(target) {
79-
var desired = p5.Vector.sub(target, this.position); // A vector pointing from the location to the target
80-
// Normalize desired and scale to maximum speed
81-
desired.normalize();
82-
desired.mult(this.maxspeed);
83-
// Steering = Desired minus Velocity
84-
var steer = p5.Vector.sub(desired, this.velocity);
85-
steer.limit(this.maxforce); // Limit to maximum steering force
86-
return steer;
87-
}
88-
89-
// Draw boid as a circle
90-
Boid.prototype.render = function() {
91-
fill(127, 127);
92-
stroke(200);
93-
ellipse(this.position.x, this.position.y, 16, 16);
94-
}
95-
96-
// Wraparound
97-
Boid.prototype.borders = function() {
98-
if (this.position.x < -this.r) this.position.x = width + this.r;
99-
if (this.position.y < -this.r) this.position.y = height + this.r;
100-
if (this.position.x > width + this.r) this.position.x = -this.r;
101-
if (this.position.y > height + this.r) this.position.y = -this.r;
102-
}
28+
class Boid {
29+
constructor(x, y) {
30+
this.acceleration = createVector(0, 0);
31+
this.velocity = p5.Vector.random2D();
32+
this.position = createVector(x, y);
33+
this.r = 3.0;
34+
this.maxspeed = 3; // Maximum speed
35+
this.maxforce = 0.05; // Maximum steering force
36+
}
10337

104-
// Separation
105-
// Method checks for nearby boids and steers away
106-
Boid.prototype.separate = function(boids) {
107-
var desiredseparation = 25.0;
108-
var steer = createVector(0, 0);
109-
var count = 0;
110-
// For every boid in the system, check if it's too close
111-
for (var i = 0; i < boids.length; i++) {
112-
var d = p5.Vector.dist(this.position, boids[i].position);
113-
// If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself)
114-
if ((d > 0) && (d < desiredseparation)) {
115-
// Calculate vector pointing away from neighbor
116-
var diff = p5.Vector.sub(this.position, boids[i].position);
117-
diff.normalize();
118-
diff.div(d); // Weight by distance
119-
steer.add(diff);
120-
count++; // Keep track of how many
121-
}
38+
run(boids) {
39+
this.flock(boids);
40+
this.update();
41+
this.borders();
42+
this.render();
12243
}
123-
// Average -- divide by how many
124-
if (count > 0) {
125-
steer.div(count);
44+
45+
// Forces go into acceleration
46+
applyForce(force) {
47+
this.acceleration.add(force);
12648
}
127-
128-
// As long as the vector is greater than 0
129-
if (steer.mag() > 0) {
130-
// Implement Reynolds: Steering = Desired - Velocity
131-
steer.normalize();
132-
steer.mult(this.maxspeed);
133-
steer.sub(this.velocity);
134-
steer.limit(this.maxforce);
49+
50+
// We accumulate a new acceleration each time based on three rules
51+
flock(boids) {
52+
let sep = this.separate(boids); // Separation
53+
let ali = this.align(boids); // Alignment
54+
let coh = this.cohesion(boids); // Cohesion
55+
// Arbitrarily weight these forces
56+
sep.mult(2.5);
57+
ali.mult(1.0);
58+
coh.mult(1.0);
59+
// Add the force vectors to acceleration
60+
this.applyForce(sep);
61+
this.applyForce(ali);
62+
this.applyForce(coh);
13563
}
136-
return steer;
137-
}
138-
139-
// Alignment
140-
// For every nearby boid in the system, calculate the average velocity
141-
Boid.prototype.align = function(boids) {
142-
var neighbordist = 50;
143-
var sum = createVector(0, 0);
144-
var count = 0;
145-
for (var i = 0; i < boids.length; i++) {
146-
var d = p5.Vector.dist(this.position, boids[i].position);
147-
if ((d > 0) && (d < neighbordist)) {
148-
sum.add(boids[i].velocity);
149-
count++;
150-
}
64+
65+
// Method to update location
66+
update() {
67+
// Update velocity
68+
this.velocity.add(this.acceleration);
69+
// Limit speed
70+
this.velocity.limit(this.maxspeed);
71+
this.position.add(this.velocity);
72+
// Reset acceleration to 0 each cycle
73+
this.acceleration.mult(0);
15174
}
152-
if (count > 0) {
153-
sum.div(count);
154-
sum.normalize();
155-
sum.mult(this.maxspeed);
156-
var steer = p5.Vector.sub(sum, this.velocity);
157-
steer.limit(this.maxforce);
75+
76+
// A method that calculates and applies a steering force towards a target
77+
// STEER = DESIRED MINUS VELOCITY
78+
seek(target) {
79+
let desired = p5.Vector.sub(target, this.position); // A vector pointing from the location to the target
80+
// Normalize desired and scale to maximum speed
81+
desired.normalize();
82+
desired.mult(this.maxspeed);
83+
// Steering = Desired minus Velocity
84+
let steer = p5.Vector.sub(desired, this.velocity);
85+
steer.limit(this.maxforce); // Limit to maximum steering force
15886
return steer;
159-
} else {
160-
return createVector(0, 0);
16187
}
162-
}
163-
164-
// Cohesion
165-
// For the average location (i.e. center) of all nearby boids, calculate steering vector towards that location
166-
Boid.prototype.cohesion = function(boids) {
167-
var neighbordist = 50;
168-
var sum = createVector(0, 0); // Start with empty vector to accumulate all locations
169-
var count = 0;
170-
for (var i = 0; i < boids.length; i++) {
171-
var d = p5.Vector.dist(this.position, boids[i].position);
172-
if ((d > 0) && (d < neighbordist)) {
173-
sum.add(boids[i].position); // Add location
174-
count++;
88+
89+
// Draw boid as a circle
90+
render() {
91+
fill(127, 127);
92+
stroke(200);
93+
ellipse(this.position.x, this.position.y, 16, 16);
94+
}
95+
96+
// Wraparound
97+
borders() {
98+
if (this.position.x < -this.r) this.position.x = width + this.r;
99+
if (this.position.y < -this.r) this.position.y = height + this.r;
100+
if (this.position.x > width + this.r) this.position.x = -this.r;
101+
if (this.position.y > height + this.r) this.position.y = -this.r;
102+
}
103+
104+
// Separation
105+
// Method checks for nearby boids and steers away
106+
separate(boids) {
107+
let desiredseparation = 25.0;
108+
let steer = createVector(0, 0);
109+
let count = 0;
110+
// For every boid in the system, check if it's too close
111+
for (let i = 0; i < boids.length; i++) {
112+
let d = p5.Vector.dist(this.position, boids[i].position);
113+
// If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself)
114+
if ((d > 0) && (d < desiredseparation)) {
115+
// Calculate vector pointing away from neighbor
116+
let diff = p5.Vector.sub(this.position, boids[i].position);
117+
diff.normalize();
118+
diff.div(d); // Weight by distance
119+
steer.add(diff);
120+
count++; // Keep track of how many
121+
}
175122
}
123+
// Average -- divide by how many
124+
if (count > 0) {
125+
steer.div(count);
126+
}
127+
128+
// As long as the vector is greater than 0
129+
if (steer.mag() > 0) {
130+
// Implement Reynolds: Steering = Desired - Velocity
131+
steer.normalize();
132+
steer.mult(this.maxspeed);
133+
steer.sub(this.velocity);
134+
steer.limit(this.maxforce);
135+
}
136+
return steer;
176137
}
177-
if (count > 0) {
178-
sum.div(count);
179-
return this.seek(sum); // Steer towards the location
180-
} else {
181-
return createVector(0, 0);
138+
139+
// Alignment
140+
// For every nearby boid in the system, calculate the average velocity
141+
align(boids) {
142+
let neighbordist = 50;
143+
let sum = createVector(0, 0);
144+
let count = 0;
145+
for (let i = 0; i < boids.length; i++) {
146+
let d = p5.Vector.dist(this.position, boids[i].position);
147+
if ((d > 0) && (d < neighbordist)) {
148+
sum.add(boids[i].velocity);
149+
count++;
150+
}
151+
}
152+
if (count > 0) {
153+
sum.div(count);
154+
sum.normalize();
155+
sum.mult(this.maxspeed);
156+
let steer = p5.Vector.sub(sum, this.velocity);
157+
steer.limit(this.maxforce);
158+
return steer;
159+
} else {
160+
return createVector(0, 0);
161+
}
182162
}
163+
164+
// Cohesion
165+
// For the average location (i.e. center) of all nearby boids, calculate steering vector towards that location
166+
cohesion(boids) {
167+
let neighbordist = 50;
168+
let sum = createVector(0, 0); // Start with empty vector to accumulate all locations
169+
let count = 0;
170+
for (let i = 0; i < boids.length; i++) {
171+
let d = p5.Vector.dist(this.position, boids[i].position);
172+
if ((d > 0) && (d < neighbordist)) {
173+
sum.add(boids[i].position); // Add location
174+
count++;
175+
}
176+
}
177+
if (count > 0) {
178+
sum.div(count);
179+
return this.seek(sum); // Steer towards the location
180+
} else {
181+
return createVector(0, 0);
182+
}
183+
}
183184
}
185+

0 commit comments

Comments
 (0)