Skip to content

Commit 76ec53a

Browse files
authored
Merge pull request #362 from AnimeshSinha1309/es6/interaction
Updating Interaction Examples to ES6 standards
2 parents 8635141 + dff3c1e commit 76ec53a

27 files changed

+289
-256
lines changed

src/data/examples/en/10_Interaction/10_Tickle.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
* @description The word "tickle" jitters when the cursor hovers over.
44
* Sometimes, it can be tickled off the screen.
55
*/
6-
var message = "tickle",
6+
let message = 'tickle',
77
font,
88
bounds, // holds x, y, w, h of the text's bounding box
99
fontsize = 60,
10-
x, y; // x and y coordinates of the text
10+
x,
11+
y; // x and y coordinates of the text
1112

1213
function preload() {
1314
font = loadFont('assets/SourceSansPro-Regular.otf');
@@ -32,11 +33,15 @@ function draw() {
3233
// write the text in black and get its bounding box
3334
fill(0);
3435
text(message, x, y);
35-
bounds = font.textBounds(message,x,y,fontsize);
36+
bounds = font.textBounds(message, x, y, fontsize);
3637

3738
// check if the mouse is inside the bounding box and tickle if so
38-
if ( mouseX >= bounds.x && mouseX <= bounds.x + bounds.w &&
39-
mouseY >= bounds.y && mouseY <= bounds.y + bounds.h) {
39+
if (
40+
mouseX >= bounds.x &&
41+
mouseX <= bounds.x + bounds.w &&
42+
mouseY >= bounds.y &&
43+
mouseY <= bounds.y + bounds.h
44+
) {
4045
x += random(-5, 5);
4146
y += random(-5, 5);
4247
}

src/data/examples/en/10_Interaction/20_Follow1.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44
* @description A line segment is pushed and pulled by the cursor.
55
* Based on code from Keith Peters.
66
*/
7-
var x = 100,
7+
let x = 100,
88
y = 100,
99
angle1 = 0.0,
1010
segLength = 50;
1111

12-
1312
function setup() {
1413
createCanvas(710, 400);
1514
strokeWeight(20.0);
@@ -22,8 +21,8 @@ function draw() {
2221
dx = mouseX - x;
2322
dy = mouseY - y;
2423
angle1 = atan2(dy, dx);
25-
x = mouseX - (cos(angle1) * segLength);
26-
y = mouseY - (sin(angle1) * segLength);
24+
x = mouseX - cos(angle1) * segLength;
25+
y = mouseY - sin(angle1) * segLength;
2726

2827
segment(x, y, angle1);
2928
ellipse(x, y, 20, 20);

src/data/examples/en/10_Interaction/21_Follow2.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
* angle between the segments is calculated with atan2() and the position
66
* calculated with sin() and cos(). Based on code from Keith Peters.
77
*/
8-
var x = [0,0],
9-
y = [0,0],
8+
let x = [0, 0],
9+
y = [0, 0],
1010
segLength = 50;
1111

1212
function setup() {
@@ -22,9 +22,9 @@ function draw() {
2222
}
2323

2424
function dragSegment(i, xin, yin) {
25-
var dx = xin - x[i];
26-
var dy = yin - y[i];
27-
var angle = atan2(dy, dx);
25+
const dx = xin - x[i];
26+
const dy = yin - y[i];
27+
const angle = atan2(dy, dx);
2828
x[i] = xin - cos(angle) * segLength;
2929
y[i] = yin - sin(angle) * segLength;
3030
segment(x[i], y[i], angle);

src/data/examples/en/10_Interaction/22_Follow3.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
* each segment to the next is calculated with atan2() and the position of
66
* the next is calculated with sin() and cos(). Based on code from Keith Peters.
77
*/
8-
var x = [],
8+
let x = [],
99
y = [],
1010
segNum = 20,
1111
segLength = 18;
1212

13-
for (var i = 0; i < segNum; i++) {
13+
for (let i = 0; i < segNum; i++) {
1414
x[i] = 0;
1515
y[i] = 0;
1616
}
@@ -24,15 +24,15 @@ function setup() {
2424
function draw() {
2525
background(0);
2626
dragSegment(0, mouseX, mouseY);
27-
for( var i=0; i<x.length-1; i++) {
28-
dragSegment(i+1, x[i], y[i]);
27+
for (let i = 0; i < x.length - 1; i++) {
28+
dragSegment(i + 1, x[i], y[i]);
2929
}
3030
}
3131

3232
function dragSegment(i, xin, yin) {
33-
var dx = xin - x[i];
34-
var dy = yin - y[i];
35-
var angle = atan2(dy, dx);
33+
const dx = xin - x[i];
34+
const dy = yin - y[i];
35+
const angle = atan2(dy, dx);
3636
x[i] = xin - cos(angle) * segLength;
3737
y[i] = yin - sin(angle) * segLength;
3838
segment(x[i], y[i], angle);

src/data/examples/en/10_Interaction/23_snake.js

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,44 @@
44
* inside the black area, and control the snake using i j k and l. Don't let
55
* the snake hit itself or the wall!<br>
66
* Example created by <a href='https://github.com/prashantgupta24' target='_blank'>Prashant Gupta
7-
*/
7+
*/
88

99
// the snake is divided into small segments, which are drawn and edited on each 'draw' call
10-
var numSegments = 10;
11-
var direction = 'right';
10+
let numSegments = 10;
11+
let direction = 'right';
1212

13-
var xStart = 0; //starting x coordinate for snake
14-
var yStart = 250; //starting y coordinate for snake
15-
var diff = 10;
13+
const xStart = 0; //starting x coordinate for snake
14+
const yStart = 250; //starting y coordinate for snake
15+
const diff = 10;
1616

17-
var xCor = [];
18-
var yCor = [];
17+
let xCor = [];
18+
let yCor = [];
1919

20-
var xFruit = 0;
21-
var yFruit = 0;
22-
var scoreElem;
20+
let xFruit = 0;
21+
let yFruit = 0;
22+
let scoreElem;
2323

2424
function setup() {
2525
scoreElem = createDiv('Score = 0');
2626
scoreElem.position(20, 20);
2727
scoreElem.id = 'score';
2828
scoreElem.style('color', 'white');
29-
29+
3030
createCanvas(500, 500);
3131
frameRate(15);
3232
stroke(255);
3333
strokeWeight(10);
3434
updateFruitCoordinates();
3535

36-
for (var i = 0; i < numSegments; i++) {
37-
xCor.push(xStart + (i * diff));
36+
for (let i = 0; i < numSegments; i++) {
37+
xCor.push(xStart + i * diff);
3838
yCor.push(yStart);
3939
}
4040
}
4141

4242
function draw() {
4343
background(0);
44-
for (var i = 0; i < numSegments - 1; i++) {
44+
for (let i = 0; i < numSegments - 1; i++) {
4545
line(xCor[i], yCor[i], xCor[i + 1], yCor[i + 1]);
4646
}
4747
updateSnakeCoordinates();
@@ -61,8 +61,7 @@ function draw() {
6161
or down, the segment's y coordinate is affected.
6262
*/
6363
function updateSnakeCoordinates() {
64-
65-
for (var i = 0; i < numSegments - 1; i++) {
64+
for (let i = 0; i < numSegments - 1; i++) {
6665
xCor[i] = xCor[i + 1];
6766
yCor[i] = yCor[i + 1];
6867
}
@@ -92,13 +91,15 @@ function updateSnakeCoordinates() {
9291
or if the snake hits itself.
9392
*/
9493
function checkGameStatus() {
95-
if (xCor[xCor.length - 1] > width ||
96-
xCor[xCor.length - 1] < 0 ||
97-
yCor[yCor.length - 1] > height ||
98-
yCor[yCor.length - 1] < 0 ||
99-
checkSnakeCollision()) {
94+
if (
95+
xCor[xCor.length - 1] > width ||
96+
xCor[xCor.length - 1] < 0 ||
97+
yCor[yCor.length - 1] > height ||
98+
yCor[yCor.length - 1] < 0 ||
99+
checkSnakeCollision()
100+
) {
100101
noLoop();
101-
var scoreVal = parseInt(scoreElem.html().substring(8));
102+
const scoreVal = parseInt(scoreElem.html().substring(8));
102103
scoreElem.html('Game ended! Your score was : ' + scoreVal);
103104
}
104105
}
@@ -108,9 +109,9 @@ function checkGameStatus() {
108109
has to be the same as one of its own segment's (x,y) coordinate.
109110
*/
110111
function checkSnakeCollision() {
111-
var snakeHeadX = xCor[xCor.length - 1];
112-
var snakeHeadY = yCor[yCor.length - 1];
113-
for (var i = 0; i < xCor.length - 1; i++) {
112+
const snakeHeadX = xCor[xCor.length - 1];
113+
const snakeHeadY = yCor[yCor.length - 1];
114+
for (let i = 0; i < xCor.length - 1; i++) {
114115
if (xCor[i] === snakeHeadX && yCor[i] === snakeHeadY) {
115116
return true;
116117
}
@@ -125,7 +126,7 @@ function checkSnakeCollision() {
125126
function checkForFruit() {
126127
point(xFruit, yFruit);
127128
if (xCor[xCor.length - 1] === xFruit && yCor[yCor.length - 1] === yFruit) {
128-
var prevScore = parseInt(scoreElem.html().substring(8));
129+
const prevScore = parseInt(scoreElem.html().substring(8));
129130
scoreElem.html('Score = ' + (prevScore + 1));
130131
xCor.unshift(xCor[0]);
131132
yCor.unshift(yCor[0]);
@@ -148,22 +149,22 @@ function updateFruitCoordinates() {
148149
function keyPressed() {
149150
switch (keyCode) {
150151
case 74:
151-
if (direction != 'right') {
152+
if (direction !== 'right') {
152153
direction = 'left';
153154
}
154155
break;
155156
case 76:
156-
if (direction != 'left') {
157+
if (direction !== 'left') {
157158
direction = 'right';
158159
}
159160
break;
160161
case 73:
161-
if (direction != 'down') {
162+
if (direction !== 'down') {
162163
direction = 'up';
163164
}
164165
break;
165166
case 75:
166-
if (direction != 'up') {
167+
if (direction !== 'up') {
167168
direction = 'down';
168169
}
169170
break;

src/data/examples/en/10_Interaction/24_Wavemaker.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ function draw() {
2020
for (let x = 0; x <= width; x = x + 30) {
2121
for (let y = 0; y <= height; y = y + 30) {
2222
// starting point of each circle depends on mouse position
23-
let xAngle = map(mouseX, 0, width, -4 * PI, 4 * PI, true);
24-
let yAngle = map(mouseY, 0, height, -4 * PI, 4 * PI, true);
23+
const xAngle = map(mouseX, 0, width, -4 * PI, 4 * PI, true);
24+
const yAngle = map(mouseY, 0, height, -4 * PI, 4 * PI, true);
2525
// and also varies based on the particle's location
26-
let angle = xAngle * (x / width) + yAngle * (y / height);
26+
const angle = xAngle * (x / width) + yAngle * (y / height);
2727

2828
// each particle moves in a circle
29-
let myX = x + 20 * cos(2 * PI * t + angle);
30-
let myY = y + 20 * sin(2 * PI * t + angle);
29+
const myX = x + 20 * cos(2 * PI * t + angle);
30+
const myY = y + 20 * sin(2 * PI * t + angle);
3131

3232
ellipse(myX, myY, 10); // draw particle
3333
}

src/data/examples/en/10_Interaction/25_reach1.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,28 @@
44
* @description The arm follows the position of the mouse by calculating the
55
* angles with atan2(). Based on code from Keith Peters.
66
*/
7-
var segLength = 80,
8-
x, y, x2, y2;
7+
let segLength = 80,
8+
x,
9+
y,
10+
x2,
11+
y2;
912

1013
function setup() {
1114
createCanvas(710, 400);
1215
strokeWeight(20);
1316
stroke(255, 100);
1417

15-
x = width/2;
16-
y = height/2;
18+
x = width / 2;
19+
y = height / 2;
1720
x2 = x;
1821
y2 = y;
1922
}
2023

2124
function draw() {
2225
background(0);
2326
dragSegment(0, mouseX, mouseY);
24-
for( var i=0; i<x.length-1; i++) {
25-
dragSegment(i+1, x[i], y[i]);
27+
for (let i = 0; i < x.length - 1; i++) {
28+
dragSegment(i + 1, x[i], y[i]);
2629
}
2730
}
2831

src/data/examples/en/10_Interaction/26_reach2.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
* @description The arm follows the position of the mouse by calculating the
55
* angles with atan2(). Based on code from Keith Peters.
66
*/
7-
var numSegments = 10,
7+
let numSegments = 10,
88
x = [],
99
y = [],
1010
angle = [],
1111
segLength = 26,
12-
targetX, targetY;
12+
targetX,
13+
targetY;
1314

14-
for (var i = 0; i < numSegments; i++) {
15+
for (let i = 0; i < numSegments; i++) {
1516
x[i] = 0;
1617
y[i] = 0;
1718
angle[i] = 0;
@@ -22,22 +23,22 @@ function setup() {
2223
strokeWeight(20);
2324
stroke(255, 100);
2425

25-
x[x.length-1] = width/2; // Set base x-coordinate
26-
y[x.length-1] = height; // Set base y-coordinate
26+
x[x.length - 1] = width / 2; // Set base x-coordinate
27+
y[x.length - 1] = height; // Set base y-coordinate
2728
}
2829

2930
function draw() {
3031
background(0);
3132

3233
reachSegment(0, mouseX, mouseY);
33-
for(var i=1; i<numSegments; i++) {
34+
for (let i = 1; i < numSegments; i++) {
3435
reachSegment(i, targetX, targetY);
3536
}
36-
for(var j=x.length-1; j>=1; j--) {
37-
positionSegment(j, j-1);
37+
for (let j = x.length - 1; j >= 1; j--) {
38+
positionSegment(j, j - 1);
3839
}
39-
for(var k=0; k<x.length; k++) {
40-
segment(x[k], y[k], angle[k], (k+1)*2);
40+
for (let k = 0; k < x.length; k++) {
41+
segment(x[k], y[k], angle[k], (k + 1) * 2);
4142
}
4243
}
4344

@@ -47,8 +48,8 @@ function positionSegment(a, b) {
4748
}
4849

4950
function reachSegment(i, xin, yin) {
50-
var dx = xin - x[i];
51-
var dy = yin - y[i];
51+
const dx = xin - x[i];
52+
const dy = yin - y[i];
5253
angle[i] = atan2(dy, dx);
5354
targetX = xin - cos(angle[i]) * segLength;
5455
targetY = yin - sin(angle[i]) * segLength;

0 commit comments

Comments
 (0)