Skip to content

Commit db590bb

Browse files
authored
Merge pull request #15 from processing/ksen0-patch-2
Added bezierVertex usage example
2 parents 96cf2c2 + 39cf42b commit db590bb

File tree

1 file changed

+98
-3
lines changed

1 file changed

+98
-3
lines changed

README.md

Lines changed: 98 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,12 @@ If you’re interested in the history of async [read on here](https://dev.to/lim
4848

4949
To play around, check out [the example from the p5.js 1.x `preload()` reference](https://p5js.org/reference/p5/preload/), but get a very big file instead of bricks.jpg.
5050

51-
This is the p5.js 1.x code, and it will result in a blank “Loading…” screen and then show the image:
51+
52+
53+
<table>
54+
<tr><th>p5.js 1.x</th><th>p5.js 2.x</th></tr>
55+
<tr><td>Blank “Loading…” screen, then image shown</td><td>Red background while image loads</td></tr>
56+
<tr><td>
5257

5358
```js
5459
let img;
@@ -71,7 +76,7 @@ function setup() {
7176
}
7277
```
7378

74-
Using p5.js 2.0, and it will result in the red background being shown before the image loads, so people viewing your sketch don’t have to look at a blank screen:
79+
</td><td>
7580

7681
```js
7782
let img;
@@ -91,6 +96,9 @@ async function setup() {
9196
}
9297
```
9398

99+
</td></tr>
100+
</table>
101+
94102
If it takes a while to load the image, the sketch will be "paused" on the line `img = await loadImage('/assets/bricks.jpg');` - once the image is loaded, it will resume.
95103

96104

@@ -145,7 +153,94 @@ And that's it! You can check this example of making an add-on library backwards-
145153

146154
## …making shapes
147155

148-
Short guide coming soon! For now, you can [find out more here](https://github.com/processing/p5.js/issues/6766)
156+
If you use `vertex` and `bezierVertex` is the p5.js 1.x code, here's how you can approach updating your code.
157+
158+
The below code is based on the [custom shapes](https://p5js.org/tutorials/custom-shapes-and-smooth-curves/) tutorial:
159+
160+
161+
<table>
162+
<tr><th>p5.js 1.x</th><th>p5.js 2.x</th></tr>
163+
<tr><td>Blank “Loading…” screen, then image shown</td><td>Red background while image loads</td></tr>
164+
<tr><td>
165+
166+
167+
```js
168+
function setup() {
169+
createCanvas(windowWidth, windowHeight);
170+
background(100);
171+
}
172+
function draw() {
173+
translate(width/2, height/2);
174+
// Draw the curved star shape.
175+
beginShape();
176+
177+
// Original anchor at top.
178+
vertex(0, -100);
179+
180+
// Top-right curve.
181+
bezierVertex(0, -50, 50, 0, 100, 0);
182+
183+
// Bottom-right curve.
184+
bezierVertex(50, 0, 0, 50, 0, 100);
185+
186+
// Bottom-left curve.
187+
bezierVertex0, 50, -50, 0, -100, 0);
188+
189+
// Top-left curve.
190+
bezierVertex(-50, 0, 0,-50, 0,-100);
191+
endShape();
192+
}
193+
```
194+
195+
</td><td>
196+
197+
198+
```js
199+
function setup() {
200+
createCanvas(windowWidth, windowHeight);
201+
background(100);
202+
}
203+
function draw() {
204+
translate(width/2, height/2);
205+
206+
// Draw the curved star shape.
207+
beginShape();
208+
209+
// Because the order is three, the curves should be
210+
// defined in sets of three after the original anchor
211+
bezierOrder(3);
212+
213+
// Original anchor at top.
214+
bezierVertex(0, -100);
215+
216+
// Top-right curve.
217+
bezierVertex(0, -50);
218+
bezierVertex(50, 0);
219+
bezierVertex(100, 0);
220+
221+
// Bottom-right curve.
222+
bezierVertex(50, 0);
223+
bezierVertex(0, 50);
224+
bezierVertex(0, 100);
225+
226+
// Bottom-left curve.
227+
bezierVertex0, 50);
228+
bezierVertex(-50, 0);
229+
bezierVertex(-100, 0);
230+
231+
// Top-left curve.
232+
bezierVertex(-50, 0);
233+
bezierVertex(0, -50);
234+
bezierVertex(0,-100);
235+
236+
endShape();
237+
}
238+
```
239+
240+
</td></tr>
241+
</table>
242+
243+
The [custom shapes tutorial](https://p5js.org/tutorials/custom-shapes-and-smooth-curves/) has a bit more detail on this, but Bézier curves need multiple points. In p5.js 1.x, they use three control points. In p5.js 2.0, that number is set by `bezierOrder`. Then, in p5.js 1.x each `bezierVertex(...)` was actually a set of three points describing a smooth curve. In p5.js 2.0, each `bezierVertext(x, y)` is just one point; you need the first point to anchor, and each curve after that needs 3 points.
149244

150245
## …using non-JavaScript data structures and functions
151246

0 commit comments

Comments
 (0)