Skip to content

Commit 8635141

Browse files
authored
Merge pull request #399 from abhieshekumar/master
Modifying 'learn' for ES6
2 parents 4f7d23d + 36b5f35 commit 8635141

File tree

6 files changed

+53
-53
lines changed

6 files changed

+53
-53
lines changed

src/assets/learn/curves/bezier/sketch.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
var x1 = 100;
2-
var y1 = 150;
3-
var x2 = 200;
4-
var y2 = 150;
5-
var x3 = 50;
6-
var y3 = 50;
7-
var x4 = 250;
8-
var y4 = 50;
9-
var d1 = false;
10-
var d2 = false;
11-
var d4 = false;
12-
var d3 = false;
1+
let x1 = 100;
2+
let y1 = 150;
3+
let x2 = 200;
4+
let y2 = 150;
5+
let x3 = 50;
6+
let y3 = 50;
7+
let x4 = 250;
8+
let y4 = 50;
9+
let d1 = false;
10+
let d2 = false;
11+
let d4 = false;
12+
let d3 = false;
1313

1414
function setup() {
1515
createCanvas(400, 400);

src/assets/learn/curves/curve_ex/sketch.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
var x1 = 80;
2-
var y1 = 80;
3-
var x2 = 160;
4-
var y2 = 120;
5-
var x3 = 200;
6-
var y3 = 200;
7-
var x4 = 120;
8-
var y4 = 240;
9-
var d1 = false;
10-
var d2 = false;
11-
var d4 = false;
12-
var d3 = false;
13-
var d5 = false;
1+
let x1 = 80;
2+
let y1 = 80;
3+
let x2 = 160;
4+
let y2 = 120;
5+
let x3 = 200;
6+
let y3 = 200;
7+
let x4 = 120;
8+
let y4 = 240;
9+
let d1 = false;
10+
let d2 = false;
11+
let d4 = false;
12+
let d3 = false;
13+
let d5 = false;
1414

1515
function setup() {
1616
createCanvas(300, 300);

src/assets/learn/program-flow/loadImage-example-1/loadImage-example-1.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var img;
1+
let img;
22
function setup(){
33
createCanvas(100, 100);
44
img = loadImage("/assets/learn/program-flow/images/clouds.jpg");

src/assets/learn/program-flow/loadImage-example-2/loadImage-example-2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var img;
1+
let img;
22
function preload(){
33
img = loadImage("/assets/learn/program-flow/images/clouds.jpg");
44
}

src/templates/pages/learn/curves.hbs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ slug: learn/
9393

9494

9595
<script type="text/p5" data-autoplay>
96-
var coords = [40, 40, 80, 60, 100, 100, 60, 120, 50, 150];
96+
let coords = [40, 40, 80, 60, 100, 100, 60, 120, 50, 150];
9797
9898
function setup() {
9999
createCanvas(150, 200);
@@ -113,7 +113,7 @@ slug: learn/
113113
curveVertex(50,150);
114114
endShape();
115115
116-
for (var i = 0; i < coords.length; i+= 2){
116+
for (let i = 0; i < coords.length; i+= 2){
117117
ellipse(coords[i], coords[i+1], 10, 10);
118118
}
119119
}

src/templates/pages/learn/program-flow.hbs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ slug: learn/
4444
<!-- this script only needs to get added once even if there are multiple widget instances -->
4545
<script src="//toolness.github.io/p5.js-widget/p5-widget.js"></script>
4646
<script type="text/p5" data-autoplay>
47-
var i = 0; // change the value of i to see the change
47+
let i = 0; // change the value of i to see the change
4848
function setup(){
4949
createCanvas(100, 100);
5050
rectMode(CENTER);
@@ -92,7 +92,7 @@ function draw(){
9292

9393
<script type="text/p5" data-autoplay>
9494
function setup(){
95-
for (var i=0; i<5; i++){
95+
for (let i=0; i<5; i++){
9696
text(i, i*10, height/2);
9797
}
9898
}
@@ -101,10 +101,10 @@ function setup(){
101101

102102
<p>The <em>for/in</em> statement loops through the properties of an object:</p>
103103
<script type="text/p5" data-autoplay>
104-
var person = {fname:"John", lname:"Doe", age:25};
105-
var myText = "";
104+
let person = {fname:"John", lname:"Doe", age:25};
105+
let myText = "";
106106
function setup(){
107-
var x;
107+
let x;
108108
for (x in person) {
109109
myText += person[x];
110110
myText += " ";
@@ -116,7 +116,7 @@ function setup(){
116116

117117
<p>The while loop cycles through a block of code as long as its specified condition is true.</p>
118118
<script type="text/p5" data-autoplay>
119-
var i = 0;
119+
let i = 0;
120120
function setup(){
121121
while (i<5){
122122
text(i, i*10, height/2);
@@ -129,7 +129,7 @@ function setup(){
129129
<p>The <em>do/while</em> loop is a variant of the <em>while</em> loop. This loop will execute the code block once, before checking if the condition is true, it will then repeat the loop as long as the condition is true.</p>
130130
<!-- Is there an example particular to do/while? -->
131131
<script type="text/p5" data-autoplay>
132-
var i = 0;
132+
let i = 0;
133133
function setup(){
134134
do {
135135
text(i, i*10, height/2);
@@ -144,7 +144,7 @@ function setup(){
144144
<h2>noLoop(), loop() and redraw()</h2>
145145
<p>The <a href="/reference/#/p5/draw">draw()</a> function in p5 runs as a loop. The code inside the draw() function runs continuously from top to bottom until the program is stopped. The draw() loop may be stopped by calling <a href="/reference/#/p5/noLoop">noLoop()</a>, and can then be resumed with <a href="/reference/#/p5/loop">loop()</a>. If using <a href="/reference/#/p5/noLoop">noLoop()</a> in <a href="/reference/#/p5/setup">setup()</a>, it should be the last line inside the block.</p>
146146
<script type="text/p5" data-autoplay>
147-
var x = 0;
147+
let x = 0;
148148
function setup() {
149149
createCanvas(100, 100);
150150
noLoop();
@@ -165,7 +165,7 @@ function mouseReleased() {
165165

166166
<p>The function <a href="/reference/#/p5/redraw">redraw()</a> executes the code within <a href="/reference/#/p5/draw">draw()</a> one time. This functions allows the program to update the display window only when necessary, such as when an event registered by <a href="/reference/#/p5/mousePressed">mousePressed()</a> or <a href="/reference/#/p5/keyPressed">keyPressed()</a> occurs. In structuring a program, it only makes sense to call <a href="/reference/#/p5/redraw">redraw()</a> within events such as <a href="/reference/#/p5/mousePressed">mousePressed()</a> outside of the draw() loop. The redraw() function does not work properly when called inside draw(). In addition, you can set the number of loops through draw by adding a single argument (an integer) to the redraw() function.</p>
167167
<script type="text/p5" data-autoplay>
168-
var x = 0;
168+
let x = 0;
169169
function setup() {
170170
createCanvas(100, 100);
171171
noLoop();
@@ -185,7 +185,7 @@ function mousePressed() {
185185
<h2>Asynchronicity in p5.js</h2>
186186
<p>In JavaScript, events may occur concurrently with the main program flow. This is considered as asynchronicity in programming. In p5, for example, when we use <a href="/reference/#/p5/loadImage">loadImage()</a> in <a href="/reference/#/p5/setup">setup()</a>, the browser begins the process of loading the image but skip onto the next line before it is finised loading. The following example demonstrates such asynchronicity.</p>
187187
<pre><code class="language-javascript">
188-
var img;
188+
let img;
189189
function setup(){
190190
createCanvas(100, 100);
191191
img = loadImage("/assets/learn/program-flow/images/clouds.jpg");
@@ -200,7 +200,7 @@ function draw(){
200200
<iframe src="/assets/learn/program-flow/loadImage-example-1/loadImage-example-1.html" width="150px" height="150px"></iframe>
201201
<!-- what's the file location??????????????????? -->
202202
<!-- <script type="text/p5" data-autoplay>
203-
var img;
203+
let img;
204204
function setup(){
205205
createCanvas(100, 100);
206206
img = loadImage("/assets/learn/program-flow/images/clouds.jpg");
@@ -217,7 +217,7 @@ function draw(){
217217
<h2>Introduction to Preload</h2>
218218
<p>To help with this issue of asynchronicity, p5.js has the <a href="/reference/#/p5/preload">preload()</a> function. Unlike <a href="/reference/#/p5/setup">setup()</a>, preload() forces the program to wait until everything has loaded before moving on. It is best to only make load calls in preload(), and do all other setup in setup().</p>
219219
<pre><code class="language-javascript">
220-
var img;
220+
let img;
221221
function preload(){
222222
img = loadImage("/assets/learn/program-flow/images/clouds.jpg");
223223
}
@@ -235,7 +235,7 @@ function draw(){
235235
<iframe src="/assets/learn/program-flow/loadImage-example-2/loadImage-example-2.html" width="150px" height="150px"></iframe>
236236

237237
<!-- <script type="text/p5" data-autoplay>
238-
var img;
238+
let img;
239239
function preload(){
240240
img = loadImage("/assets/learn/program-flow/images/clouds.jpg");
241241
}
@@ -297,14 +297,14 @@ function drawImage(img){
297297
<!-- example where loadJSON() in preload() -->
298298
<p>The following request at https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.geojson returns data of recent earthquakes in the world from USGS.</p>
299299
<script type="text/p5" data-autoplay data-preview-width="200">
300-
var earthquakes;
300+
let earthquakes;
301301
function preload(){
302302
earthquakes = loadJSON('https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.geojson');
303303
}
304304
function setup(){
305305
createCanvas(200, 100);
306-
var earthquakeMag = earthquakes.features[0].properties.mag;
307-
var earthquakePlace = earthquakes.features[0].properties.place;
306+
let earthquakeMag = earthquakes.features[0].properties.mag;
307+
let earthquakePlace = earthquakes.features[0].properties.place;
308308
text(earthquakePlace, 0, height/2);
309309
text(earthquakeMag, 0, height-height/3);
310310
}
@@ -340,8 +340,8 @@ function setup() {
340340
loadJSON('https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.geojson', showEarthquake);
341341
}
342342
function showEarthquake(earthquakes){
343-
var earthquakeMag = earthquakes.features[0].properties.mag;
344-
var earthquakePlace = earthquakes.features[0].properties.place;
343+
let earthquakeMag = earthquakes.features[0].properties.mag;
344+
let earthquakePlace = earthquakes.features[0].properties.place;
345345
text(earthquakePlace, 0, height/2);
346346
text(earthquakeMag, 0, height-height/3);
347347
}
@@ -351,7 +351,7 @@ function showEarthquake(earthquakes){
351351
<p>Sometimes we use <a href="/reference/#/p5/setInterval">setInterval()</a> to control the frequency of requests made to the API. setInterval() can also take a callback function. If you call setInterval() in <a href="/reference/#/p5/setup">setup()</a>, it will run repeatedly for the duration of the program at the interval set.</p>
352352
<!-- example setInterval() is used -->
353353
<script type="text/p5" data-autoplay data-preview-width="200">
354-
var i=1; // counter variable to keep track of the interval
354+
let i=1; // counter variable to keep track of the interval
355355
function setup() {
356356
createCanvas(200, 100);
357357
getEarthquake();
@@ -362,8 +362,8 @@ function getEarthquake(){
362362
}
363363
function showEarthquake(earthquakes){
364364
background(255);
365-
var earthquakeMag = earthquakes.features[0].properties.mag;
366-
var earthquakePlace = earthquakes.features[0].properties.place;
365+
let earthquakeMag = earthquakes.features[0].properties.mag;
366+
let earthquakePlace = earthquakes.features[0].properties.place;
367367
text("Data grabbed "+i, 0, height/3);
368368
text(earthquakePlace, 0, height/2);
369369
text(earthquakeMag, 0, height-height/3);
@@ -420,9 +420,9 @@ function showEarthquake(earthquakes){
420420

421421
<p>In this example, a canvas element is created and an event listener <a href="/reference/#/p5/mousePressed">mousePressed()</a> is attached. Function changeGrey() will only run when the mouse is pressed over the canvas, and will will change the background color to a random grey. If the mouse is pressed anywhere, even outside of the canvas, the diameter of the ellipse will increase by 10 pixels. The custom function changeGray(), in this instance, is placed within the <a href="/reference/#/p5/mousePressed">mousePressed()</a> function and is to be triggered when mouse is pressed over the canvas element. If the mouse is not pressed, false is passed and changeGrey() will not run.</p>
422422
<script type="text/p5" data-autoplay>
423-
var cnv;
424-
var d;
425-
var g;
423+
let cnv;
424+
let d;
425+
let g;
426426
function setup() {
427427
cnv = createCanvas(100, 100);
428428
cnv.mousePressed(changeGray); // attach listener for canvas click only

0 commit comments

Comments
 (0)