Skip to content

Commit a80ca74

Browse files
authored
Merge pull request #779 from processing/fix/examples
Migrate examples to 2.0
2 parents eff2412 + 5c048f1 commit a80ca74

File tree

11 files changed

+41
-59
lines changed

11 files changed

+41
-59
lines changed

src/content/examples/en/03_Imported_Media/00_Words/code.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,18 @@
22
let font;
33
let fontsize = 40;
44

5-
function preload() {
6-
// Preload the font's file in the canvas's assets directory.
7-
// loadFont() accepts .ttf or .otf files.
8-
font = loadFont('/assets/SourceSansPro-Regular.otf');
9-
}
10-
11-
function setup() {
5+
async function setup() {
126
describe(
137
'Three columns of the words “ichi,” “ni,” “san,” and “shi” on a white background. The first column is right aligned, the middle column is center aligned, and the left column is left aligned.'
148
);
159

1610
createCanvas(710, 400);
1711
background(250);
1812

13+
// Preload the font's file in the canvas's assets directory.
14+
// loadFont() accepts .ttf or .otf files.
15+
font = await loadFont('/assets/SourceSansPro-Regular.otf');
16+
1917
// Set the text styling to the predefined font and font size.
2018
textFont(font);
2119
textSize(fontsize);

src/content/examples/en/03_Imported_Media/01_Copy_Image_Data/code.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
// Define the global variables: bottomImg and topImg.
22
let bottomImg, topImg;
33

4-
function preload() {
5-
// Preload the images from the canvas's assets directory.
6-
// The bottomImg is the photograph with color,
7-
// and the topImg is the black-and-white photograph.
8-
bottomImg = loadImage('/assets/parrot-color.png');
9-
topImg = loadImage('/assets/parrot-bw.png');
10-
}
11-
function setup() {
4+
async function setup() {
125
describe(
136
'Black-and-white photograph of a parrot. The cursor, when dragged across the canvas, adds color to the photograph.'
147
);
158

169
createCanvas(720, 400);
1710

11+
// Preload the images from the canvas's assets directory.
12+
// The bottomImg is the photograph with color,
13+
// and the topImg is the black-and-white photograph.
14+
bottomImg = await loadImage('/assets/parrot-color.png');
15+
topImg = await loadImage('/assets/parrot-bw.png');
16+
1817
// Hide the cursor and replace it with a picture of
1918
// a paintbrush.
2019
noCursor();

src/content/examples/en/03_Imported_Media/02_Alpha_Mask/code.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1-
// Preload the image assets from the canvas
2-
// assets directory.
3-
function preload() {
4-
// Photo by Sergey Shmidt, https://unsplash.com/photos/koy6FlCCy5s
5-
img = loadImage('/assets/image.jpg');
6-
7-
// Photo by Mockup Graphics, https://unsplash.com/photos/_mUVHhvBYZ0
8-
imgMask = loadImage('/assets/mask.png');
9-
}
10-
11-
function setup() {
1+
async function setup() {
122
describe(
133
'Two photos, the one on the left labeled with "Masked Image" and the one on the right labeled with "Mask."'
144
);
155
createCanvas(710, 400);
166

7+
// Photo by Sergey Shmidt, https://unsplash.com/photos/koy6FlCCy5s
8+
img = await loadImage('/assets/image.jpg');
9+
10+
// Photo by Mockup Graphics, https://unsplash.com/photos/_mUVHhvBYZ0
11+
imgMask = await loadImage('/assets/mask.png');
12+
1713
// Use the mask() method to apply imgMask photo as a
1814
// mask for img.
1915
img.mask(imgMask);

src/content/examples/en/03_Imported_Media/03_Image_Transparency/code.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,15 @@ let img;
55
let offset = 0;
66
let easing = 0.05;
77

8-
function preload() {
9-
// Load the bottom image from the canvas's assets directory.
10-
img = loadImage('/assets/moonwalk.jpg');
11-
}
12-
13-
function setup() {
8+
async function setup() {
149
describe(
1510
"An astronaut on a planet as the background with a slightly transparent version of this image on top that moves with the horizontal direction of the user's mouse."
1611
);
1712

1813
createCanvas(720, 400);
14+
15+
// Load the bottom image from the canvas's assets directory.
16+
img = await loadImage('/assets/moonwalk.jpg');
1917
}
2018

2119
function draw() {

src/content/examples/en/10_Games/02_Snake/code.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,8 @@ function updateFruitCoordinates() {
251251
// When an arrow key is pressed, switch the snake's direction of movement,
252252
// but if the snake is already moving in the opposite direction,
253253
// do nothing.
254-
function keyPressed() {
255-
switch (keyCode) {
254+
function keyPressed(event) {
255+
switch (event.code) {
256256
case LEFT_ARROW:
257257
if (direction !== 'right') {
258258
direction = 'left';

src/content/examples/en/11_3D/00_Geometries/code.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
// Variable to store NASA model
22
let astronaut;
33

4-
function preload() {
5-
astronaut = loadModel('/assets/astronaut.obj');
6-
}
7-
8-
function setup() {
4+
async function setup() {
95
createCanvas(710, 400, WEBGL);
6+
astronaut = await loadModel('/assets/astronaut.obj');
107

118
angleMode(DEGREES);
129

src/content/examples/en/11_3D/02_Materials/code.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ let emissivePicker;
1414
// Selected colors
1515
let fillSelection, strokeSelection, ambientSelection, specularSelection;
1616

17-
// Load astronaut model and venus image texture
18-
function preload() {
19-
astronaut = loadModel('/assets/astronaut.obj');
20-
venus = loadImage('/assets/venus.jpg');
21-
}
2217

23-
function setup() {
18+
async function setup() {
2419
createCanvas(400, 400, WEBGL);
20+
21+
// Load astronaut model and venus image texture
22+
astronaut = await loadModel('/assets/astronaut.obj');
23+
venus = await loadImage('/assets/venus.jpg');
24+
2525
angleMode(DEGREES);
2626
createSelectionArea();
2727

src/content/examples/en/14_Loading_And_Saving_Data/02_Table/code.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@ let mousePressY = 0;
1111
// Remember whether bubble is currently being created
1212
let creatingBubble = false;
1313

14-
// Put any asynchronous data loading in preload to complete before "setup" is run
15-
function preload() {
16-
table = loadTable('/assets/bubbles.csv', 'header', loadData);
17-
}
18-
1914
// Convert saved Bubble data into Bubble Objects
2015
function loadData(table) {
2116
bubbles = [];
@@ -32,9 +27,11 @@ function loadData(table) {
3227
}
3328
}
3429

35-
function setup() {
30+
async function setup() {
3631
let p5Canvas = createCanvas(640, 360);
3732

33+
table = await loadTable('/assets/bubbles.csv', ',', 'header', loadData);
34+
3835
// When canvas is clicked, call saveMousePress()
3936
p5Canvas.mousePressed(saveMousePress);
4037

src/content/examples/en/15_Math_And_Physics/01_Soft_Body/code.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,13 @@ function drawShape() {
6666

6767
// Draw the polygon
6868

69-
curveTightness(organicConstant);
69+
splineProperty('tightness', organicConstant);
7070
let shapeColor = lerpColor(color('red'), color('yellow'), organicConstant);
7171
fill(shapeColor);
7272

7373
beginShape();
7474
for (let i = 0; i < nodes; i++) {
75-
curveVertex(nodeX[i], nodeY[i]);
75+
splineVertex(nodeX[i], nodeY[i]);
7676
}
7777
endShape(CLOSE);
7878
}

src/content/examples/en/15_Math_And_Physics/03_Smoke_Particle_System/code.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@
22
let particleTexture;
33
let particleSystem;
44

5-
function preload() {
6-
particleTexture = loadImage('/assets/particle_texture.png');
7-
}
8-
9-
function setup() {
5+
async function setup() {
106
// Set the canvas size
117
createCanvas(720, 400);
8+
particleTexture = await loadImage('/assets/particle_texture.png');
129
colorMode(HSB);
1310

1411
// Initialize the particle system

src/content/examples/en/15_Math_And_Physics/05_Mandelbrot/code.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ function setup() {
7979

8080
// Copy the RGBA values from the color to the pixel
8181
for (let i = 0; i < 4; i += 1) {
82-
pixels[index + i] = pixelColor.levels[i];
82+
pixels[index + i] = pixelColor.array()[i] * 255;
8383
}
8484

8585
x += dx;

0 commit comments

Comments
 (0)