Skip to content

Commit 156120e

Browse files
updated tutorial against 2.0 branch
1 parent ff8d650 commit 156120e

File tree

1 file changed

+232
-61
lines changed

1 file changed

+232
-61
lines changed

src/content/tutorials/en/custom-shapes-and-smooth-curves.mdx.todo renamed to src/content/tutorials/en/custom-shapes-and-smooth-curves.mdx

Lines changed: 232 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -242,13 +242,7 @@ To create a curved line, we’ll need to use the [`bezierVertex()`](/reference/p
242242
- Just like we did for the pointed sparkles, we’ll begin our shape with `beginShape()`, and end it with `endShape()`.
243243
- Any shape with `bezierVertex()` functions must still begin with a `vertex()` function. We’ll call `vertex(x, y)` first. This acts as the first point in our shape.
244244
- `x` and `y` represent the x-y coordinates for the first vertex in our star.
245-
- Next, we’ll call the `bezierVertex()` function. Since we’re working on a 2D sketch, the function takes six parameters, ordered as follows: 
246-
- the x-coordinate for the first control point
247-
- the y-coordinate for the first control point
248-
- the x-coordinate for the second control point
249-
- the y-coordinate for the second control point
250-
- the x-coordinate for the anchor point
251-
- the y-coordinate for the anchor point
245+
- Next, we’ll call the `bezierVertex()` function. 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.
252246

253247
Now that you have some context, let’s begin our shape!
254248

@@ -263,10 +257,9 @@ beginShape();
263257
vertex(0, -100);     // anchor point 1
264258

265259
// Top-right curve.
266-
bezierVertex(0, -50, // control point 1 x-y coord
267-
             50, 0,  // control point 2 x-y coord
268-
             100, 0  // anchor point 2
269-
);
260+
bezierVertex(0, -50);
261+
bezierVertex(50, 0);
262+
bezierVertex(100, 0);
270263
endShape();
271264
```
272265

@@ -292,19 +285,28 @@ Let’s complete our shape:
292285
beginShape();
293286

294287
// Original anchor at top.
295-
vertex(0, -100);
288+
bezierVertex(0, -100);
296289

297290
// Top-right curve.
298-
bezierVertex(0, -50, 50, 0, 100, 0);
291+
bezierVertex(0, -50);
292+
bezierVertex(50, 0);
293+
bezierVertex(100, 0);
299294

300295
// Bottom-right curve.
301-
bezierVertex(50, 0, 0, 50, 0, 100);
296+
bezierVertex(50, 0);
297+
bezierVertex(0, 50);
298+
bezierVertex(0, 100);
302299

303300
// Bottom-left curve.
304-
bezierVertex(  0, 50, -50, 0, -100, 0);
301+
bezierVertex(0, 50);
302+
bezierVertex(-50, 0);
303+
bezierVertex(-100, 0);
305304

306305
// Top-left curve.
307-
bezierVertex(-50, 0, 0,-50, 0,-100);
306+
bezierVertex(-50, 0);
307+
bezierVertex(0, -50);
308+
bezierVertex(0,-100);
309+
308310
endShape();
309311
```
310312

@@ -321,38 +323,47 @@ Your `curvedSparkle()` function looks like this:
321323

322324
```js
323325
function curvedSparkle() {
324-
  push();
326+
  push();
327+
328+
  // Translate to the mouse's position.
329+
  translate(mouseX, mouseY);
325330

326-
  // Translate to the mouse's position.
327-
  translate(mouseX, mouseY);
331+
  // Scale the coordinate system.
332+
  let starScale = random(0.1, 0.5);
333+
  scale(starScale);
328334

329-
  // Scale the coordinate system.
330-
  let starScale = random(0.1, 0.5);
331-
  scale(starScale);
335+
  // Set fill color.
336+
  fill(0, 0, 100);
332337

333-
  // Set fill color.
334-
  fill(0, 0, 100);
338+
  // Draw the curved star shape.
339+
  beginShape();
335340

336-
  // Draw the curved star shape.
337-
  beginShape();
341+
// Original anchor at top.
342+
bezierVertex(0, -100);
338343

339-
  // Original anchor at top.
340-
  vertex(0, -100);
344+
// Top-right curve.
345+
bezierVertex(0, -50);
346+
bezierVertex(50, 0);
347+
bezierVertex(100, 0);
341348

342-
  // Top-right curve.
343-
  bezierVertex(0, -50, 50, 0, 100, 0);
349+
// Bottom-right curve.
350+
bezierVertex(50, 0);
351+
bezierVertex(0, 50);
352+
bezierVertex(0, 100);
344353

345-
  // Bottom-right curve.
346-
  bezierVertex(50, 0, 0, 50, 0, 100);
354+
// Bottom-left curve.
355+
bezierVertex(0, 50);
356+
bezierVertex(-50, 0);
357+
bezierVertex(-100, 0);
347358

348-
  // Bottom-left curve.
349-
  bezierVertex(  0, 50, -50, 0, -100, 0);
359+
// Top-left curve.
360+
bezierVertex(-50, 0);
361+
bezierVertex(0, -50);
362+
bezierVertex(0,-100);
350363

351-
  // Top-left curve.
352-
  bezierVertex(-50, 0, 0,-50, 0,-100);
364+
endShape();
365+
  pop();
353366

354-
  endShape();
355-
  pop();
356367
}
357368
```
358369

@@ -464,37 +475,47 @@ function sparkle() {
464475
}
465476

466477
function curvedSparkle() {
467-
  push();
478+
  push();
468479

469-
  // Translate to the mouse's position.
470-
  translate(mouseX, mouseY);
480+
  // Translate to the mouse's position.
481+
  translate(mouseX, mouseY);
471482

472-
  // Scale the coordinate system.
473-
  let starScale = random(0.1, 0.5);
474-
  scale(starScale);
483+
  // Scale the coordinate system.
484+
  let starScale = random(0.1, 0.5);
485+
  scale(starScale);
475486

476-
  // Set fill color.
477-
  fill(0, 0, 100);
487+
  // Set fill color.
488+
  fill(0, 0, 100);
478489

479-
  // Draw the curved star shape.
480-
  beginShape();
490+
  // Draw the curved star shape.
491+
  beginShape();
481492

482-
  // Original anchor at top.
483-
  vertex(0, -100);
493+
// Original anchor at top.
494+
bezierVertex(0, -100);
484495

485-
  // Top-right curve.
486-
  bezierVertex(0, -50, 50, 0, 100, 0);
496+
// Top-right curve.
497+
bezierVertex(0, -50);
498+
bezierVertex(50, 0);
499+
bezierVertex(100, 0);
487500

488-
  // Bottom-right curve.
489-
  bezierVertex(50, 0, 0, 50, 0, 100);
501+
// Bottom-right curve.
502+
bezierVertex(50, 0);
503+
bezierVertex(0, 50);
504+
bezierVertex(0, 100);
490505

491-
  // Bottom-left curve.
492-
  bezierVertex(  0, 50, -50, 0, -100, 0);
506+
// Bottom-left curve.
507+
bezierVertex(0, 50);
508+
bezierVertex(-50, 0);
509+
bezierVertex(-100, 0);
510+
511+
// Top-left curve.
512+
bezierVertex(-50, 0);
513+
bezierVertex(0, -50);
514+
bezierVertex(0,-100);
515+
516+
endShape();
517+
  pop();
493518

494-
  // Top-left curve.
495-
  bezierVertex(-50, 0, 0,-50, 0,-100);
496-
  endShape();
497-
  pop();
498519
}
499520

500521
//Draws circles filled with radial gradients when we click the screen
@@ -534,9 +555,159 @@ Keep adding stickers, and you’ll be able to cover the entire screen in sticker
534555

535556
![A white person with short, dark curly hair wears pink heart-shaped glasses and a dark sleeveless shirt. He places his hand on his cheek and looks diagonally to the left, smiling softly. Image is covered in angular and curved 4-pointed white sparkles and lens flares to create a humorous effect. We see only a tiny sliver of the person's face and the gradient.](../images/drawing/even-more-sparkles.png)
536557

537-
[Here is the completed sketch](https://editor.p5js.org/juleskris/sketches/IZa2De7xH) for reference.
558+
Here is the completed sketch for reference:
559+
```
560+
let video;
561+
let snapped = false;
562+
let sparkleCounter = 0;
563+
564+
function setup() {
565+
createCanvas(640, 480);
566+
colorMode(HSB, 360, 100, 100);
567+
noStroke();
568+
569+
//instantiate the VIDEO object, and draw it on the screen at 0, 0
570+
video = createCapture(VIDEO);
571+
video.position(0, 0);
572+
573+
574+
//When we click the snap button, run the takeSnap function
575+
let snapButton = createButton("snap");
576+
snapButton.mouseClicked(takeSnap);
577+
578+
blendMode(LIGHTEST);
579+
}
580+
538581
582+
//Cycle through our three stickers each time we press the mouse
583+
function mousePressed() {
584+
if (snapped === true) {
585+
gradientFilter();
539586
587+
if (sparkleCounter % 3 === 0) {
588+
sparkle();
589+
} else if (sparkleCounter % 3 === 1) {
590+
curvedSparkle();
591+
} else {
592+
lensFlare();
593+
}
594+
595+
sparkleCounter += 1;
596+
}
597+
}
598+
599+
600+
//If we haven’t snapped a photo yet, we see our webcam video feed
601+
//Once we run the takeSnap function, set snapped to true and remove the video feed, leaving only the still photo we took
602+
function takeSnap() {
603+
if (snapped === false) {
604+
image(video, 0, 0);
605+
snapped = true;
606+
video.remove();
607+
}
608+
}
609+
610+
function sparkle() {
611+
push();
612+
613+
// Translate to the mouse's position.
614+
translate(mouseX, mouseY);
615+
616+
// Set the shape's vertices.
617+
let vertex1 = random(3, 5);
618+
let vertex2 = random(10, 50);
619+
620+
// Draw the star shape.
621+
beginShape();
622+
vertex(-vertex1, vertex1);
623+
vertex(0, vertex2);
624+
vertex(vertex1, vertex1);
625+
vertex(vertex2, 0);
626+
vertex(vertex1, -vertex1);
627+
vertex(0, -vertex2);
628+
vertex(-vertex1, -vertex1);
629+
vertex(-vertex2, 0);
630+
endShape(CLOSE);
631+
632+
pop();
633+
}
634+
635+
function curvedSparkle() {
636+
  push();
637+
638+
  // Translate to the mouse's position.
639+
  translate(mouseX, mouseY);
640+
641+
  // Scale the coordinate system.
642+
  let starScale = random(0.1, 0.5);
643+
  scale(starScale);
644+
645+
  // Set fill color.
646+
  fill(0, 0, 100);
647+
648+
  // Draw the curved star shape.
649+
  beginShape();
650+
651+
// Original anchor at top.
652+
bezierVertex(0, -100);
653+
654+
// Top-right curve.
655+
bezierVertex(0, -50);
656+
bezierVertex(50, 0);
657+
bezierVertex(100, 0);
658+
659+
// Bottom-right curve.
660+
bezierVertex(50, 0);
661+
bezierVertex(0, 50);
662+
bezierVertex(0, 100);
663+
664+
// Bottom-left curve.
665+
bezierVertex(0, 50);
666+
bezierVertex(-50, 0);
667+
bezierVertex(-100, 0);
668+
669+
// Top-left curve.
670+
bezierVertex(-50, 0);
671+
bezierVertex(0, -50);
672+
bezierVertex(0,-100);
673+
674+
endShape();
675+
  pop();
676+
677+
}
678+
679+
680+
//Draws circles filled with radial gradients when we click the screen
681+
//Each circle’s size and color are a random value contained in the diameter and h variables
682+
function lensFlare() {
683+
push();
684+
let diameter = random(50, 200);
685+
let h = random(150, 360);
686+
687+
for (let d = diameter; d > 0; d -= 1) {
688+
fill(h, 90, 90);
689+
circle(mouseX, mouseY, d);
690+
h = (h + 1) % 360;
691+
}
692+
pop();
693+
}
694+
695+
696+
//Draws a linear gradient on the screen using a for loop and lerpColor
697+
function gradientFilter() {
698+
push();
699+
let startColor = color(200, 100, 100);
700+
let endColor = color(300, 100, 100);
701+
702+
for (let y = 0; y < height; y += 1) {
703+
let amt = map(y, 0, height, 0, 1);
704+
let gradColor = lerpColor(startColor, endColor, amt);
705+
stroke(gradColor);
706+
line(0, y, width, y);
707+
}
708+
pop();
709+
}
710+
```
540711
<Callout>
541712
Use what you’ve learned so far to create a fourth sticker function, containing anything from the previous lessons. Then, add it to our existing conditional statement within mousePressed so the sketch cycles through all four shape functions each time you click the canvas.
542713
</Callout>

0 commit comments

Comments
 (0)