q5 2.7.6 → 2.9.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.
@@ -0,0 +1 @@
1
+ q5.d.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "q5",
3
- "version": "2.7.6",
3
+ "version": "2.9.0",
4
4
  "description": "A sequel to p5.js that's optimized for interactive art",
5
5
  "author": "quinton-ashley",
6
6
  "contributors": [
package/q5.d.ts CHANGED
@@ -12,7 +12,8 @@ declare global {
12
12
  * Creates an instance of Q5.
13
13
  *
14
14
  * Running `new Q5()` enables the use of q5 functions and variables
15
- * anywhere in your code.
15
+ * anywhere in your code. You can also start Q5 in global mode by
16
+ * running [`createCanvas`](https://q5js.org/learn/#canvas-createCanvas).
16
17
  * @param {string | Function} [scope] -
17
18
  * - "global": (default) top-level global mode, adds q5 functions
18
19
  * and variables to the global scope
@@ -22,6 +23,7 @@ declare global {
22
23
  * @example
23
24
  * new Q5();
24
25
  * createCanvas(200, 100);
26
+ * circle(100, 50, 80);
25
27
  */
26
28
  constructor(scope?: string | Function, parent?: HTMLElement);
27
29
 
@@ -54,7 +56,7 @@ declare global {
54
56
  * @example
55
57
  function draw() {
56
58
  background('lightgray');
57
- circle(frameCount % 200, 100, 50);
59
+ circle(frameCount % 200, 100, 80);
58
60
  }
59
61
  */
60
62
  function draw(): void;
@@ -98,7 +100,7 @@ function draw() {
98
100
  * Stops the draw loop.
99
101
  * @example
100
102
  function draw() {
101
- circle(frameCount * 5, 100, 50);
103
+ circle(frameCount * 5, 100, 80);
102
104
  noLoop();
103
105
  }
104
106
  */
@@ -109,11 +111,11 @@ function draw() {
109
111
  * it calls the draw function once.
110
112
  * @param {number} [n] - number of times to redraw the canvas, default is 1
111
113
  * @example
112
- new Q5();
114
+ createCanvas(200, 200);
113
115
  noLoop();
114
116
 
115
117
  function draw() {
116
- circle(frameCount * 5, 100, 50);
118
+ circle(frameCount * 5, 100, 80);
117
119
  }
118
120
  function mouseClicked() {
119
121
  redraw();
@@ -124,11 +126,11 @@ function mouseClicked() {
124
126
  /** ⭐️
125
127
  * Starts the draw loop again if it was stopped.
126
128
  * @example
127
- new Q5();
129
+ createCanvas(200, 200);
128
130
  noLoop();
129
131
 
130
132
  function draw() {
131
- circle(frameCount * 5, 100, 50);
133
+ circle(frameCount * 5, 100, 80);
132
134
  }
133
135
  function mouseClicked() {
134
136
  loop();
@@ -147,7 +149,7 @@ function draw() {
147
149
  if (mouseIsPressed) frameRate(10);
148
150
  else frameRate(60);
149
151
 
150
- circle(frameCount % 200, 100, 50);
152
+ circle(frameCount % 200, 100, 80);
151
153
  }
152
154
  * @example
153
155
  function draw() {
@@ -239,7 +241,7 @@ function draw() {
239
241
  frameRate(random(30, 60));
240
242
 
241
243
  x += deltaTime * 0.2;
242
- circle(x % 200, 100, 50);
244
+ circle(x % 200, 100, 20);
243
245
  }
244
246
  */
245
247
  var deltaTime: number;
@@ -249,15 +251,25 @@ function draw() {
249
251
  /** ⬜️
250
252
  * Creates a canvas element. If no input parameters are provided, the
251
253
  * canvas will be the size of the window.
254
+ *
255
+ * You can start q5 in top level global mode by running this function
256
+ * before the rest of your code.
252
257
  *
253
258
  * If this function is not run by the user, a 200x200 canvas will be
254
259
  * created automatically.
255
260
  * @param {number} [w] - width of the canvas
256
261
  * @param {number} [h] - height of the canvas
257
262
  * @param {Object} [options] - options for the canvas
258
- * @param {boolean} [options.alpha] - whether the canvas should have an alpha channel, default is false
263
+ * @param {boolean} [options.alpha] - whether the canvas should have an alpha channel that allows it to be seen through, default is false
259
264
  * @param {string} [options.colorSpace] - color space of the canvas, either "srgb" or "display-p3", default is "display-p3" for devices that support HDR colors
260
265
  * @returns {HTMLCanvasElement} created canvas element
266
+ * @example
267
+ createCanvas(200, 200, { alpha: true });
268
+
269
+ function draw() {
270
+ clear();
271
+ circle(frameCount % 200, 100, 80);
272
+ }
261
273
  */
262
274
  function createCanvas(w?: number, h?: number, options?: CanvasRenderingContext2DSettings): HTMLCanvasElement;
263
275
 
@@ -268,87 +280,118 @@ function draw() {
268
280
 
269
281
  /** ⬜️
270
282
  * The width of the canvas.
283
+ * Can also be accessed via `canvas.w`/`canvas.width`.
271
284
  */
272
285
  var width: number;
273
286
 
274
287
  /** ⬜️
275
288
  * The height of the canvas.
289
+ * Can also be accessed via `canvas.h`/`canvas.height`.
276
290
  */
277
291
  var height: number;
278
292
 
279
293
  /** ⬜️
280
- * Resizes the canvas to the specified width and height.
281
- * @param {number} w - width of the canvas
282
- * @param {number} h - height of the canvas
294
+ * Clears the canvas, making every pixel completely transparent.
295
+ *
296
+ * The canvas can only be seen through if it has an alpha channel though.
283
297
  */
284
- function resizeCanvas(w: number, h: number): void;
298
+ function clear(): void;
285
299
 
286
300
  /** ⬜️
287
- * Sets the pixel density of the canvas.
288
- * @param {number} v - pixel density value
289
- * @returns {number} pixel density
290
- */
291
- function pixelDensity(v: number): number;
301
+ * Sets the fill color for shapes. The default is white.
302
+ *
303
+ * Like the [`color`](https://q5js.org/learn/#color) function, this function can accept colors in a wide range of formats: CSS color string, hex string, grayscale value, and color component values.
304
+ * @param {string | Color} color - fill color
305
+ * @example
306
+ function draw() {
307
+ background(200);
292
308
 
293
- /** ⬜️
294
- * Returns the current display density.
295
- * @returns {number} display density
296
- */
297
- function displayDensity(): number;
309
+ fill('red');
310
+ circle(80, 80, 80);
298
311
 
299
- /** ⬜️
300
- * Enables or disables fullscreen mode.
301
- * @param {boolean} [v] - boolean indicating whether to enable or disable fullscreen mode
302
- * @returns {void | boolean} true if fullscreen mode is enabled, false otherwise
312
+ fill('lime');
313
+ square(80, 80, 80);
314
+ }
303
315
  */
304
- function fullscreen(v?: boolean): void | boolean;
316
+ function fill(color: string | Color): void;
305
317
 
306
318
  /** ⬜️
307
- * Any position coordinates or dimensions you use will be scaled based
308
- * on the unit provided to this function.
309
- * @param {number} unit - unit to scale by
319
+ * Sets the stroke (outline) color for shapes. The default is black.
320
+ *
321
+ * Like the [`color`](https://q5js.org/learn/#color) function, this function can accept colors in a wide range of formats: CSS color string, hex string, grayscale value, and color component values.
322
+ * @param {string | Color} color - stroke color
310
323
  * @example
311
- new Q5();
312
- createCanvas(200, 200);
313
- flexibleCanvas(100);
314
- // rect will appear in the middle of the canvas
315
- rect(20, 20, 60, 60);
316
- */
317
- function flexibleCanvas(unit: number): void;
318
-
319
- /** ⬜️
320
- * Sets the fill color for shapes.
321
- * @param {string | number} color - fill color
322
- */
323
- function fill(color: string | number): void;
324
+ function draw() {
325
+ background(200);
326
+ fill(36);
324
327
 
325
- /** ⬜️
326
- * Sets the stroke (outline) color for shapes.
327
- * @param {string | number} color - stroke color
328
+ stroke('red');
329
+ circle(80, 80, 80);
330
+ stroke('lime');
331
+ square(80, 80, 80);
332
+ }
328
333
  */
329
- function stroke(color: string | number): void;
334
+ function stroke(color: string | Color): void;
330
335
 
331
336
  /** ⬜️
332
337
  * After calling this function, shapes will not be filled.
338
+ * @example
339
+ function draw() {
340
+ background(200);
341
+
342
+ noFill();
343
+ stroke('red');
344
+ circle(80, 80, 80);
345
+ stroke('lime');
346
+ square(80, 80, 80);
347
+ }
333
348
  */
334
349
  function noFill(): void;
335
350
 
336
351
  /** ⬜️
337
352
  * After calling this function, shapes will not have a stroke (outline).
353
+ * @example
354
+ function draw() {
355
+ background(200);
356
+ fill(36);
357
+ stroke('red');
358
+ circle(80, 80, 80);
359
+
360
+ noStroke();
361
+ square(80, 80, 80);
362
+ }
338
363
  */
339
364
  function noStroke(): void;
340
365
 
341
366
  /** ⬜️
342
367
  * Sets the size of the stroke used for lines and the border around shapes.
343
368
  * @param {number} weight - size of the stroke in pixels
369
+ * @example
370
+ function setup() {
371
+ createCanvas(200, 200);
372
+ background(200);
373
+ stroke('red');
374
+ circle(50, 100, 80);
375
+
376
+ strokeWeight(20);
377
+ circle(150, 100, 80)
378
+ }
344
379
  */
345
380
  function strokeWeight(weight: number): void;
346
381
 
347
382
  /** ⬜️
348
- * Sets the global opacity, `ctx.globalAlpha`, which
349
- * affects all subsequent drawing operations.
350
- * 0 is completely transparent, 255 is completely opaque.
351
- * @param {number} alpha - opacity level
383
+ * Sets the global opacity, which affects all subsequent drawing operations, except `background`. Default is 1, fully opaque.
384
+ * @param {number} alpha - opacity level, ranging from 0 to 1
385
+ * @example
386
+ function draw() {
387
+ background(200);
388
+
389
+ opacity(1);
390
+ circle(80, 80, 80);
391
+
392
+ opacity(0.2);
393
+ square(80, 80, 80);
394
+ }
352
395
  */
353
396
  function opacity(alpha: number): void;
354
397
 
@@ -434,11 +477,50 @@ rect(20, 20, 60, 60);
434
477
  */
435
478
  function pop(): void;
436
479
 
480
+ /** ⬜️
481
+ * Resizes the canvas to the specified width and height.
482
+ * @param {number} w - width of the canvas
483
+ * @param {number} h - height of the canvas
484
+ */
485
+ function resizeCanvas(w: number, h: number): void;
486
+
487
+ /** ⬜️
488
+ * Sets the pixel density of the canvas.
489
+ * @param {number} v - pixel density value
490
+ * @returns {number} pixel density
491
+ */
492
+ function pixelDensity(v: number): number;
493
+
494
+ /** ⬜️
495
+ * Returns the current display density.
496
+ * @returns {number} display density
497
+ */
498
+ function displayDensity(): number;
499
+
500
+ /** ⬜️
501
+ * Enables or disables fullscreen mode.
502
+ * @param {boolean} [v] - boolean indicating whether to enable or disable fullscreen mode
503
+ * @returns {void | boolean} true if fullscreen mode is enabled, false otherwise
504
+ */
505
+ function fullscreen(v?: boolean): void | boolean;
506
+
507
+ /** ⬜️
508
+ * Any position coordinates or dimensions you use will be scaled based
509
+ * on the unit provided to this function.
510
+ * @param {number} unit - unit to scale by
511
+ * @example
512
+ createCanvas(200, 200);
513
+ flexibleCanvas(100);
514
+ // rect will appear in the middle of the canvas
515
+ rect(20, 20, 60, 60);
516
+ */
517
+ function flexibleCanvas(unit: number): void;
518
+
437
519
  /** ⬜️
438
520
  * Creates a graphics buffer.
439
521
  * @param {number} w - width
440
522
  * @param {number} h - height
441
- * @param {CanvasRenderingContext2DSettings} [opt] - options
523
+ * @param {Object} [opt] - options
442
524
  * @returns {Q5} a new Q5 graphics buffer
443
525
  */
444
526
  function createGraphics(w: number, h: number, opt?: CanvasRenderingContext2DSettings): Q5;
@@ -458,12 +540,12 @@ rect(20, 20, 60, 60);
458
540
  /** 💻
459
541
  * The `displayMode` function lets you customize how your canvas is presented.
460
542
  * @param {string} mode -
461
- * - "normal": no styling to canvas or its parent element
543
+ * - "normal": (default) no styling to canvas or its parent element
462
544
  * - "centered": canvas will be centered horizontally and vertically within its parent and if it's display size is bigger than its parent it will not clip
463
545
  * - "maxed": canvas will fill the parent element, same as fullscreen for a global mode canvas inside a `main` element
464
546
  * - "fullscreen": canvas will fill the screen with letterboxing if necessary to preserve its aspect ratio, like css object-fit contain
465
547
  * @param {string} renderQuality -
466
- * - "default": pixelDensity set to displayDensity
548
+ * - "smooth": (default) no changes to the default render quality
467
549
  * - "pixelated": pixelDensity set to 1 and various css styles are applied to the canvas to make it render without image smoothing
468
550
  * @param {number} scale - can be given as a string (for example "x2") or a number
469
551
  */
@@ -530,16 +612,7 @@ rect(20, 20, 60, 60);
530
612
  * @param {number} [mode] - drawing mode, can be `PIE`, `CHORD`, or `OPEN`
531
613
  * @param {number} [detail] - resolution of the arc
532
614
  */
533
- function arc(
534
- x: number,
535
- y: number,
536
- w: number,
537
- h: number,
538
- start: number,
539
- stop: number,
540
- mode?: number,
541
- detail?: number
542
- ): void;
615
+ function arc(x: number, y: number, w: number, h: number, start: number, stop: number, mode?: number, detail?: number): void;
543
616
 
544
617
  /** 🧑‍🎨
545
618
  * Draws a line on the canvas.
@@ -715,16 +788,74 @@ rect(20, 20, 60, 60);
715
788
  // 🌆 image
716
789
 
717
790
  /** 🌆
718
- * Applies a filter to the image.
719
- * @param {string} type - type of filter
720
- * @param {number} [value] - optional parameter, depending on filter type
791
+ * Loads an image from a URL and optionally runs a callback function.
792
+ * @param {string} url - url of the image to load
793
+ * @param {(img: any) => void} [cb] - callback function after the image is loaded
794
+ * @param {any} [opt] - optional parameters for loading the image
795
+ * @example
796
+ createCanvas(200, 200);
797
+
798
+ let logo = loadImage('/q5js_logo.webp');
799
+
800
+ function draw() {
801
+ image(logo, 0, 0, 200, 200);
802
+ }
721
803
  */
722
- function filter(type: string, value?: number): void;
804
+ function loadImage(url: string, cb?: (img: any) => void, opt?: any): void;
805
+
806
+ /** 🌆
807
+ * Draws an image to the canvas.
808
+ * @param {any} img - image to draw
809
+ * @param {number} dx - x position to draw the image at
810
+ * @param {number} dy - y position to draw the image at
811
+ * @param {number} [dw] - width of the destination image
812
+ * @param {number} [dh] - height of the destination image
813
+ * @param {number} [sx] - x position in the source to start clipping a subsection from
814
+ * @param {number} [sy] - y position in the source to start clipping a subsection from
815
+ * @param {number} [sw] - width of the subsection of the source image
816
+ * @param {number} [sh] - height of the subsection of the source image
817
+ * @example
818
+ createCanvas(200, 200);
819
+
820
+ let logo = loadImage('/q5js_logo.webp');
821
+
822
+ function draw() {
823
+ image(logo, 0, 0, 200, 200, 256, 256, 512, 512);
824
+ }
825
+ */
826
+ function image(img: any, dx: number, dy: number, dw?: number, dh?: number, sx?: number, sy?: number, sw?: number, sh?: number): void;
827
+
828
+ /** 🌆
829
+ * Sets the image mode, which determines the position and alignment of images drawn on the canvas.
830
+ * - `CORNER`: (default) images will be drawn from the top-left corner
831
+ * - `CORNERS`: images will be drawn from the top-left to the bottom-right corner
832
+ * - `CENTER`: images will be drawn centered at (dx, dy)
833
+ * @param {string} mode
834
+ * @example
835
+ createCanvas(200, 200);
836
+
837
+ let logo = loadImage('/q5js_logo.webp');
838
+
839
+ function draw() {
840
+ imageMode(CENTER);
841
+ image(logo, 100, 100, 200, 200);
842
+ }
843
+ */
844
+ function imageMode(mode: string): void;
723
845
 
724
846
  /** 🌆
725
847
  * Resizes the image.
726
848
  * @param {number} w - new width
727
849
  * @param {number} h - new height
850
+ * @example
851
+ createCanvas(200, 200);
852
+
853
+ let logo = loadImage('/q5js_logo.webp');
854
+
855
+ function setup() {
856
+ logo.resize(128, 128);
857
+ image(logo, 0, 0, 200, 200);
858
+ }
728
859
  */
729
860
  function resize(w: number, h: number): void;
730
861
 
@@ -734,6 +865,28 @@ rect(20, 20, 60, 60);
734
865
  */
735
866
  function trim(): Image;
736
867
 
868
+ /** 🌆
869
+ * Enables smooth image rendering.
870
+ */
871
+ function smooth(): void;
872
+
873
+ /** 🌆
874
+ * Disables smooth image rendering for a pixelated look.
875
+ */
876
+ function noSmooth(): void;
877
+
878
+ /** 🌆
879
+ * Applies a tint (color overlay) to the drawing.
880
+ * Tinting affects all subsequent images drawn.
881
+ * @param {string | number} color - tint color
882
+ */
883
+ function tint(color: string | number): void;
884
+
885
+ /** 🌆
886
+ * Images drawn after this function is run will not be tinted.
887
+ */
888
+ function noTint(): void;
889
+
737
890
  /** 🌆
738
891
  * Masks the image with another image.
739
892
  * @param {Image} img - image to use as a mask
@@ -751,25 +904,27 @@ rect(20, 20, 60, 60);
751
904
  /** 🌆
752
905
  * Displays a region of the image on another region of the image.
753
906
  * Can be used to create a detail inset, aka a magnifying glass effect.
754
- * @param {number} srcX - x-coordinate of the source region
755
- * @param {number} srcY - y-coordinate of the source region
756
- * @param {number} srcW - width of the source region
757
- * @param {number} srcH - height of the source region
758
- * @param {number} dstX - x-coordinate of the destination region
759
- * @param {number} dstY - y-coordinate of the destination region
760
- * @param {number} dstW - width of the destination region
761
- * @param {number} dstH - height of the destination region
762
- */
763
- function inset(
764
- srcX: number,
765
- srcY: number,
766
- srcW: number,
767
- srcH: number,
768
- dstX: number,
769
- dstY: number,
770
- dstW: number,
771
- dstH: number
772
- ): void;
907
+ * @param {number} sx - x-coordinate of the source region
908
+ * @param {number} sy - y-coordinate of the source region
909
+ * @param {number} sw - width of the source region
910
+ * @param {number} sh - height of the source region
911
+ * @param {number} dx - x-coordinate of the destination region
912
+ * @param {number} dy - y-coordinate of the destination region
913
+ * @param {number} dw - width of the destination region
914
+ * @param {number} dh - height of the destination region
915
+ * @example
916
+ let logo;
917
+ function preload() {
918
+ logo = loadImage('/q5js_logo.webp');
919
+ }
920
+ function setup() {
921
+ logo.inset(256, 256, 512, 512, 0, 0, 200, 200);
922
+ }
923
+ function draw() {
924
+
925
+ }
926
+ */
927
+ function inset(sx: number, sy: number, sw: number, sh: number, dx: number, dy: number, dw: number, dh: number): void;
773
928
 
774
929
  /** 🌆
775
930
  * Returns a copy of the image.
@@ -814,76 +969,17 @@ rect(20, 20, 60, 60);
814
969
  function updatePixels(): void;
815
970
 
816
971
  /** 🌆
817
- * Enables smooth image rendering.
818
- */
819
- function smooth(): void;
820
-
821
- /** 🌆
822
- * Disables smooth image rendering for a pixelated look.
823
- */
824
- function noSmooth(): void;
825
-
826
- /** 🌆
827
- * Applies a tint (color overlay) to the drawing.
828
- * Tinting affects all subsequent images drawn.
829
- * @param {string | number} color - tint color
830
- */
831
- function tint(color: string | number): void;
832
-
833
- /** 🌆
834
- * Images drawn after this function is run will not be tinted.
835
- */
836
- function noTint(): void;
837
-
838
- /** 🌆
839
- * Creates a new image.
840
- * @param {number} w - width
841
- * @param {number} h - height
842
- * @param {any} [opt] - optional settings for the image
843
- * @returns {Image}
844
- */
845
- function createImage(w: number, h: number, opt?: any): Image;
846
-
847
- /** 🌆
848
- * Sets the image mode, which determines the position and alignment of images drawn on the canvas.
849
- * - 'CORNER': images will be drawn from the top-left corner (default).
850
- * - 'CORNERS': images will be drawn from the top-left to the bottom-right corner.
851
- * - 'CENTER': images will be drawn centered at (dx, dy).
852
- * @param {string} mode
972
+ * Masks the image with another image.
973
+ * @param {Image} img - image to use as a mask
853
974
  */
854
- function imageMode(mode: string): void;
855
-
856
- /** 🌆
857
- * Draws an image to the canvas.
858
- * @param {any} img - image to draw
859
- * @param {number} dx - x-coordinate of the destination corner
860
- * @param {number} dy - y-coordinate of the destination corner
861
- * @param {number} [dWidth] - width of the destination image
862
- * @param {number} [dHeight] - height of the destination image
863
- * @param {number} [sx] - x-coordinate of the source corner
864
- * @param {number} [sy] - y-coordinate of the source corner
865
- * @param {number} [sWidth] - width of the source image
866
- * @param {number} [sHeight] - height of the source image
867
- */
868
- function image(
869
- img: any,
870
- dx: number,
871
- dy: number,
872
- dWidth?: number,
873
- dHeight?: number,
874
- sx?: number,
875
- sy?: number,
876
- sWidth?: number,
877
- sHeight?: number
878
- ): void;
975
+ function mask(img: Image): void;
879
976
 
880
977
  /** 🌆
881
- * Loads an image from a URL and optionally runs a callback function.
882
- * @param {string} url - uRL of the image to load
883
- * @param {(img: any) => void} [cb] - callback function after the image is loaded
884
- * @param {any} [opt] - optional parameters for loading the image
978
+ * Applies a filter to the image.
979
+ * @param {string} type - type of filter
980
+ * @param {number} [value] - optional parameter, depending on filter type
885
981
  */
886
- function loadImage(url: string, cb?: (img: any) => void, opt?: any): void;
982
+ function filter(type: string, value?: number): void;
887
983
 
888
984
  /** 🌆
889
985
  * Converts the image to black and white pixels depending if they are above or below a certain threshold.
@@ -925,6 +1021,15 @@ rect(20, 20, 60, 60);
925
1021
  */
926
1022
  const BLUR: 8;
927
1023
 
1024
+ /** 🌆
1025
+ * Creates a new image.
1026
+ * @param {number} w - width
1027
+ * @param {number} h - height
1028
+ * @param {any} [opt] - optional settings for the image
1029
+ * @returns {Image}
1030
+ */
1031
+ function createImage(w: number, h: number, opt?: any): Image;
1032
+
928
1033
  // ✍️ text
929
1034
 
930
1035
  /** ✍️