q5 2.27.2 → 2.27.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. package/deno.json +1 -1
  2. package/package.json +1 -1
  3. package/q5.d.ts +190 -71
  4. package/q5.js +43 -41
  5. package/q5.min.js +1 -1
package/deno.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@q5/q5",
3
- "version": "2.27.1",
3
+ "version": "2.27.3",
4
4
  "license": "LGPL-3.0",
5
5
  "description": "Beginner friendly graphics powered by WebGPU and optimized for interactive art!",
6
6
  "author": "quinton-ashley",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "q5",
3
- "version": "2.27.2",
3
+ "version": "2.27.4",
4
4
  "description": "Beginner friendly graphics powered by WebGPU and optimized for interactive art!",
5
5
  "author": "quinton-ashley",
6
6
  "contributors": [
package/q5.d.ts CHANGED
@@ -28,10 +28,22 @@ function draw() {
28
28
 
29
29
  /** ⭐️
30
30
  * The setup function is run once, when the program starts.
31
+ *
32
+ * It can also be defined as an async function and used to load assets.
31
33
  * @example
32
34
  function setup() {
33
35
  createCanvas(200, 100);
34
36
  background('aqua');
37
+ }
38
+ * @example
39
+ let logo;
40
+
41
+ async function setup() {
42
+ logo = await loadImage('/q5js_logo.webp');
43
+ }
44
+
45
+ function draw() {
46
+ background(logo);
35
47
  }
36
48
  */
37
49
  function setup(): void;
@@ -257,34 +269,30 @@ function draw() {
257
269
  var deltaTime: number;
258
270
 
259
271
  /** ⭐️
260
- * By default, q5 uses the same preload system as p5.js v1
261
- * to load assets asynchronously, before the setup and draw
262
- * functions are run. It makes it easy for users to
263
- * load many images, sounds, and other assets in parallel.
272
+ * By default, q5 supports the p5.js v1
273
+ * [preload](https://q5js.org/learn/#preload)
274
+ * system, which uses
275
+ * [`Promise.all`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise/all)
276
+ * behind the scenes to load assets in parallel.
264
277
  *
265
- * In p5 v2, the preload system was entirely removed in
266
- * favor of having load* functions, such as `loadImage`,
267
- * return promises.
278
+ * To match p5.js v2 behavior, q5 automatically makes
279
+ * load* functions, such as `loadImage`, return promises
280
+ * in `setup` if it's defined as an async function.
268
281
  *
269
- * By default, q5 also supports use of `async setup` for loading.
270
- * Use the [`load`](https://q5js.org/learn/#load)
271
- * function to load a file or multiple files. It returns
272
- * a promise that resolves when the file(s) are loaded.
282
+ * This function can be used at any point in your sketch
283
+ * to make load* functions return promises or not. Yet, consider
284
+ * using [`load`](https://q5js.org/learn/#load) instead.
273
285
  *
274
- * Alternatively, disable the preload system in q5 to make
275
- * load* functions return promises, to match p5 v2 behavior.
276
- * @param {boolean} val true by default, whether to enable or disable the preload system, affects the return value of load* functions
286
+ * @param {boolean} [val] Whether load* functions should return promises or not. If this parameter is undefined the value is set to true.
277
287
  * @example
278
288
  createCanvas(200);
279
- usePreloadSystem(false);
280
289
 
281
- logo = await loadImage('/q5js_logo.webp');
290
+ usePromiseLoading();
282
291
 
283
- function draw() {
284
- background(logo);
285
- }
292
+ let logo = await loadImage('/q5js_logo.webp');
293
+ background(logo);
286
294
  */
287
- function usePreloadSystem(val: boolean): void;
295
+ function usePromiseLoading(val?: boolean): void;
288
296
 
289
297
  class Q5 {
290
298
  /** ⭐️
@@ -334,6 +342,11 @@ q.circle(100, 50, 20);
334
342
  */
335
343
  static supportsHDR: boolean;
336
344
 
345
+ /** ⭐️
346
+ * Set to true to keep draw looping after an error. False by default.
347
+ */
348
+ static errorTolerant: boolean;
349
+
337
350
  /** ⭐️
338
351
  * Modules added to this object will be added to new Q5 instances.
339
352
  */
@@ -402,7 +415,7 @@ q.draw = () => {
402
415
  * q5 functions. The origin of a WebGPU canvas is at its center.
403
416
  * @param {number} [w] width or size of the canvas
404
417
  * @param {number} [h] height of the canvas
405
- * @param {Object} [opt] options for the canvas
418
+ * @param {object} [opt] options for the canvas
406
419
  * @param {boolean} [opt.alpha] whether the canvas should have an alpha channel that allows it to be seen through, default is false
407
420
  * @param {string} [opt.colorSpace] color space of the canvas, either "srgb" or "display-p3", default is "display-p3" for devices that support HDR colors
408
421
  * @returns {HTMLCanvasElement} created canvas element
@@ -861,7 +874,7 @@ rect(20, 20, 60, 60);
861
874
  * See issue [#104](https://github.com/q5js/q5.js/issues/104) for details.
862
875
  * @param {number} w width
863
876
  * @param {number} h height
864
- * @param {Object} [opt] options
877
+ * @param {object} [opt] options
865
878
  * @returns {Q5} a new Q5 graphics buffer
866
879
  */
867
880
  function createGraphics(w: number, h: number, opt?: any): Q5;
@@ -1397,70 +1410,78 @@ triangle(50, 130, 150, 180, 50, 180);
1397
1410
  function strokeJoin(val: CanvasLineJoin): void;
1398
1411
 
1399
1412
  /** 🧑‍🎨
1400
- * Set to `CORNER`, `CENTER`, `RADIUS`, or `CORNERS`.
1413
+ * Set to `CORNER` (default), `CENTER`, `RADIUS`, or `CORNERS`.
1401
1414
  *
1402
1415
  * Changes how the first four inputs to
1403
1416
  * `rect` and `square` are interpreted.
1404
- * @param {string} val rectangle mode
1417
+ * @param {string} mode
1405
1418
  * @example
1406
1419
  createCanvas(200, 100);
1407
1420
  background(200);
1408
-
1409
1421
  rectMode(CORNER);
1422
+
1423
+ // ( x, y, w, h)
1410
1424
  rect(50, 25, 100, 50);
1411
1425
  * @example
1412
1426
  createCanvas(200, 100);
1413
1427
  background(200);
1414
-
1415
1428
  rectMode(CENTER);
1429
+
1430
+ // ( cX, cY, w, h)
1416
1431
  rect(100, 50, 100, 50);
1417
1432
  * @example
1418
1433
  createCanvas(200, 100);
1419
1434
  background(200);
1420
-
1421
1435
  rectMode(RADIUS);
1436
+
1437
+ // ( cX, cY, rX, rY)
1422
1438
  rect(100, 50, 50, 25);
1423
1439
  * @example
1424
1440
  createCanvas(200, 100);
1425
1441
  background(200);
1426
-
1427
1442
  rectMode(CORNERS);
1443
+
1444
+ // ( x1, y1, x2, y2)
1428
1445
  rect(50, 25, 150, 75);
1429
1446
  */
1430
- function rectMode(val: string): void;
1447
+ function rectMode(mode: string): void;
1431
1448
 
1432
1449
  /** 🧑‍🎨
1433
- * Set to `CENTER`, `RADIUS`, `CORNER`, or `CORNERS`.
1450
+ * Set to `CENTER` (default), `RADIUS`, `CORNER`, or `CORNERS`.
1434
1451
  *
1435
1452
  * Changes how the first four inputs to
1436
1453
  * `ellipse`, `circle`, and `arc` are interpreted.
1437
- * @param {string} val ellipse mode
1454
+ * @param {string} mode
1438
1455
  * @example
1439
1456
  createCanvas(200, 100);
1440
1457
  background(200);
1441
-
1442
1458
  ellipseMode(CENTER);
1459
+
1460
+ // ( x, y, w, h)
1443
1461
  ellipse(100, 50, 100, 50);
1444
1462
  * @example
1445
1463
  createCanvas(200, 100);
1446
1464
  background(200);
1447
-
1448
1465
  ellipseMode(RADIUS);
1466
+
1467
+ // ( x, y, rX, rY)
1449
1468
  ellipse(100, 50, 50, 25);
1450
1469
  * @example
1451
1470
  createCanvas(200, 100);
1452
1471
  background(200);
1453
-
1454
1472
  ellipseMode(CORNER);
1473
+
1474
+ // (lX, tY, w, h)
1455
1475
  ellipse(50, 25, 100, 50);
1456
1476
  * @example
1457
1477
  createCanvas(200, 100);
1458
1478
  background(200);
1459
-
1460
1479
  ellipseMode(CORNERS);
1480
+
1481
+ // ( x1, y1, x2, y2)
1461
1482
  ellipse(50, 25, 150, 75);
1462
1483
  */
1463
- function ellipseMode(val: string): void;
1484
+ function ellipseMode(mode: string): void;
1464
1485
 
1465
1486
 
1466
1487
  /** 🧑‍🎨
@@ -1622,15 +1643,15 @@ curve(-100, -200, -50, 0, 50, 0, 100, -200);
1622
1643
  */
1623
1644
  const CORNERS: 'corners';
1624
1645
 
1625
-
1626
1646
  // 🌆 image
1627
1647
 
1628
1648
  /** 🌆
1629
1649
  * Loads an image from a URL and optionally runs a callback function.
1650
+ *
1651
+ * Returns a promise if used in `async setup`.
1652
+ *
1630
1653
  * @param {string} url url of the image to load
1631
- * @param {(img: any) => void} [cb] callback function after the image is loaded
1632
- * @param {any} [opt] optional parameters for loading the image
1633
- * @returns {Q5.Image} image
1654
+ * @returns {Q5.Image | Promise<Q5.Image>} image or promise
1634
1655
  * @example
1635
1656
  createCanvas(200);
1636
1657
 
@@ -1649,11 +1670,11 @@ q.draw = () => {
1649
1670
  background(logo);
1650
1671
  };
1651
1672
  */
1652
- function loadImage(url: string, cb?: (img: any) => void, opt?: any): Q5.Image;
1673
+ function loadImage(url: string): Q5.Image | Promise<Q5.Image>;
1653
1674
 
1654
1675
  /** 🌆
1655
- * Draws an image to the canvas.
1656
- * @param {any} img image to draw
1676
+ * Draws an image or video frame to the canvas.
1677
+ * @param {Q5.Image | HTMLVideoElement} img image or video to draw
1657
1678
  * @param {number} dx x position to draw the image at
1658
1679
  * @param {number} dy y position to draw the image at
1659
1680
  * @param {number} [dw] width of the destination image
@@ -1679,23 +1700,42 @@ function draw() {
1679
1700
  image(logo, 0, 0, 200, 200, 256, 256, 512, 512);
1680
1701
  }
1681
1702
  */
1682
- function image(img: any, dx: number, dy: number, dw?: number, dh?: number, sx?: number, sy?: number, sw?: number, sh?: number): void;
1703
+ function image(img: Q5.Image | HTMLVideoElement, dx: number, dy: number, dw?: number, dh?: number, sx?: number, sy?: number, sw?: number, sh?: number): void;
1683
1704
 
1684
1705
  /** 🌆
1685
- * Sets the image mode, which determines the position and alignment of images drawn on the canvas.
1706
+ * Set to `CORNER` (default), `CORNERS`, or `CENTER`.
1686
1707
  *
1687
- * - `CORNER`: (default) images will be drawn from the top-left corner
1688
- * - `CORNERS`: images will be drawn from the top-left to the bottom-right corner
1689
- * - `CENTER`: images will be drawn centered at (dx, dy)
1708
+ * Changes how inputs to `image` are interpreted.
1690
1709
  * @param {string} mode
1691
1710
  * @example
1692
1711
  createCanvas(200);
1712
+ let logo = loadImage('/q5js_logo.webp');
1713
+
1714
+ function draw() {
1715
+ imageMode(CORNER);
1693
1716
 
1717
+ // ( img, x, y, w, h)
1718
+ image(logo, 50, 50, 100, 100);
1719
+ }
1720
+ * @example
1721
+ createCanvas(200);
1694
1722
  let logo = loadImage('/q5js_logo.webp');
1695
1723
 
1696
1724
  function draw() {
1697
1725
  imageMode(CENTER);
1698
- image(logo, 100, 100, 200, 200);
1726
+
1727
+ // ( img, cX, cY, w, h)
1728
+ image(logo, 100, 100, 100, 100);
1729
+ }
1730
+ * @example
1731
+ createCanvas(200);
1732
+ let logo = loadImage('/q5js_logo.webp');
1733
+
1734
+ function draw() {
1735
+ imageMode(CORNERS);
1736
+
1737
+ // ( img, x1, y1, x2, y2)
1738
+ image(logo, 50, 50, 100, 100);
1699
1739
  }
1700
1740
  */
1701
1741
  function imageMode(mode: string): void;
@@ -2028,9 +2068,11 @@ text(info, 12, 30, 20, 6);
2028
2068
  * with the file ending "-msdf.json" can be used to render text with
2029
2069
  * the `text` function. Fonts in other formats can be used with the
2030
2070
  * [`textImage`](https://q5js.org/learn/#textImage) function.
2031
- * @param {string} url uRL of the font to load
2032
- * @param {(font: FontFace) => void} [cb] optional callback function that receives the font name as an argument once the font is loaded
2033
- * @returns {FontFace} font
2071
+ *
2072
+ * Returns a promise if used in `async setup`.
2073
+ *
2074
+ * @param {string} url URL of the font to load
2075
+ * @returns {FontFace | Promise<FontFace>} font or promise
2034
2076
  * @example
2035
2077
  createCanvas(200, 56);
2036
2078
 
@@ -2044,7 +2086,7 @@ function setup() {
2044
2086
  * @example
2045
2087
  createCanvas(200, 74);
2046
2088
 
2047
- let pacifico = loadFont(
2089
+ loadFont(
2048
2090
  'fonts.googleapis.com/css2?family=Pacifico'
2049
2091
  );
2050
2092
 
@@ -2054,7 +2096,7 @@ function setup() {
2054
2096
  text('Hello!', 2, 68);
2055
2097
  }
2056
2098
  */
2057
- function loadFont(url: string, cb?: (font: FontFace) => void): FontFace;
2099
+ function loadFont(url: string): FontFace | Promise<FontFace>
2058
2100
 
2059
2101
  /** ✍️
2060
2102
  * Sets the current font to be used for rendering text.
@@ -2532,9 +2574,65 @@ function keyReleased() {
2532
2574
  /** 🖲️
2533
2575
  * Array of current touches, each touch being an object with
2534
2576
  * id, x, and y properties.
2577
+ * @example
2578
+ function draw() {
2579
+ background(200);
2580
+ for (let touch of touches) {
2581
+ circle(touch.x, touch.y, 100);
2582
+ }
2583
+ }
2584
+
2585
+ function touchStarted() {}
2535
2586
  */
2536
2587
  let touches: any[];
2537
2588
 
2589
+ /** 🖲️
2590
+ * Define this function to respond to touch down events.
2591
+ *
2592
+ * By default this function will prevent the default behavior of
2593
+ * scrolling the page when the touch started inside the canvas.
2594
+ * Return true to allow the default behavior.
2595
+ * @example
2596
+ createCanvas(200);
2597
+
2598
+ let gray = 95;
2599
+ function touchStarted() {
2600
+ background(gray % 256);
2601
+ gray += 40;
2602
+ }
2603
+ */
2604
+ function touchStarted(): void;
2605
+
2606
+ /** 🖲️
2607
+ * Define this function to respond to touch down events.
2608
+ *
2609
+ * By default this function will prevent the default behavior of
2610
+ * scrolling the page when the touch started inside the canvas.
2611
+ * Return true to allow the default behavior.
2612
+ * @example
2613
+ createCanvas(200);
2614
+
2615
+ let gray = 95;
2616
+ function touchEnded() {
2617
+ background(gray % 256);
2618
+ gray += 40;
2619
+ }
2620
+ */
2621
+ function touchEnded(): void;
2622
+
2623
+ /** 🖲️
2624
+ * Define this function to respond to touch move events.
2625
+ * @example
2626
+ createCanvas(200);
2627
+ let gray = 95;
2628
+
2629
+ function touchMoved() {
2630
+ background(gray % 256);
2631
+ gray++;
2632
+ }
2633
+ */
2634
+ function touchMoved(): void;
2635
+
2538
2636
  /** 🖲️
2539
2637
  * Sets the cursor to a [CSS cursor type](https://developer.mozilla.org/docs/Web/CSS/cursor) or image.
2540
2638
  * If an image is provided, optional x and y coordinates can
@@ -3012,8 +3110,11 @@ function draw() {
3012
3110
  * For backwards compatibility with the p5.sound API, the functions
3013
3111
  * `setVolume`, `setLoop`, `setPan`, `isLoaded`, and `isPlaying`
3014
3112
  * are also implemented, but their use is deprecated.
3113
+ *
3114
+ * Returns a promise if used in `async setup`.
3115
+ *
3015
3116
  * @param {string} url sound file
3016
- * @returns {Sound} a new `Sound` object
3117
+ * @returns {Sound | Promise<Sound>} a new `Sound` object or promise
3017
3118
  * @example
3018
3119
  createCanvas(200);
3019
3120
 
@@ -3024,7 +3125,7 @@ function mousePressed() {
3024
3125
  sound.play();
3025
3126
  }
3026
3127
  */
3027
- function loadSound(url: string): Sound;
3128
+ function loadSound(url: string): Sound | Promise<Sound>;
3028
3129
 
3029
3130
  /**
3030
3131
  * Loads audio data from a file and returns an [HTMLAudioElement](https://developer.mozilla.org/docs/Web/API/HTMLMediaElement).
@@ -3033,8 +3134,11 @@ function mousePressed() {
3033
3134
  *
3034
3135
  * Note that audio can only be played after the first user
3035
3136
  * interaction with the page!
3137
+ *
3138
+ * Returns a promise if used in `async setup`.
3139
+ *
3036
3140
  * @param url audio file
3037
- * @returns {HTMLAudioElement} an HTMLAudioElement
3141
+ * @returns {HTMLAudioElement | Promise<HTMLAudioElement>} an HTMLAudioElement or promise
3038
3142
  * @example
3039
3143
  createCanvas(200);
3040
3144
 
@@ -3045,7 +3149,7 @@ function mousePressed() {
3045
3149
  audio.play();
3046
3150
  }
3047
3151
  */
3048
- function loadAudio(url: string): HTMLAudioElement;
3152
+ function loadAudio(url: string): HTMLAudioElement | Promise<HTMLAudioElement>;
3049
3153
 
3050
3154
  /** 🔊
3051
3155
  * Returns the AudioContext in use or undefined if it doesn't exist.
@@ -3363,7 +3467,11 @@ function draw() {
3363
3467
  *
3364
3468
  * The video element can be hidden and its content can be
3365
3469
  * displayed on the canvas using the `image` function.
3470
+ *
3471
+ * Returns a promise if used in `async setup`.
3472
+ *
3366
3473
  * @param {string} src url of the video
3474
+ * @returns {HTMLVideoElement | Promise<HTMLVideoElement>} a new video element or promise
3367
3475
  * @example
3368
3476
  createCanvas(0);
3369
3477
 
@@ -3386,7 +3494,7 @@ function draw() {
3386
3494
  filter(HUE_ROTATE, 90);
3387
3495
  }
3388
3496
  */
3389
- function createVideo(src: string): HTMLVideoElement;
3497
+ function createVideo(src: string): HTMLVideoElement | Promise<HTMLVideoElement>;
3390
3498
 
3391
3499
  /** 📑
3392
3500
  * Creates a capture from a connected camera, such as a webcam.
@@ -3401,9 +3509,12 @@ function draw() {
3401
3509
  * by default. The first parameter to this function can be used to
3402
3510
  * specify the constraints for the capture. See [`getUserMedia`](https://developer.mozilla.org/docs/Web/API/MediaDevices/getUserMedia)
3403
3511
  * for more info.
3512
+ *
3513
+ * Returns a promise if used in `async setup`.
3514
+ *
3404
3515
  * @param {string} [type] type of capture, can be only `VIDEO` or only `AUDIO`, the default is to use both video and audio
3405
3516
  * @param {boolean} [flipped] whether to mirror the video horizontally, true by default
3406
- * @param {(vid: HTMLVideoElement) => void} [cb] callback function after the capture is created
3517
+ * @returns {HTMLVideoElement | Promise<HTMLVideoElement>} a new video element or promise
3407
3518
  * @example
3408
3519
  createCanvas(200);
3409
3520
 
@@ -3436,7 +3547,7 @@ function mousePressed() {
3436
3547
  canvas.remove();
3437
3548
  }
3438
3549
  */
3439
- function createCapture(type?: string, flipped?: boolean, cb?: (vid: HTMLVideoElement) => void): HTMLVideoElement;
3550
+ function createCapture(type?: string, flipped?: boolean): HTMLVideoElement | Promise<HTMLVideoElement>;
3440
3551
 
3441
3552
  /** 📑
3442
3553
  * Finds the first element in the DOM that matches the given [CSS selector](https://developer.mozilla.org/docs/Learn_web_development/Core/Styling_basics/Basic_selectors).
@@ -3613,26 +3724,34 @@ function mousePressed() {
3613
3724
  function save(data?: object, fileName?: string): void;
3614
3725
 
3615
3726
  /** 🛠️
3616
- * Loads a text file from the specified url. Result is one string.
3727
+ * Loads a text file from the specified url.
3728
+ *
3729
+ * Returns a promise if used in `async setup`.
3730
+ *
3617
3731
  * @param {string} url text file
3618
- * @param {(result: string) => void} cb a callback function that is run when the file is loaded
3732
+ * @returns {object | Promise} an object containing the loaded text in the property `obj.text` or a promise
3619
3733
  */
3620
- function loadText(url: string, cb: (result: string) => void): void;
3734
+ function loadText(url: string): object | Promise<object>;
3621
3735
 
3622
3736
  /** 🛠️
3623
- * Loads a JSON file from the specified url. Result depends on the
3624
- * JSON file's contents, but is typically an object or array.
3737
+ * Loads a JSON file from the specified url.
3738
+ *
3739
+ * Returns a promise if used in `async setup`.
3740
+ *
3625
3741
  * @param {string} url JSON file
3626
- * @param {(result: any) => void} cb a callback function that is run when the file is loaded
3742
+ * @returns {any | Promise} an object or array containing the loaded JSON or a promise
3627
3743
  */
3628
- function loadJSON(url: string, cb: (result: any) => void): void;
3744
+ function loadJSON(url: string): any | Promise<any>;
3629
3745
 
3630
3746
  /** 🛠️
3631
- * Loads a CSV file from the specified url. Result is an array of objects.
3747
+ * Loads a CSV file from the specified url.
3748
+ *
3749
+ * Returns a promise if used in `async setup`.
3750
+ *
3632
3751
  * @param {string} url CSV file
3633
- * @param {(result: object[]) => void} cb a callback function that is run when the file is loaded
3752
+ * @returns {object[] | Promise<object[]>} an array of objects containing the loaded CSV or a promise
3634
3753
  */
3635
- function loadCSV(url: string, cb: (result: object[]) => void): void;
3754
+ function loadCSV(url: string): object[] | Promise<object[]>;
3636
3755
 
3637
3756
  /** 🛠️
3638
3757
  * nf is short for number format. It formats a number