q5 2.20.10 → 2.21.0

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 (6) hide show
  1. package/README.md +4 -4
  2. package/deno.json +1 -1
  3. package/package.json +3 -3
  4. package/q5.d.ts +1141 -920
  5. package/q5.js +261 -136
  6. package/q5.min.js +2 -2
package/q5.d.ts CHANGED
@@ -848,6 +848,173 @@ rect(20, 20, 60, 60);
848
848
  */
849
849
  var drawingContext: CanvasRenderingContext2D;
850
850
 
851
+ // 🎨 color
852
+
853
+ /** 🎨
854
+ * Creates a new `Color` object, which is primarily useful for storing
855
+ * a color that your sketch will reuse or modify later.
856
+ *
857
+ * With the default RGB color mode, colors have these components:
858
+ *
859
+ * `r`/`red`, `g`/`green`, `b`/`blue`, and `a`/`alpha`.
860
+ *
861
+ * The default color format is integer, so set color components
862
+ * to values between 0 and 255.
863
+ *
864
+ * In q5 WebGPU, the default color mode is RGB in float format, so
865
+ * set color components to values between 0 and 1.
866
+ *
867
+ * The [`fill`](https://q5js.org/learn/#fill), [`stroke`](https://q5js.org/learn/#stroke), and [`background`](https://q5js.org/learn/#background) functions
868
+ * accept the same wide range of color representations as this function.
869
+ *
870
+ * Here are some examples of valid use:
871
+ *
872
+ * - `color(255)` (grayscale)
873
+ * - `color(255, 200)` (grayscale, alpha)
874
+ * - `color(255, 0, 0)` (r, g, b)
875
+ * - `color(255, 0, 0, 10)` (r, g, b, a)
876
+ * - `color('red')` (colorName)
877
+ * - `color('#ff0000')` (hexColor)
878
+ * - `color([255, 0, 0])` (colorComponents)
879
+ * @param {string | number | Color | number[]} c0 color or first color component
880
+ * @param {number} [c1] second color component
881
+ * @param {number} [c2] third color component
882
+ * @param {number} [c3] fourth color component (alpha)
883
+ * @returns {Color} a new `Color` object
884
+ * @example
885
+ createCanvas(200);
886
+ rect(0, 0, 100, 200);
887
+
888
+ // ( r, g, b, a)
889
+ let bottle = color(90, 100, 255, 100);
890
+ fill(bottle);
891
+ stroke(bottle);
892
+ strokeWeight(30);
893
+ circle(100, 100, 155);
894
+ * @example
895
+ createCanvas(200);
896
+ // (gray, alpha)
897
+ let c = color(200, 50);
898
+
899
+ function draw() {
900
+ background(c);
901
+ circle(mouseX, mouseY, 50);
902
+ c.g = (c.g + 1) % 255;
903
+ }
904
+ */
905
+ function color(c0: string | number | Color | number[], c1?: number, c2?: number, c3?: number): Color;
906
+
907
+ /** 🎨
908
+ * Sets the color mode for the sketch. Changes the type of color object created by color functions.
909
+ *
910
+ * In c2d, the default color mode is RGB in legacy integer format.
911
+ *
912
+ * In WebGPU, the default color mode is RGB in float format.
913
+ *
914
+ * See the documentation for q5's color constants below for more info.
915
+ * @param {'rgb' | 'srgb' | 'oklch'} mode color mode
916
+ * @param {1 | 255} format color format (1 for float, 255 for integer)
917
+ * @example
918
+ createCanvas(200);
919
+
920
+ colorMode(RGB, 1);
921
+ fill(1, 0, 0);
922
+ rect(0, 0, 66, 200);
923
+ fill(0, 1, 0);
924
+ rect(66, 0, 67, 200);
925
+ fill(0, 0, 1);
926
+ rect(133, 0, 67, 200);
927
+ * @example
928
+ createCanvas(200);
929
+
930
+ colorMode(OKLCH);
931
+
932
+ fill(0.25, 0.15, 0);
933
+ rect(0, 0, 100, 200);
934
+
935
+ fill(0.75, 0.15, 0)
936
+ rect(100, 0, 100, 200);
937
+ */
938
+ function colorMode(mode: 'rgb' | 'srgb' | 'oklch', format: 1 | 255): void;
939
+
940
+ /** 🎨
941
+ * RGB colors have components `r`/`red`, `g`/`green`, `b`/`blue`,
942
+ * and `a`/`alpha`.
943
+ *
944
+ * By default when a canvas is using the HDR `display-p3` color space,
945
+ * rgb colors are mapped to the full P3 gamut, even when they use the
946
+ * legacy integer 0-255 format.
947
+ * @example
948
+ createCanvas(200, 100);
949
+
950
+ colorMode(RGB, 255);
951
+
952
+ background(255, 0, 0);
953
+ */
954
+ const RGB: 'rgb';
955
+
956
+ /** 🎨
957
+ * This color mode limits the gamut of rgb colors to sRGB.
958
+ *
959
+ * If your display is HDR capable, take a look at the following
960
+ * example, note that full red appears less saturated, as it would
961
+ * on an SDR display.
962
+ * @example
963
+ createCanvas(200, 100);
964
+
965
+ colorMode(SRGB, 255);
966
+
967
+ background(255, 0, 0);
968
+ */
969
+ const SRGB: 'srgb';
970
+
971
+ /** 🎨
972
+ * OKLCH colors have components `l`/`lightness`, `c`/`chroma`,
973
+ * `h`/`hue`, and `a`/`alpha`.
974
+ *
975
+ * You may be familiar with the outdated HSL/HSV color formats,
976
+ * which were created in the 1970s to be more intuitive for humans
977
+ * to work with than RGB. But due to technical limitations of that
978
+ * time, they're not perceptually uniform, meaning colors at the same
979
+ * brightness values may appear lighter or darker depending on the hue.
980
+ *
981
+ * The OKLCH format is similar to HSL/HSV but it's perceptually
982
+ * uniform and supports HDR colors. Use this oklch color picker to
983
+ * explore the color space: https://oklch.com
984
+ *
985
+ * `lightness`: 0 to 1
986
+ *
987
+ * `chroma`: 0 to 0.3
988
+ *
989
+ * `hue`: 0 to 360
990
+ *
991
+ * `alpha`: 0 to 1
992
+ *
993
+ * Note how seamless the hue transitions are in the following example.
994
+ * @example
995
+ createCanvas(200);
996
+ colorMode(OKLCH);
997
+
998
+ function draw() {
999
+ background(0.7, 0.16, frameCount % 360);
1000
+ }
1001
+ */
1002
+ const OKLCH: 'oklch';
1003
+
1004
+ class Color {
1005
+ /** 🎨
1006
+ * Use the `color` function for greater flexibility, it runs
1007
+ * this constructor internally.
1008
+ *
1009
+ * This constructor only accepts 4 numbers, which are the color
1010
+ * components.
1011
+ *
1012
+ * `Color` is not actually a class itself, it's a reference to a
1013
+ * Q5 color class based on the color mode and format.
1014
+ */
1015
+ constructor(c0: number, c1: number, c2: number, c3: number);
1016
+ }
1017
+
851
1018
  // 💻 display
852
1019
 
853
1020
  /** 💻
@@ -869,7 +1036,7 @@ circle(25, 12.5, 16);
869
1036
  */
870
1037
  function displayMode(mode: string, renderQuality: string, scale: string | number): void;
871
1038
 
872
- // 🧑‍🎨 drawing
1039
+ // 🧑‍🎨 shapes
873
1040
 
874
1041
  /** 🧑‍🎨
875
1042
  * Draws over the entire canvas with a color or image.
@@ -1164,663 +1331,332 @@ point(125, 50);
1164
1331
  */
1165
1332
  function inStroke(x: number, y: number): boolean;
1166
1333
 
1167
- // 📑 dom
1168
-
1169
- /** 📑
1170
- * The Document Object Model (DOM) is an interface for
1171
- * creating and editing web pages with JavaScript.
1172
- */
1334
+ // 🌆 image
1173
1335
 
1174
- /** 📑
1175
- * Creates a new HTML element and adds it to the page. `createEl` is
1176
- * an alias.
1177
- *
1178
- * Modify the element's CSS [`style`](https://developer.mozilla.org/docs/Web/API/HTMLElement/style) to change its appearance.
1179
- *
1180
- * Use [`addEventListener`](https://developer.mozilla.org/docs/Web/API/EventTarget/addEventListener) to respond to events such as:
1181
- * - "click": when the element is clicked
1182
- * - "mouseover": when the mouse hovers over the element
1183
- * - "mouseout": when the mouse stops hovering over the element
1184
- * - "input": when a form element's value changes
1185
- *
1186
- * q5 adds some extra functionality to the elements it creates:
1187
- *
1188
- * - the `position` function makes it easy to place the element
1189
- * relative to the canvas
1190
- * - the `size` function sets the width and height of the element
1191
- * - alternatively, use the element's `x`, `y`, `width`, and `height` properties
1192
- * @param {string} tag tag name of the element
1193
- * @param {string} [content] content of the element
1194
- * @returns {HTMLElement} element
1336
+ /** 🌆
1337
+ * Loads an image from a URL and optionally runs a callback function.
1338
+ * @param {string} url url of the image to load
1339
+ * @param {(img: any) => void} [cb] callback function after the image is loaded
1340
+ * @param {any} [opt] optional parameters for loading the image
1341
+ * @returns {Q5.Image} image
1195
1342
  * @example
1196
1343
  createCanvas(200);
1197
1344
 
1198
- let el = createEl('div', '*');
1199
- el.position(50, 50);
1200
- el.size(100, 100);
1201
- el.style.fontSize = '136px';
1202
- el.style.textAlign = 'center';
1203
- el.style.backgroundColor = 'blue';
1204
- el.style.color = 'white';
1345
+ let logo = loadImage('/q5js_logo.webp');
1346
+
1347
+ function draw() {
1348
+ image(logo, 0, 0, 200, 200);
1349
+ }
1205
1350
  */
1206
- function createElement(tag: string, content?: string): HTMLElement;
1351
+ function loadImage(url: string, cb?: (img: any) => void, opt?: any): Q5.Image;
1207
1352
 
1208
- /** 📑
1209
- * Creates a link element.
1210
- * @param {string} href url
1211
- * @param {string} [text] text content
1212
- * @param {boolean} [newTab] whether to open the link in a new tab
1353
+ /** 🌆
1354
+ * Draws an image to the canvas.
1355
+ * @param {any} img image to draw
1356
+ * @param {number} dx x position to draw the image at
1357
+ * @param {number} dy y position to draw the image at
1358
+ * @param {number} [dw] width of the destination image
1359
+ * @param {number} [dh] height of the destination image
1360
+ * @param {number} [sx] x position in the source to start clipping a subsection from
1361
+ * @param {number} [sy] y position in the source to start clipping a subsection from
1362
+ * @param {number} [sw] width of the subsection of the source image
1363
+ * @param {number} [sh] height of the subsection of the source image
1213
1364
  * @example
1214
1365
  createCanvas(200);
1215
1366
 
1216
- let link = createA('https://q5js.org', 'q5.js');
1217
- link.position(16, 42);
1218
- link.style.fontSize = '80px';
1367
+ let logo = loadImage('/q5js_logo.webp');
1219
1368
 
1220
- link.addEventListener('mouseover', () => {
1221
- background('blue');
1222
- });
1369
+ function draw() {
1370
+ image(logo, 0, 0, 200, 200, 256, 256, 512, 512);
1371
+ }
1223
1372
  */
1224
- function createA(href: string, text?: string): HTMLAnchorElement;
1373
+ function image(img: any, dx: number, dy: number, dw?: number, dh?: number, sx?: number, sy?: number, sw?: number, sh?: number): void;
1225
1374
 
1226
- /** 📑
1227
- * Creates a button element.
1228
- * @param {string} [content] text content
1375
+ /** 🌆
1376
+ * Sets the image mode, which determines the position and alignment of images drawn on the canvas.
1377
+ * `CORNER`: (default) images will be drawn from the top-left corner
1378
+ * `CORNERS`: images will be drawn from the top-left to the bottom-right corner
1379
+ * `CENTER`: images will be drawn centered at (dx, dy)
1380
+ * @param {string} mode
1229
1381
  * @example
1230
- createCanvas(200, 100);
1382
+ createCanvas(200);
1231
1383
 
1232
- let btn = createButton('Click me!');
1384
+ let logo = loadImage('/q5js_logo.webp');
1233
1385
 
1234
- btn.addEventListener('click', () => {
1235
- background(random(100, 255));
1236
- });
1386
+ function draw() {
1387
+ imageMode(CENTER);
1388
+ image(logo, 100, 100, 200, 200);
1389
+ }
1237
1390
  */
1238
- function createButton(content?: string): HTMLButtonElement;
1391
+ function imageMode(mode: string): void;
1239
1392
 
1240
- /** 📑
1241
- * Creates a checkbox element.
1393
+ /** 🌆
1394
+ * Sets the default image scale, which is applied to images when
1395
+ * they are drawn without a specified width or height.
1242
1396
  *
1243
- * Use the `checked` property to get or set the checkbox's state.
1397
+ * By default it is 0.5 so images appear at their actual size
1398
+ * when pixel density is 2. Images will be drawn at a consistent
1399
+ * default size relative to the canvas regardless of pixel density.
1244
1400
  *
1245
- * The `label` property is the text label element next to the checkbox.
1246
- * @param {string} [label] text label placed next to the checkbox
1247
- * @param {boolean} [checked] initial state
1401
+ * This function must be called before images are loaded to
1402
+ * have an effect.
1403
+ * @param {number} scale
1404
+ * @returns {number} default image scale
1405
+ */
1406
+ function defaultImageScale(scale: number): number;
1407
+
1408
+ /** 🌆
1409
+ * Resizes the image.
1410
+ * @param {number} w new width
1411
+ * @param {number} h new height
1248
1412
  * @example
1249
- createCanvas(200, 100);
1413
+ createCanvas(200);
1250
1414
 
1251
- let box = createCheckbox('Check me!');
1252
- box.label.style.color = 'lime';
1415
+ let logo = loadImage('/q5js_logo.webp');
1253
1416
 
1254
- box.addEventListener('input', () => {
1255
- if (box.checked) background('lime');
1256
- else background('black');
1257
- });
1417
+ function setup() {
1418
+ logo.resize(128, 128);
1419
+ image(logo, 0, 0, 200, 200);
1420
+ }
1258
1421
  */
1259
- function createCheckbox(label?: string, checked?: boolean): HTMLInputElement;
1422
+ function resize(w: number, h: number): void;
1260
1423
 
1261
- /** 📑
1262
- * Creates a color input element.
1263
- *
1264
- * Use the `value` property to get or set the color value.
1265
- * @param {string} [value] initial color value
1424
+ /** 🌆
1425
+ * Returns a trimmed image, cropping out transparent pixels from the edges.
1426
+ * @returns {Image}
1427
+ */
1428
+ function trim(): Q5.Image;
1429
+
1430
+ /** 🌆
1431
+ * Enables smooth rendering of images displayed larger than
1432
+ * their actual size. This is the default setting, so running this
1433
+ * function only has an effect if `noSmooth` has been called.
1266
1434
  * @example
1267
- createCanvas(200, 100);
1435
+ createCanvas(200);
1268
1436
 
1269
- let picker = createColorPicker();
1270
- picker.value = '#fd7575';
1437
+ let icon = loadImage('/q5js_icon.png');
1271
1438
 
1272
- function draw() {
1273
- background(picker.value);
1439
+ function setup() {
1440
+ image(icon, 0, 0, 200, 200);
1274
1441
  }
1275
1442
  */
1276
- function createColorPicker(value?: string): HTMLInputElement;
1443
+ function smooth(): void;
1277
1444
 
1278
- /** 📑
1279
- * Creates an image element.
1280
- * @param {string} src url of the image
1445
+ /** 🌆
1446
+ * Disables smooth image rendering for a pixelated look.
1281
1447
  * @example
1282
- createCanvas(200, 100);
1448
+ createCanvas(200);
1283
1449
 
1284
- let img = createImg('/assets/p5play_logo.webp')
1285
- .position(0, 0)
1286
- .size(100, 100);
1450
+ let icon = loadImage('/q5js_icon.png');
1451
+
1452
+ function setup() {
1453
+ noSmooth();
1454
+ image(icon, 0, 0, 200, 200);
1455
+ }
1287
1456
  */
1288
- function createImg(src: string): HTMLImageElement;
1457
+ function noSmooth(): void;
1289
1458
 
1290
- /** 📑
1291
- * Creates an input element.
1459
+ /** 🌆
1460
+ * Applies a tint (color overlay) to the drawing.
1292
1461
  *
1293
- * Use the `value` property to get or set the input's value.
1462
+ * The alpha value of the tint color determines the
1463
+ * strength of the tint. To change an image's opacity,
1464
+ * use the `opacity` function.
1294
1465
  *
1295
- * Use the `placeholder` property to set label text that appears
1296
- * inside the input when it's empty.
1466
+ * Tinting affects all subsequent images drawn. The tint
1467
+ * color is applied to images using the "multiply" blend mode.
1297
1468
  *
1298
- * See MDN's [input documentation](https://developer.mozilla.org/docs/Web/HTML/Element/input#input_types) for the full list of input types.
1299
- * @param {string} [value] initial value
1300
- * @param {string} [type] text input type, can be 'text', 'password', 'email', 'number', 'range', 'search', 'tel', 'url'
1469
+ * Since the tinting process is performance intensive, each time
1470
+ * an image is tinted, q5 caches the result. `image` will draw the
1471
+ * cached tinted image unless the tint color has changed or the
1472
+ * image being tinted was edited.
1473
+ *
1474
+ * If you need to draw an image multiple times each frame with
1475
+ * different tints, consider making copies of the image and tinting
1476
+ * each copy separately.
1477
+ * @param {string | number} color tint color
1301
1478
  * @example
1302
- createCanvas(200, 100);
1303
- textSize(64);
1479
+ createCanvas(200);
1304
1480
 
1305
- let input = createInput();
1306
- input.placeholder = 'Type here!';
1307
- input.size(200, 32);
1481
+ let logo = loadImage('/q5js_logo.webp');
1308
1482
 
1309
- input.addEventListener('input', () => {
1310
- background('orange');
1311
- text(input.value, 10, 70);
1312
- });
1483
+ function setup() {
1484
+ tint(255, 0, 0, 128);
1485
+ image(logo, 0, 0, 200, 200);
1486
+ }
1313
1487
  */
1314
- function createInput(value?: string, type?: string): HTMLInputElement;
1488
+ function tint(color: string | number): void;
1315
1489
 
1316
- /** 📑
1317
- * Creates a paragraph element.
1318
- * @param {string} [content] text content
1319
- * @example
1320
- createCanvas(200, 50);
1321
- background('coral');
1490
+ /** 🌆
1491
+ * Images drawn after this function is run will not be tinted.
1492
+ */
1493
+ function noTint(): void;
1322
1494
 
1323
- let p = createP('Hello, world!');
1324
- p.style.color = 'pink';
1495
+ /** 🌆
1496
+ * Masks the image with another image.
1497
+ * @param {Q5.Image} img image to use as a mask
1325
1498
  */
1326
- function createP(content?: string): HTMLParagraphElement;
1499
+ function mask(img: Q5.Image): void;
1327
1500
 
1328
- /** 📑
1329
- * Creates a radio button group.
1330
- *
1331
- * Use the `option(label, value)` function to add new radio buttons
1332
- * to the group.
1333
- *
1334
- * Use the `value` property to get or set the value of the selected radio button.
1335
- * @param {string} [groupName]
1501
+ /** 🌆
1502
+ * Retrieves a subsection of an image or canvas, as a q5 Image.
1503
+ * Or if width and height are both 1, returns the color of the pixel at the given coordinates in `[R, G, B, A]` array format.
1504
+ * @param {number} x
1505
+ * @param {number} y
1506
+ * @param {number} [w] width of the area
1507
+ * @param {number} [h] height of the area
1508
+ * @returns {Image | number[]}
1336
1509
  * @example
1337
- createCanvas(200, 100);
1510
+ createCanvas(200);
1338
1511
 
1339
- let radio = createRadio()
1340
- .option('square', '1')
1341
- .option('circle', '2');
1512
+ let logo = loadImage('/q5js_logo.webp');
1342
1513
 
1343
- function draw() {
1344
- background(200);
1345
- if (radio.value == '1') square(75, 25, 50);
1346
- if (radio.value == '2') circle(100, 50, 50);
1514
+ function setup() {
1515
+ let cropped = logo.get(256, 256, 512, 512);
1516
+ image(cropped, 0, 0, 200, 200);
1347
1517
  }
1348
1518
  */
1349
- function createRadio(groupName): HTMLDivElement;
1519
+ function get(x: number, y: number, w?: number, h?: number): Q5.Image | number[];
1350
1520
 
1351
- /** 📑
1352
- * Creates a select element.
1353
- *
1354
- * Use the `option(label, value)` function to add new options to
1355
- * the select element.
1356
- *
1357
- * Set `multiple` to `true` to allow multiple options to be selected.
1521
+ /** 🌆
1522
+ * Sets a pixel's color in the image or canvas.
1358
1523
  *
1359
- * Use the `value` property to get or set the selected option value.
1524
+ * Or if a canvas or image is provided, it's drawn on top of the
1525
+ * destination image or canvas, ignoring its tint setting.
1360
1526
  *
1361
- * Use the `selected` property get the labels of the selected
1362
- * options or set the selected options by label. Can be a single
1363
- * string or an array of strings.
1364
- * @param {string} [placeholder] optional placeholder text that appears before an option is selected
1527
+ * Run `updatePixels` to apply the changes.
1528
+ * @param {number} x
1529
+ * @param {number} y
1530
+ * @param {any} c color, canvas, or image
1365
1531
  * @example
1366
- createCanvas(200, 100);
1532
+ createCanvas(200);
1533
+ let c = color('lime');
1367
1534
 
1368
- let sel = createSelect('Select a color')
1369
- .option('Red', '#f55')
1370
- .option('Green', '#5f5');
1535
+ function draw() {
1536
+ set(randomX(), randomY(), c);
1537
+ updatePixels();
1538
+ }
1539
+ */
1540
+ function set(x: number, y: number, c: any): void;
1371
1541
 
1372
- sel.addEventListener('change', () => {
1373
- background(sel.value);
1374
- });
1542
+ /** 🌆
1543
+ * Returns a copy of the image.
1544
+ * @returns {Q5.Image}
1375
1545
  */
1376
- function createSelect(placeholder): HTMLSelectElement;
1546
+ function copy(): Q5.Image;
1377
1547
 
1378
- /** 📑
1379
- * Creates a slider element.
1380
- *
1381
- * Use the `value` property to get or set the slider's value.
1382
- *
1383
- * Use the `val` function to get the slider's value as a number.
1384
- * @param {number} min minimum value
1385
- * @param {number} max maximum value
1386
- * @param {number} [value] initial value
1387
- * @param {number} [step] step size
1548
+ /** 🌆
1549
+ * Displays a region of the image on another region of the image.
1550
+ * Can be used to create a detail inset, aka a magnifying glass effect.
1551
+ * @param {number} sx x-coordinate of the source region
1552
+ * @param {number} sy y-coordinate of the source region
1553
+ * @param {number} sw width of the source region
1554
+ * @param {number} sh height of the source region
1555
+ * @param {number} dx x-coordinate of the destination region
1556
+ * @param {number} dy y-coordinate of the destination region
1557
+ * @param {number} dw width of the destination region
1558
+ * @param {number} dh height of the destination region
1388
1559
  * @example
1389
1560
  createCanvas(200);
1390
1561
 
1391
- let slider = createSlider(0, 255)
1392
- .position(10, 10)
1393
- .size(180);
1562
+ let logo = loadImage('/q5js_logo.webp');
1394
1563
 
1395
- function draw() {
1396
- background(slider.val());
1564
+ function setup() {
1565
+ logo.inset(256, 256, 512, 512, 0, 0, 256, 256);
1566
+ image(logo, 0, 0, 200, 200);
1397
1567
  }
1398
1568
  */
1399
- function createSlider(min: number, max: number, value?: number, step?: number): HTMLInputElement;
1400
-
1401
- /** 📑
1402
- * Creates a video element.
1403
- *
1404
- * Note that videos must be muted to autoplay and the `play` and
1405
- * `pause` functions can only be run after a user interaction.
1406
- *
1407
- * The video element can be hidden and its content can be
1408
- * displayed on the canvas using the `image` function.
1409
- * @param {string} src url of the video
1410
- * @example
1411
- createCanvas(0);
1412
-
1413
- let vid = createVideo('/assets/apollo4.mp4');
1414
- vid.size(200, 150);
1415
- vid.autoplay = vid.muted = vid.loop = true;
1416
- vid.controls = true;
1417
-
1418
- * @example
1419
- createCanvas(200, 150);
1420
- let vid = createVideo('/assets/apollo4.mp4');
1421
- vid.hide();
1422
-
1423
- function mousePressed() {
1424
- vid.currentTime = 0;
1425
- vid.play();
1426
- }
1427
- function draw() {
1428
- image(vid, 0, 0, 200, 150);
1429
- filter(HUE_ROTATE, 90);
1430
- }
1431
- */
1432
- function createVideo(src: string): HTMLVideoElement;
1433
-
1434
- /** 📑
1435
- * Creates a capture from a connected camera, such as a webcam.
1436
- *
1437
- * The capture video element can be hidden and its content can be
1438
- * displayed on the canvas using the `image` function.
1439
- *
1440
- * Can preload to ensure the capture is ready to use when your
1441
- * sketch starts.
1442
- *
1443
- * Requests the highest video resolution from the user facing camera
1444
- * by default. The first parameter to this function can be used to
1445
- * specify the constraints for the capture. See [`getUserMedia`](https://developer.mozilla.org/docs/Web/API/MediaDevices/getUserMedia)
1446
- * for more info.
1447
- * @param {string} [type] type of capture, can be only `VIDEO` or only `AUDIO`, the default is to use both video and audio
1448
- * @param {boolean} [flipped] whether to mirror the video horizontally, true by default
1449
- * @param {(vid: HTMLVideoElement) => void} [cb] callback function after the capture is created
1450
- * @example
1451
- createCanvas(200);
1452
-
1453
- function mousePressed() {
1454
- let cap = createCapture(VIDEO);
1455
- cap.size(200, 112.5);
1456
- canvas.remove();
1457
- }
1458
- * @example
1459
- createCanvas(200);
1460
-
1461
- let cap;
1462
- function mousePressed() {
1463
- cap = createCapture(VIDEO);
1464
- cap.hide();
1465
- }
1466
-
1467
- function draw() {
1468
- let y = frameCount % height;
1469
- image(cap, 0, y, 200, 200);
1470
- }
1471
- * @example
1472
- createCanvas(200);
1473
-
1474
- function mousePressed() {
1475
- let cap = createCapture({
1476
- video: { width: 640, height: 480 }
1477
- });
1478
- cap.size(200, 150);
1479
- canvas.remove();
1480
- }
1481
- */
1482
- function createCapture(type?: string, flipped?: boolean, cb?: (vid: HTMLVideoElement) => void): HTMLVideoElement;
1483
-
1484
- /** 📑
1485
- * 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).
1486
- * @param {string} selector
1487
- * @returns {HTMLElement} element
1488
- */
1489
- function findElement(selector: string): HTMLElement;
1490
-
1491
- /** 📑
1492
- * Finds all elements in the DOM that match the given [CSS selector](https://developer.mozilla.org/docs/Learn_web_development/Core/Styling_basics/Basic_selectors).
1493
- * @param {string} selector
1494
- * @returns {HTMLElement[]} elements
1495
- */
1496
- function findElements(selector: string): HTMLElement[];
1497
-
1498
- // 🌆 image
1569
+ function inset(sx: number, sy: number, sw: number, sh: number, dx: number, dy: number, dw: number, dh: number): void;
1499
1570
 
1500
1571
  /** 🌆
1501
- * Loads an image from a URL and optionally runs a callback function.
1502
- * @param {string} url url of the image to load
1503
- * @param {(img: any) => void} [cb] callback function after the image is loaded
1504
- * @param {any} [opt] optional parameters for loading the image
1505
- * @returns {Q5.Image} image
1506
- * @example
1507
- createCanvas(200);
1508
-
1509
- let logo = loadImage('/q5js_logo.webp');
1510
-
1511
- function draw() {
1512
- image(logo, 0, 0, 200, 200);
1513
- }
1572
+ * Array of pixels in the canvas or image. Use `loadPixels` to load the pixel data.
1514
1573
  */
1515
- function loadImage(url: string, cb?: (img: any) => void, opt?: any): Q5.Image;
1574
+ var pixels: number[];
1516
1575
 
1517
1576
  /** 🌆
1518
- * Draws an image to the canvas.
1519
- * @param {any} img image to draw
1520
- * @param {number} dx x position to draw the image at
1521
- * @param {number} dy y position to draw the image at
1522
- * @param {number} [dw] width of the destination image
1523
- * @param {number} [dh] height of the destination image
1524
- * @param {number} [sx] x position in the source to start clipping a subsection from
1525
- * @param {number} [sy] y position in the source to start clipping a subsection from
1526
- * @param {number} [sw] width of the subsection of the source image
1527
- * @param {number} [sh] height of the subsection of the source image
1577
+ * Loads pixel data into the canvas' or image's `pixels` array.
1528
1578
  * @example
1529
1579
  createCanvas(200);
1580
+ let icon = loadImage('/q5js_icon.png');
1530
1581
 
1531
- let logo = loadImage('/q5js_logo.webp');
1532
-
1533
- function draw() {
1534
- image(logo, 0, 0, 200, 200, 256, 256, 512, 512);
1582
+ function setup() {
1583
+ icon.loadPixels();
1584
+ for (let i=0; i < 65536; i+=16) icon.pixels[i] = 255;
1585
+ icon.updatePixels();
1586
+ image(icon, 0, 0, 200, 200);
1535
1587
  }
1536
1588
  */
1537
- function image(img: any, dx: number, dy: number, dw?: number, dh?: number, sx?: number, sy?: number, sw?: number, sh?: number): void;
1589
+ function loadPixels(): void;
1538
1590
 
1539
1591
  /** 🌆
1540
- * Sets the image mode, which determines the position and alignment of images drawn on the canvas.
1541
- * `CORNER`: (default) images will be drawn from the top-left corner
1542
- * `CORNERS`: images will be drawn from the top-left to the bottom-right corner
1543
- * `CENTER`: images will be drawn centered at (dx, dy)
1544
- * @param {string} mode
1592
+ * Applies changes in the `pixels` array to the canvas or image.
1545
1593
  * @example
1546
1594
  createCanvas(200);
1547
-
1548
- let logo = loadImage('/q5js_logo.webp');
1549
-
1550
- function draw() {
1551
- imageMode(CENTER);
1552
- image(logo, 100, 100, 200, 200);
1595
+ function setup() {
1596
+ for (let x = 0; x < 200; x += 5) {
1597
+ for (let y = 0; y < 200; y += 5) {
1598
+ set(x, y, color('red'));
1599
+ }
1600
+ }
1601
+ updatePixels();
1553
1602
  }
1554
1603
  */
1555
- function imageMode(mode: string): void;
1604
+ function updatePixels(): void;
1556
1605
 
1557
1606
  /** 🌆
1558
- * Sets the default image scale, which is applied to images when
1559
- * they are drawn without a specified width or height.
1607
+ * Applies a filter to the image.
1560
1608
  *
1561
- * By default it is 0.5 so images appear at their actual size
1562
- * when pixel density is 2. Images will be drawn at a consistent
1563
- * default size relative to the canvas regardless of pixel density.
1609
+ * See the documentation for q5's filter constants below for more info.
1564
1610
  *
1565
- * This function must be called before images are loaded to
1566
- * have an effect.
1567
- * @param {number} scale
1568
- * @returns {number} default image scale
1569
- */
1570
- function defaultImageScale(scale: number): number;
1571
-
1572
- /** 🌆
1573
- * Resizes the image.
1574
- * @param {number} w new width
1575
- * @param {number} h new height
1611
+ * A CSS filter string can also be used.
1612
+ * https://developer.mozilla.org/docs/Web/CSS/filter
1613
+ * @param {string} type filter type or a CSS filter string
1614
+ * @param {number} [value] optional value, depends on filter type
1576
1615
  * @example
1577
1616
  createCanvas(200);
1578
-
1579
1617
  let logo = loadImage('/q5js_logo.webp');
1580
1618
 
1581
1619
  function setup() {
1582
- logo.resize(128, 128);
1620
+ logo.filter(INVERT);
1583
1621
  image(logo, 0, 0, 200, 200);
1584
1622
  }
1585
1623
  */
1586
- function resize(w: number, h: number): void;
1624
+ function filter(type: string, value?: number): void;
1587
1625
 
1588
1626
  /** 🌆
1589
- * Returns a trimmed image, cropping out transparent pixels from the edges.
1590
- * @returns {Image}
1627
+ * Converts the image to black and white pixels depending if they are above or below a certain threshold.
1591
1628
  */
1592
- function trim(): Q5.Image;
1629
+ const THRESHOLD: 1;
1593
1630
 
1594
1631
  /** 🌆
1595
- * Enables smooth rendering of images displayed larger than
1596
- * their actual size. This is the default setting, so running this
1597
- * function only has an effect if `noSmooth` has been called.
1598
- * @example
1599
- createCanvas(200);
1600
-
1601
- let icon = loadImage('/q5js_icon.png');
1602
-
1603
- function setup() {
1604
- image(icon, 0, 0, 200, 200);
1605
- }
1632
+ * Converts the image to grayscale by setting each pixel to its luminance.
1606
1633
  */
1607
- function smooth(): void;
1634
+ const GRAY: 2;
1608
1635
 
1609
1636
  /** 🌆
1610
- * Disables smooth image rendering for a pixelated look.
1611
- * @example
1612
- createCanvas(200);
1613
-
1614
- let icon = loadImage('/q5js_icon.png');
1615
-
1616
- function setup() {
1617
- noSmooth();
1618
- image(icon, 0, 0, 200, 200);
1619
- }
1637
+ * Sets the alpha channel to fully opaque.
1620
1638
  */
1621
- function noSmooth(): void;
1639
+ const OPAQUE: 3;
1622
1640
 
1623
1641
  /** 🌆
1624
- * Applies a tint (color overlay) to the drawing.
1625
- *
1626
- * The alpha value of the tint color determines the
1627
- * strength of the tint. To change an image's opacity,
1628
- * use the `opacity` function.
1629
- *
1630
- * Tinting affects all subsequent images drawn. The tint
1631
- * color is applied to images using the "multiply" blend mode.
1632
- *
1633
- * Since the tinting process is performance intensive, each time
1634
- * an image is tinted, q5 caches the result. `image` will draw the
1635
- * cached tinted image unless the tint color has changed or the
1636
- * image being tinted was edited.
1637
- *
1638
- * If you need to draw an image multiple times each frame with
1639
- * different tints, consider making copies of the image and tinting
1640
- * each copy separately.
1641
- * @param {string | number} color tint color
1642
- * @example
1643
- createCanvas(200);
1644
-
1645
- let logo = loadImage('/q5js_logo.webp');
1642
+ * Inverts the color of each pixel.
1643
+ */
1644
+ const INVERT: 4;
1646
1645
 
1647
- function setup() {
1648
- tint(255, 0, 0, 128);
1649
- image(logo, 0, 0, 200, 200);
1650
- }
1646
+ /** 🌆
1647
+ * Limits each channel of the image to the number of colors specified as an argument.
1651
1648
  */
1652
- function tint(color: string | number): void;
1649
+ const POSTERIZE: 5;
1653
1650
 
1654
1651
  /** 🌆
1655
- * Images drawn after this function is run will not be tinted.
1652
+ * Increases the size of bright areas.
1656
1653
  */
1657
- function noTint(): void;
1654
+ const DILATE: 6;
1658
1655
 
1659
1656
  /** 🌆
1660
- * Masks the image with another image.
1661
- * @param {Q5.Image} img image to use as a mask
1657
+ * Increases the size of dark areas.
1662
1658
  */
1663
- function mask(img: Q5.Image): void;
1664
-
1665
- /** 🌆
1666
- * Retrieves a subsection of an image or canvas, as a q5 Image.
1667
- * Or if width and height are both 1, returns the color of the pixel at the given coordinates in `[R, G, B, A]` array format.
1668
- * @param {number} x
1669
- * @param {number} y
1670
- * @param {number} [w] width of the area
1671
- * @param {number} [h] height of the area
1672
- * @returns {Image | number[]}
1673
- * @example
1674
- createCanvas(200);
1675
-
1676
- let logo = loadImage('/q5js_logo.webp');
1677
-
1678
- function setup() {
1679
- let cropped = logo.get(256, 256, 512, 512);
1680
- image(cropped, 0, 0, 200, 200);
1681
- }
1682
- */
1683
- function get(x: number, y: number, w?: number, h?: number): Q5.Image | number[];
1684
-
1685
- /** 🌆
1686
- * Sets a pixel's color in the image or canvas.
1687
- *
1688
- * Or if a canvas or image is provided, it's drawn on top of the
1689
- * destination image or canvas, ignoring its tint setting.
1690
- *
1691
- * Run `updatePixels` to apply the changes.
1692
- * @param {number} x
1693
- * @param {number} y
1694
- * @param {any} c color, canvas, or image
1695
- * @example
1696
- createCanvas(200);
1697
- let c = color('lime');
1698
-
1699
- function draw() {
1700
- set(randomX(), randomY(), c);
1701
- updatePixels();
1702
- }
1703
- */
1704
- function set(x: number, y: number, c: any): void;
1705
-
1706
- /** 🌆
1707
- * Returns a copy of the image.
1708
- * @returns {Q5.Image}
1709
- */
1710
- function copy(): Q5.Image;
1711
-
1712
- /** 🌆
1713
- * Displays a region of the image on another region of the image.
1714
- * Can be used to create a detail inset, aka a magnifying glass effect.
1715
- * @param {number} sx x-coordinate of the source region
1716
- * @param {number} sy y-coordinate of the source region
1717
- * @param {number} sw width of the source region
1718
- * @param {number} sh height of the source region
1719
- * @param {number} dx x-coordinate of the destination region
1720
- * @param {number} dy y-coordinate of the destination region
1721
- * @param {number} dw width of the destination region
1722
- * @param {number} dh height of the destination region
1723
- * @example
1724
- createCanvas(200);
1725
-
1726
- let logo = loadImage('/q5js_logo.webp');
1727
-
1728
- function setup() {
1729
- logo.inset(256, 256, 512, 512, 0, 0, 256, 256);
1730
- image(logo, 0, 0, 200, 200);
1731
- }
1732
- */
1733
- function inset(sx: number, sy: number, sw: number, sh: number, dx: number, dy: number, dw: number, dh: number): void;
1734
-
1735
- /** 🌆
1736
- * Array of pixels in the canvas or image. Use `loadPixels` to load the pixel data.
1737
- */
1738
- var pixels: number[];
1739
-
1740
- /** 🌆
1741
- * Loads pixel data into the canvas' or image's `pixels` array.
1742
- * @example
1743
- createCanvas(200);
1744
- let icon = loadImage('/q5js_icon.png');
1745
-
1746
- function setup() {
1747
- icon.loadPixels();
1748
- for (let i=0; i < 65536; i+=16) icon.pixels[i] = 255;
1749
- icon.updatePixels();
1750
- image(icon, 0, 0, 200, 200);
1751
- }
1752
- */
1753
- function loadPixels(): void;
1754
-
1755
- /** 🌆
1756
- * Applies changes in the `pixels` array to the canvas or image.
1757
- * @example
1758
- createCanvas(200);
1759
- function setup() {
1760
- for (let x = 0; x < 200; x += 5) {
1761
- for (let y = 0; y < 200; y += 5) {
1762
- set(x, y, color('red'));
1763
- }
1764
- }
1765
- updatePixels();
1766
- }
1767
- */
1768
- function updatePixels(): void;
1769
-
1770
- /** 🌆
1771
- * Applies a filter to the image.
1772
- *
1773
- * See the documentation for q5's filter constants below for more info.
1774
- *
1775
- * A CSS filter string can also be used.
1776
- * https://developer.mozilla.org/docs/Web/CSS/filter
1777
- * @param {string} type filter type or a CSS filter string
1778
- * @param {number} [value] optional value, depends on filter type
1779
- * @example
1780
- createCanvas(200);
1781
- let logo = loadImage('/q5js_logo.webp');
1782
-
1783
- function setup() {
1784
- logo.filter(INVERT);
1785
- image(logo, 0, 0, 200, 200);
1786
- }
1787
- */
1788
- function filter(type: string, value?: number): void;
1789
-
1790
- /** 🌆
1791
- * Converts the image to black and white pixels depending if they are above or below a certain threshold.
1792
- */
1793
- const THRESHOLD: 1;
1794
-
1795
- /** 🌆
1796
- * Converts the image to grayscale by setting each pixel to its luminance.
1797
- */
1798
- const GRAY: 2;
1799
-
1800
- /** 🌆
1801
- * Sets the alpha channel to fully opaque.
1802
- */
1803
- const OPAQUE: 3;
1804
-
1805
- /** 🌆
1806
- * Inverts the color of each pixel.
1807
- */
1808
- const INVERT: 4;
1809
-
1810
- /** 🌆
1811
- * Limits each channel of the image to the number of colors specified as an argument.
1812
- */
1813
- const POSTERIZE: 5;
1814
-
1815
- /** 🌆
1816
- * Increases the size of bright areas.
1817
- */
1818
- const DILATE: 6;
1819
-
1820
- /** 🌆
1821
- * Increases the size of dark areas.
1822
- */
1823
- const ERODE: 7;
1659
+ const ERODE: 7;
1824
1660
 
1825
1661
  /** 🌆
1826
1662
  * Applies a Gaussian blur to the image.
@@ -2129,236 +1965,61 @@ text(nf(PI, 4, 2), 10, 60);
2129
1965
  */
2130
1966
  const BASELINE: 'alphabetic';
2131
1967
 
2132
- // ai
1968
+ // 🖲️ input
2133
1969
 
2134
- /**
2135
- * Run this function before a line of code that isn't working as expected.
2136
- * @param {string} [question] question to ask the AI
1970
+ /** 🖲️
1971
+ * Current X position of the mouse.
1972
+ * @example
1973
+ function draw() {
1974
+ background(200);
1975
+ textSize(64);
1976
+ text(round(mouseX), 50, 120);
1977
+ }
2137
1978
  */
2138
- function askAI(question?: string): void;
1979
+ let mouseX: number;
2139
1980
 
2140
- // 🎨 color
1981
+ /** 🖲️
1982
+ * Current Y position of the mouse.
1983
+ * @example
1984
+ function draw() {
1985
+ background(200);
1986
+ circle(100, mouseY, 100);
1987
+ }
1988
+ */
1989
+ let mouseY: number;
2141
1990
 
2142
- /** 🎨
2143
- * Creates a new `Color` object, which is primarily useful for storing
2144
- * a color that your sketch will reuse or modify later.
2145
- *
2146
- * With the default RGB color mode, colors have these components:
2147
- *
2148
- * `r`/`red`, `g`/`green`, `b`/`blue`, and `a`/`alpha`.
2149
- *
2150
- * The default color format is integer, so set color components
2151
- * to values between 0 and 255.
2152
- *
2153
- * In q5 WebGPU, the default color mode is RGB in float format, so
2154
- * set color components to values between 0 and 1.
2155
- *
2156
- * The [`fill`](https://q5js.org/learn/#fill), [`stroke`](https://q5js.org/learn/#stroke), and [`background`](https://q5js.org/learn/#background) functions
2157
- * accept the same wide range of color representations as this function.
2158
- *
2159
- * Here are some examples of valid use:
1991
+ /** 🖲️
1992
+ * Previous X position of the mouse.
1993
+ */
1994
+ let pmouseX: number;
1995
+
1996
+ /** 🖲️
1997
+ * Previous Y position of the mouse.
1998
+ */
1999
+ let pmouseY: number;
2000
+
2001
+ /** 🖲️
2002
+ * The current button being pressed: 'left', 'right', 'center').
2160
2003
  *
2161
- * - `color(255)` (grayscale)
2162
- * - `color(255, 200)` (grayscale, alpha)
2163
- * - `color(255, 0, 0)` (r, g, b)
2164
- * - `color(255, 0, 0, 10)` (r, g, b, a)
2165
- * - `color('red')` (colorName)
2166
- * - `color('#ff0000')` (hexColor)
2167
- * - `color([255, 0, 0])` (colorComponents)
2168
- * @param {string | number | Color | number[]} c0 color or first color component
2169
- * @param {number} [c1] second color component
2170
- * @param {number} [c2] third color component
2171
- * @param {number} [c3] fourth color component (alpha)
2172
- * @returns {Color} a new `Color` object
2004
+ * The default value is an empty string.
2173
2005
  * @example
2174
- createCanvas(200);
2175
- rect(0, 0, 100, 200);
2006
+ function draw() {
2007
+ background(200);
2008
+ textSize(64);
2009
+ text(mouseButton, 20, 120);
2010
+ }
2011
+ */
2012
+ let mouseButton: string;
2176
2013
 
2177
- // ( r, g, b, a)
2178
- let bottle = color(90, 100, 255, 100);
2179
- fill(bottle);
2180
- stroke(bottle);
2181
- strokeWeight(30);
2182
- circle(100, 100, 155);
2014
+ /** 🖲️
2015
+ * True if the mouse is currently pressed, false otherwise.
2183
2016
  * @example
2184
- createCanvas(200);
2185
- // (gray, alpha)
2186
- let c = color(200, 50);
2187
-
2188
2017
  function draw() {
2189
- background(c);
2190
- circle(mouseX, mouseY, 50);
2191
- c.g = (c.g + 1) % 255;
2018
+ if (mouseIsPressed) background(100);
2019
+ else background(200);
2192
2020
  }
2193
2021
  */
2194
- function color(c0: string | number | Color | number[], c1?: number, c2?: number, c3?: number): Color;
2195
-
2196
- /** 🎨
2197
- * Sets the color mode for the sketch. Changes the type of color object created by color functions.
2198
- *
2199
- * In c2d, the default color mode is RGB in legacy integer format.
2200
- *
2201
- * In WebGPU, the default color mode is RGB in float format.
2202
- *
2203
- * See the documentation for q5's color constants below for more info.
2204
- * @param {'rgb' | 'srgb' | 'oklch'} mode color mode
2205
- * @param {1 | 255} format color format (1 for float, 255 for integer)
2206
- * @example
2207
- createCanvas(200);
2208
-
2209
- colorMode(RGB, 1);
2210
- fill(1, 0, 0);
2211
- rect(0, 0, 66, 200);
2212
- fill(0, 1, 0);
2213
- rect(66, 0, 67, 200);
2214
- fill(0, 0, 1);
2215
- rect(133, 0, 67, 200);
2216
- * @example
2217
- createCanvas(200);
2218
-
2219
- colorMode(OKLCH);
2220
-
2221
- fill(0.25, 0.15, 0);
2222
- rect(0, 0, 100, 200);
2223
-
2224
- fill(0.75, 0.15, 0)
2225
- rect(100, 0, 100, 200);
2226
- */
2227
- function colorMode(mode: 'rgb' | 'srgb' | 'oklch', format: 1 | 255): void;
2228
-
2229
- /** 🎨
2230
- * RGB colors have components `r`/`red`, `g`/`green`, `b`/`blue`,
2231
- * and `a`/`alpha`.
2232
- *
2233
- * By default when a canvas is using the HDR `display-p3` color space,
2234
- * rgb colors are mapped to the full P3 gamut, even when they use the
2235
- * legacy integer 0-255 format.
2236
- * @example
2237
- createCanvas(200, 100);
2238
-
2239
- colorMode(RGB, 255);
2240
-
2241
- background(255, 0, 0);
2242
- */
2243
- const RGB: 'rgb';
2244
-
2245
- /** 🎨
2246
- * This color mode limits the gamut of rgb colors to sRGB.
2247
- *
2248
- * If your display is HDR capable, take a look at the following
2249
- * example, note that full red appears less saturated, as it would
2250
- * on an SDR display.
2251
- * @example
2252
- createCanvas(200, 100);
2253
-
2254
- colorMode(SRGB, 255);
2255
-
2256
- background(255, 0, 0);
2257
- */
2258
- const SRGB: 'srgb';
2259
-
2260
- /** 🎨
2261
- * OKLCH colors have components `l`/`lightness`, `c`/`chroma`,
2262
- * `h`/`hue`, and `a`/`alpha`.
2263
- *
2264
- * You may be familiar with the outdated HSL/HSV color formats,
2265
- * which were created in the 1970s to be more intuitive for humans
2266
- * to work with than RGB. But due to technical limitations of that
2267
- * time, they're not perceptually uniform, meaning colors at the same
2268
- * brightness values may appear lighter or darker depending on the hue.
2269
- *
2270
- * The OKLCH format is similar to HSL/HSV but it's perceptually
2271
- * uniform and supports HDR colors. Use this oklch color picker to
2272
- * explore the color space: https://oklch.com
2273
- *
2274
- * `lightness`: 0 to 1
2275
- *
2276
- * `chroma`: 0 to 0.3
2277
- *
2278
- * `hue`: 0 to 360
2279
- *
2280
- * `alpha`: 0 to 1
2281
- *
2282
- * Note how seamless the hue transitions are in the following example.
2283
- * @example
2284
- createCanvas(200);
2285
- colorMode(OKLCH);
2286
-
2287
- function draw() {
2288
- background(0.7, 0.16, frameCount % 360);
2289
- }
2290
- */
2291
- const OKLCH: 'oklch';
2292
-
2293
- class Color {
2294
- /** 🎨
2295
- * Use the `color` function for greater flexibility, it runs
2296
- * this constructor internally.
2297
- *
2298
- * This constructor only accepts 4 numbers, which are the color
2299
- * components.
2300
- *
2301
- * `Color` is not actually a class itself, it's a reference to a
2302
- * Q5 color class based on the color mode and format.
2303
- */
2304
- constructor(c0: number, c1: number, c2: number, c3: number);
2305
- }
2306
-
2307
- // 🖲️ input
2308
-
2309
- /** 🖲️
2310
- * Current X position of the mouse.
2311
- * @example
2312
- function draw() {
2313
- background(200);
2314
- textSize(64);
2315
- text(round(mouseX), 50, 120);
2316
- }
2317
- */
2318
- let mouseX: number;
2319
-
2320
- /** 🖲️
2321
- * Current Y position of the mouse.
2322
- * @example
2323
- function draw() {
2324
- background(200);
2325
- circle(100, mouseY, 100);
2326
- }
2327
- */
2328
- let mouseY: number;
2329
-
2330
- /** 🖲️
2331
- * Previous X position of the mouse.
2332
- */
2333
- let pmouseX: number;
2334
-
2335
- /** 🖲️
2336
- * Previous Y position of the mouse.
2337
- */
2338
- let pmouseY: number;
2339
-
2340
- /** 🖲️
2341
- * The current button being pressed: 'left', 'right', 'center').
2342
- *
2343
- * The default value is an empty string.
2344
- * @example
2345
- function draw() {
2346
- background(200);
2347
- textSize(64);
2348
- text(mouseButton, 20, 120);
2349
- }
2350
- */
2351
- let mouseButton: string;
2352
-
2353
- /** 🖲️
2354
- * True if the mouse is currently pressed, false otherwise.
2355
- * @example
2356
- function draw() {
2357
- if (mouseIsPressed) background(100);
2358
- else background(200);
2359
- }
2360
- */
2361
- let mouseIsPressed: boolean;
2022
+ let mouseIsPressed: boolean;
2362
2023
 
2363
2024
  /** 🖲️
2364
2025
  * Define this function to respond to mouse events immediately.
@@ -2705,30 +2366,496 @@ function draw() {
2705
2366
  */
2706
2367
  function noiseSeed(seed: number): void;
2707
2368
 
2708
- /** 🧮
2709
- * Sets the level of detail for noise generation.
2710
- * @param {number} lod level of detail (number of octaves)
2711
- * @param {number} falloff falloff rate for each octave
2712
- */
2713
- function noiseDetail(lod: number, falloff: number): void;
2369
+ /** 🧮
2370
+ * Sets the level of detail for noise generation.
2371
+ * @param {number} lod level of detail (number of octaves)
2372
+ * @param {number} falloff falloff rate for each octave
2373
+ */
2374
+ function noiseDetail(lod: number, falloff: number): void;
2375
+
2376
+ /** 🧮
2377
+ * The ratio of a circle's circumference to its diameter.
2378
+ * Approximately 3.14159.
2379
+ */
2380
+ const PI: number;
2381
+
2382
+ /** 🧮
2383
+ * 2 * PI.
2384
+ * Approximately 6.28319.
2385
+ */
2386
+ const TWO_PI: number;
2387
+
2388
+ /** 🧮
2389
+ * Alias for 2 * PI.
2390
+ * Approximately 6.28319.
2391
+ */
2392
+ const TAU: number;
2393
+
2394
+ // 🔊 sound
2395
+
2396
+ /** 🔊
2397
+ * q5.js includes low latency sound playback and basic mixing powered
2398
+ * by WebAudio.
2399
+ *
2400
+ * For audio filtering, synthesis, and analysis, consider using
2401
+ * [p5.sound](https://p5js.org/reference/p5.sound/).
2402
+ */
2403
+
2404
+ /** 🔊
2405
+ * Loads audio data from a file and returns a `Q5.Sound` object.
2406
+ *
2407
+ * Use functions like `play`, `pause`, and `stop` to
2408
+ * control playback. Note that sounds can only be played after the
2409
+ * first user interaction with the page!
2410
+ *
2411
+ * Set `volume` to a value between 0 (silent) and 1 (full volume).
2412
+ * Set `pan` to a value between -1 (left) and 1 (right) to adjust
2413
+ * the sound's stereo position. Set `loop` to true to loop the sound.
2414
+ *
2415
+ * Use `loaded`, `paused`, and `ended` to check the sound's status.
2416
+ *
2417
+ * For backwards compatibility with the p5.sound API, the functions
2418
+ * `setVolume`, `setLoop`, `setPan`, `isLoaded`, and `isPlaying`
2419
+ * are also implemented, but their use is deprecated.
2420
+ * @param {string} url sound file
2421
+ * @returns {Sound} a new `Sound` object
2422
+ * @example
2423
+ createCanvas(200);
2424
+
2425
+ let sound = loadSound('/assets/jump.wav');
2426
+ sound.volume = 0.3;
2427
+
2428
+ function mousePressed() {
2429
+ sound.play();
2430
+ }
2431
+ */
2432
+ function loadSound(url: string): Sound;
2433
+
2434
+ /**
2435
+ * Loads audio data from a file and returns an [HTMLAudioElement](https://developer.mozilla.org/docs/Web/API/HTMLMediaElement).
2436
+ *
2437
+ * Audio is considered loaded when the [canplaythrough event](https://developer.mozilla.org/docs/Web/API/HTMLMediaElement/canplaythrough_event) is fired.
2438
+ *
2439
+ * Note that audio can only be played after the first user
2440
+ * interaction with the page!
2441
+ * @param url audio file
2442
+ * @returns {HTMLAudioElement} an HTMLAudioElement
2443
+ * @example
2444
+ createCanvas(200);
2445
+
2446
+ let audio = loadAudio('/assets/retro.flac');
2447
+ audio.volume = 0.4;
2448
+
2449
+ function mousePressed() {
2450
+ audio.play();
2451
+ }
2452
+ */
2453
+ function loadAudio(url: string): HTMLAudioElement;
2454
+
2455
+ /** 🔊
2456
+ * Returns the AudioContext in use or undefined if it doesn't exist.
2457
+ * @returns {AudioContext} AudioContext instance
2458
+ */
2459
+ function getAudioContext(): AudioContext | void;
2460
+
2461
+ /** 🔊
2462
+ * Creates a new AudioContext or resumes it if it was suspended.
2463
+ * @returns {Promise<void>} a promise that resolves when the AudioContext is resumed
2464
+ */
2465
+ function userStartAudio(): Promise<void>;
2466
+
2467
+ class Sound {
2468
+ /** 🔊
2469
+ * Creates a new `Q5.Sound` object.
2470
+ */
2471
+ constructor();
2472
+
2473
+ /** 🔊
2474
+ * Set the sound's volume to a value between
2475
+ * 0 (silent) and 1 (full volume).
2476
+ */
2477
+ volume: number;
2478
+
2479
+ /** 🔊
2480
+ * Set the sound's stereo position between -1 (left) and 1 (right).
2481
+ */
2482
+ pan: number;
2483
+
2484
+ /** 🔊
2485
+ * Set to true to make the sound loop continuously.
2486
+ */
2487
+ loop: boolean;
2488
+
2489
+ /** 🔊
2490
+ * True if the sound data has finished loading.
2491
+ */
2492
+ loaded: boolean;
2493
+
2494
+ /** 🔊
2495
+ * True if the sound is currently paused.
2496
+ */
2497
+ paused: boolean;
2498
+
2499
+ /** 🔊
2500
+ * True if the sound has finished playing.
2501
+ */
2502
+ ended: boolean;
2503
+
2504
+ /** 🔊
2505
+ * Plays the sound.
2506
+ *
2507
+ * If this function is run when the sound is already playing,
2508
+ * a new playback will start, causing a layering effect.
2509
+ *
2510
+ * If this function is run when the sound is paused,
2511
+ * all playback instances will be resumed.
2512
+ */
2513
+ play(): void;
2514
+
2515
+ /** 🔊
2516
+ * Pauses the sound, allowing it to be resumed.
2517
+ */
2518
+ pause(): void;
2519
+
2520
+ /** 🔊
2521
+ * Stops the sound, resetting its playback position
2522
+ * to the beginning.
2523
+ *
2524
+ * Removes all playback instances.
2525
+ */
2526
+ stop(): void;
2527
+ }
2528
+
2529
+ // 📑 dom
2530
+
2531
+ /** 📑
2532
+ * The Document Object Model (DOM) is an interface for
2533
+ * creating and editing web pages with JavaScript.
2534
+ */
2535
+
2536
+ /** 📑
2537
+ * Creates a new HTML element and adds it to the page. `createEl` is
2538
+ * an alias.
2539
+ *
2540
+ * Modify the element's CSS [`style`](https://developer.mozilla.org/docs/Web/API/HTMLElement/style) to change its appearance.
2541
+ *
2542
+ * Use [`addEventListener`](https://developer.mozilla.org/docs/Web/API/EventTarget/addEventListener) to respond to events such as:
2543
+ * - "click": when the element is clicked
2544
+ * - "mouseover": when the mouse hovers over the element
2545
+ * - "mouseout": when the mouse stops hovering over the element
2546
+ * - "input": when a form element's value changes
2547
+ *
2548
+ * q5 adds some extra functionality to the elements it creates:
2549
+ *
2550
+ * - the `position` function makes it easy to place the element
2551
+ * relative to the canvas
2552
+ * - the `size` function sets the width and height of the element
2553
+ * - alternatively, use the element's `x`, `y`, `width`, and `height` properties
2554
+ * @param {string} tag tag name of the element
2555
+ * @param {string} [content] content of the element
2556
+ * @returns {HTMLElement} element
2557
+ * @example
2558
+ createCanvas(200);
2559
+
2560
+ let el = createEl('div', '*');
2561
+ el.position(50, 50);
2562
+ el.size(100, 100);
2563
+ el.style.fontSize = '136px';
2564
+ el.style.textAlign = 'center';
2565
+ el.style.backgroundColor = 'blue';
2566
+ el.style.color = 'white';
2567
+ */
2568
+ function createElement(tag: string, content?: string): HTMLElement;
2569
+
2570
+ /** 📑
2571
+ * Creates a link element.
2572
+ * @param {string} href url
2573
+ * @param {string} [text] text content
2574
+ * @param {boolean} [newTab] whether to open the link in a new tab
2575
+ * @example
2576
+ createCanvas(200);
2577
+
2578
+ let link = createA('https://q5js.org', 'q5.js');
2579
+ link.position(16, 42);
2580
+ link.style.fontSize = '80px';
2581
+
2582
+ link.addEventListener('mouseover', () => {
2583
+ background('blue');
2584
+ });
2585
+ */
2586
+ function createA(href: string, text?: string): HTMLAnchorElement;
2587
+
2588
+ /** 📑
2589
+ * Creates a button element.
2590
+ * @param {string} [content] text content
2591
+ * @example
2592
+ createCanvas(200, 100);
2593
+
2594
+ let btn = createButton('Click me!');
2595
+
2596
+ btn.addEventListener('click', () => {
2597
+ background(random(100, 255));
2598
+ });
2599
+ */
2600
+ function createButton(content?: string): HTMLButtonElement;
2601
+
2602
+ /** 📑
2603
+ * Creates a checkbox element.
2604
+ *
2605
+ * Use the `checked` property to get or set the checkbox's state.
2606
+ *
2607
+ * The `label` property is the text label element next to the checkbox.
2608
+ * @param {string} [label] text label placed next to the checkbox
2609
+ * @param {boolean} [checked] initial state
2610
+ * @example
2611
+ createCanvas(200, 100);
2612
+
2613
+ let box = createCheckbox('Check me!');
2614
+ box.label.style.color = 'lime';
2615
+
2616
+ box.addEventListener('input', () => {
2617
+ if (box.checked) background('lime');
2618
+ else background('black');
2619
+ });
2620
+ */
2621
+ function createCheckbox(label?: string, checked?: boolean): HTMLInputElement;
2622
+
2623
+ /** 📑
2624
+ * Creates a color input element.
2625
+ *
2626
+ * Use the `value` property to get or set the color value.
2627
+ * @param {string} [value] initial color value
2628
+ * @example
2629
+ createCanvas(200, 100);
2630
+
2631
+ let picker = createColorPicker();
2632
+ picker.value = '#fd7575';
2633
+
2634
+ function draw() {
2635
+ background(picker.value);
2636
+ }
2637
+ */
2638
+ function createColorPicker(value?: string): HTMLInputElement;
2639
+
2640
+ /** 📑
2641
+ * Creates an image element.
2642
+ * @param {string} src url of the image
2643
+ * @example
2644
+ createCanvas(200, 100);
2645
+
2646
+ let img = createImg('/assets/p5play_logo.webp')
2647
+ .position(0, 0)
2648
+ .size(100, 100);
2649
+ */
2650
+ function createImg(src: string): HTMLImageElement;
2651
+
2652
+ /** 📑
2653
+ * Creates an input element.
2654
+ *
2655
+ * Use the `value` property to get or set the input's value.
2656
+ *
2657
+ * Use the `placeholder` property to set label text that appears
2658
+ * inside the input when it's empty.
2659
+ *
2660
+ * See MDN's [input documentation](https://developer.mozilla.org/docs/Web/HTML/Element/input#input_types) for the full list of input types.
2661
+ * @param {string} [value] initial value
2662
+ * @param {string} [type] text input type, can be 'text', 'password', 'email', 'number', 'range', 'search', 'tel', 'url'
2663
+ * @example
2664
+ createCanvas(200, 100);
2665
+ textSize(64);
2666
+
2667
+ let input = createInput();
2668
+ input.placeholder = 'Type here!';
2669
+ input.size(200, 32);
2670
+
2671
+ input.addEventListener('input', () => {
2672
+ background('orange');
2673
+ text(input.value, 10, 70);
2674
+ });
2675
+ */
2676
+ function createInput(value?: string, type?: string): HTMLInputElement;
2677
+
2678
+ /** 📑
2679
+ * Creates a paragraph element.
2680
+ * @param {string} [content] text content
2681
+ * @example
2682
+ createCanvas(200, 50);
2683
+ background('coral');
2684
+
2685
+ let p = createP('Hello, world!');
2686
+ p.style.color = 'pink';
2687
+ */
2688
+ function createP(content?: string): HTMLParagraphElement;
2689
+
2690
+ /** 📑
2691
+ * Creates a radio button group.
2692
+ *
2693
+ * Use the `option(label, value)` function to add new radio buttons
2694
+ * to the group.
2695
+ *
2696
+ * Use the `value` property to get or set the value of the selected radio button.
2697
+ * @param {string} [groupName]
2698
+ * @example
2699
+ createCanvas(200, 100);
2700
+
2701
+ let radio = createRadio()
2702
+ .option('square', '1')
2703
+ .option('circle', '2');
2704
+
2705
+ function draw() {
2706
+ background(200);
2707
+ if (radio.value == '1') square(75, 25, 50);
2708
+ if (radio.value == '2') circle(100, 50, 50);
2709
+ }
2710
+ */
2711
+ function createRadio(groupName): HTMLDivElement;
2712
+
2713
+ /** 📑
2714
+ * Creates a select element.
2715
+ *
2716
+ * Use the `option(label, value)` function to add new options to
2717
+ * the select element.
2718
+ *
2719
+ * Set `multiple` to `true` to allow multiple options to be selected.
2720
+ *
2721
+ * Use the `value` property to get or set the selected option value.
2722
+ *
2723
+ * Use the `selected` property get the labels of the selected
2724
+ * options or set the selected options by label. Can be a single
2725
+ * string or an array of strings.
2726
+ * @param {string} [placeholder] optional placeholder text that appears before an option is selected
2727
+ * @example
2728
+ createCanvas(200, 100);
2729
+
2730
+ let sel = createSelect('Select a color')
2731
+ .option('Red', '#f55')
2732
+ .option('Green', '#5f5');
2733
+
2734
+ sel.addEventListener('change', () => {
2735
+ background(sel.value);
2736
+ });
2737
+ */
2738
+ function createSelect(placeholder): HTMLSelectElement;
2739
+
2740
+ /** 📑
2741
+ * Creates a slider element.
2742
+ *
2743
+ * Use the `value` property to get or set the slider's value.
2744
+ *
2745
+ * Use the `val` function to get the slider's value as a number.
2746
+ * @param {number} min minimum value
2747
+ * @param {number} max maximum value
2748
+ * @param {number} [value] initial value
2749
+ * @param {number} [step] step size
2750
+ * @example
2751
+ createCanvas(200);
2752
+
2753
+ let slider = createSlider(0, 255)
2754
+ .position(10, 10)
2755
+ .size(180);
2756
+
2757
+ function draw() {
2758
+ background(slider.val());
2759
+ }
2760
+ */
2761
+ function createSlider(min: number, max: number, value?: number, step?: number): HTMLInputElement;
2762
+
2763
+ /** 📑
2764
+ * Creates a video element.
2765
+ *
2766
+ * Note that videos must be muted to autoplay and the `play` and
2767
+ * `pause` functions can only be run after a user interaction.
2768
+ *
2769
+ * The video element can be hidden and its content can be
2770
+ * displayed on the canvas using the `image` function.
2771
+ * @param {string} src url of the video
2772
+ * @example
2773
+ createCanvas(0);
2774
+
2775
+ let vid = createVideo('/assets/apollo4.mp4');
2776
+ vid.size(200, 150);
2777
+ vid.autoplay = vid.muted = vid.loop = true;
2778
+ vid.controls = true;
2779
+
2780
+ * @example
2781
+ createCanvas(200, 150);
2782
+ let vid = createVideo('/assets/apollo4.mp4');
2783
+ vid.hide();
2784
+
2785
+ function mousePressed() {
2786
+ vid.currentTime = 0;
2787
+ vid.play();
2788
+ }
2789
+ function draw() {
2790
+ image(vid, 0, 0, 200, 150);
2791
+ filter(HUE_ROTATE, 90);
2792
+ }
2793
+ */
2794
+ function createVideo(src: string): HTMLVideoElement;
2795
+
2796
+ /** 📑
2797
+ * Creates a capture from a connected camera, such as a webcam.
2798
+ *
2799
+ * The capture video element can be hidden and its content can be
2800
+ * displayed on the canvas using the `image` function.
2801
+ *
2802
+ * Can preload to ensure the capture is ready to use when your
2803
+ * sketch starts.
2804
+ *
2805
+ * Requests the highest video resolution from the user facing camera
2806
+ * by default. The first parameter to this function can be used to
2807
+ * specify the constraints for the capture. See [`getUserMedia`](https://developer.mozilla.org/docs/Web/API/MediaDevices/getUserMedia)
2808
+ * for more info.
2809
+ * @param {string} [type] type of capture, can be only `VIDEO` or only `AUDIO`, the default is to use both video and audio
2810
+ * @param {boolean} [flipped] whether to mirror the video horizontally, true by default
2811
+ * @param {(vid: HTMLVideoElement) => void} [cb] callback function after the capture is created
2812
+ * @example
2813
+ createCanvas(200);
2814
+
2815
+ function mousePressed() {
2816
+ let cap = createCapture(VIDEO);
2817
+ cap.size(200, 112.5);
2818
+ canvas.remove();
2819
+ }
2820
+ * @example
2821
+ createCanvas(200);
2822
+
2823
+ let cap;
2824
+ function mousePressed() {
2825
+ cap = createCapture(VIDEO);
2826
+ cap.hide();
2827
+ }
2828
+
2829
+ function draw() {
2830
+ let y = frameCount % height;
2831
+ image(cap, 0, y, 200, 200);
2832
+ }
2833
+ * @example
2834
+ createCanvas(200);
2714
2835
 
2715
- /** 🧮
2716
- * The ratio of a circle's circumference to its diameter.
2717
- * Approximately 3.14159.
2836
+ function mousePressed() {
2837
+ let cap = createCapture({
2838
+ video: { width: 640, height: 480 }
2839
+ });
2840
+ cap.size(200, 150);
2841
+ canvas.remove();
2842
+ }
2718
2843
  */
2719
- const PI: number;
2844
+ function createCapture(type?: string, flipped?: boolean, cb?: (vid: HTMLVideoElement) => void): HTMLVideoElement;
2720
2845
 
2721
- /** 🧮
2722
- * 2 * PI.
2723
- * Approximately 6.28319.
2846
+ /** 📑
2847
+ * 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).
2848
+ * @param {string} selector
2849
+ * @returns {HTMLElement} element
2724
2850
  */
2725
- const TWO_PI: number;
2851
+ function findElement(selector: string): HTMLElement;
2726
2852
 
2727
- /** 🧮
2728
- * Alias for 2 * PI.
2729
- * Approximately 6.28319.
2853
+ /** 📑
2854
+ * Finds all elements in the DOM that match the given [CSS selector](https://developer.mozilla.org/docs/Learn_web_development/Core/Styling_basics/Basic_selectors).
2855
+ * @param {string} selector
2856
+ * @returns {HTMLElement[]} elements
2730
2857
  */
2731
- const TAU: number;
2858
+ function findElements(selector: string): HTMLElement[];
2732
2859
 
2733
2860
  // 🎞️ record
2734
2861
 
@@ -2810,141 +2937,6 @@ function mousePressed() {
2810
2937
  */
2811
2938
  var recording: boolean;
2812
2939
 
2813
- // 🔊 sound
2814
-
2815
- /** 🔊
2816
- * q5.js includes low latency sound playback and basic mixing powered
2817
- * by WebAudio.
2818
- *
2819
- * For audio filtering, synthesis, and analysis, consider using
2820
- * [p5.sound](https://p5js.org/reference/p5.sound/).
2821
- */
2822
-
2823
- /** 🔊
2824
- * Loads audio data from a file and returns a `Q5.Sound` object.
2825
- *
2826
- * Use functions like `play`, `pause`, and `stop` to
2827
- * control playback. Note that sounds can only be played after the
2828
- * first user interaction with the page!
2829
- *
2830
- * Set `volume` to a value between 0 (silent) and 1 (full volume).
2831
- * Set `pan` to a value between -1 (left) and 1 (right) to adjust
2832
- * the sound's stereo position. Set `loop` to true to loop the sound.
2833
- *
2834
- * Use `loaded`, `paused`, and `ended` to check the sound's status.
2835
- *
2836
- * For backwards compatibility with the p5.sound API, the functions
2837
- * `setVolume`, `setLoop`, `setPan`, `isLoaded`, and `isPlaying`
2838
- * are also implemented, but their use is deprecated.
2839
- * @param {string} url sound file
2840
- * @returns {Sound} a new `Sound` object
2841
- * @example
2842
- createCanvas(200);
2843
-
2844
- let sound = loadSound('/assets/jump.wav');
2845
- sound.volume = 0.3;
2846
-
2847
- function mousePressed() {
2848
- sound.play();
2849
- }
2850
- */
2851
- function loadSound(url: string): Sound;
2852
-
2853
- /**
2854
- * Loads audio data from a file and returns an [HTMLAudioElement](https://developer.mozilla.org/docs/Web/API/HTMLMediaElement).
2855
- *
2856
- * Audio is considered loaded when the [canplaythrough event](https://developer.mozilla.org/docs/Web/API/HTMLMediaElement/canplaythrough_event) is fired.
2857
- *
2858
- * Note that audio can only be played after the first user
2859
- * interaction with the page!
2860
- * @param url audio file
2861
- * @returns {HTMLAudioElement} an HTMLAudioElement
2862
- * @example
2863
- createCanvas(200);
2864
-
2865
- let audio = loadAudio('/assets/retro.flac');
2866
- audio.volume = 0.4;
2867
-
2868
- function mousePressed() {
2869
- audio.play();
2870
- }
2871
- */
2872
- function loadAudio(url: string): HTMLAudioElement;
2873
-
2874
- /** 🔊
2875
- * Returns the AudioContext in use or undefined if it doesn't exist.
2876
- * @returns {AudioContext} AudioContext instance
2877
- */
2878
- function getAudioContext(): AudioContext | void;
2879
-
2880
- /** 🔊
2881
- * Creates a new AudioContext or resumes it if it was suspended.
2882
- * @returns {Promise<void>} a promise that resolves when the AudioContext is resumed
2883
- */
2884
- function userStartAudio(): Promise<void>;
2885
-
2886
- class Sound {
2887
- /** 🔊
2888
- * Creates a new `Q5.Sound` object.
2889
- */
2890
- constructor();
2891
-
2892
- /** 🔊
2893
- * Set the sound's volume to a value between
2894
- * 0 (silent) and 1 (full volume).
2895
- */
2896
- volume: number;
2897
-
2898
- /** 🔊
2899
- * Set the sound's stereo position between -1 (left) and 1 (right).
2900
- */
2901
- pan: number;
2902
-
2903
- /** 🔊
2904
- * Set to true to make the sound loop continuously.
2905
- */
2906
- loop: boolean;
2907
-
2908
- /** 🔊
2909
- * True if the sound data has finished loading.
2910
- */
2911
- loaded: boolean;
2912
-
2913
- /** 🔊
2914
- * True if the sound is currently paused.
2915
- */
2916
- paused: boolean;
2917
-
2918
- /** 🔊
2919
- * True if the sound has finished playing.
2920
- */
2921
- ended: boolean;
2922
-
2923
- /** 🔊
2924
- * Plays the sound.
2925
- *
2926
- * If this function is run when the sound is already playing,
2927
- * a new playback will start, causing a layering effect.
2928
- *
2929
- * If this function is run when the sound is paused,
2930
- * all playback instances will be resumed.
2931
- */
2932
- play(): void;
2933
-
2934
- /** 🔊
2935
- * Pauses the sound, allowing it to be resumed.
2936
- */
2937
- pause(): void;
2938
-
2939
- /** 🔊
2940
- * Stops the sound, resetting its playback position
2941
- * to the beginning.
2942
- *
2943
- * Removes all playback instances.
2944
- */
2945
- stop(): void;
2946
- }
2947
-
2948
2940
  // 🛠️ utilities
2949
2941
 
2950
2942
  /** 🛠️
@@ -2975,7 +2967,8 @@ createCanvas(200);
2975
2967
  await load('/assets/Robotica.ttf');
2976
2968
 
2977
2969
  background(255);
2978
- text('Hello, world!', 20, 100);
2970
+ textSize(24);
2971
+ text('Hello, world!', 16, 100);
2979
2972
  * @example
2980
2973
  let q = new Q5();
2981
2974
  createCanvas(200);
@@ -3260,6 +3253,234 @@ function mousePressed() {
3260
3253
  */
3261
3254
  static fromAngle(angle: number, length?: number): Vector;
3262
3255
  }
3256
+
3257
+ // ✨ ai
3258
+
3259
+ /** ✨
3260
+ * Run this function before a line of code that isn't working as expected.
3261
+ * @param {string} [question] question to ask the AI
3262
+ */
3263
+ function askAI(question?: string): void;
3264
+
3265
+ // ⚡️ shaders
3266
+
3267
+ /** ⚡️
3268
+ * Custom shaders written in WGSL (WebGPU Shading Language) can be
3269
+ * used to create advanced visual effects in q5!
3270
+ *
3271
+ * For more info, read the ["Custom Shaders in q5 WebGPU"](https://github.com/q5js/q5.js/wiki/Custom-Shaders-in-q5-WebGPU)
3272
+ * wiki page.
3273
+ */
3274
+
3275
+ /** ⚡️
3276
+ * Creates a shader that q5 can use to draw shapes.
3277
+ *
3278
+ * Use this function to customize a copy of the
3279
+ * [default shapes shader](https://github.com/q5js/q5.js/blob/main/src/shaders/shapes.wgsl).
3280
+ * @param {string} code WGSL shader code excerpt
3281
+ * @returns {GPUShaderModule} a shader program
3282
+ * @example
3283
+ let q = await Q5.webgpu();
3284
+
3285
+ let wobble = createShader(`
3286
+ @vertex
3287
+ fn vertexMain(v: VertexParams) -> FragParams {
3288
+ var vert = transformVertex(v.pos, v.matrixIndex);
3289
+
3290
+ let i = f32(v.vertexIndex) % 4 * 100;
3291
+ vert.x += cos((q.time + i) * 0.01) * 0.1;
3292
+
3293
+ var f: FragParams;
3294
+ f.position = vert;
3295
+ f.color = vec4f(1, 0, 0, 1);
3296
+ return f;
3297
+ }`);
3298
+
3299
+ q.draw = () => {
3300
+ clear();
3301
+ shader(wobble);
3302
+ plane(0, 0, 100);
3303
+ };
3304
+ * @example
3305
+ let q = await Q5.webgpu();
3306
+
3307
+ let stripes = createShader(`
3308
+ @fragment
3309
+ fn fragMain(f: FragParams) -> @location(0) vec4f {
3310
+ let r = cos((q.mouseY + f.position.y) * 0.2);
3311
+ return vec4(r, 0.0, 1, 1);
3312
+ }`);
3313
+
3314
+ q.draw = () => {
3315
+ shader(stripes);
3316
+ triangle(-50, -50, 0, 50, 50, -50);
3317
+ };
3318
+ */
3319
+ function createShader(code: string): GPUShaderModule;
3320
+
3321
+ /** ⚡️
3322
+ * A plane is a centered rectangle with no stroke.
3323
+ * @param {number} x center x
3324
+ * @param {number} y center y
3325
+ * @param {number} w width or side length
3326
+ * @param {number} [h] height
3327
+ * @example
3328
+ let q = await Q5.webgpu();
3329
+ createCanvas(200);
3330
+ plane(0, 0, 100);
3331
+ */
3332
+ function plane(x: number, y: number, w: number, h?: number): void;
3333
+
3334
+ /** ⚡️
3335
+ * Applies a shader.
3336
+ * @param {GPUShaderModule} shaderModule a shader program
3337
+ */
3338
+ function shader(shaderModule: GPUShaderModule): void;
3339
+
3340
+ /** ⚡️
3341
+ * Creates a shader that q5 can use to draw images.
3342
+ *
3343
+ * Use this function to customize a copy of the
3344
+ * [default image shader](https://github.com/q5js/q5.js/blob/main/src/shaders/image.wgsl).
3345
+ * @param {string} code WGSL shader code excerpt
3346
+ * @returns {GPUShaderModule} a shader program
3347
+ * @example
3348
+ let q = await Q5.webgpu();
3349
+ imageMode(CENTER);
3350
+
3351
+ let logo = loadImage('/q5js_logo.webp');
3352
+
3353
+ let grate = createImageShader(`
3354
+ @fragment
3355
+ fn fragMain(f: FragParams) -> @location(0) vec4f {
3356
+ var texColor = textureSample(tex, samp, f.texCoord);
3357
+ texColor.a -= (q.mouseX + f.position.x) % 10 / 10;
3358
+ return texColor;
3359
+ }`);
3360
+
3361
+ q.draw = () => {
3362
+ clear();
3363
+ shader(grate);
3364
+ image(logo, 0, 0, 200, 200);
3365
+ };
3366
+ */
3367
+ function createImageShader(code: string): GPUShaderModule;
3368
+
3369
+ /** ⚡️
3370
+ * Creates a shader that q5 can use to draw video frames.
3371
+ *
3372
+ * Use this function to customize a copy of the
3373
+ * [default video shader](https://github.com/q5js/q5.js/blob/main/src/shaders/video.wgsl).
3374
+ * @param {string} code WGSL shader code excerpt
3375
+ * @returns {GPUShaderModule} a shader program
3376
+ * @example
3377
+ let q = await Q5.webgpu();
3378
+ createCanvas(200, 600);
3379
+
3380
+ let vid = createVideo('/assets/apollo4.mp4');
3381
+ vid.hide();
3382
+
3383
+ let flipper = createVideoShader(`
3384
+ @vertex
3385
+ fn vertexMain(v: VertexParams) -> FragParams {
3386
+ var vert = transformVertex(v.pos, v.matrixIndex);
3387
+
3388
+ vert.y *= cos((q.frameCount + f32(v.vertexIndex) * 10) * 0.03);
3389
+
3390
+ var f: FragParams;
3391
+ f.position = vert;
3392
+ f.texCoord = v.texCoord;
3393
+ return f;
3394
+ }
3395
+
3396
+ @fragment
3397
+ fn fragMain(f: FragParams) -> @location(0) vec4f {
3398
+ var texColor = textureSampleBaseClampToEdge(tex, samp, f.texCoord);
3399
+ texColor.r = 0;
3400
+ texColor.b *= 2;
3401
+ return texColor;
3402
+ }`);
3403
+
3404
+ q.draw = () => {
3405
+ clear();
3406
+ if (mouseIsPressed) vid.play();
3407
+ shader(flipper);
3408
+ image(vid, -100, 150, 200, 150);
3409
+ };
3410
+ //
3411
+ */
3412
+ function createVideoShader(code: string): GPUShaderModule;
3413
+
3414
+ /** ⚡️
3415
+ * Creates a shader that q5 can use to draw text.
3416
+ *
3417
+ * Use this function to customize a copy of the
3418
+ * [default text shader](https://github.com/q5js/q5.js/blob/main/src/shaders/text.wgsl).
3419
+ * @param {string} code WGSL shader code excerpt
3420
+ * @returns {GPUShaderModule} a shader program
3421
+ * @example
3422
+ let q = await Q5.webgpu();
3423
+ textAlign(CENTER, CENTER);
3424
+
3425
+ let spin = createTextShader(`
3426
+ @vertex
3427
+ fn vertexMain(v : VertexParams) -> FragParams {
3428
+ let char = textChars[v.instanceIndex];
3429
+ let text = textMetadata[i32(char.w)];
3430
+ let fontChar = fontChars[i32(char.z)];
3431
+ let pos = calcPos(v.vertexIndex, char, fontChar, text);
3432
+
3433
+ var vert = transformVertex(pos, text.matrixIndex);
3434
+
3435
+ let i = f32(v.instanceIndex + 1);
3436
+ vert.y *= cos((q.frameCount - 5 * i) * 0.05);
3437
+
3438
+ var f : FragParams;
3439
+ f.position = vert;
3440
+ f.texCoord = calcUV(v.vertexIndex, fontChar);
3441
+ f.fillColor = colors[i32(text.fillIndex)];
3442
+ f.strokeColor = colors[i32(text.strokeIndex)];
3443
+ f.strokeWeight = text.strokeWeight;
3444
+ return f;
3445
+ }`);
3446
+
3447
+ q.draw = () => {
3448
+ clear();
3449
+ shader(spin);
3450
+ fill(1, 0, 1);
3451
+ textSize(32);
3452
+ text('Hello, World!', 0, 0);
3453
+ };
3454
+ */
3455
+ function createTextShader(code: string): GPUShaderModule;
3456
+
3457
+ /** ⚡️
3458
+ * Makes q5 use a default shader.
3459
+ * @param {string} [type] can be "shapes" (default), "image", "video", or "text"
3460
+ * @example
3461
+ let q = await Q5.webgpu();
3462
+
3463
+ let stripes = createShader(`
3464
+ @fragment
3465
+ fn fragMain(f: FragParams) -> @location(0) vec4f {
3466
+ let g = cos((q.mouseY + f.position.y) * 0.05);
3467
+ return vec4(1, g, 0, 1);
3468
+ }`);
3469
+
3470
+ q.draw = () => {
3471
+ shader(stripes);
3472
+ background(0);
3473
+
3474
+ resetShader();
3475
+ triangle(-50, -50, 0, 50, 50, -50);
3476
+ };
3477
+ */
3478
+ function resetShader(type?: string): void;
3479
+
3480
+ /** ⚡️
3481
+ * Makes q5 use default shaders.
3482
+ */
3483
+ function resetShaders(): void;
3263
3484
  }
3264
3485
 
3265
3486
  export {};