Skip to content

Commit edc6180

Browse files
authored
Merge pull request #358 from AnimeshSinha1309/es6/dom
Updating DOM Examples to ES6 standards
2 parents 0dc1ce3 + 3164ce2 commit edc6180

24 files changed

+187
-192
lines changed

src/data/examples/en/16_Dom/03_Input_Button.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
/*
22
* @name Input and Button
3-
* @description You will need to include the
3+
* @description You will need to include the
44
* <a href="http://p5js.org/reference/#/libraries/p5.dom">p5.dom library</a>
55
* for this example to work in your own project.<br><br>
66
* Input text and click the button to see it affect the the canvas.
77
*/
8-
var input, button, greeting;
8+
let input, button, greeting;
99

1010
function setup() {
11-
1211
// create canvas
1312
createCanvas(710, 400);
1413

@@ -27,15 +26,15 @@ function setup() {
2726
}
2827

2928
function greet() {
30-
var name = input.value();
31-
greeting.html('hello '+name+'!');
29+
const name = input.value();
30+
greeting.html('hello ' + name + '!');
3231
input.value('');
3332

34-
for (var i=0; i<200; i++) {
33+
for (let i = 0; i < 200; i++) {
3534
push();
3635
fill(random(255), 255, 255);
3736
translate(random(width), random(height));
38-
rotate(random(2*PI));
37+
rotate(random(2 * PI));
3938
text(name, 0, 0);
4039
pop();
4140
}

src/data/examples/en/16_Dom/04_Slider.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
22
* @name Slider
3-
* @description You will need to include the
3+
* @description You will need to include the
44
* <a href="http://p5js.org/reference/#/libraries/p5.dom">p5.dom library</a>
55
* for this example to work in your own project.<br><br>
66
* Move the sliders to control the R, G, B values of the background.
77
*/
8-
var rSlider, gSlider, bSlider;
8+
let rSlider, gSlider, bSlider;
99

1010
function setup() {
1111
// create canvas
@@ -23,11 +23,11 @@ function setup() {
2323
}
2424

2525
function draw() {
26-
var r = rSlider.value();
27-
var g = gSlider.value();
28-
var b = bSlider.value();
26+
const r = rSlider.value();
27+
const g = gSlider.value();
28+
const b = bSlider.value();
2929
background(r, g, b);
30-
text("red", rSlider.x * 2 + rSlider.width, 35);
31-
text("green", gSlider.x * 2 + gSlider.width, 65);
32-
text("blue", bSlider.x * 2 + bSlider.width, 95);
30+
text('red', rSlider.x * 2 + rSlider.width, 35);
31+
text('green', gSlider.x * 2 + gSlider.width, 65);
32+
text('blue', bSlider.x * 2 + bSlider.width, 95);
3333
}

src/data/examples/en/16_Dom/07_Modify_DOM.js

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,52 @@
22
* @name Modifying the DOM
33
* @frame 710,300
44
* @description <p>Create DOM elements and modify their properties every time
5-
* draw() is called. You will need to include the
5+
* draw() is called. You will need to include the
66
* <a href="http://p5js.org/reference/#/libraries/p5.dom">p5.dom library</a>
77
* for this example to work in your own project.</p>
88
*/
9-
var dancingWords = [];
9+
let dancingWords = [];
1010

11-
function DanceSpan(element, x, y) {
12-
element.position(x, y);
13-
14-
this.brownian = function() {
11+
class DanceSpan {
12+
constructor(element, x, y) {
13+
element.position(x, y);
14+
}
15+
16+
brownian() {
1517
x += random(-6, 6);
1618
y += random(-6, 6);
1719
element.position(x, y);
18-
};
20+
}
1921
}
2022

2123
function setup() {
2224
// This paragraph is created aside of the main block of code.
23-
// It's to differentiate the creation of an element from its
24-
// selection. Selected elements don't need to be created by
25+
// It's to differentiate the creation of an element from its
26+
// selection. Selected elements don't need to be created by
2527
// p5js, they can be just plain HTML.
26-
createP('I learn in this Letter, that Don Peter of Aragon, '
27-
+ ' comes this night to Messina').addClass('text');
28+
createP(
29+
'I learn in this Letter, that Don Peter of Aragon, ' +
30+
' comes this night to Messina'
31+
).addClass('text');
2832

29-
// This line grabs the paragraph just created, but it would
33+
// This line grabs the paragraph just created, but it would
3034
// also grab any other elements with class 'text' in the HTML
3135
// page.
32-
var texts = selectAll('.text');
36+
const texts = selectAll('.text');
3337

34-
for (var i=0; i<texts.length; i++) {
35-
var paragraph = texts[i].html();
36-
var words = paragraph.split(' ');
37-
for (var j=0; j<words.length; j++) {
38-
var spannedWord = createSpan(words[j]);
39-
var dw = new DanceSpan(spannedWord, random(600), random(200));
38+
for (let i = 0; i < texts.length; i++) {
39+
const paragraph = texts[i].html();
40+
const words = paragraph.split(' ');
41+
for (let j = 0; j < words.length; j++) {
42+
const spannedWord = createSpan(words[j]);
43+
const dw = new DanceSpan(spannedWord, random(600), random(200));
4044
dancingWords.push(dw);
4145
}
4246
}
4347
}
4448

4549
function draw() {
46-
for (var i=0; i<dancingWords.length; i++) {
50+
for (let i = 0; i < dancingWords.length; i++) {
4751
dancingWords[i].brownian();
4852
}
4953
}
50-

src/data/examples/en/16_Dom/08_Video.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,18 @@
22
* @name Video
33
* @frame 710,250
44
* @description <p>Load a video with multiple formats and toggle between playing
5-
* and paused with a button press.
5+
* and paused with a button press.
66
* <p><em><span class="small"> To run this example locally, you will need at least
77
* one video file, and the
88
* <a href="http://p5js.org/reference/#/libraries/p5.dom">p5.dom library</a>.</span></em></p>
99
*/
10-
var playing = false;
11-
var fingers;
12-
var button;
13-
10+
let playing = false;
11+
let fingers;
12+
let button;
1413

1514
function setup() {
1615
// specify multiple formats for different browsers
17-
fingers = createVideo(['assets/fingers.mov',
18-
'assets/fingers.webm']);
16+
fingers = createVideo(['assets/fingers.mov', 'assets/fingers.webm']);
1917
button = createButton('play');
2018
button.mousePressed(toggleVid); // attach button listener
2119
}

src/data/examples/en/16_Dom/09_Video_Canvas.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,24 @@
55
* <a href="http://p5js.org/reference/#/libraries/p5.dom">p5.dom library</a>
66
* at least one video file, and a running <a href="https://github.com/processing/p5.js/wiki/Local-server">local server</a>.</span></em></p>
77
*/
8-
var fingers;
8+
let fingers;
99

1010
function setup() {
1111
createCanvas(710, 400);
1212
// specify multiple formats for different browsers
13-
fingers = createVideo(['assets/fingers.mov',
14-
'assets/fingers.webm']);
13+
fingers = createVideo(['assets/fingers.mov', 'assets/fingers.webm']);
1514
fingers.hide(); // by default video shows up in separate dom
16-
// element. hide it and draw it to the canvas
17-
// instead
15+
// element. hide it and draw it to the canvas
16+
// instead
1817
}
1918

2019
function draw() {
2120
background(150);
22-
image(fingers,10,10); // draw the video frame to canvas
21+
image(fingers, 10, 10); // draw the video frame to canvas
2322
filter('GRAY');
24-
image(fingers,150,150); // draw a second copy to canvas
23+
image(fingers, 150, 150); // draw a second copy to canvas
2524
}
2625

2726
function mousePressed() {
28-
fingers.loop(); // set the video to loop and start playing
29-
}
27+
fingers.loop(); // set the video to loop and start playing
28+
}

src/data/examples/en/16_Dom/10_Video_Pixels.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66
* <a href="http://p5js.org/reference/#/libraries/p5.dom">p5.dom library</a>
77
* at least one video file, and a running <a href="https://github.com/processing/p5.js/wiki/Local-server">local server</a>.</span></em></p>
88
*/
9-
var fingers;
9+
let fingers;
1010

1111
function setup() {
1212
createCanvas(320, 240);
1313
// specify multiple formats for different browsers
14-
fingers = createVideo(['assets/fingers.mov',
15-
'assets/fingers.webm']);
14+
fingers = createVideo(['assets/fingers.mov', 'assets/fingers.webm']);
1615
fingers.loop();
1716
fingers.hide();
1817
noStroke();
@@ -22,12 +21,12 @@ function setup() {
2221
function draw() {
2322
background(255);
2423
fingers.loadPixels();
25-
var stepSize = round(constrain(mouseX / 8, 6, 32));
26-
for (var y=0; y<height; y+=stepSize) {
27-
for (var x=0; x<width; x+=stepSize) {
28-
var i = y * width + x;
29-
var darkness = (255 - fingers.pixels[i*4]) / 255;
30-
var radius = stepSize * darkness;
24+
const stepSize = round(constrain(mouseX / 8, 6, 32));
25+
for (let y = 0; y < height; y += stepSize) {
26+
for (let x = 0; x < width; x += stepSize) {
27+
const i = y * width + x;
28+
const darkness = (255 - fingers.pixels[i * 4]) / 255;
29+
const radius = stepSize * darkness;
3130
ellipse(x, y, radius, radius);
3231
}
3332
}

src/data/examples/en/16_Dom/11_Capture.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* default the capture feed shows up, too. You can hide the
1010
* feed by uncommenting the capture.hide() line.
1111
*/
12-
var capture;
12+
let capture;
1313

1414
function setup() {
1515
createCanvas(390, 240);

src/data/examples/en/16_Dom/12_Drop.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
/*
22
* @name Drop
3-
* @description You will need to include the
3+
* @description You will need to include the
44
* <a href="http://p5js.org/reference/#/libraries/p5.dom">p5.dom library</a>
55
* for this example to work in your own project.<br><br>
66
* Drag an image file onto the canvas to see it displayed.
77
*/
88

99
function setup() {
1010
// create canvas
11-
var c = createCanvas(710, 400);
11+
const c = createCanvas(710, 400);
1212
background(100);
1313
// Add an event for when a file is dropped onto the canvas
1414
c.drop(gotFile);
@@ -19,15 +19,15 @@ function draw() {
1919
noStroke();
2020
textSize(24);
2121
textAlign(CENTER);
22-
text('Drag an image file onto the canvas.', width/2, height/2);
22+
text('Drag an image file onto the canvas.', width / 2, height / 2);
2323
noLoop();
2424
}
2525

2626
function gotFile(file) {
2727
// If it's an image file
2828
if (file.type === 'image') {
2929
// Create an image DOM element but don't show it
30-
var img = createImg(file.data).hide();
30+
const img = createImg(file.data).hide();
3131
// Draw the image onto the canvas
3232
image(img, 0, 0, width, height);
3333
} else {

src/data/examples/es/16_Dom/03_Input_Button.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
* para que este ejemplo funcione en tu proyecto.<br><br>
66
* Ingresa texto y haz click en el botón para ver cómo afecta al lienzo
77
*/
8-
var input, button, greeting;
8+
let input, button, greeting;
99

1010
function setup() {
11-
1211
// crear un lienzo
1312
createCanvas(710, 400);
1413

@@ -22,20 +21,20 @@ function setup() {
2221
greeting = createElement('h2', 'what is your name?');
2322
greeting.position(20, 5);
2423

25-
textAlign(CENTER)
24+
textAlign(CENTER);
2625
textSize(50);
2726
}
2827

2928
function greet() {
30-
var name = input.value();
31-
greeting.html('hello '+name+'!');
29+
const name = input.value();
30+
greeting.html('hello ' + name + '!');
3231
input.value('');
3332

34-
for (var i=0; i<200; i++) {
33+
for (let i = 0; i < 200; i++) {
3534
push();
3635
fill(random(255), 255, 255);
3736
translate(random(width), random(height));
38-
rotate(random(2*PI));
37+
rotate(random(2 * PI));
3938
text(name, 0, 0);
4039
pop();
4140
}

src/data/examples/es/16_Dom/04_Slider.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
* para que este ejemplo funcione en tu propio proyecto.<br><br>
66
* Mueve las barras deslizantes para controlar los valores de color RGB del fondo.
77
*/
8-
var rSlider, gSlider, bSlider;
8+
let rSlider, gSlider, bSlider;
99

1010
function setup() {
1111
// crear lienzo
1212
createCanvas(710, 400);
13-
textSize(15)
13+
textSize(15);
1414
noStroke();
1515

1616
// crear barras deslizantes
@@ -23,11 +23,11 @@ function setup() {
2323
}
2424

2525
function draw() {
26-
var r = rSlider.value();
27-
var g = gSlider.value();
28-
var b = bSlider.value();
26+
const r = rSlider.value();
27+
const g = gSlider.value();
28+
const b = bSlider.value();
2929
background(r, g, b);
30-
text("red", 165, 35);
31-
text("green", 165, 65);
32-
text("blue", 165, 95);
30+
text('red', 165, 35);
31+
text('green', 165, 65);
32+
text('blue', 165, 95);
3333
}

0 commit comments

Comments
 (0)