Skip to content

Commit 7ad279a

Browse files
authored
Merge pull request #347 from AnimeshSinha1309/es6/structure
Updating Structure Examples to ES6 standards
2 parents 22331cd + de61beb commit 7ad279a

27 files changed

+208
-212
lines changed
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/*
22
* @name Coordinates
3-
* @description All shapes drawn to the screen have a position that is
3+
* @description All shapes drawn to the screen have a position that is
44
* specified as a coordinate. All coordinates are measured as the distance from
55
* the origin in units of pixels. The origin [0, 0] is the coordinate in the
6-
* upper left of the window and the coordinate in the lower right is [width-1,
6+
* upper left of the window and the coordinate in the lower right is [width-1,
77
* height-1].
88
*/
99
function setup() {
@@ -16,24 +16,24 @@ function draw() {
1616
background(0);
1717
noFill();
1818

19-
// The two parameters of the point() method each specify
19+
// The two parameters of the point() method each specify
2020
// coordinates.
21-
// The first parameter is the x-coordinate and the second is the Y
21+
// The first parameter is the x-coordinate and the second is the Y
2222
stroke(255);
2323
point(width * 0.5, height * 0.5);
24-
point(width * 0.5, height * 0.25);
24+
point(width * 0.5, height * 0.25);
2525

2626
// Coordinates are used for drawing all shapes, not just points.
27-
// Parameters for different functions are used for different
28-
// purposes. For example, the first two parameters to line()
29-
// specify the coordinates of the first endpoint and the second
27+
// Parameters for different functions are used for different
28+
// purposes. For example, the first two parameters to line()
29+
// specify the coordinates of the first endpoint and the second
3030
// two parameters specify the second endpoint
3131
stroke(0, 153, 255);
32-
line(0, height*0.33, width, height*0.33);
32+
line(0, height * 0.33, width, height * 0.33);
3333

34-
// By default, the first two parameters to rect() are the
34+
// By default, the first two parameters to rect() are the
3535
// coordinates of the upper-left corner and the second pair
3636
// is the width and height
3737
stroke(255, 153, 0);
38-
rect(width*0.25, height*0.1, width * 0.5, height * 0.8);
38+
rect(width * 0.25, height * 0.1, width * 0.5, height * 0.8);
3939
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* @name Width and Height
3-
* @description The 'width' and 'height' variables contain the
4-
* width and height of the display window as defined in the createCanvas()
3+
* @description The 'width' and 'height' variables contain the
4+
* width and height of the display window as defined in the createCanvas()
55
* function.
66
*/
77
function setup() {
@@ -11,10 +11,10 @@ function setup() {
1111
function draw() {
1212
background(127);
1313
noStroke();
14-
for (var i = 0; i < height; i += 20) {
14+
for (let i = 0; i < height; i += 20) {
1515
fill(129, 206, 15);
1616
rect(0, i, width, 10);
1717
fill(255);
1818
rect(i, 0, 10, height);
1919
}
20-
}
20+
}
Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
/*
22
* @name Setup and Draw
3-
* @description The code inside the draw() function runs continuously from top
3+
* @description The code inside the draw() function runs continuously from top
44
* to bottom until the program is stopped.
55
*/
6-
var y = 100;
6+
let y = 100;
77

8-
// The statements in the setup() function
8+
// The statements in the setup() function
99
// execute once when the program begins
1010
function setup() {
11-
// createCanvas must be the first statement
12-
createCanvas(720, 400);
13-
stroke(255); // Set line drawing color to white
11+
// createCanvas must be the first statement
12+
createCanvas(720, 400);
13+
stroke(255); // Set line drawing color to white
1414
frameRate(30);
1515
}
16-
// The statements in draw() are executed until the
17-
// program is stopped. Each statement is executed in
18-
// sequence and after the last line is read, the first
16+
// The statements in draw() are executed until the
17+
// program is stopped. Each statement is executed in
18+
// sequence and after the last line is read, the first
1919
// line is executed again.
20-
function draw() {
21-
background(0); // Set the background to black
22-
y = y - 1;
23-
if (y < 0) {
24-
y = height;
25-
}
26-
line(0, y, width, y);
27-
}
20+
function draw() {
21+
background(0); // Set the background to black
22+
y = y - 1;
23+
if (y < 0) {
24+
y = height;
25+
}
26+
line(0, y, width, y);
27+
}
Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
/*
22
* @name No Loop
3-
* @description The noLoop() function causes draw() to only execute once.
3+
* @description The noLoop() function causes draw() to only execute once.
44
* Without calling noLoop(), the code inside draw() is run continually.
55
*/
6-
var y;
6+
let y;
77

8-
// The statements in the setup() function
8+
// The statements in the setup() function
99
// execute once when the program begins
10-
function setup()
11-
{
10+
function setup() {
1211
// createCanvas should be the first statement
13-
createCanvas(720, 400);
14-
stroke(255); // Set line drawing color to white
12+
createCanvas(720, 400);
13+
stroke(255); // Set line drawing color to white
1514
noLoop();
16-
15+
1716
y = height * 0.5;
1817
}
1918

20-
// The statements in draw() are executed until the
21-
// program is stopped. Each statement is executed in
22-
// sequence and after the last line is read, the first
19+
// The statements in draw() are executed until the
20+
// program is stopped. Each statement is executed in
21+
// sequence and after the last line is read, the first
2322
// line is executed again.
24-
function draw()
25-
{
26-
background(0); // Set the background to black
27-
y = y - 1;
28-
if (y < 0) { y = height; }
29-
line(0, y, width, y);
30-
}
23+
function draw() {
24+
background(0); // Set the background to black
25+
y = y - 1;
26+
if (y < 0) {
27+
y = height;
28+
}
29+
line(0, y, width, y);
30+
}
Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
/*
22
* @name Loop
3-
* @description The code inside the draw() function runs continuously from top
3+
* @description The code inside the draw() function runs continuously from top
44
* to bottom until the program is stopped.
55
*/
6-
var y = 100;
6+
let y = 100;
77

8-
// The statements in the setup() function
8+
// The statements in the setup() function
99
// execute once when the program begins
1010
function setup() {
11-
createCanvas(720, 400); // Size must be the first statement
12-
stroke(255); // Set line drawing color to white
11+
createCanvas(720, 400); // Size must be the first statement
12+
stroke(255); // Set line drawing color to white
1313
frameRate(30);
1414
}
15-
// The statements in draw() are executed until the
16-
// program is stopped. Each statement is executed in
17-
// sequence and after the last line is read, the first
15+
// The statements in draw() are executed until the
16+
// program is stopped. Each statement is executed in
17+
// sequence and after the last line is read, the first
1818
// line is executed again.
19-
function draw() {
20-
background(0); // Set the background to black
21-
y = y - 1;
22-
if (y < 0) {
23-
y = height;
24-
}
25-
line(0, y, width, y);
26-
}
19+
function draw() {
20+
background(0); // Set the background to black
21+
y = y - 1;
22+
if (y < 0) {
23+
y = height;
24+
}
25+
line(0, y, width, y);
26+
}

src/data/examples/en/00_Structure/05_Redraw.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
* @name Redraw
33
* @description The redraw() function makes draw() execute once. In this example,
44
* draw() is executed once every time the mouse is clicked.
5-
*/
5+
*/
66

7-
var y;
7+
let y;
88

9-
// The statements in the setup() function
9+
// The statements in the setup() function
1010
// execute once when the program begins
1111
function setup() {
1212
createCanvas(720, 400);
@@ -15,9 +15,9 @@ function setup() {
1515
y = height * 0.5;
1616
}
1717

18-
// The statements in draw() are executed until the
19-
// program is stopped. Each statement is executed in
20-
// sequence and after the last line is read, the first
18+
// The statements in draw() are executed until the
19+
// program is stopped. Each statement is executed in
20+
// sequence and after the last line is read, the first
2121
// line is executed again.
2222
function draw() {
2323
background(0);
@@ -29,5 +29,5 @@ function draw() {
2929
}
3030

3131
function mousePressed() {
32-
redraw();
33-
}
32+
redraw();
33+
}
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/*
22
*@name Functions
3-
*@description The drawTarget() function makes it easy to draw many distinct
3+
*@description The drawTarget() function makes it easy to draw many distinct
44
*targets. Each call to drawTarget() specifies the position, size, and number of
55
*rings for each target.
6-
*/
6+
*/
77

88
function setup() {
99
createCanvas(720, 400);
@@ -13,16 +13,16 @@ function setup() {
1313
}
1414

1515
function draw() {
16-
drawTarget(width*0.25, height*0.4, 200, 4);
17-
drawTarget(width*0.5, height*0.5, 300, 10);
18-
drawTarget(width*0.75, height*0.3, 120, 6);
16+
drawTarget(width * 0.25, height * 0.4, 200, 4);
17+
drawTarget(width * 0.5, height * 0.5, 300, 10);
18+
drawTarget(width * 0.75, height * 0.3, 120, 6);
1919
}
2020

2121
function drawTarget(xloc, yloc, size, num) {
22-
var grayvalues = 255/num;
23-
var steps = size/num;
24-
for (var i = 0; i < num; i++) {
25-
fill(i*grayvalues);
26-
ellipse(xloc, yloc, size - i*steps, size - i*steps);
22+
const grayvalues = 255 / num;
23+
const steps = size / num;
24+
for (let i = 0; i < num; i++) {
25+
fill(i * grayvalues);
26+
ellipse(xloc, yloc, size - i * steps, size - i * steps);
2727
}
2828
}

src/data/examples/en/00_Structure/07_Recursion.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*@description A demonstration of recursion, which means functions call themselves.
44
* Notice how the drawCircle() function calls itself at the end of its block.
55
* It continues to do this until the variable "level" is equal to 1.
6-
*/
6+
*/
77

88
function setup() {
99
createCanvas(720, 400);
@@ -12,16 +12,16 @@ function setup() {
1212
}
1313

1414
function draw() {
15-
drawCircle(width/2, 280, 6);
15+
drawCircle(width / 2, 280, 6);
1616
}
1717

18-
function drawCircle(x, radius, level) {
19-
var tt = 126 * level/4.0;
18+
function drawCircle(x, radius, level) {
19+
const tt = (126 * level) / 4.0;
2020
fill(tt);
21-
ellipse(x, height/2, radius*2, radius*2);
22-
if(level > 1) {
21+
ellipse(x, height / 2, radius * 2, radius * 2);
22+
if (level > 1) {
2323
level = level - 1;
24-
drawCircle(x - radius/2, radius/2, level);
25-
drawCircle(x + radius/2, radius/2, level);
24+
drawCircle(x - radius / 2, radius / 2, level);
25+
drawCircle(x + radius / 2, radius / 2, level);
2626
}
27-
}
27+
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
/*
22
* @name Create Graphics
3-
* @description Creates and returns a new p5.Renderer object. Use this
3+
* @description Creates and returns a new p5.Renderer object. Use this
44
* class if you need to draw into an off-screen graphics buffer. The two parameters
55
* define the width and height in pixels.
66
*/
77

8-
var pg;
8+
let pg;
99

10-
function setup(){
10+
function setup() {
1111
createCanvas(710, 400);
1212
pg = createGraphics(400, 250);
1313
}
1414

15-
function draw(){
15+
function draw() {
1616
fill(0, 12);
1717
rect(0, 0, width, height);
1818
fill(255);
@@ -22,8 +22,8 @@ function draw(){
2222
pg.background(51);
2323
pg.noFill();
2424
pg.stroke(255);
25-
pg.ellipse(mouseX-150, mouseY-75, 60, 60);
25+
pg.ellipse(mouseX - 150, mouseY - 75, 60, 60);
2626

2727
//Draw the offscreen buffer to the screen with image()
2828
image(pg, 150, 75);
29-
}
29+
}

src/data/examples/es/00_Structure/00_Coordinates.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
* El origen [0, 0] es la coordenada en la esquina superior izquierda de la ventana y la coordenada de la esquina inferior derecha es [ancho-1, altura-1].
66
*/
77
function setup() {
8-
98
// Definir lienzo de 720 pixeles de ancho y 400 pixeles de alto
109
createCanvas(720, 400);
1110
}
@@ -28,12 +27,11 @@ function draw() {
2827
// de line() especifican las coordenadas del primer extremo
2928
//y los siguientes dos parámetros del segundo extremo.
3029
stroke(0, 153, 255);
31-
line(0, height*0.33, width, height*0.33);
32-
30+
line(0, height * 0.33, width, height * 0.33);
3331

3432
// Por defecto, los dos primeros parámetros de rect() son
3533
// las coordenadas de la esquina superior izquierda y el
3634
// segundo par son el ancho y el alto.
3735
stroke(255, 153, 0);
38-
rect(width*0.25, height*0.1, width * 0.5, height * 0.8);
36+
rect(width * 0.25, height * 0.1, width * 0.5, height * 0.8);
3937
}

0 commit comments

Comments
 (0)