You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
48
48
functionsetup(){
49
49
createCanvas(100, 100);
50
50
rectMode(CENTER);
@@ -92,7 +92,7 @@ function draw(){
92
92
93
93
<scripttype="text/p5" data-autoplay>
94
94
functionsetup(){
95
-
for (var i=0; i<5; i++){
95
+
for (let i=0; i<5; i++){
96
96
text(i, i*10, height/2);
97
97
}
98
98
}
@@ -101,10 +101,10 @@ function setup(){
101
101
102
102
<p>The <em>for/in</em> statement loops through the properties of an object:</p>
103
103
<scripttype="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 ="";
106
106
functionsetup(){
107
-
var x;
107
+
let x;
108
108
for (x in person) {
109
109
myText += person[x];
110
110
myText +="";
@@ -116,7 +116,7 @@ function setup(){
116
116
117
117
<p>The while loop cycles through a block of code as long as its specified condition is true.</p>
118
118
<scripttype="text/p5" data-autoplay>
119
-
var i =0;
119
+
let i =0;
120
120
functionsetup(){
121
121
while (i<5){
122
122
text(i, i*10, height/2);
@@ -129,7 +129,7 @@ function setup(){
129
129
<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>
130
130
<!-- Is there an example particular to do/while? -->
131
131
<scripttype="text/p5" data-autoplay>
132
-
var i =0;
132
+
let i =0;
133
133
functionsetup(){
134
134
do {
135
135
text(i, i*10, height/2);
@@ -144,7 +144,7 @@ function setup(){
144
144
<h2>noLoop(), loop() and redraw()</h2>
145
145
<p>The <ahref="/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 <ahref="/reference/#/p5/noLoop">noLoop()</a>, and can then be resumed with <ahref="/reference/#/p5/loop">loop()</a>. If using <ahref="/reference/#/p5/noLoop">noLoop()</a> in <ahref="/reference/#/p5/setup">setup()</a>, it should be the last line inside the block.</p>
146
146
<scripttype="text/p5" data-autoplay>
147
-
var x =0;
147
+
let x =0;
148
148
functionsetup() {
149
149
createCanvas(100, 100);
150
150
noLoop();
@@ -165,7 +165,7 @@ function mouseReleased() {
165
165
166
166
<p>The function <ahref="/reference/#/p5/redraw">redraw()</a> executes the code within <ahref="/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 <ahref="/reference/#/p5/mousePressed">mousePressed()</a> or <ahref="/reference/#/p5/keyPressed">keyPressed()</a> occurs. In structuring a program, it only makes sense to call <ahref="/reference/#/p5/redraw">redraw()</a> within events such as <ahref="/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>
167
167
<scripttype="text/p5" data-autoplay>
168
-
var x =0;
168
+
let x =0;
169
169
functionsetup() {
170
170
createCanvas(100, 100);
171
171
noLoop();
@@ -185,7 +185,7 @@ function mousePressed() {
185
185
<h2>Asynchronicity in p5.js</h2>
186
186
<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 <ahref="/reference/#/p5/loadImage">loadImage()</a> in <ahref="/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>
<p>To help with this issue of asynchronicity, p5.js has the <ahref="/reference/#/p5/preload">preload()</a> function. Unlike <ahref="/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>
<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>
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;
345
345
text(earthquakePlace, 0, height/2);
346
346
text(earthquakeMag, 0, height-height/3);
347
347
}
@@ -351,7 +351,7 @@ function showEarthquake(earthquakes){
351
351
<p>Sometimes we use <ahref="/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 <ahref="/reference/#/p5/setup">setup()</a>, it will run repeatedly for the duration of the program at the interval set.</p>
var i=1; // counter variable to keep track of the interval
354
+
let i=1; // counter variable to keep track of the interval
355
355
functionsetup() {
356
356
createCanvas(200, 100);
357
357
getEarthquake();
@@ -362,8 +362,8 @@ function getEarthquake(){
362
362
}
363
363
functionshowEarthquake(earthquakes){
364
364
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;
367
367
text("Data grabbed "+i, 0, height/3);
368
368
text(earthquakePlace, 0, height/2);
369
369
text(earthquakeMag, 0, height-height/3);
@@ -420,9 +420,9 @@ function showEarthquake(earthquakes){
420
420
421
421
<p>In this example, a canvas element is created and an event listener <ahref="/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 <ahref="/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>
422
422
<scripttype="text/p5" data-autoplay>
423
-
var cnv;
424
-
var d;
425
-
var g;
423
+
let cnv;
424
+
let d;
425
+
let g;
426
426
functionsetup() {
427
427
cnv =createCanvas(100, 100);
428
428
cnv.mousePressed(changeGray); // attach listener for canvas click only
0 commit comments