p5 2.0.0 → 2.0.1

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 (188) hide show
  1. package/{src → dist}/accessibility/color_namer.js +48 -3
  2. package/{src → dist}/accessibility/describe.js +2 -2
  3. package/{src → dist}/accessibility/gridOutput.js +2 -2
  4. package/dist/accessibility/index.js +60 -0
  5. package/{src → dist}/accessibility/outputs.js +2 -2
  6. package/{src → dist}/accessibility/textOutput.js +2 -2
  7. package/dist/app.js +120 -0
  8. package/{src → dist}/color/color_conversion.js +48 -10
  9. package/{src → dist}/color/color_spaces/hsb.js +3 -1
  10. package/dist/color/creating_reading.js +3 -0
  11. package/dist/color/index.js +13 -0
  12. package/dist/color/p5.Color.culori.js +1 -0
  13. package/dist/color/p5.Color.js +3 -0
  14. package/{src → dist}/color/setting.js +9 -6
  15. package/{src/core/constants.js → dist/constants-C-g_eAdC.js} +266 -130
  16. package/{src → dist}/core/States.js +3 -1
  17. package/dist/core/constants.js +1 -0
  18. package/{src → dist}/core/environment.js +7 -6
  19. package/{src → dist}/core/friendly_errors/browser_errors.js +1 -1
  20. package/{src → dist}/core/friendly_errors/fes_core.js +14 -44
  21. package/{src → dist}/core/friendly_errors/file_errors.js +6 -3
  22. package/dist/core/friendly_errors/index.js +23 -0
  23. package/dist/core/friendly_errors/param_validator.js +5455 -0
  24. package/{src → dist}/core/friendly_errors/sketch_reader.js +50 -4
  25. package/{src → dist}/core/friendly_errors/sketch_verifier.js +6 -6
  26. package/{src → dist}/core/friendly_errors/stacktrace.js +3 -5
  27. package/{src → dist}/core/friendly_errors/validate_params.js +50 -41
  28. package/{src → dist}/core/helpers.js +9 -6
  29. package/dist/core/init.js +105 -0
  30. package/dist/core/internationalization.js +302 -0
  31. package/dist/core/legacy.js +73 -0
  32. package/dist/core/main.js +44 -0
  33. package/dist/core/noop.js +3 -0
  34. package/dist/core/p5.Graphics.js +40 -0
  35. package/dist/core/p5.Renderer.js +11 -0
  36. package/dist/core/p5.Renderer2D.js +44 -0
  37. package/dist/core/reference.js +1 -0
  38. package/dist/core/rendering.js +40 -0
  39. package/{src → dist}/core/structure.js +3 -3
  40. package/{src → dist}/core/transform.js +2 -2
  41. package/{src/color/creating_reading.js → dist/creating_reading-D4AAKRbx.js} +841 -13
  42. package/{src → dist}/data/index.js +3 -1
  43. package/{src → dist}/data/local_storage.js +2 -8
  44. package/{src → dist}/dom/dom.js +11 -5
  45. package/dist/dom/index.js +18 -0
  46. package/{src → dist}/dom/p5.Element.js +14 -12
  47. package/{src → dist}/dom/p5.File.js +4 -4
  48. package/{src → dist}/dom/p5.MediaElement.js +10 -4
  49. package/{src → dist}/events/acceleration.js +2 -2
  50. package/{src → dist}/events/index.js +3 -1
  51. package/{src → dist}/events/keyboard.js +14 -11
  52. package/{src → dist}/events/pointer.js +16 -17
  53. package/dist/image/const.js +9 -0
  54. package/{src → dist}/image/filterRenderer2D.js +57 -37
  55. package/{src → dist}/image/filters.js +1 -3
  56. package/dist/image/image.js +40 -0
  57. package/dist/image/index.js +51 -0
  58. package/dist/image/loading_displaying.js +40 -0
  59. package/dist/image/p5.Image.js +11 -0
  60. package/{src → dist}/image/pixels.js +4 -3
  61. package/{src → dist}/io/csv.js +72 -70
  62. package/dist/io/files.js +40 -0
  63. package/dist/io/index.js +51 -0
  64. package/{src → dist}/io/p5.Table.js +6 -6
  65. package/{src → dist}/io/p5.TableRow.js +3 -4
  66. package/{src → dist}/io/p5.XML.js +2 -5
  67. package/{src → dist}/io/utilities.js +1 -1
  68. package/{src/core/p5.Renderer2D.js → dist/main-s72KWcUy.js} +735 -57
  69. package/{src → dist}/math/Matrices/Matrix.js +10 -8
  70. package/{src → dist}/math/Matrices/MatrixInterface.js +5 -3
  71. package/{src → dist}/math/Matrices/MatrixNumjs.js +12 -26
  72. package/{src → dist}/math/calculation.js +2 -2
  73. package/{src → dist}/math/index.js +6 -3
  74. package/{src → dist}/math/math.js +2 -2
  75. package/{src → dist}/math/noise.js +2 -2
  76. package/{src → dist}/math/p5.Matrix.js +7 -4
  77. package/{src → dist}/math/p5.Vector.js +6 -6
  78. package/{src → dist}/math/random.js +2 -2
  79. package/{src → dist}/math/trigonometry.js +16 -15
  80. package/{src/image/p5.Image.js → dist/p5.Renderer-CwAYZOC2.js} +390 -19
  81. package/dist/rendering--aAe5aq3.js +24925 -0
  82. package/{src → dist}/shape/2d_primitives.js +18 -17
  83. package/{src → dist}/shape/attributes.js +18 -17
  84. package/{src → dist}/shape/curves.js +2 -2
  85. package/{src → dist}/shape/custom_shapes.js +44 -64
  86. package/{src → dist}/shape/index.js +10 -2
  87. package/{src → dist}/shape/vertex.js +2 -3
  88. package/dist/type/index.js +25 -0
  89. package/{src → dist}/type/lib/Typr.js +76 -94
  90. package/{src → dist}/type/p5.Font.js +37 -61
  91. package/{src → dist}/type/textCore.js +34 -57
  92. package/{src → dist}/type/unicodeRanges.js +3 -1
  93. package/{src → dist}/utilities/conversion.js +2 -2
  94. package/{src → dist}/utilities/index.js +3 -1
  95. package/{src → dist}/utilities/time_date.js +6 -7
  96. package/{src → dist}/utilities/utility_functions.js +2 -2
  97. package/dist/webgl/3d_primitives.js +40 -0
  98. package/{src → dist}/webgl/GeometryBufferCache.js +3 -1
  99. package/{src → dist}/webgl/GeometryBuilder.js +12 -8
  100. package/{src → dist}/webgl/ShaderGenerator.js +79 -82
  101. package/{src → dist}/webgl/ShapeBuilder.js +26 -23
  102. package/dist/webgl/index.js +76 -0
  103. package/{src → dist}/webgl/interaction.js +7 -6
  104. package/dist/webgl/light.js +40 -0
  105. package/{src → dist}/webgl/loading.js +45 -12
  106. package/dist/webgl/material.js +40 -0
  107. package/dist/webgl/p5.Camera.js +40 -0
  108. package/{src → dist}/webgl/p5.DataArray.js +3 -5
  109. package/dist/webgl/p5.Framebuffer.js +40 -0
  110. package/{src → dist}/webgl/p5.Geometry.js +12 -15
  111. package/{src → dist}/webgl/p5.Quat.js +5 -4
  112. package/{src → dist}/webgl/p5.RenderBuffer.js +2 -3
  113. package/dist/webgl/p5.RendererGL.js +40 -0
  114. package/dist/webgl/p5.Shader.js +40 -0
  115. package/dist/webgl/p5.Texture.js +40 -0
  116. package/{src → dist}/webgl/text.js +51 -9
  117. package/lib/p5.esm.js +102 -48
  118. package/lib/p5.js +102 -48
  119. package/lib/p5.min.js +1 -1
  120. package/package.json +17 -16
  121. package/translations/dev.js +6 -6
  122. package/translations/index.js +1 -1
  123. package/src/README.md +0 -27
  124. package/src/accessibility/index.js +0 -13
  125. package/src/app.js +0 -61
  126. package/src/color/index.js +0 -9
  127. package/src/color/p5.Color.culori.js +0 -66
  128. package/src/color/p5.Color.js +0 -851
  129. package/src/core/README.md +0 -91
  130. package/src/core/friendly_errors/index.js +0 -13
  131. package/src/core/friendly_errors/param_validator.js +0 -561
  132. package/src/core/init.js +0 -58
  133. package/src/core/internationalization.js +0 -195
  134. package/src/core/legacy.js +0 -29
  135. package/src/core/main.js +0 -689
  136. package/src/core/noop.js +0 -1
  137. package/src/core/p5.Graphics.js +0 -696
  138. package/src/core/p5.Renderer.js +0 -408
  139. package/src/core/reference.js +0 -2060
  140. package/src/core/rendering.js +0 -697
  141. package/src/dom/index.js +0 -11
  142. package/src/image/const.js +0 -6
  143. package/src/image/image.js +0 -731
  144. package/src/image/index.js +0 -15
  145. package/src/image/loading_displaying.js +0 -1431
  146. package/src/io/files.js +0 -2210
  147. package/src/io/index.js +0 -11
  148. package/src/math/README.md +0 -40
  149. package/src/type/index.js +0 -9
  150. package/src/webgl/3d_primitives.js +0 -2741
  151. package/src/webgl/index.js +0 -37
  152. package/src/webgl/light.js +0 -1851
  153. package/src/webgl/material.js +0 -3854
  154. package/src/webgl/p5.Camera.js +0 -4010
  155. package/src/webgl/p5.Framebuffer.js +0 -1865
  156. package/src/webgl/p5.RendererGL.js +0 -2867
  157. package/src/webgl/p5.Shader.js +0 -1505
  158. package/src/webgl/p5.Texture.js +0 -541
  159. package/src/webgl/shaders/basic.frag +0 -6
  160. package/src/webgl/shaders/filters/base.frag +0 -22
  161. package/src/webgl/shaders/filters/base.vert +0 -19
  162. package/src/webgl/shaders/filters/blur.frag +0 -60
  163. package/src/webgl/shaders/filters/default.vert +0 -18
  164. package/src/webgl/shaders/filters/dilate.frag +0 -39
  165. package/src/webgl/shaders/filters/erode.frag +0 -39
  166. package/src/webgl/shaders/filters/gray.frag +0 -16
  167. package/src/webgl/shaders/filters/invert.frag +0 -15
  168. package/src/webgl/shaders/filters/opaque.frag +0 -12
  169. package/src/webgl/shaders/filters/posterize.frag +0 -29
  170. package/src/webgl/shaders/filters/threshold.frag +0 -23
  171. package/src/webgl/shaders/font.frag +0 -216
  172. package/src/webgl/shaders/font.vert +0 -44
  173. package/src/webgl/shaders/imageLight.vert +0 -33
  174. package/src/webgl/shaders/imageLightDiffused.frag +0 -82
  175. package/src/webgl/shaders/imageLightSpecular.frag +0 -134
  176. package/src/webgl/shaders/light.vert +0 -37
  177. package/src/webgl/shaders/light_texture.frag +0 -26
  178. package/src/webgl/shaders/lighting.glsl +0 -227
  179. package/src/webgl/shaders/line.frag +0 -74
  180. package/src/webgl/shaders/line.vert +0 -294
  181. package/src/webgl/shaders/normal.frag +0 -6
  182. package/src/webgl/shaders/normal.vert +0 -72
  183. package/src/webgl/shaders/phong.frag +0 -84
  184. package/src/webgl/shaders/phong.vert +0 -87
  185. package/src/webgl/shaders/point.frag +0 -29
  186. package/src/webgl/shaders/point.vert +0 -19
  187. package/src/webgl/shaders/sphereMapping.frag +0 -26
  188. package/src/webgl/shaders/webgl2Compatibility.glsl +0 -34
@@ -1,15 +1,16 @@
1
- import * as constants from './constants';
2
- import p5 from './main';
3
- import { Renderer } from './p5.Renderer';
4
- import { Graphics } from './p5.Graphics';
5
- import { Image } from '../image/p5.Image';
6
- import { Element } from '../dom/p5.Element';
7
- import { MediaElement } from '../dom/p5.MediaElement';
8
- import { RGBHDR } from '../color/creating_reading';
9
- import FilterRenderer2D from '../image/filterRenderer2D';
10
- import { Matrix } from '../math/p5.Matrix';
11
- import { PrimitiveToPath2DConverter } from '../shape/custom_shapes';
12
-
1
+ import { P as P2D, a0 as WEBGL, u as BLEND, a2 as _DEFAULT_FILL, a3 as _DEFAULT_STROKE, j as ROUND, a4 as REMOVE, a5 as SUBTRACT, a6 as DARKEST, a7 as LIGHTEST, a8 as DIFFERENCE, a9 as MULTIPLY, aa as EXCLUSION, ab as SCREEN, ac as REPLACE, ad as OVERLAY, ae as HARD_LIGHT, af as SOFT_LIGHT, ag as DODGE, ah as BURN, ai as ADD, aj as PIE, ak as CHORD, f as TWO_PI, S as SQUARE, k as PROJECT, B as BEVEL, l as MITER, O as OPEN, v as constants, al as VERSION } from './constants-C-g_eAdC.js';
2
+ import transform from './core/transform.js';
3
+ import structure from './core/structure.js';
4
+ import environment from './core/environment.js';
5
+ import { G as Graphics, y as rendering, z as graphics } from './rendering--aAe5aq3.js';
6
+ import { R as Renderer, I as Image, r as renderer } from './p5.Renderer-CwAYZOC2.js';
7
+ import { Element } from './dom/p5.Element.js';
8
+ import { MediaElement } from './dom/p5.MediaElement.js';
9
+ import { b as RGBHDR } from './creating_reading-D4AAKRbx.js';
10
+ import FilterRenderer2D from './image/filterRenderer2D.js';
11
+ import './math/p5.Matrix.js';
12
+ import { PrimitiveToPath2DConverter } from './shape/custom_shapes.js';
13
+ import { Matrix } from './math/Matrices/Matrix.js';
13
14
 
14
15
  const styleEmpty = 'rgba(0,0,0,0)';
15
16
  // const alphaThreshold = 0.00125; // minimum visible
@@ -39,7 +40,7 @@ class Renderer2D extends Renderer {
39
40
  get() {
40
41
  return this.wrappedElt[p];
41
42
  }
42
- })
43
+ });
43
44
  }
44
45
  }
45
46
 
@@ -96,7 +97,7 @@ class Renderer2D extends Renderer {
96
97
  new Graphics(
97
98
  this.width,
98
99
  this.height,
99
- constants.WEBGL,
100
+ WEBGL,
100
101
  pInst
101
102
  );
102
103
  }
@@ -119,10 +120,10 @@ class Renderer2D extends Renderer {
119
120
  _applyDefaults() {
120
121
  this.states.setValue('_cachedFillStyle', undefined);
121
122
  this.states.setValue('_cachedStrokeStyle', undefined);
122
- this._cachedBlendMode = constants.BLEND;
123
- this._setFill(constants._DEFAULT_FILL);
124
- this._setStroke(constants._DEFAULT_STROKE);
125
- this.drawingContext.lineCap = constants.ROUND;
123
+ this._cachedBlendMode = BLEND;
124
+ this._setFill(_DEFAULT_FILL);
125
+ this._setStroke(_DEFAULT_STROKE);
126
+ this.drawingContext.lineCap = ROUND;
126
127
  this.drawingContext.font = 'normal 12px sans-serif';
127
128
  }
128
129
 
@@ -242,7 +243,7 @@ class Renderer2D extends Renderer {
242
243
 
243
244
  // cache blendMode
244
245
  const tempBlendMode = this._cachedBlendMode;
245
- this.blendMode(constants.REMOVE);
246
+ this.blendMode(REMOVE);
246
247
  this._cachedBlendMode = tempBlendMode;
247
248
 
248
249
  this._isErasing = true;
@@ -290,7 +291,7 @@ class Renderer2D extends Renderer {
290
291
 
291
292
  // cache blendMode
292
293
  const tempBlendMode = this._cachedBlendMode;
293
- this.blendMode(constants.BLEND);
294
+ this.blendMode(BLEND);
294
295
  this._cachedBlendMode = tempBlendMode;
295
296
 
296
297
  // Since everything must be in one path, create a new single Path2D to chain all shapes onto.
@@ -463,24 +464,24 @@ class Renderer2D extends Renderer {
463
464
  //////////////////////////////////////////////
464
465
 
465
466
  blendMode(mode) {
466
- if (mode === constants.SUBTRACT) {
467
+ if (mode === SUBTRACT) {
467
468
  console.warn('blendMode(SUBTRACT) only works in WEBGL mode.');
468
469
  } else if (
469
- mode === constants.BLEND ||
470
- mode === constants.REMOVE ||
471
- mode === constants.DARKEST ||
472
- mode === constants.LIGHTEST ||
473
- mode === constants.DIFFERENCE ||
474
- mode === constants.MULTIPLY ||
475
- mode === constants.EXCLUSION ||
476
- mode === constants.SCREEN ||
477
- mode === constants.REPLACE ||
478
- mode === constants.OVERLAY ||
479
- mode === constants.HARD_LIGHT ||
480
- mode === constants.SOFT_LIGHT ||
481
- mode === constants.DODGE ||
482
- mode === constants.BURN ||
483
- mode === constants.ADD
470
+ mode === BLEND ||
471
+ mode === REMOVE ||
472
+ mode === DARKEST ||
473
+ mode === LIGHTEST ||
474
+ mode === DIFFERENCE ||
475
+ mode === MULTIPLY ||
476
+ mode === EXCLUSION ||
477
+ mode === SCREEN ||
478
+ mode === REPLACE ||
479
+ mode === OVERLAY ||
480
+ mode === HARD_LIGHT ||
481
+ mode === SOFT_LIGHT ||
482
+ mode === DODGE ||
483
+ mode === BURN ||
484
+ mode === ADD
484
485
  ) {
485
486
  this._cachedBlendMode = mode;
486
487
  this.drawingContext.globalCompositeOperation = mode;
@@ -640,11 +641,6 @@ class Renderer2D extends Renderer {
640
641
  */
641
642
  arc(x, y, w, h, start, stop, mode) {
642
643
  const ctx = this.clipPa || this.drawingContext;
643
- const rx = w / 2.0;
644
- const ry = h / 2.0;
645
- const epsilon = 0.00001; // Smallest visible angle on displays up to 4K.
646
- let arcToDraw = 0;
647
- const curves = [];
648
644
 
649
645
  const centerX = x + w / 2,
650
646
  centerY = y + h / 2,
@@ -655,9 +651,9 @@ class Renderer2D extends Renderer {
655
651
  // when the mode is PIE or default; as well as when the start and end
656
652
  // angles do not form a full circle.
657
653
  const createPieSlice = ! (
658
- mode === constants.CHORD ||
659
- mode === constants.OPEN ||
660
- (stop - start) % constants.TWO_PI === 0
654
+ mode === CHORD ||
655
+ mode === OPEN ||
656
+ (stop - start) % TWO_PI === 0
661
657
  );
662
658
 
663
659
  // Fill curves
@@ -674,13 +670,13 @@ class Renderer2D extends Renderer {
674
670
  if (!this._clipping) ctx.beginPath();
675
671
  ctx.ellipse(centerX, centerY, radiusX, radiusY, 0, start, stop);
676
672
 
677
- if (mode === constants.PIE && createPieSlice) {
673
+ if (mode === PIE && createPieSlice) {
678
674
  // In PIE mode, stroke is added to the center and back to path,
679
675
  // unless the pie forms a complete ellipse (see: createPieSlice)
680
676
  ctx.lineTo(centerX, centerY);
681
677
  }
682
678
 
683
- if (mode === constants.PIE || mode === constants.CHORD) {
679
+ if (mode === PIE || mode === CHORD) {
684
680
  // Stroke connects back to path begin for both PIE and CHORD
685
681
  ctx.closePath();
686
682
  }
@@ -754,7 +750,7 @@ class Renderer2D extends Renderer {
754
750
  this._setFill(s);
755
751
  }
756
752
  if (!this._clipping) ctx.beginPath();
757
- ctx.arc(x, y, ctx.lineWidth / 2, 0, constants.TWO_PI, false);
753
+ ctx.arc(x, y, ctx.lineWidth / 2, 0, TWO_PI, false);
758
754
  if (!this._clipping) {
759
755
  ctx.fill();
760
756
  this._setFill(f);
@@ -910,9 +906,9 @@ class Renderer2D extends Renderer {
910
906
 
911
907
  strokeCap(cap) {
912
908
  if (
913
- cap === constants.ROUND ||
914
- cap === constants.SQUARE ||
915
- cap === constants.PROJECT
909
+ cap === ROUND ||
910
+ cap === SQUARE ||
911
+ cap === PROJECT
916
912
  ) {
917
913
  this.drawingContext.lineCap = cap;
918
914
  }
@@ -921,9 +917,9 @@ class Renderer2D extends Renderer {
921
917
 
922
918
  strokeJoin(join) {
923
919
  if (
924
- join === constants.ROUND ||
925
- join === constants.BEVEL ||
926
- join === constants.MITER
920
+ join === ROUND ||
921
+ join === BEVEL ||
922
+ join === MITER
927
923
  ) {
928
924
  this.drawingContext.lineJoin = join;
929
925
  }
@@ -1052,13 +1048,695 @@ function renderer2D(p5, fn){
1052
1048
  * @private
1053
1049
  */
1054
1050
  p5.Renderer2D = Renderer2D;
1055
- p5.renderers[constants.P2D] = Renderer2D;
1051
+ p5.renderers[P2D] = Renderer2D;
1056
1052
  p5.renderers['p2d-hdr'] = new Proxy(Renderer2D, {
1057
1053
  construct(target, [pInst, w, h, isMainCanvas, elt]){
1058
1054
  return new target(pInst, w, h, isMainCanvas, elt, {colorSpace: "display-p3"})
1059
1055
  }
1060
- })
1056
+ });
1057
+ }
1058
+
1059
+ /**
1060
+ * @module Structure
1061
+ * @submodule Structure
1062
+ * @for p5
1063
+ * @requires constants
1064
+ */
1065
+
1066
+
1067
+ /**
1068
+ * This is the p5 instance constructor.
1069
+ *
1070
+ * A p5 instance holds all the properties and methods related to
1071
+ * a p5 sketch. It expects an incoming sketch closure and it can also
1072
+ * take an optional node parameter for attaching the generated p5 canvas
1073
+ * to a node. The sketch closure takes the newly created p5 instance as
1074
+ * its sole argument and may optionally set an asynchronous function
1075
+ * using `async/await`, along with the standard <a href="#/p5/setup">setup()</a>,
1076
+ * and/or <a href="#/p5/setup">setup()</a>, and/or <a href="#/p5/draw">draw()</a>
1077
+ * properties on it for running a sketch.
1078
+ *
1079
+ * A p5 sketch can run in "global" or "instance" mode:
1080
+ * "global" - all properties and methods are attached to the window
1081
+ * "instance" - all properties and methods are bound to this p5 object
1082
+ *
1083
+ * @class p5
1084
+ * @param {function(p5)} sketch a closure that can set optional <a href="#/p5/preload">preload()</a>,
1085
+ * <a href="#/p5/setup">setup()</a>, and/or <a href="#/p5/draw">draw()</a> properties on the
1086
+ * given p5 instance
1087
+ * @param {HTMLElement} [node] element to attach canvas to
1088
+ * @return {p5} a p5 instance
1089
+ */
1090
+ class p5 {
1091
+ static VERSION = VERSION;
1092
+ // This is a pointer to our global mode p5 instance, if we're in
1093
+ // global mode.
1094
+ static instance = null;
1095
+ static lifecycleHooks = {
1096
+ presetup: [],
1097
+ postsetup: [],
1098
+ predraw: [],
1099
+ postdraw: [],
1100
+ remove: []
1101
+ };
1102
+
1103
+ // FES stub
1104
+ static _checkForUserDefinedFunctions = () => {};
1105
+ static _friendlyFileLoadError = () => {};
1106
+
1107
+ constructor(sketch, node) {
1108
+ //////////////////////////////////////////////
1109
+ // PRIVATE p5 PROPERTIES AND METHODS
1110
+ //////////////////////////////////////////////
1111
+
1112
+ this.hitCriticalError = false;
1113
+ this._setupDone = false;
1114
+ this._userNode = node;
1115
+ this._curElement = null;
1116
+ this._elements = [];
1117
+ this._glAttributes = null;
1118
+ this._requestAnimId = 0;
1119
+ this._isGlobal = false;
1120
+ this._loop = true;
1121
+ this._startListener = null;
1122
+ this._initializeInstanceVariables();
1123
+ this._events = {
1124
+ // keep track of user-events for unregistering later
1125
+ pointerdown: null,
1126
+ pointerup: null,
1127
+ pointermove: null,
1128
+ dragend: null,
1129
+ dragover: null,
1130
+ click: null,
1131
+ dblclick: null,
1132
+ mouseover: null,
1133
+ mouseout: null,
1134
+ keydown: null,
1135
+ keyup: null,
1136
+ keypress: null,
1137
+ wheel: null,
1138
+ resize: null,
1139
+ blur: null
1140
+ };
1141
+ this._millisStart = -1;
1142
+ this._recording = false;
1143
+
1144
+ // States used in the custom random generators
1145
+ this._lcg_random_state = null; // NOTE: move to random.js
1146
+ this._gaussian_previous = false; // NOTE: move to random.js
1147
+
1148
+ if (window.DeviceOrientationEvent) {
1149
+ this._events.deviceorientation = null;
1150
+ }
1151
+ if (window.DeviceMotionEvent && !window._isNodeWebkit) {
1152
+ this._events.devicemotion = null;
1153
+ }
1154
+
1155
+ // ensure correct reporting of window dimensions
1156
+ this._updateWindowSize();
1157
+
1158
+ const bindGlobal = (property) => {
1159
+ Object.defineProperty(window, property, {
1160
+ configurable: true,
1161
+ enumerable: true,
1162
+ get: () => {
1163
+ if(typeof this[property] === 'function'){
1164
+ return this[property].bind(this);
1165
+ }else {
1166
+ return this[property];
1167
+ }
1168
+ },
1169
+ set: (newValue) => {
1170
+ Object.defineProperty(window, property, {
1171
+ configurable: true,
1172
+ enumerable: true,
1173
+ value: newValue,
1174
+ writable: true
1175
+ });
1176
+ if (!p5.disableFriendlyErrors) {
1177
+ console.log(`You just changed the value of "${property}", which was a p5 global value. This could cause problems later if you're not careful.`);
1178
+ }
1179
+ }
1180
+ });
1181
+ };
1182
+ // If the user has created a global setup or draw function,
1183
+ // assume "global" mode and make everything global (i.e. on the window)
1184
+ if (!sketch) {
1185
+ this._isGlobal = true;
1186
+ if (window.hitCriticalError) {
1187
+ return;
1188
+ }
1189
+ p5.instance = this;
1190
+
1191
+ // Loop through methods on the prototype and attach them to the window
1192
+ // All methods and properties with name starting with '_' will be skipped
1193
+ for (const p of Object.getOwnPropertyNames(p5.prototype)) {
1194
+ if(p[0] === '_') continue;
1195
+ bindGlobal(p);
1196
+ }
1197
+
1198
+ const protectedProperties = ['constructor', 'length'];
1199
+ // Attach its properties to the window
1200
+ for (const p in this) {
1201
+ if (this.hasOwnProperty(p)) {
1202
+ if(p[0] === '_' || protectedProperties.includes(p)) continue;
1203
+ bindGlobal(p);
1204
+ }
1205
+ }
1206
+ } else {
1207
+ // Else, the user has passed in a sketch closure that may set
1208
+ // user-provided 'setup', 'draw', etc. properties on this instance of p5
1209
+ sketch(this);
1210
+
1211
+ // Run a check to see if the user has misspelled 'setup', 'draw', etc
1212
+ // detects capitalization mistakes only ( Setup, SETUP, MouseClicked, etc)
1213
+ p5._checkForUserDefinedFunctions(this);
1214
+ }
1215
+
1216
+ // Bind events to window (not using container div bc key events don't work)
1217
+ for (const e in this._events) {
1218
+ const f = this[`_on${e}`];
1219
+ if (f) {
1220
+ const m = f.bind(this);
1221
+ window.addEventListener(e, m, { passive: false });
1222
+ this._events[e] = m;
1223
+ }
1224
+ }
1225
+
1226
+ const focusHandler = () => {
1227
+ this.focused = true;
1228
+ };
1229
+ const blurHandler = () => {
1230
+ this.focused = false;
1231
+ };
1232
+ window.addEventListener('focus', focusHandler);
1233
+ window.addEventListener('blur', blurHandler);
1234
+ p5.lifecycleHooks.remove.push(function() {
1235
+ window.removeEventListener('focus', focusHandler);
1236
+ window.removeEventListener('blur', blurHandler);
1237
+ });
1238
+
1239
+ // Initialization complete, start runtime
1240
+ if (document.readyState === 'complete') {
1241
+ this.#_start();
1242
+ } else {
1243
+ this._startListener = this.#_start.bind(this);
1244
+ window.addEventListener('load', this._startListener, false);
1245
+ }
1246
+ }
1247
+
1248
+ get pixels(){
1249
+ return this._renderer.pixels;
1250
+ }
1251
+
1252
+ get drawingContext(){
1253
+ return this._renderer.drawingContext;
1254
+ }
1255
+
1256
+ static registerAddon(addon) {
1257
+ const lifecycles = {};
1258
+ addon(p5, p5.prototype, lifecycles);
1259
+
1260
+ const validLifecycles = Object.keys(p5.lifecycleHooks);
1261
+ for(const name of validLifecycles){
1262
+ if(typeof lifecycles[name] === 'function'){
1263
+ p5.lifecycleHooks[name].push(lifecycles[name]);
1264
+ }
1265
+ }
1266
+ }
1267
+
1268
+ async #_start() {
1269
+ if (this.hitCriticalError) return;
1270
+ // Find node if id given
1271
+ if (this._userNode) {
1272
+ if (typeof this._userNode === 'string') {
1273
+ this._userNode = document.getElementById(this._userNode);
1274
+ }
1275
+ }
1276
+
1277
+ await this.#_setup();
1278
+ if (this.hitCriticalError) return;
1279
+ if (!this._recording) {
1280
+ this._draw();
1281
+ }
1282
+ }
1283
+
1284
+ async #_setup() {
1285
+ // Run `presetup` hooks
1286
+ await this._runLifecycleHook('presetup');
1287
+ if (this.hitCriticalError) return;
1288
+
1289
+ // Always create a default canvas.
1290
+ // Later on if the user calls createCanvas, this default one
1291
+ // will be replaced
1292
+ this.createCanvas(
1293
+ 100,
1294
+ 100,
1295
+ P2D
1296
+ );
1297
+
1298
+ // Record the time when sketch starts
1299
+ this._millisStart = window.performance.now();
1300
+
1301
+ const context = this._isGlobal ? window : this;
1302
+ if (typeof context.setup === 'function') {
1303
+ await context.setup();
1304
+ }
1305
+ if (this.hitCriticalError) return;
1306
+
1307
+ // unhide any hidden canvases that were created
1308
+ const canvases = document.getElementsByTagName('canvas');
1309
+
1310
+ // Apply touchAction = 'none' to canvases if pointer events exist
1311
+ if (Object.keys(this._events).some(event => event.startsWith('pointer'))) {
1312
+ for (const k of canvases) {
1313
+ k.style.touchAction = 'none';
1314
+ }
1315
+ }
1316
+
1317
+
1318
+ for (const k of canvases) {
1319
+ if (k.dataset.hidden === 'true') {
1320
+ k.style.visibility = '';
1321
+ delete k.dataset.hidden;
1322
+ }
1323
+ }
1324
+
1325
+ this._lastTargetFrameTime = window.performance.now();
1326
+ this._lastRealFrameTime = window.performance.now();
1327
+ this._setupDone = true;
1328
+ if (this._accessibleOutputs.grid || this._accessibleOutputs.text) {
1329
+ this._updateAccsOutput();
1330
+ }
1331
+
1332
+ // Run `postsetup` hooks
1333
+ await this._runLifecycleHook('postsetup');
1334
+ }
1335
+
1336
+ // While '#_draw' here is async, it is not awaited as 'requestAnimationFrame'
1337
+ // does not await its callback. Thus it is not recommended for 'draw()` to be
1338
+ // async and use await within as the next frame may start rendering before the
1339
+ // current frame finish awaiting. The same goes for lifecycle hooks 'predraw'
1340
+ // and 'postdraw'.
1341
+ async _draw(requestAnimationFrameTimestamp) {
1342
+ if (this.hitCriticalError) return;
1343
+ const now = requestAnimationFrameTimestamp || window.performance.now();
1344
+ const timeSinceLastFrame = now - this._lastTargetFrameTime;
1345
+ const targetTimeBetweenFrames = 1000 / this._targetFrameRate;
1346
+
1347
+ // only draw if we really need to; don't overextend the browser.
1348
+ // draw if we're within 5ms of when our next frame should paint
1349
+ // (this will prevent us from giving up opportunities to draw
1350
+ // again when it's really about time for us to do so). fixes an
1351
+ // issue where the frameRate is too low if our refresh loop isn't
1352
+ // in sync with the browser. note that we have to draw once even
1353
+ // if looping is off, so we bypass the time delay if that
1354
+ // is the case.
1355
+ const epsilon = 5;
1356
+ if (
1357
+ !this._loop ||
1358
+ timeSinceLastFrame >= targetTimeBetweenFrames - epsilon
1359
+ ) {
1360
+ //mandatory update values(matrixes and stack)
1361
+ this.deltaTime = now - this._lastRealFrameTime;
1362
+ this._frameRate = 1000.0 / this.deltaTime;
1363
+ await this.redraw();
1364
+ this._lastTargetFrameTime = Math.max(this._lastTargetFrameTime
1365
+ + targetTimeBetweenFrames, now);
1366
+ this._lastRealFrameTime = now;
1367
+
1368
+ // If the user is actually using mouse module, then update
1369
+ // coordinates, otherwise skip. We can test this by simply
1370
+ // checking if any of the mouse functions are available or not.
1371
+ // NOTE : This reflects only in complete build or modular build.
1372
+ if (typeof this._updateMouseCoords !== 'undefined') {
1373
+ this._updateMouseCoords();
1374
+
1375
+ //reset delta values so they reset even if there is no mouse event to set them
1376
+ // for example if the mouse is outside the screen
1377
+ this.movedX = 0;
1378
+ this.movedY = 0;
1379
+ }
1380
+ }
1381
+
1382
+ // get notified the next time the browser gives us
1383
+ // an opportunity to draw.
1384
+ if (this._loop) {
1385
+ this._requestAnimId = window.requestAnimationFrame(
1386
+ this._draw.bind(this)
1387
+ );
1388
+ }
1389
+ }
1390
+
1391
+ /**
1392
+ * Removes the sketch from the web page.
1393
+ *
1394
+ * Calling `remove()` stops the draw loop and removes any HTML elements
1395
+ * created by the sketch, including the canvas. A new sketch can be
1396
+ * created by using the <a href="#/p5/p5">p5()</a> constructor, as in
1397
+ * `new p5()`.
1398
+ *
1399
+ * @example
1400
+ * <div>
1401
+ * <code>
1402
+ * // Double-click to remove the canvas.
1403
+ *
1404
+ * function setup() {
1405
+ * createCanvas(100, 100);
1406
+ *
1407
+ * describe(
1408
+ * 'A white circle on a gray background. The circle follows the mouse as the user moves. The sketch disappears when the user double-clicks.'
1409
+ * );
1410
+ * }
1411
+ *
1412
+ * function draw() {
1413
+ * // Paint the background repeatedly.
1414
+ * background(200);
1415
+ *
1416
+ * // Draw circles repeatedly.
1417
+ * circle(mouseX, mouseY, 40);
1418
+ * }
1419
+ *
1420
+ * // Remove the sketch when the user double-clicks.
1421
+ * function doubleClicked() {
1422
+ * remove();
1423
+ * }
1424
+ * </code>
1425
+ * </div>
1426
+ */
1427
+ async remove() {
1428
+ // Remove start listener to prevent orphan canvas being created
1429
+ if(this._startListener){
1430
+ window.removeEventListener('load', this._startListener, false);
1431
+ }
1432
+
1433
+ if (this._curElement) {
1434
+ // stop draw
1435
+ this._loop = false;
1436
+ if (this._requestAnimId) {
1437
+ window.cancelAnimationFrame(this._requestAnimId);
1438
+ }
1439
+
1440
+ // unregister events sketch-wide
1441
+ for (const ev in this._events) {
1442
+ window.removeEventListener(ev, this._events[ev]);
1443
+ }
1444
+
1445
+ // remove DOM elements created by p5, and listeners
1446
+ for (const e of this._elements) {
1447
+ if (e.elt && e.elt.parentNode) {
1448
+ e.elt.parentNode.removeChild(e.elt);
1449
+ }
1450
+ for (const elt_ev in e._events) {
1451
+ e.elt.removeEventListener(elt_ev, e._events[elt_ev]);
1452
+ }
1453
+ }
1454
+
1455
+ // Run `remove` hooks
1456
+ await this._runLifecycleHook('remove');
1457
+ }
1458
+
1459
+ // remove window bound properties and methods
1460
+ if (this._isGlobal) {
1461
+ for (const p in p5.prototype) {
1462
+ try {
1463
+ delete window[p];
1464
+ } catch (x) {
1465
+ window[p] = undefined;
1466
+ }
1467
+ }
1468
+ for (const p2 in this) {
1469
+ if (this.hasOwnProperty(p2)) {
1470
+ try {
1471
+ delete window[p2];
1472
+ } catch (x) {
1473
+ window[p2] = undefined;
1474
+ }
1475
+ }
1476
+ }
1477
+ p5.instance = null;
1478
+ }
1479
+ }
1480
+
1481
+ async _runLifecycleHook(hookName) {
1482
+ for(const hook of p5.lifecycleHooks[hookName]){
1483
+ await hook.call(this);
1484
+ }
1485
+ }
1486
+
1487
+ _initializeInstanceVariables() {
1488
+ this._accessibleOutputs = {
1489
+ text: false,
1490
+ grid: false,
1491
+ textLabel: false,
1492
+ gridLabel: false
1493
+ };
1494
+
1495
+ this._styles = [];
1496
+ this._downKeys = {}; //Holds the key codes of currently pressed keys
1497
+ this._downKeyCodes = {};
1498
+ }
1499
+ }
1500
+
1501
+ // Attach constants to p5 prototype
1502
+ for (const k in constants) {
1503
+ p5.prototype[k] = constants[k];
1061
1504
  }
1062
1505
 
1063
- export default renderer2D;
1064
- export { Renderer2D };
1506
+ //////////////////////////////////////////////
1507
+ // PUBLIC p5 PROPERTIES AND METHODS
1508
+ //////////////////////////////////////////////
1509
+
1510
+ /**
1511
+ * A function that's called once when the sketch begins running.
1512
+ *
1513
+ * Declaring the function `setup()` sets a code block to run once
1514
+ * automatically when the sketch starts running. It's used to perform
1515
+ * setup tasks such as creating the canvas and initializing variables:
1516
+ *
1517
+ * ```js
1518
+ * function setup() {
1519
+ * // Code to run once at the start of the sketch.
1520
+ * }
1521
+ * ```
1522
+ *
1523
+ * Code placed in `setup()` will run once before code placed in
1524
+ * <a href="#/p5/draw">draw()</a> begins looping. If the
1525
+ * <a href="#/p5/preload">preload()</a> is declared, then `setup()` will
1526
+ * run immediately after <a href="#/p5/preload">preload()</a> finishes
1527
+ *
1528
+ *
1529
+ * loading assets.
1530
+ *
1531
+ * Note: `setup()` doesn’t have to be declared, but it’s common practice to do so.
1532
+ *
1533
+ * @method setup
1534
+ * @for p5
1535
+ *
1536
+ * @example
1537
+ * <div>
1538
+ * <code>
1539
+ * function setup() {
1540
+ * createCanvas(100, 100);
1541
+ *
1542
+ * background(200);
1543
+ *
1544
+ * // Draw the circle.
1545
+ * circle(50, 50, 40);
1546
+ *
1547
+ * describe('A white circle on a gray background.');
1548
+ * }
1549
+ * </code>
1550
+ * </div>
1551
+ *
1552
+ * <div>
1553
+ * <code>
1554
+ * function setup() {
1555
+ * createCanvas(100, 100);
1556
+ *
1557
+ * // Paint the background once.
1558
+ * background(200);
1559
+ *
1560
+ * describe(
1561
+ * 'A white circle on a gray background. The circle follows the mouse as the user moves, leaving a trail.'
1562
+ * );
1563
+ * }
1564
+ *
1565
+ * function draw() {
1566
+ * // Draw circles repeatedly.
1567
+ * circle(mouseX, mouseY, 40);
1568
+ * }
1569
+ * </code>
1570
+ * </div>
1571
+ *
1572
+ * <div>
1573
+ * <code>
1574
+ * let img;
1575
+ *
1576
+ * async function setup() {
1577
+ * img = await loadImage('assets/bricks.jpg');
1578
+ *
1579
+ * createCanvas(100, 100);
1580
+ *
1581
+ * // Draw the image.
1582
+ * image(img, 0, 0);
1583
+ *
1584
+ * describe(
1585
+ * 'A white circle on a brick wall. The circle follows the mouse as the user moves, leaving a trail.'
1586
+ * );
1587
+ * }
1588
+ *
1589
+ * function draw() {
1590
+ * // Style the circle.
1591
+ * noStroke();
1592
+ *
1593
+ * // Draw the circle.
1594
+ * circle(mouseX, mouseY, 10);
1595
+ * }
1596
+ * </code>
1597
+ * </div>
1598
+ */
1599
+
1600
+ /**
1601
+ * A function that's called repeatedly while the sketch runs.
1602
+ *
1603
+ * Declaring the function `draw()` sets a code block to run repeatedly
1604
+ * once the sketch starts. It’s used to create animations and respond to
1605
+ * user inputs:
1606
+ *
1607
+ * ```js
1608
+ * function draw() {
1609
+ * // Code to run repeatedly.
1610
+ * }
1611
+ * ```
1612
+ *
1613
+ * This is often called the "draw loop" because p5.js calls the code in
1614
+ * `draw()` in a loop behind the scenes. By default, `draw()` tries to run
1615
+ * 60 times per second. The actual rate depends on many factors. The
1616
+ * drawing rate, called the "frame rate", can be controlled by calling
1617
+ * <a href="#/p5/frameRate">frameRate()</a>. The number of times `draw()`
1618
+ * has run is stored in the system variable
1619
+ * <a href="#/p5/frameCount">frameCount()</a>.
1620
+ *
1621
+ * Code placed within `draw()` begins looping after
1622
+ * <a href="#/p5/setup">setup()</a> runs. `draw()` will run until the user
1623
+ * closes the sketch. `draw()` can be stopped by calling the
1624
+ * <a href="#/p5/noLoop">noLoop()</a> function. `draw()` can be resumed by
1625
+ * calling the <a href="#/p5/loop">loop()</a> function.
1626
+ *
1627
+ * @method draw
1628
+ * @for p5
1629
+ *
1630
+ * @example
1631
+ * <div>
1632
+ * <code>
1633
+ * function setup() {
1634
+ * createCanvas(100, 100);
1635
+ *
1636
+ * // Paint the background once.
1637
+ * background(200);
1638
+ *
1639
+ * describe(
1640
+ * 'A white circle on a gray background. The circle follows the mouse as the user moves, leaving a trail.'
1641
+ * );
1642
+ * }
1643
+ *
1644
+ * function draw() {
1645
+ * // Draw circles repeatedly.
1646
+ * circle(mouseX, mouseY, 40);
1647
+ * }
1648
+ * </code>
1649
+ * </div>
1650
+ *
1651
+ * <div>
1652
+ * <code>
1653
+ * function setup() {
1654
+ * createCanvas(100, 100);
1655
+ *
1656
+ * describe(
1657
+ * 'A white circle on a gray background. The circle follows the mouse as the user moves.'
1658
+ * );
1659
+ * }
1660
+ *
1661
+ * function draw() {
1662
+ * // Paint the background repeatedly.
1663
+ * background(200);
1664
+ *
1665
+ * // Draw circles repeatedly.
1666
+ * circle(mouseX, mouseY, 40);
1667
+ * }
1668
+ * </code>
1669
+ * </div>
1670
+ *
1671
+ * <div>
1672
+ * <code>
1673
+ * // Double-click the canvas to change the circle's color.
1674
+ *
1675
+ * function setup() {
1676
+ * createCanvas(100, 100);
1677
+ *
1678
+ * describe(
1679
+ * 'A white circle on a gray background. The circle follows the mouse as the user moves. The circle changes color to pink when the user double-clicks.'
1680
+ * );
1681
+ * }
1682
+ *
1683
+ * function draw() {
1684
+ * // Paint the background repeatedly.
1685
+ * background(200);
1686
+ *
1687
+ * // Draw circles repeatedly.
1688
+ * circle(mouseX, mouseY, 40);
1689
+ * }
1690
+ *
1691
+ * // Change the fill color when the user double-clicks.
1692
+ * function doubleClicked() {
1693
+ * fill('deeppink');
1694
+ * }
1695
+ * </code>
1696
+ * </div>
1697
+ */
1698
+
1699
+ /**
1700
+ * Turns off the parts of the Friendly Error System (FES) that impact performance.
1701
+ *
1702
+ * The <a href="https://github.com/processing/p5.js/blob/main/contributor_docs/friendly_error_system.md" target="_blank">FES</a>
1703
+ * can cause sketches to draw slowly because it does extra work behind the
1704
+ * scenes. For example, the FES checks the arguments passed to functions,
1705
+ * which takes time to process. Disabling the FES can significantly improve
1706
+ * performance by turning off these checks.
1707
+ *
1708
+ * @property {Boolean} disableFriendlyErrors
1709
+ *
1710
+ * @example
1711
+ * <div>
1712
+ * <code>
1713
+ * // Disable the FES.
1714
+ * p5.disableFriendlyErrors = true;
1715
+ *
1716
+ * function setup() {
1717
+ * createCanvas(100, 100);
1718
+ *
1719
+ * background(200);
1720
+ *
1721
+ * // The circle() function requires three arguments. The
1722
+ * // next line would normally display a friendly error that
1723
+ * // points this out. Instead, nothing happens and it fails
1724
+ * // silently.
1725
+ * circle(50, 50);
1726
+ *
1727
+ * describe('A gray square.');
1728
+ * }
1729
+ * </code>
1730
+ * </div>
1731
+ */
1732
+ p5.disableFriendlyErrors = false;
1733
+
1734
+ p5.registerAddon(transform);
1735
+ p5.registerAddon(structure);
1736
+ p5.registerAddon(environment);
1737
+ p5.registerAddon(rendering);
1738
+ p5.registerAddon(renderer);
1739
+ p5.registerAddon(renderer2D);
1740
+ p5.registerAddon(graphics);
1741
+
1742
+ export { Renderer2D as R, p5 as p, renderer2D as r };