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
Copy file name to clipboardExpand all lines: dist/es/learn/interactivity.html
+28-28Lines changed: 28 additions & 28 deletions
Original file line number
Diff line number
Diff line change
@@ -189,10 +189,10 @@ <h2>Mouse Data</h2>
189
189
noStroke();
190
190
}
191
191
functiondraw(){
192
-
varx=mouseX;
193
-
vary=mouseY;
194
-
varix=width-mouseX;// Inverse X
195
-
variy=height-mouseY;// Inverse Y
192
+
letx=mouseX;
193
+
lety=mouseY;
194
+
letix=width-mouseX;// Inverse X
195
+
letiy=height-mouseY;// Inverse Y
196
196
background(126);
197
197
fill(255,150);
198
198
ellipse(x,height/2,y,y);
@@ -396,7 +396,7 @@ <h2>Keyboard data</h2>
396
396
</script>
397
397
398
398
<scripttype="text/p5" data-autoplay>
399
-
varx=20;
399
+
letx=20;
400
400
functionsetup(){
401
401
createCanvas(100,100);
402
402
strokeWeight(4);
@@ -461,14 +461,14 @@ <h2>Coded keys</h2>
461
461
}
462
462
functiondraw(){
463
463
if(keyIsPressed===true){
464
-
varx=keyCode-32;
464
+
letx=keyCode-32;
465
465
line(x,0,x,height);
466
466
}
467
467
}
468
468
</script>
469
469
470
470
<scripttype="text/p5" data-autoplay>
471
-
varangle=0;
471
+
letangle=0;
472
472
functionsetup(){
473
473
createCanvas(100,100);
474
474
stroke(0);
@@ -488,7 +488,7 @@ <h2>Coded keys</h2>
488
488
<p>In addition to reading key values for numbers, letters, and symbols, p5 can also read the values from other keys including the arrow keys and the Alt, Control, Shift, Backspace, Tab, Enter, Return, Escape, and Delete keys. The variable <ahref="/reference/#/p5/keyCode">keyCode</a> stores the BACKSPACE, DELETE, ENTER, RETURN, TAB, ESCAPE, SHIFT, CONTROL, OPTION, ALT, UP_ARROW, DOWN_ARROW, LEFT_ARROW, RIGHT_ARROW. If you're making cross-platform projects, note that the Enter key is commonly used on PCs and UNIX and the Return key is used on Macintosh. Check for both Enter and Return to make sure your program will work for all platforms (see code 12-17).</p>
489
489
490
490
<scripttype="text/p5" data-autoplay>
491
-
vary=35;
491
+
lety=35;
492
492
functionsetup(){
493
493
createCanvas(100,100);
494
494
stroke(0);
@@ -518,13 +518,13 @@ <h2>Coded keys</h2>
518
518
}
519
519
function draw() {
520
520
if (keyIsPressed === true) {
521
-
var x = keyCode - 32;
521
+
let x = keyCode - 32;
522
522
line(x, 0, x, height);
523
523
}
524
524
}
525
525
</script>
526
526
<script type="text/p5" data-autoplay>
527
-
var angle = 0;
527
+
let angle = 0;
528
528
function setup() {
529
529
createCanvas(100, 100);
530
530
stroke(0);
@@ -543,7 +543,7 @@ <h2>Coded keys</h2>
543
543
<h2>Coded keys</h2>
544
544
<p>In addition to reading key values for numbers, letters, and symbols, p5 can also read the values from other keys including the arrow keys and the Alt, Control, Shift, Backspace, Tab, Enter, Return, Escape, and Delete keys. The variable <a href="/reference/#/p5/keyCode">keyCode</a> stores the BACKSPACE, DELETE, ENTER, RETURN, TAB, ESCAPE, SHIFT, CONTROL, OPTION, ALT, UP_ARROW, DOWN_ARROW, LEFT_ARROW, RIGHT_ARROW. If you're making cross-platform projects, note that the Enter key is commonly used on PCs and UNIX and the Return key is used on Macintosh. Check for both Enter and Return to make sure your program will work for all platforms (see code 12-17).</p>
545
545
<script type="text/p5" data-autoplay>
546
-
var y = 35;
546
+
let y = 35;
547
547
function setup() {
548
548
createCanvas(100, 100);
549
549
stroke(0);
@@ -586,7 +586,7 @@ <h2>Mouse events</h2>
586
586
<p>The <ahref="/reference/#/p5/mousePressed">mousePressed()</a> function works differently than the <ahref="/reference/#/p5/mouseIsPressed">mouseIsPressed</a> variable. The value of the <ahref="/reference/#/p5/mouseIsPressed">mouseIsPressed</a> variable is true until the mouse button is released. It can therefore be used within <ahref="/reference/#/p5/draw">draw()</a> to have a line of code run while the mouse is pressed. In contrast, the code inside the <ahref="/reference/#/p5/mousePressed">mousePressed()</a> function only runs once when a button is pressed. This makes it useful when a mouse click is used to trigger an action, such as clearing the screen. In the following example, the background value becomes lighter each time a mouse button is pressed. Run the example on your computer to see the change in response to your finger.</p>
587
587
588
588
<scripttype="text/p5" data-autoplay>
589
-
vargray=0;
589
+
letgray=0;
590
590
functionsetup(){
591
591
createCanvas(100,100);
592
592
}
@@ -601,7 +601,7 @@ <h2>Mouse events</h2>
601
601
<p>The following example is the same as the one above, but the gray variable is set in the <ahref="/reference/#/p5/mouseReleased">mouseReleased()</a> event function, which is called once every time a button is released. This difference can be seen only by running the program and clicking the mouse button. Keep the mouse button pressed for a long time and notice that the background value changes only when the button is released.</p>
602
602
603
603
<scripttype="text/p5" data-autoplay>
604
-
vargray=0;
604
+
letgray=0;
605
605
functionsetup(){
606
606
createCanvas(100,100);
607
607
}
@@ -616,7 +616,7 @@ <h2>Mouse events</h2>
616
616
<!-- mouseClicked() example added-->
617
617
<p>Similarly, the gray variable is set in the <ahref="/reference/#/p5/mouseClicked">mouseClicked()</a> event function, which is called once after a mouse button has been pressed and then released. Browsers handle clicks differently, so this function is only guaranteed to be run when the left mouse button is clicked. To handle other mouse buttons being pressed or released, use <ahref="/reference/#/p5/mousePressed">mousePressed()</a> or <ahref="/reference/#/p5/mouseReleased">mouseReleased()</a>. </p>
618
618
<scripttype="text/p5" data-autoplay>
619
-
vargray=0;
619
+
letgray=0;
620
620
functionsetup(){
621
621
createCanvas(100,100);
622
622
}
@@ -646,7 +646,7 @@ <h2>Mouse events</h2>
646
646
<p>The code inside the <ahref="/reference/#/p5/mouseMoved">mouseMoved()</a> and <ahref="/reference/#/p5/mouseDragged">mouseDragged()</a> event functions are run when there is a change in the mouse position. The code in the <ahref="/reference/#/p5/mouseMoved">mouseMoved()</a> block is run at the end of each frame when the mouse moves and no button is pressed. The code in the <ahref="/reference/#/p5/mouseDragged">mouseDragged()</a> block does the same when the mouse button is pressed. If the mouse stays in the same position from frame to frame, the code inside these functions does not run. In this example, the gray circle follows the mouse when the button is not pressed, and the black circle follows the mouse when a mouse button is pressed.</p>
647
647
648
648
<scripttype="text/p5" data-autoplay>
649
-
vardragX,dragY,moveX,moveY;
649
+
letdragX,dragY,moveX,moveY;
650
650
functionsetup(){
651
651
createCanvas(100,100);
652
652
noStroke();
@@ -671,8 +671,8 @@ <h2>Mouse events</h2>
671
671
<!-- examples added for mouseOut() and mouseOver() -->
672
672
<p>The <ahref="/reference/#/p5/mouseOver">.mouseOver()</a> function is called once after every time a mouse moves onto the element. In this example, the diameter of the ellipse increase by 10 every time the mouse moves onto the canvas.</p>
673
673
<scripttype="text/p5" data-autoplay>
674
-
varcnv;
675
-
vard;
674
+
letcnv;
675
+
letd;
676
676
functionsetup(){
677
677
cnv=createCanvas(100,100);
678
678
cnv.mouseOver(changeD);
@@ -688,8 +688,8 @@ <h2>Mouse events</h2>
688
688
689
689
<p>The <ahref="/reference/#/p5/mouseOut">.mouseOut()</a> function is called once after every time a mouse moves off the element. Similar to the above example, the diameter of the ellipse increase by 10 every time the mouse moves out of the canvas.</p>
690
690
<scripttype="text/p5" data-autoplay>
691
-
varcnv;
692
-
vard;
691
+
letcnv;
692
+
letd;
693
693
functionsetup(){
694
694
cnv=createCanvas(100,100);
695
695
cnv.mouseOut(changeD);
@@ -708,9 +708,9 @@ <h2>Wheel Events</h2>
708
708
<p>In this example, an event listener is attached to the canvas element, and function changeSize() would run when scrolling is performed on canvas. By using the <ahref="/reference/#/p5/deltaY">event.deltaY</a> variable, scrolling up on canvas would increase the diameter of the ellipse and scrolling down would decrease the diameter. If scrolling is performed anywhere in any direction, the background is going to be darker.</p>
709
709
710
710
<scripttype="text/p5" data-autoplay>
711
-
varcnv;
712
-
vard;
713
-
varg;
711
+
letcnv;
712
+
letd;
713
+
letg;
714
714
functionsetup(){
715
715
cnv=createCanvas(100,100);
716
716
cnv.mouseWheel(changeSize);// attach listener for activity on canvas only
@@ -753,7 +753,7 @@ <h2>Key events</h2>
753
753
<p>Each time a key is pressed, the code inside the <ahref="/reference/#/p5/keyPressed">keyPressed()</a> block is run once. Within this block, it's possible to test which key has been pressed and to use this value for any purpose. If a key is held down for an extended time, the code inside the <ahref="/reference/#/p5/keyPressed">keyPressed()</a> block might run many times in a rapid succession because most operating systems will take over and repeatedly call the <ahref="/reference/#/p5/keyPressed">keyPressed()</a> function. The amount of time it takes to start repeating and the rate of repetitions will be different from computer to computer, depending on the keyboard preference settings. In this example, the value of the boolean variable drawT is set from false to true when the T key is pressed; this causes the lines of code to render the rectangles in <ahref="/reference/#/p5/draw">draw()</a> to start running.</p>
754
754
755
755
<scripttype="text/p5" data-autoplay>
756
-
vardrawT=false;
756
+
letdrawT=false;
757
757
functionsetup(){
758
758
createCanvas(100,100);
759
759
noStroke();
@@ -778,7 +778,7 @@ <h2>Key events</h2>
778
778
<p>Each time a key is released, the code inside the <ahref="/reference/#/p5/keyReleased">keyReleased()</a> block is run once. The following example builds on the previous code; each time the key is released the boolean variable drawT is set back to false to stop the shape from displaying within <ahref="/reference/#/p5/draw">draw()</a>.</p>
779
779
780
780
<scripttype="text/p5" data-autoplay>
781
-
vardrawT=false;
781
+
letdrawT=false;
782
782
functionsetup(){
783
783
createCanvas(100,100);
784
784
noStroke();
@@ -804,7 +804,7 @@ <h2>Event flow</h2>
804
804
<p>As discussed previously, programs written with <ahref="/reference/#/p5/draw">draw()</a> display frames to the screen sixty frames each second. The <ahref="/reference/#/p5/frameRate">frameRate()</a> function is used to set a limit on the number of frames that will display each second, and the <ahref="/reference/#/p5/noLoop">noLoop()</a> function can be used to stop draw() from looping. The additional functions <ahref="/reference/#/p5/loop">loop()</a> and <ahref="/reference/#/p5/redraw">redraw()</a> provide more options when used in combination with the mouse and keyboard event functions. If a program has been paused with <ahref="/reference/#/p5/noLoop">noLoop()</a>, running <ahref="/reference/#/p5/loop">loop()</a> resumes its action. Because the event functions are the only elements that continue to run when a program is paused with noLoop(), the loop() function can be used within these events to continue running the code in draw(). The following example runs the draw() function for about two seconds each time a mouse button is pressed and then pauses the program after that time has elapsed.</p>
805
805
806
806
<scripttype="text/p5" data-autoplay>
807
-
varframe=0;
807
+
letframe=0;
808
808
functionsetup(){
809
809
createCanvas(100,100);
810
810
}
@@ -848,9 +848,9 @@ <h2>Event flow</h2>
848
848
<p>Function|Boolean: function to be fired when mouse is pressed over the element. if false is passed instead, the previously firing function will no longer fire.</p>
849
849
<p>In this example</p>
850
850
<script type="text/p5" data-autoplay>
851
-
var cnv;
852
-
var d;
853
-
var g;
851
+
let cnv;
852
+
let d;
853
+
let g;
854
854
function setup() {
855
855
cnv = createCanvas(100, 100);
856
856
cnv.mousePressed(changeGray); // attach listener for canvas click only
0 commit comments