Skip to content

Commit 4983002

Browse files
authored
Merge pull request #384 from camerenisonfire/issue313-interactivity-tut-es6
resolves #313 - Converting Interactivity tutorial to ES6.
2 parents 00b6e40 + be9ec87 commit 4983002

File tree

4 files changed

+112
-112
lines changed

4 files changed

+112
-112
lines changed

dist/es/learn/interactivity.html

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,10 @@ <h2>Mouse Data</h2>
189189
noStroke();
190190
}
191191
function draw() {
192-
var x = mouseX;
193-
var y = mouseY;
194-
var ix = width - mouseX; // Inverse X
195-
var iy = height - mouseY; // Inverse Y
192+
let x = mouseX;
193+
let y = mouseY;
194+
let ix = width - mouseX; // Inverse X
195+
let iy = height - mouseY; // Inverse Y
196196
background(126);
197197
fill(255, 150);
198198
ellipse(x, height/2, y, y);
@@ -396,7 +396,7 @@ <h2>Keyboard data</h2>
396396
</script>
397397

398398
<script type="text/p5" data-autoplay>
399-
var x = 20;
399+
let x = 20;
400400
function setup() {
401401
createCanvas(100, 100);
402402
strokeWeight(4);
@@ -461,14 +461,14 @@ <h2>Coded keys</h2>
461461
}
462462
function draw() {
463463
if (keyIsPressed === true) {
464-
var x = keyCode - 32;
464+
let x = keyCode - 32;
465465
line(x, 0, x, height);
466466
}
467467
}
468468
</script>
469469

470470
<script type="text/p5" data-autoplay>
471-
var angle = 0;
471+
let angle = 0;
472472
function setup() {
473473
createCanvas(100, 100);
474474
stroke(0);
@@ -488,7 +488,7 @@ <h2>Coded keys</h2>
488488
<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>
489489

490490
<script type="text/p5" data-autoplay>
491-
var y = 35;
491+
let y = 35;
492492
function setup() {
493493
createCanvas(100, 100);
494494
stroke(0);
@@ -518,13 +518,13 @@ <h2>Coded keys</h2>
518518
}
519519
function draw() {
520520
if (keyIsPressed === true) {
521-
var x = keyCode - 32;
521+
let x = keyCode - 32;
522522
line(x, 0, x, height);
523523
}
524524
}
525525
</script>
526526
<script type="text/p5" data-autoplay>
527-
var angle = 0;
527+
let angle = 0;
528528
function setup() {
529529
createCanvas(100, 100);
530530
stroke(0);
@@ -543,7 +543,7 @@ <h2>Coded keys</h2>
543543
<h2>Coded keys</h2>
544544
<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>
545545
<script type="text/p5" data-autoplay>
546-
var y = 35;
546+
let y = 35;
547547
function setup() {
548548
createCanvas(100, 100);
549549
stroke(0);
@@ -586,7 +586,7 @@ <h2>Mouse events</h2>
586586
<p>The <a href="/reference/#/p5/mousePressed">mousePressed()</a> function works differently than the <a href="/reference/#/p5/mouseIsPressed">mouseIsPressed</a> variable. The value of the <a href="/reference/#/p5/mouseIsPressed">mouseIsPressed</a> variable is true until the mouse button is released. It can therefore be used within <a href="/reference/#/p5/draw">draw()</a> to have a line of code run while the mouse is pressed. In contrast, the code inside the <a href="/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>
587587

588588
<script type="text/p5" data-autoplay>
589-
var gray = 0;
589+
let gray = 0;
590590
function setup() {
591591
createCanvas(100, 100);
592592
}
@@ -601,7 +601,7 @@ <h2>Mouse events</h2>
601601
<p>The following example is the same as the one above, but the gray variable is set in the <a href="/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>
602602

603603
<script type="text/p5" data-autoplay>
604-
var gray = 0;
604+
let gray = 0;
605605
function setup() {
606606
createCanvas(100, 100);
607607
}
@@ -616,7 +616,7 @@ <h2>Mouse events</h2>
616616
<!-- mouseClicked() example added-->
617617
<p>Similarly, the gray variable is set in the <a href="/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 <a href="/reference/#/p5/mousePressed">mousePressed()</a> or <a href="/reference/#/p5/mouseReleased">mouseReleased()</a>. </p>
618618
<script type="text/p5" data-autoplay>
619-
var gray = 0;
619+
let gray = 0;
620620
function setup() {
621621
createCanvas(100, 100);
622622
}
@@ -646,7 +646,7 @@ <h2>Mouse events</h2>
646646
<p>The code inside the <a href="/reference/#/p5/mouseMoved">mouseMoved()</a> and <a href="/reference/#/p5/mouseDragged">mouseDragged()</a> event functions are run when there is a change in the mouse position. The code in the <a href="/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 <a href="/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>
647647

648648
<script type="text/p5" data-autoplay>
649-
var dragX, dragY, moveX, moveY;
649+
let dragX, dragY, moveX, moveY;
650650
function setup() {
651651
createCanvas(100, 100);
652652
noStroke();
@@ -671,8 +671,8 @@ <h2>Mouse events</h2>
671671
<!-- examples added for mouseOut() and mouseOver() -->
672672
<p>The <a href="/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>
673673
<script type="text/p5" data-autoplay>
674-
var cnv;
675-
var d;
674+
let cnv;
675+
let d;
676676
function setup() {
677677
cnv = createCanvas(100, 100);
678678
cnv.mouseOver(changeD);
@@ -688,8 +688,8 @@ <h2>Mouse events</h2>
688688

689689
<p>The <a href="/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>
690690
<script type="text/p5" data-autoplay>
691-
var cnv;
692-
var d;
691+
let cnv;
692+
let d;
693693
function setup() {
694694
cnv = createCanvas(100, 100);
695695
cnv.mouseOut(changeD);
@@ -708,9 +708,9 @@ <h2>Wheel Events</h2>
708708
<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 <a href="/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>
709709

710710
<script type="text/p5" data-autoplay>
711-
var cnv;
712-
var d;
713-
var g;
711+
let cnv;
712+
let d;
713+
let g;
714714
function setup() {
715715
cnv = createCanvas(100, 100);
716716
cnv.mouseWheel(changeSize); // attach listener for activity on canvas only
@@ -753,7 +753,7 @@ <h2>Key events</h2>
753753
<p>Each time a key is pressed, the code inside the <a href="/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 <a href="/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 <a href="/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 <a href="/reference/#/p5/draw">draw()</a> to start running.</p>
754754

755755
<script type="text/p5" data-autoplay>
756-
var drawT = false;
756+
let drawT = false;
757757
function setup() {
758758
createCanvas(100, 100);
759759
noStroke();
@@ -778,7 +778,7 @@ <h2>Key events</h2>
778778
<p>Each time a key is released, the code inside the <a href="/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 <a href="/reference/#/p5/draw">draw()</a>.</p>
779779

780780
<script type="text/p5" data-autoplay>
781-
var drawT = false;
781+
let drawT = false;
782782
function setup() {
783783
createCanvas(100, 100);
784784
noStroke();
@@ -804,7 +804,7 @@ <h2>Event flow</h2>
804804
<p>As discussed previously, programs written with <a href="/reference/#/p5/draw">draw()</a> display frames to the screen sixty frames each second. The <a href="/reference/#/p5/frameRate">frameRate()</a> function is used to set a limit on the number of frames that will display each second, and the <a href="/reference/#/p5/noLoop">noLoop()</a> function can be used to stop draw() from looping. The additional functions <a href="/reference/#/p5/loop">loop()</a> and <a href="/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 <a href="/reference/#/p5/noLoop">noLoop()</a>, running <a href="/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>
805805

806806
<script type="text/p5" data-autoplay>
807-
var frame = 0;
807+
let frame = 0;
808808
function setup() {
809809
createCanvas(100, 100);
810810
}
@@ -848,9 +848,9 @@ <h2>Event flow</h2>
848848
<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>
849849
<p>In this example</p>
850850
<script type="text/p5" data-autoplay>
851-
var cnv;
852-
var d;
853-
var g;
851+
let cnv;
852+
let d;
853+
let g;
854854
function setup() {
855855
cnv = createCanvas(100, 100);
856856
cnv.mousePressed(changeGray); // attach listener for canvas click only

0 commit comments

Comments
 (0)