Skip to content

Commit 4f7d23d

Browse files
authored
Merge pull request #355 from AnimeshSinha1309/es6/input
Updating Input Examples to ES6 standards
2 parents ad451b4 + 3116589 commit 4f7d23d

30 files changed

+516
-507
lines changed
Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,45 @@
11
/*
22
* @name Clock
3-
* @description The current time can be read with the second(),
4-
* minute(), and hour() functions. In this example, sin() and
3+
* @description The current time can be read with the second(),
4+
* minute(), and hour() functions. In this example, sin() and
55
* cos() values are used to set the position of the hands.
66
*/
7-
var cx, cy;
8-
var secondsRadius;
9-
var minutesRadius;
10-
var hoursRadius;
11-
var clockDiameter;
7+
let cx, cy;
8+
let secondsRadius;
9+
let minutesRadius;
10+
let hoursRadius;
11+
let clockDiameter;
1212

1313
function setup() {
1414
createCanvas(720, 400);
1515
stroke(255);
16-
17-
var radius = min(width, height) / 2;
16+
17+
let radius = min(width, height) / 2;
1818
secondsRadius = radius * 0.71;
19-
minutesRadius = radius * 0.60;
20-
hoursRadius = radius * 0.50;
19+
minutesRadius = radius * 0.6;
20+
hoursRadius = radius * 0.5;
2121
clockDiameter = radius * 1.7;
22-
22+
2323
cx = width / 2;
2424
cy = height / 2;
2525
}
2626

2727
function draw() {
2828
background(230);
29-
29+
3030
// Draw the clock background
3131
noStroke();
32-
fill(244,122,158);
32+
fill(244, 122, 158);
3333
ellipse(cx, cy, clockDiameter + 25, clockDiameter + 25);
34-
fill(237,34,93);
34+
fill(237, 34, 93);
3535
ellipse(cx, cy, clockDiameter, clockDiameter);
36-
36+
3737
// Angles for sin() and cos() start at 3 o'clock;
3838
// subtract HALF_PI to make them start at the top
39-
var s = map(second(), 0, 60, 0, TWO_PI) - HALF_PI;
40-
var m = map(minute() + norm(second(), 0, 60), 0, 60, 0, TWO_PI) - HALF_PI;
41-
var h = map(hour() + norm(minute(), 0, 60), 0, 24, 0, TWO_PI * 2) - HALF_PI;
42-
39+
let s = map(second(), 0, 60, 0, TWO_PI) - HALF_PI;
40+
let m = map(minute() + norm(second(), 0, 60), 0, 60, 0, TWO_PI) - HALF_PI;
41+
let h = map(hour() + norm(minute(), 0, 60), 0, 24, 0, TWO_PI * 2) - HALF_PI;
42+
4343
// Draw the hands of the clock
4444
stroke(255);
4545
strokeWeight(1);
@@ -48,15 +48,15 @@ function draw() {
4848
line(cx, cy, cx + cos(m) * minutesRadius, cy + sin(m) * minutesRadius);
4949
strokeWeight(4);
5050
line(cx, cy, cx + cos(h) * hoursRadius, cy + sin(h) * hoursRadius);
51-
51+
5252
// Draw the minute ticks
5353
strokeWeight(2);
5454
beginShape(POINTS);
55-
for (var a = 0; a < 360; a+=6) {
56-
var angle = radians(a);
57-
var x = cx + cos(angle) * secondsRadius;
58-
var y = cy + sin(angle) * secondsRadius;
55+
for (let a = 0; a < 360; a += 6) {
56+
let angle = radians(a);
57+
let x = cx + cos(angle) * secondsRadius;
58+
let y = cy + sin(angle) * secondsRadius;
5959
vertex(x, y);
6060
}
6161
endShape();
62-
}
62+
}
Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
11
/*
22
* @name Constrain
3-
* @description Move the mouse across the screen to move
3+
* @description Move the mouse across the screen to move
44
* the circle. The program constrains the circle to its box.
55
*/
6-
var mx = 1;
7-
var my = 1;
8-
var easing = 0.05;
9-
var radius = 24;
10-
var edge = 100;
11-
var inner = edge + radius;
6+
let mx = 1;
7+
let my = 1;
8+
let easing = 0.05;
9+
let radius = 24;
10+
let edge = 100;
11+
let inner = edge + radius;
1212

1313
function setup() {
1414
createCanvas(720, 400);
15-
noStroke();
15+
noStroke();
1616
ellipseMode(RADIUS);
1717
rectMode(CORNERS);
1818
}
1919

2020
function draw() {
2121
background(230);
22-
22+
2323
if (abs(mouseX - mx) > 0.1) {
2424
mx = mx + (mouseX - mx) * easing;
2525
}
2626
if (abs(mouseY - my) > 0.1) {
27-
my = my + (mouseY- my) * easing;
27+
my = my + (mouseY - my) * easing;
2828
}
29-
29+
3030
mx = constrain(mx, inner, width - inner);
3131
my = constrain(my, inner, height - inner);
32-
fill(237,34,93);
33-
rect(edge, edge, width-edge, height-edge);
32+
fill(237, 34, 93);
33+
rect(edge, edge, width - edge, height - edge);
3434
fill(255);
3535
ellipse(mx, my, radius, radius);
36-
}
36+
}
Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,30 @@
11
/*
22
* @name Easing
33
* @description Move the mouse across the screen and the symbol
4-
* will follow. Between drawing each frame of the animation, the
4+
* will follow. Between drawing each frame of the animation, the
55
* program calculates the difference between the position of the
66
* symbol and the cursor. If the distance is larger than 1 pixel,
77
* the symbol moves part of the distance (0.05) from its current
88
* position toward the cursor.
99
*/
10-
var x = 1;
11-
var y = 1;
12-
var easing = 0.05;
10+
let x = 1;
11+
let y = 1;
12+
let easing = 0.05;
1313

1414
function setup() {
1515
createCanvas(720, 400);
1616
noStroke();
1717
}
1818

19-
function draw()
20-
{
21-
background(237,34,93);
22-
var targetX = mouseX;
23-
var dx = targetX - x;
24-
x += dx * easing;
19+
function draw() {
20+
background(237, 34, 93);
21+
let targetX = mouseX;
22+
let dx = targetX - x;
23+
x += dx * easing;
2524

26-
var targetY = mouseY;
27-
var dy = targetY - y;
28-
y += dy * easing;
25+
let targetY = mouseY;
26+
let dy = targetY - y;
27+
y += dy * easing;
2928

30-
ellipse(x,y,66,66);
31-
32-
}
29+
ellipse(x, y, 66, 66);
30+
}
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
11
/*
22
* @name Keyboard
3-
* @description Click on the image to give it focus and
4-
* press the letter keys to create forms in time and space.
5-
* Each key has a unique identifying number. These numbers
3+
* @description Click on the image to give it focus and
4+
* press the letter keys to create forms in time and space.
5+
* Each key has a unique identifying number. These numbers
66
* can be used to position shapes in space.
77
*/
8-
var rectWidth;
9-
8+
let rectWidth;
9+
1010
function setup() {
1111
createCanvas(720, 400);
1212
noStroke();
1313
background(230);
14-
rectWidth = width/4;
14+
rectWidth = width / 4;
1515
}
1616

17-
function draw() {
17+
function draw() {
1818
// keep draw() here to continue looping while waiting for keys
1919
}
2020

2121
function keyPressed() {
22-
var keyIndex = -1;
22+
let keyIndex = -1;
2323
if (key >= 'a' && key <= 'z') {
2424
keyIndex = key.charCodeAt(0) - 'a'.charCodeAt(0);
2525
}
26-
if (keyIndex == -1) {
26+
if (keyIndex === -1) {
2727
// If it's not a letter key, clear the screen
2828
background(230);
29-
} else {
29+
} else {
3030
// It's a letter key, fill a rectangle
3131
randFill_r = Math.floor(Math.random() * 255 + 1);
3232
randFill_g = Math.floor(Math.random() * 255 + 1);
3333
randFill_b = Math.floor(Math.random() * 255 + 1);
34-
fill(randFill_r,randFill_g,randFill_b);
35-
var x = map(keyIndex, 0, 25, 0, width - rectWidth);
34+
fill(randFill_r, randFill_g, randFill_b);
35+
let x = map(keyIndex, 0, 25, 0, width - rectWidth);
3636
rect(x, 0, rectWidth, height);
3737
}
38-
}
38+
}

src/data/examples/en/21_Input/04_Mouse1D.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ function setup() {
1313
function draw() {
1414
background(230);
1515

16-
var r1 = map(mouseX, 0, width, 0, height);
17-
var r2 = height - r1;
18-
19-
fill(237,34,93,r1);
20-
rect(width/2 + r1/2, height/2, r1, r1);
21-
22-
fill(237,34,93,r2);
23-
rect(width/2 - r2/2, height/2, r2, r2);
24-
}
16+
let r1 = map(mouseX, 0, width, 0, height);
17+
let r2 = height - r1;
18+
19+
fill(237, 34, 93, r1);
20+
rect(width / 2 + r1 / 2, height / 2, r1, r1);
21+
22+
fill(237, 34, 93, r2);
23+
rect(width / 2 - r2 / 2, height / 2, r2, r2);
24+
}
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
/*
22
* @name Mouse 2D
3-
* @description Moving the mouse changes the position and
3+
* @description Moving the mouse changes the position and
44
* size of each box.
55
*/
66
function setup() {
7-
createCanvas(720, 400);
7+
createCanvas(720, 400);
88
noStroke();
99
rectMode(CENTER);
1010
}
1111

1212
function draw() {
13-
background(230);
14-
fill(244,122,158);
15-
rect(mouseX, height/2, mouseY/2+10, mouseY/2+10);
16-
fill(237,34,93);
17-
var inverseX = width-mouseX;
18-
var inverseY = height-mouseY;
19-
rect(inverseX, height/2, (inverseY/2)+10, (inverseY/2)+10);
20-
}
13+
background(230);
14+
fill(244, 122, 158);
15+
rect(mouseX, height / 2, mouseY / 2 + 10, mouseY / 2 + 10);
16+
fill(237, 34, 93);
17+
let inverseX = width - mouseX;
18+
let inverseY = height - mouseY;
19+
rect(inverseX, height / 2, inverseY / 2 + 10, inverseY / 2 + 10);
20+
}

src/data/examples/en/21_Input/06_MouseIsPressed.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ function setup() {
1010
}
1111

1212
function draw() {
13-
if (mouseIsPressed){
13+
if (mouseIsPressed) {
1414
stroke(255);
15+
} else {
16+
stroke(237, 34, 93);
1517
}
16-
else {
17-
stroke(237,34,93);
18-
}
19-
line(mouseX-66, mouseY, mouseX+66, mouseY);
20-
line(mouseX, mouseY-66, mouseX, mouseY+66);
21-
}
18+
line(mouseX - 66, mouseY, mouseX + 66, mouseY);
19+
line(mouseX, mouseY - 66, mouseX, mouseY + 66);
20+
}

0 commit comments

Comments
 (0)