q5 2.15.4 → 2.16.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.
- package/README.md +1 -1
- package/deno.json +1 -1
- package/package.json +1 -1
- package/q5.d.ts +258 -68
- package/q5.js +123 -48
- package/q5.min.js +2 -2
package/README.md
CHANGED
|
@@ -50,7 +50,7 @@ We need your support though! If you enjoy using q5.js, please donate via [GitHub
|
|
|
50
50
|
|
|
51
51
|
Are you interested in volunteering to write code for q5.js or improve the q5 ecosystem? Contributions are welcome!
|
|
52
52
|
|
|
53
|
-
Please report issues or comment on existing issues before working on a pull request. Check out the [q5 planning board](https://github.com/orgs/q5js/projects/1/views/1).
|
|
53
|
+
Please report issues or comment on existing issues before working on a pull request. Check out the [q5 project planning board](https://github.com/orgs/q5js/projects/1/views/1).
|
|
54
54
|
|
|
55
55
|
All contributors must agree to the [code of conduct](CODE_OF_CONDUCT.md).
|
|
56
56
|
|
package/deno.json
CHANGED
package/package.json
CHANGED
package/q5.d.ts
CHANGED
|
@@ -28,8 +28,11 @@ function setup() {
|
|
|
28
28
|
function setup(): void;
|
|
29
29
|
|
|
30
30
|
/** ⭐️
|
|
31
|
-
* Load assets in the preload function to ensure that they'
|
|
32
|
-
*
|
|
31
|
+
* Load assets in the preload function to ensure that they'll be
|
|
32
|
+
* ready to use in the setup and draw functions.
|
|
33
|
+
*
|
|
34
|
+
* q5's preload system can also be used without a preload function
|
|
35
|
+
* if you create a canvas first, as shown in the second example.
|
|
33
36
|
* @example
|
|
34
37
|
let logo;
|
|
35
38
|
|
|
@@ -37,6 +40,14 @@ function preload() {
|
|
|
37
40
|
logo = loadImage('/q5js_logo.webp');
|
|
38
41
|
}
|
|
39
42
|
|
|
43
|
+
function draw() {
|
|
44
|
+
background(logo);
|
|
45
|
+
}
|
|
46
|
+
* @example
|
|
47
|
+
createCanvas(200, 100);
|
|
48
|
+
|
|
49
|
+
let logo = loadImage('/q5js_logo.webp');
|
|
50
|
+
|
|
40
51
|
function draw() {
|
|
41
52
|
background(logo);
|
|
42
53
|
}
|
|
@@ -177,6 +188,7 @@ function draw() {
|
|
|
177
188
|
background(200);
|
|
178
189
|
circle(frameCount % 200, 100, 80);
|
|
179
190
|
}
|
|
191
|
+
|
|
180
192
|
function postProcess() {
|
|
181
193
|
filter(INVERT);
|
|
182
194
|
}
|
|
@@ -235,6 +247,34 @@ function draw() {
|
|
|
235
247
|
*/
|
|
236
248
|
var deltaTime: number;
|
|
237
249
|
|
|
250
|
+
/** ⭐️
|
|
251
|
+
* q5 uses the same preload system as p5.js v1
|
|
252
|
+
* to load assets asynchronously, before the setup and draw
|
|
253
|
+
* functions are run. It makes it very easy for users to
|
|
254
|
+
* load many images, sounds, and other assets at the same time.
|
|
255
|
+
*
|
|
256
|
+
* In p5.js v2, the preload system was entirely removed in
|
|
257
|
+
* favor of having load* functions, such as `loadImage`,
|
|
258
|
+
* return promises.
|
|
259
|
+
*
|
|
260
|
+
* In q5 the `load` function can be used to load a file or
|
|
261
|
+
* multiple files, and it returns a promise that resolves
|
|
262
|
+
* when the file(s) are loaded.
|
|
263
|
+
*
|
|
264
|
+
* Disable the preload system in q5 to make load* functions
|
|
265
|
+
* return promises, to match p5.js v2 behavior.
|
|
266
|
+
* @example
|
|
267
|
+
new Q5();
|
|
268
|
+
disablePreloadSystem();
|
|
269
|
+
|
|
270
|
+
logo = await loadImage('/q5js_logo.webp');
|
|
271
|
+
|
|
272
|
+
function draw() {
|
|
273
|
+
background(logo);
|
|
274
|
+
}
|
|
275
|
+
*/
|
|
276
|
+
function disablePreloadSystem(): void;
|
|
277
|
+
|
|
238
278
|
class Q5 {
|
|
239
279
|
/** ⭐️
|
|
240
280
|
* Creates an instance of Q5.
|
|
@@ -315,18 +355,24 @@ function draw() {
|
|
|
315
355
|
|
|
316
356
|
/** ⬜️
|
|
317
357
|
* The canvas element associated with the Q5 instance.
|
|
358
|
+
*
|
|
359
|
+
* @prop {Number} w
|
|
360
|
+
* @prop {Number} width
|
|
361
|
+
* @prop {Number} h
|
|
362
|
+
* @prop {Number} height
|
|
363
|
+
* @prop {Number} hw half the width
|
|
364
|
+
* @prop {Number} hh half the height
|
|
365
|
+
* @prop {String} renderer either "c2d" (Canvas2D) or "webgpu"
|
|
318
366
|
*/
|
|
319
367
|
var canvas: HTMLCanvasElement;
|
|
320
368
|
|
|
321
369
|
/** ⬜️
|
|
322
370
|
* The width of the canvas.
|
|
323
|
-
* Can also be accessed via `canvas.w`/`canvas.width`.
|
|
324
371
|
*/
|
|
325
372
|
var width: number;
|
|
326
373
|
|
|
327
374
|
/** ⬜️
|
|
328
375
|
* The height of the canvas.
|
|
329
|
-
* Can also be accessed via `canvas.h`/`canvas.height`.
|
|
330
376
|
*/
|
|
331
377
|
var height: number;
|
|
332
378
|
|
|
@@ -750,12 +796,17 @@ rect(20, 20, 60, 60);
|
|
|
750
796
|
* @param {string} mode
|
|
751
797
|
* - "normal": (default) no styling to canvas or its parent element
|
|
752
798
|
* - "centered": canvas will be centered horizontally and vertically within its parent and if the window is smaller than the canvas, the canvas will be resized to avoid clipping
|
|
753
|
-
* - "maxed": canvas will fill the parent element,
|
|
754
|
-
* - "fullscreen": canvas will fill the screen with letterboxing if necessary to preserve its aspect ratio
|
|
799
|
+
* - "maxed": canvas will fill the parent element, with letterboxing if necessary to preserve its aspect ratio
|
|
755
800
|
* @param {string} renderQuality
|
|
756
801
|
* - "smooth": (default) no change to the default render quality
|
|
757
802
|
* - "pixelated": runs `pixelDensity(1)` and `noSmooth()` then sets the canvas CSS styles `image-rendering: pixelated` and `font-smooth: never`
|
|
758
803
|
* @param {number} scale can also be given as a string (for example "x2")
|
|
804
|
+
* @example
|
|
805
|
+
createCanvas(50, 25);
|
|
806
|
+
|
|
807
|
+
displayMode('centered', 'pixelated', 4);
|
|
808
|
+
|
|
809
|
+
circle(25, 12.5, 16);
|
|
759
810
|
*/
|
|
760
811
|
function displayMode(mode: string, renderQuality: string, scale: string | number): void;
|
|
761
812
|
|
|
@@ -764,31 +815,53 @@ rect(20, 20, 60, 60);
|
|
|
764
815
|
/** 🧑🎨
|
|
765
816
|
* Draws over the entire canvas with a color or image.
|
|
766
817
|
* @param {string | number} color color or image to draw
|
|
818
|
+
* @example
|
|
819
|
+
createCanvas(200, 100);
|
|
820
|
+
background('crimson');
|
|
821
|
+
* @example
|
|
822
|
+
function draw() {
|
|
823
|
+
background(128, 20);
|
|
824
|
+
circle(mouseX, mouseY, 20);
|
|
825
|
+
}
|
|
767
826
|
*/
|
|
768
827
|
function background(color: string | number): void;
|
|
769
828
|
|
|
770
829
|
/** 🧑🎨
|
|
771
|
-
* Draws a rectangle.
|
|
830
|
+
* Draws a rectangle or a rounded rectangle.
|
|
772
831
|
* @param {number} x x-coordinate
|
|
773
832
|
* @param {number} y y-coordinate
|
|
774
833
|
* @param {number} w width of the rectangle
|
|
775
834
|
* @param {number} [h] height of the rectangle
|
|
776
|
-
* @param {number} [tl] top-left radius
|
|
777
|
-
* @param {number} [tr] top-right radius
|
|
778
|
-
* @param {number} [br] bottom-right radius
|
|
779
|
-
* @param {number} [bl] bottom-left radius
|
|
835
|
+
* @param {number} [tl] top-left radius
|
|
836
|
+
* @param {number} [tr] top-right radius
|
|
837
|
+
* @param {number} [br] bottom-right radius
|
|
838
|
+
* @param {number} [bl] bottom-left radius
|
|
839
|
+
* @example
|
|
840
|
+
createCanvas(200, 200);
|
|
841
|
+
background(200);
|
|
842
|
+
|
|
843
|
+
rect(30, 20, 40, 60);
|
|
844
|
+
rect(80, 70, 40, 60, 10);
|
|
845
|
+
rect(130, 120, 40, 60, 30, 2, 8, 20);
|
|
780
846
|
*/
|
|
781
847
|
function rect(x: number, y: number, w: number, h?: number, tl?: number, tr?: number, br?: number, bl?: number): void;
|
|
782
848
|
|
|
783
849
|
/** 🧑🎨
|
|
784
|
-
* Draws a square.
|
|
850
|
+
* Draws a square or a rounded square.
|
|
785
851
|
* @param {number} x x-coordinate
|
|
786
852
|
* @param {number} y y-coordinate
|
|
787
853
|
* @param {number} size size of the sides of the square
|
|
788
|
-
* @param {number} [tl] top-left radius
|
|
789
|
-
* @param {number} [tr] top-right radius
|
|
790
|
-
* @param {number} [br] bottom-right radius
|
|
791
|
-
* @param {number} [bl] bottom-left radius
|
|
854
|
+
* @param {number} [tl] top-left radius
|
|
855
|
+
* @param {number} [tr] top-right radius
|
|
856
|
+
* @param {number} [br] bottom-right radius
|
|
857
|
+
* @param {number} [bl] bottom-left radius
|
|
858
|
+
* @example
|
|
859
|
+
createCanvas(200, 200);
|
|
860
|
+
background(200);
|
|
861
|
+
|
|
862
|
+
square(30, 30, 40);
|
|
863
|
+
square(80, 80, 40, 10);
|
|
864
|
+
square(130, 130, 40, 30, 2, 8, 20);
|
|
792
865
|
*/
|
|
793
866
|
function square(x: number, y: number, size: number, tl?: number, tr?: number, br?: number, bl?: number): void;
|
|
794
867
|
|
|
@@ -797,6 +870,9 @@ rect(20, 20, 60, 60);
|
|
|
797
870
|
* @param {number} x x-coordinate
|
|
798
871
|
* @param {number} y y-coordinate
|
|
799
872
|
* @param {number} diameter diameter of the circle
|
|
873
|
+
* @example
|
|
874
|
+
createCanvas(200, 100);
|
|
875
|
+
circle(100, 50, 80);
|
|
800
876
|
*/
|
|
801
877
|
function circle(x: number, y: number, diameter: number): void;
|
|
802
878
|
|
|
@@ -806,6 +882,9 @@ rect(20, 20, 60, 60);
|
|
|
806
882
|
* @param {number} y y-coordinate
|
|
807
883
|
* @param {number} width width of the ellipse
|
|
808
884
|
* @param {number} [height] height of the ellipse
|
|
885
|
+
* @example
|
|
886
|
+
createCanvas(200, 100);
|
|
887
|
+
ellipse(100, 50, 160, 80);
|
|
809
888
|
*/
|
|
810
889
|
function ellipse(x: number, y: number, width: number, height?: number): void;
|
|
811
890
|
|
|
@@ -821,14 +900,13 @@ rect(20, 20, 60, 60);
|
|
|
821
900
|
* @param {number} stop angle to stop the arc
|
|
822
901
|
* @param {number} [mode] shape and stroke style setting, default is `PIE_OPEN` for a pie shape with an unclosed stroke, can be `PIE`, `CHORD`, or `CHORD_OPEN`
|
|
823
902
|
* @example
|
|
824
|
-
|
|
825
|
-
|
|
903
|
+
createCanvas(200, 200);
|
|
904
|
+
background(200);
|
|
826
905
|
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
}
|
|
906
|
+
arc(40, 40, 40, 40, 0.8, -0.8);
|
|
907
|
+
arc(80, 80, 40, 40, 0.8, -0.8, PIE);
|
|
908
|
+
arc(120, 120, 40, 40, 0.8, -0.8, CHORD_OPEN);
|
|
909
|
+
arc(160, 160, 40, 40, 0.8, -0.8, CHORD);
|
|
832
910
|
*/
|
|
833
911
|
function arc(x: number, y: number, w: number, h: number, start: number, stop: number, mode?: number): void;
|
|
834
912
|
|
|
@@ -838,6 +916,10 @@ function draw() {
|
|
|
838
916
|
* @param {number} y1 y-coordinate of the first point
|
|
839
917
|
* @param {number} x2 x-coordinate of the second point
|
|
840
918
|
* @param {number} y2 y-coordinate of the second point
|
|
919
|
+
* @example
|
|
920
|
+
createCanvas(200, 100);
|
|
921
|
+
stroke('lime');
|
|
922
|
+
line(20, 20, 180, 80);
|
|
841
923
|
*/
|
|
842
924
|
function line(x1: number, y1: number, x2: number, y2: number): void;
|
|
843
925
|
|
|
@@ -845,6 +927,13 @@ function draw() {
|
|
|
845
927
|
* Draws a point on the canvas.
|
|
846
928
|
* @param {number} x x-coordinate
|
|
847
929
|
* @param {number} y y-coordinate
|
|
930
|
+
* @example
|
|
931
|
+
createCanvas(200, 100);
|
|
932
|
+
stroke('white');
|
|
933
|
+
point(75, 50);
|
|
934
|
+
|
|
935
|
+
strokeWeight(10);
|
|
936
|
+
point(125, 50);
|
|
848
937
|
*/
|
|
849
938
|
function point(x: number, y: number): void;
|
|
850
939
|
|
|
@@ -989,6 +1078,8 @@ function draw() {
|
|
|
989
1078
|
|
|
990
1079
|
/** 🧑🎨
|
|
991
1080
|
* Checks if a given point is within the current path's fill area.
|
|
1081
|
+
*
|
|
1082
|
+
* Not available in q5 WebGPU.
|
|
992
1083
|
* @param {number} x x-coordinate of the point
|
|
993
1084
|
* @param {number} y y-coordinate of the point
|
|
994
1085
|
* @returns {boolean} true if the point is within the fill area, false otherwise
|
|
@@ -997,6 +1088,8 @@ function draw() {
|
|
|
997
1088
|
|
|
998
1089
|
/** 🧑🎨
|
|
999
1090
|
* Checks if a given point is within the current path's stroke.
|
|
1091
|
+
*
|
|
1092
|
+
* Not available in q5 WebGPU.
|
|
1000
1093
|
* @param {number} x x-coordinate of the point
|
|
1001
1094
|
* @param {number} y y-coordinate of the point
|
|
1002
1095
|
* @returns {boolean} true if the point is within the stroke, false otherwise
|
|
@@ -1171,7 +1264,7 @@ function setup() {
|
|
|
1171
1264
|
|
|
1172
1265
|
/** 🌆
|
|
1173
1266
|
* Saves the image.
|
|
1174
|
-
* @param {string} filename filename or
|
|
1267
|
+
* @param {string} filename filename or url
|
|
1175
1268
|
* @param {string} extension file extension
|
|
1176
1269
|
* @param {number} [quality] quality of the saved image
|
|
1177
1270
|
*/
|
|
@@ -1361,13 +1454,12 @@ function setup() {
|
|
|
1361
1454
|
|
|
1362
1455
|
/** ✍️
|
|
1363
1456
|
* Renders text to the screen. Text can be positioned with the x and y
|
|
1364
|
-
* parameters and can optionally be constrained
|
|
1365
|
-
* per line and a line limit.
|
|
1457
|
+
* parameters and can optionally be constrained.
|
|
1366
1458
|
* @param {string} str string of text to display
|
|
1367
1459
|
* @param {number} x x-coordinate of the text's position
|
|
1368
1460
|
* @param {number} y y-coordinate of the text's position
|
|
1369
|
-
* @param {number} [
|
|
1370
|
-
* @param {number} [
|
|
1461
|
+
* @param {number} [wrapWidth] maximum line width in characters
|
|
1462
|
+
* @param {number} [lineLimit] maximum number of lines
|
|
1371
1463
|
* @example
|
|
1372
1464
|
createCanvas(200, 200);
|
|
1373
1465
|
background('silver');
|
|
@@ -1382,8 +1474,9 @@ textSize(20);
|
|
|
1382
1474
|
let info = "q5.js is a JavaScript library for creative coding. It's a sequel to p5.js that's optimized for interactive art.";
|
|
1383
1475
|
|
|
1384
1476
|
text(info, 12, 30, 20, 6);
|
|
1477
|
+
//
|
|
1385
1478
|
*/
|
|
1386
|
-
function text(str: string, x: number, y: number,
|
|
1479
|
+
function text(str: string, x: number, y: number, wrapWidth?: number, lineLimit?: number): void;
|
|
1387
1480
|
|
|
1388
1481
|
/** ✍️
|
|
1389
1482
|
* Loads a font from a URL and optionally runs a callback function
|
|
@@ -1401,8 +1494,8 @@ text(info, 12, 30, 20, 6);
|
|
|
1401
1494
|
* https://q5js.org/fonts/YaHei-msdf.json
|
|
1402
1495
|
* https://q5js.org/fonts/YaHei.png
|
|
1403
1496
|
* @param {string} url uRL of the font to load
|
|
1404
|
-
* @param {(
|
|
1405
|
-
* @returns {
|
|
1497
|
+
* @param {(font: FontFace) => void} [cb] optional callback function that receives the font name as an argument once the font is loaded
|
|
1498
|
+
* @returns {FontFace} font
|
|
1406
1499
|
* @example
|
|
1407
1500
|
createCanvas(200, 200);
|
|
1408
1501
|
|
|
@@ -1414,7 +1507,7 @@ function setup() {
|
|
|
1414
1507
|
text('Hello!', 12, 114);
|
|
1415
1508
|
}
|
|
1416
1509
|
*/
|
|
1417
|
-
function loadFont(url: string, cb?: (
|
|
1510
|
+
function loadFont(url: string, cb?: (font: FontFace) => void): string;
|
|
1418
1511
|
|
|
1419
1512
|
/** ✍️
|
|
1420
1513
|
* Sets the current font to be used for rendering text.
|
|
@@ -1425,7 +1518,7 @@ function setup() {
|
|
|
1425
1518
|
* In q5 c2d, you can set the font to any font accepted in CSS,
|
|
1426
1519
|
* such as "serif" or "monospace".
|
|
1427
1520
|
* https://developer.mozilla.org/docs/Web/CSS/font-family
|
|
1428
|
-
* @param {string} fontName name of the font or
|
|
1521
|
+
* @param {string} fontName name of the font family or a FontFace object
|
|
1429
1522
|
* @example
|
|
1430
1523
|
createCanvas(200, 200);
|
|
1431
1524
|
background(200);
|
|
@@ -1540,15 +1633,13 @@ createCanvas(200, 200);
|
|
|
1540
1633
|
function textDescent(str: string): number;
|
|
1541
1634
|
|
|
1542
1635
|
/** ✍️
|
|
1543
|
-
* Creates an image from a string of text.
|
|
1544
|
-
* will not be the width and height of the text image, but of
|
|
1545
|
-
* the bounding box that the text will be constrained within.
|
|
1636
|
+
* Creates an image from a string of text.
|
|
1546
1637
|
* @param {string} str string of text
|
|
1547
|
-
* @param {number}
|
|
1548
|
-
* @param {number}
|
|
1638
|
+
* @param {number} [wrapWidth] maximum line width in characters
|
|
1639
|
+
* @param {number} [lineLimit] maximum number of lines
|
|
1549
1640
|
* @returns {Q5.Image} an image object representing the rendered text
|
|
1550
1641
|
*/
|
|
1551
|
-
function createTextImage(str: string,
|
|
1642
|
+
function createTextImage(str: string, wrapWidth: number, lineLimit: number): Q5.Image;
|
|
1552
1643
|
|
|
1553
1644
|
/** ✍️
|
|
1554
1645
|
* Renders an image generated from text onto the canvas. The
|
|
@@ -1642,7 +1733,7 @@ createCanvas(200, 200);
|
|
|
1642
1733
|
* as a grayscale value. If only two numeric inputs are provided,
|
|
1643
1734
|
* they will be used as grayscale and alpha values.
|
|
1644
1735
|
*
|
|
1645
|
-
* `fill
|
|
1736
|
+
* [`fill`](https://q5js.org/learn/#fill), [`stroke`](https://q5js.org/learn/#stroke), and [`background`](https://q5js.org/learn/#background) functions can accept the same
|
|
1646
1737
|
* wide range of inputs as this function.
|
|
1647
1738
|
* @param {string | number | Color | number[]} c0 first color component, a CSS color string, a `Color` object (to make copy), or an array of components
|
|
1648
1739
|
* @param {number} [c1] second color component
|
|
@@ -1652,7 +1743,7 @@ createCanvas(200, 200);
|
|
|
1652
1743
|
* @example
|
|
1653
1744
|
createCanvas(200, 200);
|
|
1654
1745
|
|
|
1655
|
-
let c = color(
|
|
1746
|
+
let c = color('gray');
|
|
1656
1747
|
|
|
1657
1748
|
function draw() {
|
|
1658
1749
|
background(c);
|
|
@@ -1811,7 +1902,7 @@ function draw() {
|
|
|
1811
1902
|
* True if the mouse is currently pressed, false otherwise.
|
|
1812
1903
|
* @example
|
|
1813
1904
|
function draw() {
|
|
1814
|
-
if (mouseIsPressed) background(
|
|
1905
|
+
if (mouseIsPressed) background(100);
|
|
1815
1906
|
else background(200);
|
|
1816
1907
|
}
|
|
1817
1908
|
*/
|
|
@@ -1832,7 +1923,7 @@ function draw() {
|
|
|
1832
1923
|
* True if a key is currently pressed, false otherwise.
|
|
1833
1924
|
* @example
|
|
1834
1925
|
function draw() {
|
|
1835
|
-
if (keyIsPressed) background(
|
|
1926
|
+
if (keyIsPressed) background(100);
|
|
1836
1927
|
else background(200);
|
|
1837
1928
|
}
|
|
1838
1929
|
*/
|
|
@@ -1854,13 +1945,8 @@ function draw() {
|
|
|
1854
1945
|
function keyIsDown(key: string): boolean;
|
|
1855
1946
|
|
|
1856
1947
|
/** 🖲️
|
|
1857
|
-
*
|
|
1858
|
-
*
|
|
1859
|
-
*/
|
|
1860
|
-
let keyCode: number;
|
|
1861
|
-
|
|
1862
|
-
/** 🖲️
|
|
1863
|
-
* Array of current touches, each touch being an object with x, y, id, etc.
|
|
1948
|
+
* Array of current touches, each touch being an object with
|
|
1949
|
+
* id, x, and y properties.
|
|
1864
1950
|
*/
|
|
1865
1951
|
let touches: any[];
|
|
1866
1952
|
|
|
@@ -1868,7 +1954,7 @@ function draw() {
|
|
|
1868
1954
|
* Sets the cursor to a [CSS cursor type](https://developer.mozilla.org/docs/Web/CSS/cursor) or image.
|
|
1869
1955
|
* If an image is provided, optional x and y coordinates can
|
|
1870
1956
|
* specify the active point of the cursor.
|
|
1871
|
-
* @param {string} name name of the cursor or the
|
|
1957
|
+
* @param {string} name name of the cursor or the url to an image
|
|
1872
1958
|
* @param {number} [x] x-coordinate of the cursor's hot spot
|
|
1873
1959
|
* @param {number} [y] y-coordinate of the cursor's hot spot
|
|
1874
1960
|
*/
|
|
@@ -2080,7 +2166,7 @@ noCursor();
|
|
|
2080
2166
|
* For backwards compatibility with the p5.sound API, the functions
|
|
2081
2167
|
* `setVolume`, `setLoop`, `setPan`, `isLoaded`, and `isPlaying`
|
|
2082
2168
|
* are also implemented, but their use is deprecated.
|
|
2083
|
-
* @param {string} url
|
|
2169
|
+
* @param {string} url sound file
|
|
2084
2170
|
* @returns {Sound} a new `Sound` object
|
|
2085
2171
|
* @example
|
|
2086
2172
|
createCanvas(200, 200);
|
|
@@ -2095,14 +2181,23 @@ function mousePressed() {
|
|
|
2095
2181
|
function loadSound(url: string): Sound;
|
|
2096
2182
|
|
|
2097
2183
|
/**
|
|
2098
|
-
* Loads audio data from a file and returns an HTMLAudioElement.
|
|
2099
|
-
* https://developer.mozilla.org/docs/Web/API/HTMLMediaElement
|
|
2184
|
+
* Loads audio data from a file and returns an [HTMLAudioElement](https://developer.mozilla.org/docs/Web/API/HTMLMediaElement).
|
|
2100
2185
|
*
|
|
2101
|
-
* Audio is considered loaded when the canplaythrough event is fired.
|
|
2186
|
+
* Audio is considered loaded when the [canplaythrough event](https://developer.mozilla.org/docs/Web/API/HTMLMediaElement/canplaythrough_event) is fired.
|
|
2102
2187
|
*
|
|
2103
2188
|
* Note that audio can only be played after the first user
|
|
2104
2189
|
* interaction with the page!
|
|
2105
|
-
* @param url
|
|
2190
|
+
* @param url audio file
|
|
2191
|
+
* @returns {HTMLAudioElement} an HTMLAudioElement
|
|
2192
|
+
* @example
|
|
2193
|
+
createCanvas(200, 200);
|
|
2194
|
+
|
|
2195
|
+
let audio = loadAudio('/assets/retro.flac');
|
|
2196
|
+
audio.volume = 0.4;
|
|
2197
|
+
|
|
2198
|
+
function mousePressed() {
|
|
2199
|
+
audio.play();
|
|
2200
|
+
}
|
|
2106
2201
|
*/
|
|
2107
2202
|
function loadAudio(url: string): Audio;
|
|
2108
2203
|
|
|
@@ -2121,36 +2216,131 @@ function mousePressed() {
|
|
|
2121
2216
|
class Sound {
|
|
2122
2217
|
/** 🔊
|
|
2123
2218
|
* Creates a new `Q5.Sound` object.
|
|
2219
|
+
*/
|
|
2220
|
+
constructor();
|
|
2221
|
+
|
|
2222
|
+
/** 🔊
|
|
2223
|
+
* Set the sound's volume to a value between
|
|
2224
|
+
* 0 (silent) and 1 (full volume).
|
|
2225
|
+
*/
|
|
2226
|
+
volume: number;
|
|
2227
|
+
|
|
2228
|
+
/** 🔊
|
|
2229
|
+
* Set the sound's stereo position between -1 (left) and 1 (right).
|
|
2230
|
+
*/
|
|
2231
|
+
pan: number;
|
|
2232
|
+
|
|
2233
|
+
/** 🔊
|
|
2234
|
+
* Set to true to make the sound loop continuously.
|
|
2235
|
+
*/
|
|
2236
|
+
loop: boolean;
|
|
2237
|
+
|
|
2238
|
+
/** 🔊
|
|
2239
|
+
* True if the sound data has finished loading.
|
|
2240
|
+
*/
|
|
2241
|
+
loaded: boolean;
|
|
2242
|
+
|
|
2243
|
+
/** 🔊
|
|
2244
|
+
* True if the sound is currently paused.
|
|
2245
|
+
*/
|
|
2246
|
+
paused: boolean;
|
|
2247
|
+
|
|
2248
|
+
/** 🔊
|
|
2249
|
+
* True if the sound has finished playing.
|
|
2250
|
+
*/
|
|
2251
|
+
ended: boolean;
|
|
2252
|
+
|
|
2253
|
+
/** 🔊
|
|
2254
|
+
* Plays the sound.
|
|
2255
|
+
*
|
|
2256
|
+
* If this function is run when the sound is already playing,
|
|
2257
|
+
* a new playback will start, causing a layering effect.
|
|
2124
2258
|
*
|
|
2125
|
-
*
|
|
2126
|
-
*
|
|
2259
|
+
* If this function is run when the sound is paused,
|
|
2260
|
+
* all playback instances will be resumed.
|
|
2127
2261
|
*/
|
|
2128
|
-
|
|
2262
|
+
play(): void;
|
|
2263
|
+
|
|
2264
|
+
/** 🔊
|
|
2265
|
+
* Pauses the sound, allowing it to be resumed.
|
|
2266
|
+
*/
|
|
2267
|
+
pause(): void;
|
|
2268
|
+
|
|
2269
|
+
/** 🔊
|
|
2270
|
+
* Stops the sound, resetting its playback position
|
|
2271
|
+
* to the beginning.
|
|
2272
|
+
*
|
|
2273
|
+
* Removes all playback instances.
|
|
2274
|
+
*/
|
|
2275
|
+
stop(): void;
|
|
2129
2276
|
}
|
|
2130
2277
|
|
|
2131
2278
|
// 🛠️ utilities
|
|
2132
2279
|
|
|
2133
2280
|
/** 🛠️
|
|
2134
|
-
* Loads a
|
|
2135
|
-
*
|
|
2281
|
+
* Loads a file or multiple files.
|
|
2282
|
+
*
|
|
2283
|
+
* File type is determined by file extension. q5 supports loading
|
|
2284
|
+
* text, json, csv, font, audio, and image files.
|
|
2285
|
+
*
|
|
2286
|
+
* To load many files, it may be easier to use load* functions,
|
|
2287
|
+
* like `loadImage`, with q5's preload system.
|
|
2288
|
+
* @param {...string} urls
|
|
2289
|
+
* @returns {Promise<any[]>} a promise that resolves with objects
|
|
2290
|
+
* @example
|
|
2291
|
+
let logo;
|
|
2292
|
+
|
|
2293
|
+
async function setup() {
|
|
2294
|
+
logo = await load('/q5js_logo.webp');
|
|
2295
|
+
}
|
|
2296
|
+
|
|
2297
|
+
function draw() {
|
|
2298
|
+
image(logo, 0, 0, 200, 200);
|
|
2299
|
+
}
|
|
2300
|
+
* @example
|
|
2301
|
+
new Q5();
|
|
2302
|
+
createCanvas(200, 200);
|
|
2303
|
+
|
|
2304
|
+
// use with top level await in a module
|
|
2305
|
+
await load('/assets/Robotica.ttf');
|
|
2306
|
+
|
|
2307
|
+
background(255);
|
|
2308
|
+
text('Hello, world!', 20, 100);
|
|
2309
|
+
* @example
|
|
2310
|
+
let q = new Q5();
|
|
2311
|
+
createCanvas(200, 200);
|
|
2312
|
+
|
|
2313
|
+
let [jump, retro] = await load(
|
|
2314
|
+
'/assets/jump.wav', '/assets/retro.flac'
|
|
2315
|
+
);
|
|
2316
|
+
|
|
2317
|
+
q.mousePressed = () => {
|
|
2318
|
+
mouseButton == 'left' ? jump.play() : retro.play();
|
|
2319
|
+
};
|
|
2320
|
+
*/
|
|
2321
|
+
function load(...urls: string[]): Promise<any[]>;
|
|
2322
|
+
|
|
2323
|
+
/** 🛠️
|
|
2324
|
+
* Loads a text file from the specified url. Result is one string.
|
|
2325
|
+
* @param {string} url text file
|
|
2136
2326
|
* @param {(result: string) => void} cb a callback function that is run when the file is loaded
|
|
2137
2327
|
*/
|
|
2138
|
-
function loadText(
|
|
2328
|
+
function loadText(url: string, cb: (result: string) => void): void;
|
|
2139
2329
|
|
|
2140
2330
|
/** 🛠️
|
|
2141
|
-
* Loads a JSON file from the specified
|
|
2331
|
+
* Loads a JSON file from the specified url. Result depends on the
|
|
2142
2332
|
* JSON file's contents, but is typically an object or array.
|
|
2143
|
-
* @param {string}
|
|
2333
|
+
* @param {string} url JSON file
|
|
2144
2334
|
* @param {(result: any) => void} cb a callback function that is run when the file is loaded
|
|
2145
2335
|
*/
|
|
2146
|
-
function loadJSON(
|
|
2336
|
+
function loadJSON(url: string, cb: (result: any) => void): void;
|
|
2147
2337
|
|
|
2148
2338
|
/** 🛠️
|
|
2149
|
-
* Loads a CSV file from the specified
|
|
2150
|
-
* @param {string}
|
|
2339
|
+
* Loads a CSV file from the specified url. Result is an array of objects.
|
|
2340
|
+
* @param {string} url CSV file
|
|
2151
2341
|
* @param {(result: object[]) => void} cb a callback function that is run when the file is loaded
|
|
2152
2342
|
*/
|
|
2153
|
-
function loadCSV(
|
|
2343
|
+
function loadCSV(url: string, cb: (result: object[]) => void): void;
|
|
2154
2344
|
|
|
2155
2345
|
/** 🛠️
|
|
2156
2346
|
* Stores an item in localStorage.
|