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
Copy file name to clipboardExpand all lines: README.md
+174-1Lines changed: 174 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -116,7 +116,7 @@ All of the above usages in p5.js 1.x remain available with the [preload.js](http
116
116
117
117
## …using registerPreloadMethod in an add-on libraries
118
118
119
-
Under to hood, returns a **Promise** from each loadImage, loadSound, and similar functions. Promises are widely used in JavaScript, so it is possible to use a callback in p5.js 1.x to create a Promise, but p5.js 1.x doesn't expect promises to be used, so you have to ensure yourself that, for example, your draw function doesn't start running before loading is done. For an example of a Promise using a callback, check out the example below that makes p5.sound.js compatible with both 1.x and 2.0:
119
+
Under the hood, returns a **Promise** from each loadImage, loadSound, and similar functions. Promises are widely used in JavaScript, so it is possible to use a callback in p5.js 1.x to create a Promise, but p5.js 1.x doesn't expect promises to be used, so you have to ensure yourself that, for example, your draw function doesn't start running before loading is done. For an example of a Promise using a callback, check out the example below that makes p5.sound.js compatible with both 1.x and 2.0:
120
120
121
121
If your add-on library built with p5.js 1.x uses `registerPreloadMethod` such as in this example from [p5.sound.js](https://github.com/processing/p5.sound.js):
122
122
@@ -431,3 +431,176 @@ Finally, touch and mouse event handling has been combined to improve sketch cons
431
431
In p5.js 2.0, instead of having separate methods for mouse and touch, we now use the browser's pointer API to handle both simultaneously. Try defining mouse functions as usual and accessing the global touches array to see what pointers are active for multitouch support!
432
432
433
433
All of the above usages in p5.js 1.x remain available with the [data.js](https://github.com/processing/p5.js-compatibility/blob/main/src/data.js) compatibility add-on library.
434
+
435
+
## …using mouseButton events:
436
+
437
+
In 1.X, where the `mouseButton` was a single variable that could have values `left`, `right` and `center`, we cannot detect if the `left` and `right` button have been pressed together.
438
+
In 2.X, the `mouseButton` is now an object with properties: `left`, `right` and `center`, which are booleans indicating whether each button has been pressed respectively.
439
+
This means that we can now detect if multiple buttons are pressed together (like if the `left` and `right` button are pressed together).
440
+
441
+
```js
442
+
functionsetup() {
443
+
createCanvas(100, 100);
444
+
445
+
describe(
446
+
"A gray square. Different shapes appear at its center depending on the mouse button that's pressed."
447
+
);
448
+
}
449
+
```
450
+
451
+
<table>
452
+
<tr><th>p5.js 1.x</th><th>p5.js 2.x</th></tr>
453
+
<tr><td>
454
+
455
+
```js
456
+
functiondraw() {
457
+
background(200);
458
+
fill(255, 50);
459
+
460
+
if (mouseIsPressed ===true) {
461
+
if (mouseButton ===LEFT) {
462
+
circle(50, 50, 50);
463
+
}
464
+
if (mouseButton ===RIGHT) {
465
+
square(25, 25, 50);
466
+
}
467
+
if (mouseButton ===CENTER) {
468
+
triangle(23, 75, 50, 20, 78, 75);
469
+
}
470
+
}
471
+
}
472
+
```
473
+
474
+
</td><td>
475
+
476
+
477
+
```js
478
+
functiondraw() {
479
+
background(200);
480
+
fill(255, 50);
481
+
482
+
if (mouseIsPressed ===true) {
483
+
if (mouseButton.left) {
484
+
circle(50, 50, 50);
485
+
}
486
+
if (mouseButton.right) {
487
+
square(25, 25, 50);
488
+
}
489
+
if (mouseButton.center) {
490
+
triangle(23, 75, 50, 20, 78, 75);
491
+
}
492
+
}
493
+
}
494
+
```
495
+
496
+
</td></tr>
497
+
</table>
498
+
499
+
Notice that when you press multiple buttons at the same time, multiple shapes can be obtained.
500
+
501
+
## …using keyCode events:
502
+
503
+
`keyCode` is still a `Number` system variable in 2.x.
504
+
```js
505
+
if (keyCode ===13) {
506
+
// Code to run if the enter key was pressed.
507
+
}
508
+
```
509
+
510
+
In 1.x system variables could be used using keyCode
511
+
```js
512
+
if (keyCode ==="ENTER") {
513
+
// Code to run if the enter key was pressed.
514
+
}
515
+
```
516
+
517
+
Instead, in 2.x you can use the `key` or `code` function to directly compare the key value.
518
+
519
+
Using `key`:
520
+
```js
521
+
if (key ==='Enter') {
522
+
// Code to run if the Enter key was pressed.
523
+
}
524
+
```
525
+
526
+
Using `code`:
527
+
```js
528
+
if (code ==='KeyA') {
529
+
// Code to run if the 'A' key was pressed.
530
+
}
531
+
```
532
+
533
+
A more detailed comparison.
534
+
535
+
```js
536
+
let x =50;
537
+
let y =50;
538
+
539
+
functionsetup() {
540
+
createCanvas(100, 100);
541
+
542
+
background(200);
543
+
544
+
describe(
545
+
'A gray square with a black circle at its center. The circle moves when the user presses an arrow key. It leaves a trail as it moves.'
546
+
);
547
+
}
548
+
```
549
+
550
+
<table>
551
+
<tr><th>p5.js 1.x</th><th>p5.js 2.x</th></tr>
552
+
<tr><td>
553
+
554
+
```js
555
+
functiondraw() {
556
+
// Update x and y if an arrow key is pressed.
557
+
if (keyIsPressed ===true) {
558
+
if (keyCode ===UP_ARROW) {
559
+
y -=1;
560
+
} elseif (keyCode ===DOWN_ARROW) {
561
+
y +=1;
562
+
} elseif (keyCode ===LEFT_ARROW) {
563
+
x -=1;
564
+
} elseif (keyCode ===RIGHT_ARROW) {
565
+
x +=1;
566
+
}
567
+
}
568
+
569
+
// Style the circle.
570
+
fill(0);
571
+
572
+
// Draw the circle at (x, y).
573
+
circle(x, y, 5);
574
+
}
575
+
```
576
+
577
+
</td><td>
578
+
579
+
580
+
```js
581
+
functiondraw() {
582
+
// Update x and y if an arrow key is pressed.
583
+
if (keyIsPressed ===true) {
584
+
if (key ==='ArrowUp') { // Up arrow key
585
+
y -=1;
586
+
} elseif (key ==='ArrowDown') { // Down arrow key
587
+
y +=1;
588
+
} elseif (key ==='ArrowLeft') { // Left arrow key
589
+
x -=1;
590
+
} elseif (key ==='ArrowRight') { // Right arrow key
591
+
x +=1;
592
+
}
593
+
}
594
+
595
+
// Style the circle.
596
+
fill(0);
597
+
598
+
// Draw the circle at (x, y).
599
+
circle(x, y, 5);
600
+
}
601
+
```
602
+
603
+
</td></tr>
604
+
</table>
605
+
606
+
More key codes can be found at websites such as [keycode.info](https://www.toptal.com/developers/keycode)
0 commit comments