Skip to content

Commit 1e8eff8

Browse files
committed
convert var to let
1 parent 556abde commit 1e8eff8

32 files changed

+807
-807
lines changed

src/data/examples/es/09_Simulate/00_Forces.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@
99
// cuerpos sujetos a resistencia de fluidos cuando están en el "agua"
1010

1111
// Cinco cuerpos en movimiento
12-
var movers = [];
12+
let movers = [];
1313

1414
// Líquido
15-
var liquid;
15+
let liquid;
1616

1717
function setup() {
1818
createCanvas(640, 360);
1919
reset();
2020
// Crear objeto líquido
21-
liquid = new Liquid(0, height/2, width, height/2, 0.1);
21+
liquid = new Liquid(0, height / 2, width, height / 2, 0.1);
2222
}
2323

2424
function draw() {
@@ -27,18 +27,18 @@ function draw() {
2727
// Dibujar el agua
2828
liquid.display();
2929

30-
for (var i = 0; i < movers.length; i++) {
30+
for (let i = 0; i < movers.length; i++) {
3131

3232
// ¿Está el objeto Mover dentro del objeto líquido?
3333
if (liquid.contains(movers[i])) {
3434
// Calcular fuerza de arrastre
35-
var dragForce = liquid.calculateDrag(movers[i]);
35+
let dragForce = liquid.calculateDrag(movers[i]);
3636
// Aplicar fuerza de arrastre a Mover
3737
movers[i].applyForce(dragForce);
3838
}
3939

4040
// Aquí se escala la gravedad según la masa
41-
var gravity = createVector(0, 0.1*movers[i].mass);
41+
let gravity = createVector(0, 0.1*movers[i].mass);
4242
// Aplicar gravedad
4343
movers[i].applyForce(gravity);
4444

@@ -57,12 +57,12 @@ function mousePressed() {
5757

5858
// Reiniciar todos los objetos Mover aleatoriamente
5959
function reset() {
60-
for (var i = 0; i < 9; i++) {
61-
movers[i] = new Mover(random(0.5, 3), 40+i*70, 0);
60+
for (let i = 0; i < 9; i++) {
61+
movers[i] = new Mover(random(0.5, 3), 40 + i * 70, 0);
6262
}
6363
}
6464

65-
var Liquid = function(x, y, w, h, c) {
65+
let Liquid = function(x, y, w, h, c) {
6666
this.x = x;
6767
this.y = y;
6868
this.w = w;
@@ -72,19 +72,19 @@ var Liquid = function(x, y, w, h, c) {
7272

7373
// ¿Está el objeto Mover dentro del objeto líquido?
7474
Liquid.prototype.contains = function(m) {
75-
var l = m.position;
75+
let l = m.position;
7676
return l.x > this.x && l.x < this.x + this.w &&
7777
l.y > this.y && l.y < this.y + this.h;
7878
};
7979

8080
// Calcular fuerza de arrastre
8181
Liquid.prototype.calculateDrag = function(m) {
8282
// Magnitud es coeficiente * velocidad al cuadrado
83-
var speed = m.velocity.mag();
84-
var dragMagnitude = this.c * speed * speed;
83+
let speed = m.velocity.mag();
84+
let dragMagnitude = this.c * speed * speed;
8585

8686
// Dirección es el inverso de la velocidad
87-
var dragForce = m.velocity.copy();
87+
let dragForce = m.velocity.copy();
8888
dragForce.mult(-1);
8989

9090
// Escalar según magnitud
@@ -100,17 +100,17 @@ Liquid.prototype.display = function() {
100100
rect(this.x, this.y, this.w, this.h);
101101
};
102102

103-
function Mover(m,x,y) {
103+
function Mover(m, x, y) {
104104
this.mass = m;
105-
this.position = createVector(x,y);
106-
this.velocity = createVector(0,0);
107-
this.acceleration = createVector(0,0);
105+
this.position = createVector(x, y);
106+
this.velocity = createVector(0, 0);
107+
this.acceleration = createVector(0, 0);
108108
}
109109

110110
// Segunda ley de Newton: F = M * A
111111
// ó A = F / M
112112
Mover.prototype.applyForce = function(force) {
113-
var f = p5.Vector.div(force,this.mass);
113+
let f = p5.Vector.div(force,this.mass);
114114
this.acceleration.add(f);
115115
};
116116

@@ -126,15 +126,15 @@ Mover.prototype.update = function() {
126126
Mover.prototype.display = function() {
127127
stroke(0);
128128
strokeWeight(2);
129-
fill(255,127);
130-
ellipse(this.position.x,this.position.y,this.mass*16,this.mass*16);
129+
fill(255, 127);
130+
ellipse(this.position.x, this.position.y, this.mass * 16, this.mass * 16);
131131
};
132132

133133
// Rebotar contra la parte inferior de la ventana
134134
Mover.prototype.checkEdges = function() {
135-
if (this.position.y > (height - this.mass*8)) {
135+
if (this.position.y > (height - this.mass * 8)) {
136136
// Un poco de amortiguamiento al rebotar contra el fondo
137137
this.velocity.y *= -0.9;
138-
this.position.y = (height - this.mass*8);
138+
this.position.y = (height - this.mass * 8);
139139
}
140140
};

src/data/examples/es/09_Simulate/01_ParticleSystem.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
* @description Este es un sistema básico de partículas
44
* (<a href="http://natureofcode.com">natureofcode.com</a>)
55
*/
6-
var system;
6+
let system;
77

88
function setup() {
99
createCanvas(720, 400);
10-
system = new ParticleSystem(createVector(width/2, 50));
10+
system = new ParticleSystem(createVector(width / 2, 50));
1111
}
1212

1313
function draw() {
@@ -17,7 +17,7 @@ function draw() {
1717
}
1818

1919
// Una clase simple de partícula (Particle)
20-
var Particle = function(position) {
20+
let Particle = function(position) {
2121
this.acceleration = createVector(0, 0.05);
2222
this.velocity = createVector(random(-1, 1), random(-1, 0));
2323
this.position = position.copy();
@@ -53,7 +53,7 @@ Particle.prototype.isDead = function(){
5353
}
5454
};
5555

56-
var ParticleSystem = function(position) {
56+
let ParticleSystem = function(position) {
5757
this.origin = position.copy();
5858
this.particles = [];
5959
};
@@ -63,8 +63,8 @@ ParticleSystem.prototype.addParticle = function() {
6363
};
6464

6565
ParticleSystem.prototype.run = function() {
66-
for (var i = this.particles.length-1; i >= 0; i--) {
67-
var p = this.particles[i];
66+
for (let i = this.particles.length-1; i >= 0; i--) {
67+
let p = this.particles[i];
6868
p.run();
6969
if (p.isDead()) {
7070
this.particles.splice(i, 1);

src/data/examples/es/09_Simulate/02_Flocking.js

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

1010

11-
var flock;
11+
let flock;
1212

13-
var text;
13+
let text;
1414

1515
function setup() {
16-
createCanvas(640,360);
16+
createCanvas(640, 360);
1717
createP("Drag the mouse to generate new boids.");
1818

1919
flock = new Flock();
2020
// Añade un conjunto inicial de boids al sistema
21-
for (var i = 0; i < 100; i++) {
22-
var b = new Boid(width/2,height/2);
21+
for (let i = 0; i < 100; i++) {
22+
let b = new Boid(width / 2,height / 2);
2323
flock.addBoid(b);
2424
}
2525
}
@@ -31,7 +31,7 @@ function draw() {
3131

3232
// Añade un nuevo boid al sistema
3333
function mouseDragged() {
34-
flock.addBoid(new Boid(mouseX,mouseY));
34+
flock.addBoid(new Boid(mouseX, mouseY));
3535
}
3636

3737
// The Nature of Code
@@ -47,7 +47,7 @@ function Flock() {
4747
}
4848

4949
Flock.prototype.run = function() {
50-
for (var i = 0; i < this.boids.length; i++) {
50+
for (let i = 0; i < this.boids.length; i++) {
5151
this.boids[i].run(this.boids); // Pasar la lista entera de boids a cada boid de forma individual
5252
}
5353
}
@@ -63,9 +63,9 @@ Flock.prototype.addBoid = function(b) {
6363
// Clase Boid
6464
// Métodos para Separación, Cohesión, alineamiento
6565

66-
function Boid(x,y) {
67-
this.acceleration = createVector(0,0);
68-
this.velocity = createVector(random(-1,1),random(-1,1));
66+
function Boid(x, y) {
67+
this.acceleration = createVector(0, 0);
68+
this.velocity = createVector(random(-1, 1),random(-1, 1));
6969
this.position = createVector(x,y);
7070
this.r = 3.0;
7171
this.maxspeed = 3; // Velocidad máxima
@@ -86,9 +86,9 @@ Boid.prototype.applyForce = function(force) {
8686

8787
// Acumular una nueva aceleración cada vez basado en tres reglas
8888
Boid.prototype.flock = function(boids) {
89-
var sep = this.separate(boids); // Separación
90-
var ali = this.align(boids); // Alineamiento
91-
var coh = this.cohesion(boids); // Cohesión
89+
let sep = this.separate(boids); // Separación
90+
let ali = this.align(boids); // Alineamiento
91+
let coh = this.cohesion(boids); // Cohesión
9292
// Dar un peso arbitrario a cada fuerza
9393
sep.mult(1.5);
9494
ali.mult(1.0);
@@ -113,28 +113,28 @@ Boid.prototype.update = function() {
113113
// Un método que calcula y aplica una fuerza de viraje hacia una posición objetivo
114114
// VIRAJE = DESEADO - VELOCIDAD
115115
Boid.prototype.seek = function(target) {
116-
var desired = p5.Vector.sub(target,this.position); // Un vector apuntando desde la ubicación hacia el objetivo
116+
let desired = p5.Vector.sub(target, this.position); // Un vector apuntando desde la ubicación hacia el objetivo
117117
// Normalizar deseado y escalar según velocidad máxima
118118
desired.normalize();
119119
desired.mult(this.maxspeed);
120120
// Viraje = Deseado - Velocidad
121-
var steer = p5.Vector.sub(desired,this.velocity);
121+
let steer = p5.Vector.sub(desired, this.velocity);
122122
steer.limit(this.maxforce); // Limita al máximo de fuerza de viraje
123123
return steer;
124124
}
125125

126126
Boid.prototype.render = function() {
127127
// Dibuja un triángulo rotado en la dirección de la velocidad
128-
var theta = this.velocity.heading() + radians(90);
128+
let theta = this.velocity.heading() + radians(90);
129129
fill(127);
130130
stroke(200);
131131
push();
132-
translate(this.position.x,this.position.y);
132+
translate(this.position.x, this.position.y);
133133
rotate(theta);
134134
beginShape();
135-
vertex(0, -this.r*2);
136-
vertex(-this.r, this.r*2);
137-
vertex(this.r, this.r*2);
135+
vertex(0, -this.r * 2);
136+
vertex(-this.r, this.r * 2);
137+
vertex(this.r, this.r * 2);
138138
endShape(CLOSE);
139139
pop();
140140
}
@@ -143,23 +143,23 @@ Boid.prototype.render = function() {
143143
Boid.prototype.borders = function() {
144144
if (this.position.x < -this.r) this.position.x = width +this.r;
145145
if (this.position.y < -this.r) this.position.y = height+this.r;
146-
if (this.position.x > width +this.r) this.position.x = -this.r;
147-
if (this.position.y > height+this.r) this.position.y = -this.r;
146+
if (this.position.x > width + this.r) this.position.x = -this.r;
147+
if (this.position.y > height+ this.r) this.position.y = -this.r;
148148
}
149149

150150
// Separación
151151
// Método que revisa los boids cercanos y vira para alejarse de ellos
152152
Boid.prototype.separate = function(boids) {
153-
var desiredseparation = 25.0;
154-
var steer = createVector(0,0);
155-
var count = 0;
153+
let desiredseparation = 25.0;
154+
let steer = createVector(0, 0);
155+
let count = 0;
156156
// Por cada boid en el sistema, revisa si está muy cerca
157-
for (var i = 0; i < boids.length; i++) {
158-
var d = p5.Vector.dist(this.position,boids[i].position);
157+
for (let i = 0; i < boids.length; i++) {
158+
let d = p5.Vector.dist(this.position, boids[i].position);
159159
// Si la distancia es mayor a 0 y menor que una cantidad arbitraria (0 cuando eres tú mismo)
160160
if ((d > 0) && (d < desiredseparation)) {
161161
// Calcular el vector apuntando a alejarse del vecino
162-
var diff = p5.Vector.sub(this.position,boids[i].position);
162+
let diff = p5.Vector.sub(this.position, boids[i].position);
163163
diff.normalize();
164164
diff.div(d); // Peso por distancia
165165
steer.add(diff);
@@ -185,11 +185,11 @@ Boid.prototype.separate = function(boids) {
185185
// Alineamiento
186186
// Para cada boid cercano en el sistema, calcula la velocidad promedio
187187
Boid.prototype.align = function(boids) {
188-
var neighbordist = 50;
189-
var sum = createVector(0,0);
190-
var count = 0;
191-
for (var i = 0; i < boids.length; i++) {
192-
var d = p5.Vector.dist(this.position,boids[i].position);
188+
let neighbordist = 50;
189+
let sum = createVector(0, 0);
190+
let count = 0;
191+
for (let i = 0; i < boids.length; i++) {
192+
let d = p5.Vector.dist(this.position,boids[i].position);
193193
if ((d > 0) && (d < neighbordist)) {
194194
sum.add(boids[i].velocity);
195195
count++;
@@ -199,22 +199,22 @@ Boid.prototype.align = function(boids) {
199199
sum.div(count);
200200
sum.normalize();
201201
sum.mult(this.maxspeed);
202-
var steer = p5.Vector.sub(sum,this.velocity);
202+
let steer = p5.Vector.sub(sum, this.velocity);
203203
steer.limit(this.maxforce);
204204
return steer;
205205
} else {
206-
return createVector(0,0);
206+
return createVector(0, 0);
207207
}
208208
}
209209

210210
// Cohesión
211211
// Para la ubicación promedio (centro) de todos los boids cercanos, calcula el vector de viraje hacia esa ubicación.
212212
Boid.prototype.cohesion = function(boids) {
213-
var neighbordist = 50;
214-
var sum = createVector(0,0); // Empieza con un vector vacío para acumular todas las posiciones
215-
var count = 0;
216-
for (var i = 0; i < boids.length; i++) {
217-
var d = p5.Vector.dist(this.position,boids[i].position);
213+
let neighbordist = 50;
214+
let sum = createVector(0, 0); // Empieza con un vector vacío para acumular todas las posiciones
215+
let count = 0;
216+
for (let i = 0; i < boids.length; i++) {
217+
let d = p5.Vector.dist(this.position,boids[i].position);
218218
if ((d > 0) && (d < neighbordist)) {
219219
sum.add(boids[i].position); // Añada posición
220220
count++;
@@ -224,6 +224,6 @@ Boid.prototype.cohesion = function(boids) {
224224
sum.div(count);
225225
return this.seek(sum); // Vira hacia la posición
226226
} else {
227-
return createVector(0,0);
227+
return createVector(0, 0);
228228
}
229229
}

0 commit comments

Comments
 (0)