You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -242,13 +242,7 @@ To create a curved line, we’ll need to use the [`bezierVertex()`](/reference/p
242
242
- Just like we did for the pointed sparkles, we’ll begin our shape with `beginShape()`, and end it with `endShape()`.
243
243
- 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.
244
244
-`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.
252
246
253
247
Now that you have some context, let’s begin our shape!
254
248
@@ -263,10 +257,9 @@ beginShape();
263
257
vertex(0, -100); // anchor point 1
264
258
265
259
// 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);
270
263
endShape();
271
264
```
272
265
@@ -292,19 +285,28 @@ Let’s complete our shape:
292
285
beginShape();
293
286
294
287
// Original anchor at top.
295
-
vertex(0, -100);
288
+
bezierVertex(0, -100);
296
289
297
290
// Top-right curve.
298
-
bezierVertex(0, -50, 50, 0, 100, 0);
291
+
bezierVertex(0, -50);
292
+
bezierVertex(50, 0);
293
+
bezierVertex(100, 0);
299
294
300
295
// Bottom-right curve.
301
-
bezierVertex(50, 0, 0, 50, 0, 100);
296
+
bezierVertex(50, 0);
297
+
bezierVertex(0, 50);
298
+
bezierVertex(0, 100);
302
299
303
300
// Bottom-left curve.
304
-
bezierVertex( 0, 50, -50, 0, -100, 0);
301
+
bezierVertex(0, 50);
302
+
bezierVertex(-50, 0);
303
+
bezierVertex(-100, 0);
305
304
306
305
// Top-left curve.
307
-
bezierVertex(-50, 0, 0,-50, 0,-100);
306
+
bezierVertex(-50, 0);
307
+
bezierVertex(0, -50);
308
+
bezierVertex(0,-100);
309
+
308
310
endShape();
309
311
```
310
312
@@ -321,38 +323,47 @@ Your `curvedSparkle()` function looks like this:
321
323
322
324
```js
323
325
functioncurvedSparkle() {
324
-
push();
326
+
push();
327
+
328
+
// Translate to the mouse's position.
329
+
translate(mouseX, mouseY);
325
330
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);
328
334
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);
332
337
333
-
// Set fill color.
334
-
fill(0, 0, 100);
338
+
//Draw the curved star shape.
339
+
beginShape();
335
340
336
-
// Draw the curved star shape.
337
-
beginShape();
341
+
//Original anchor at top.
342
+
bezierVertex(0, -100);
338
343
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);
341
348
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);
344
353
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);
347
358
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);
350
363
351
-
// Top-left curve.
352
-
bezierVertex(-50, 0, 0,-50, 0,-100);
364
+
endShape();
365
+
pop();
353
366
354
-
endShape();
355
-
pop();
356
367
}
357
368
```
358
369
@@ -464,37 +475,47 @@ function sparkle() {
464
475
}
465
476
466
477
functioncurvedSparkle() {
467
-
push();
478
+
push();
468
479
469
-
// Translate to the mouse's position.
470
-
translate(mouseX, mouseY);
480
+
// Translate to the mouse's position.
481
+
translate(mouseX, mouseY);
471
482
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);
475
486
476
-
// Set fill color.
477
-
fill(0, 0, 100);
487
+
// Set fill color.
488
+
fill(0, 0, 100);
478
489
479
-
// Draw the curved star shape.
480
-
beginShape();
490
+
// Draw the curved star shape.
491
+
beginShape();
481
492
482
-
// Original anchor at top.
483
-
vertex(0, -100);
493
+
// Original anchor at top.
494
+
bezierVertex(0, -100);
484
495
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);
487
500
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);
490
505
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();
493
518
494
-
// Top-left curve.
495
-
bezierVertex(-50, 0, 0,-50, 0,-100);
496
-
endShape();
497
-
pop();
498
519
}
499
520
500
521
//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
534
555
535
556

536
557
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
+
538
581
582
+
//Cycle through our three stickers each time we press the mouse
583
+
function mousePressed() {
584
+
if (snapped === true) {
585
+
gradientFilter();
539
586
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
+
```
540
711
<Callout>
541
712
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.
0 commit comments