Skip to content

Commit b6376df

Browse files
committed
Handle constants differently
1 parent acaa4f8 commit b6376df

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2178
-34
lines changed

public/reference/data.json

Lines changed: 184 additions & 1 deletion
Large diffs are not rendered by default.

public/search-indices/en.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

public/search-indices/es.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

public/search-indices/hi.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

public/search-indices/ko.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

public/search-indices/zh-Hans.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
title: height
3+
module: DOM
4+
submodule: DOM
5+
file: src/dom/p5.Element.js
6+
description: A <code>Number</code> property that stores the element's height.
7+
line: 2493
8+
isConstructor: false
9+
itemtype: property
10+
class: p5.Element
11+
type: Number
12+
---
13+
14+
15+
# height
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
title: width
3+
module: DOM
4+
submodule: DOM
5+
file: src/dom/p5.Element.js
6+
description: A <code>Number</code> property that stores the element's width.
7+
line: 2493
8+
isConstructor: false
9+
itemtype: property
10+
class: p5.Element
11+
type: Number
12+
---
13+
14+
15+
# width
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
title: src
3+
module: DOM
4+
submodule: DOM
5+
file: src/dom/p5.MediaElement.js
6+
description: Path to the media element's source as a string.
7+
line: 1807
8+
isConstructor: false
9+
itemtype: property
10+
example:
11+
- |-
12+
<div>
13+
<code>
14+
let beat;
15+
16+
function setup() {
17+
createCanvas(100, 100);
18+
19+
// Create a p5.MediaElement using createAudio().
20+
beat = createAudio('/assets/beat.mp3');
21+
22+
describe('The text "https://p5js.org/reference//assets/beat.mp3" written in black on a gray background.');
23+
}
24+
25+
function draw() {
26+
background(200);
27+
28+
textWrap(CHAR);
29+
text(beat.src, 10, 10, 80, 80);
30+
}
31+
</code>
32+
</div>
33+
class: p5.MediaElement
34+
---
35+
36+
37+
# src
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
---
2+
title: async_await
3+
module: Foundation
4+
submodule: Foundation
5+
file: src/core/reference.js
6+
description: >-
7+
<p>Asynchronous Asset Loading with Async/Await.</p>
8+
9+
<p>The keywords <code>async</code> and <code>await</code> let you write
10+
asynchronous code in a more
11+
12+
straightforward, linear style. Instead of nesting callbacks or juggling
13+
14+
multiple promise chains, you can pause execution at <code>await</code> while
15+
waiting
16+
17+
for a promise to resolve. This makes your code flow more naturally, as if
18+
19+
it were synchronous.</p>
20+
21+
<p>When you mark a function with the <code>async</code> keyword—like
22+
<code>async function setup() {...}</code>—it
23+
24+
signals that the function contains asynchronous operations and will return a
25+
26+
promise. Any time you use the <code>await</code> keyword inside this function,
27+
JavaScript
28+
29+
will pause the function’s execution until the awaited promise settles.</p>
30+
31+
<p>In p5.js, you can use <code>async/await</code> to handle media loading
32+
functions such as
33+
34+
<code>loadImage()</code>, <code>loadJSON()</code>, <code>loadSound()</code>,
35+
and so on. This allows you to:</p>
36+
37+
<ul><li>load files in a more readable, top-to-bottom manner</li><li>decide
38+
when the assets are fully available before proceeding</li></ul><p>Nested
39+
callbacks require managing additional information and behavior.
40+
41+
Lazy loading of assets with <code>async/await</code> can simplify control
42+
flow,
43+
44+
but it also requires you to design your sketch around waiting for
45+
46+
each operation to complete.</p>
47+
48+
<p>Callbacks are still fully supported, so code that passes success / error
49+
50+
functions to loaders like <code>loadImage()</code> or <code>loadJSON()</code>
51+
will behave exactly
52+
53+
as it always has. This compatibility means sketches written with the older
54+
55+
pattern don’t need any changes, and you can freely mix callbacks and
56+
57+
<code>async/await</code> in the same project if that suits your workflow.</p>
58+
59+
<p>In the example below, <code>setup()</code> is declared as an async
60+
function. We <code>await</code>
61+
62+
the completion of both <code>loadImage()</code> and <code>loadJSON()</code>
63+
before calling
64+
65+
<code>createCanvas()</code>. Only then does the sketch proceed, guaranteeing
66+
the assets
67+
68+
are available for immediate use.</p>
69+
70+
<pre><code class="language-js">let img, data;
71+
72+
73+
async function setup() {
74+
// Wait until the image and JSON data have fully loaded.
75+
img = await loadImage("./my-image.png");
76+
data = await loadJSON("./my-data.json");
77+
78+
// Once the assets are loaded, create the canvas.
79+
createCanvas(400, 400);
80+
}</code></pre>
81+
line: 1
82+
isConstructor: false
83+
itemtype: property
84+
example:
85+
- |-
86+
<div>
87+
<code>
88+
// Click and drag the mouse to view the scene from different angles.
89+
90+
let shape;
91+
92+
// Load the file and create a p5.Geometry object.
93+
async function setup() {
94+
shape = await loadModel('//assets/teapot.obj');
95+
96+
createCanvas(100, 100, WEBGL);
97+
98+
describe('A white teapot drawn against a gray background.');
99+
}
100+
101+
function draw() {
102+
background(200);
103+
104+
// Enable orbiting with the mouse.
105+
orbitControl();
106+
107+
// Draw the shape.
108+
model(shape);
109+
}
110+
</code>
111+
</div>
112+
113+
<div>
114+
<code>
115+
let font;
116+
117+
async function setup() {
118+
// Load a font for WebGL mode.
119+
font = await loadFont('/assets/inconsolata.otf');
120+
121+
createCanvas(100, 100, WEBGL);
122+
123+
describe(
124+
"A gray square. The mouse's x- and y-coordinates are displayed as the user moves the mouse."
125+
);
126+
}
127+
128+
function draw() {
129+
background(200);
130+
131+
// Style the text.
132+
textAlign(CENTER);
133+
textSize(16);
134+
textFont(font);
135+
fill(0);
136+
137+
// Display the mouse's coordinates.
138+
text(`x: ${mouseX} y: ${mouseY}`, 0, 0);
139+
}
140+
</code>
141+
</div>
142+
class: p5
143+
---
144+
145+
146+
# async\_await

0 commit comments

Comments
 (0)