p5 2.2.3 → 2.3.0-rc.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 (137) hide show
  1. package/dist/accessibility/color_namer.js +9 -11
  2. package/dist/accessibility/describe.js +0 -1
  3. package/dist/accessibility/gridOutput.js +0 -1
  4. package/dist/accessibility/index.js +9 -10
  5. package/dist/accessibility/outputs.js +0 -1
  6. package/dist/accessibility/textOutput.js +0 -1
  7. package/dist/app.js +11 -10
  8. package/dist/app.node.js +122 -0
  9. package/dist/color/color_conversion.js +9 -11
  10. package/dist/color/creating_reading.js +1 -1
  11. package/dist/color/index.js +2 -2
  12. package/dist/color/p5.Color.js +1 -1
  13. package/dist/color/setting.js +25 -12
  14. package/dist/{constants-BdTiYOQI.js → constants-CYF6mp5_.js} +2 -2
  15. package/dist/core/States.js +1 -1
  16. package/dist/core/constants.js +1 -1
  17. package/dist/core/environment.js +28 -29
  18. package/dist/core/filterShaders.js +1 -1
  19. package/dist/core/friendly_errors/fes_core.js +9 -8
  20. package/dist/core/friendly_errors/file_errors.js +1 -2
  21. package/dist/core/friendly_errors/index.js +1 -1
  22. package/dist/core/friendly_errors/param_validator.js +737 -640
  23. package/dist/core/friendly_errors/sketch_verifier.js +1 -1
  24. package/dist/core/friendly_errors/stacktrace.js +0 -1
  25. package/dist/core/helpers.js +3 -4
  26. package/dist/core/init.js +24 -21
  27. package/dist/core/internationalization.js +1 -1
  28. package/dist/core/legacy.js +9 -11
  29. package/dist/core/main.js +9 -10
  30. package/dist/core/p5.Graphics.js +5 -5
  31. package/dist/core/p5.Renderer.js +3 -3
  32. package/dist/core/p5.Renderer2D.js +9 -10
  33. package/dist/core/p5.Renderer3D.js +5 -5
  34. package/dist/core/rendering.js +5 -5
  35. package/dist/core/structure.js +0 -1
  36. package/dist/core/transform.js +7 -16
  37. package/dist/{creating_reading-C7hu6sg1.js → creating_reading-DLkHH80h.js} +11 -8
  38. package/dist/data/local_storage.js +0 -1
  39. package/dist/dom/dom.js +2 -3
  40. package/dist/dom/index.js +2 -2
  41. package/dist/dom/p5.Element.js +2 -2
  42. package/dist/dom/p5.MediaElement.js +2 -2
  43. package/dist/events/acceleration.js +5 -3
  44. package/dist/events/keyboard.js +0 -1
  45. package/dist/events/pointer.js +0 -2
  46. package/dist/image/const.js +1 -1
  47. package/dist/image/filterRenderer2D.js +19 -12
  48. package/dist/image/image.js +5 -5
  49. package/dist/image/index.js +5 -5
  50. package/dist/image/loading_displaying.js +5 -5
  51. package/dist/image/p5.Image.js +3 -3
  52. package/dist/image/pixels.js +0 -1
  53. package/dist/io/files.js +5 -5
  54. package/dist/io/index.js +5 -5
  55. package/dist/io/p5.Table.js +0 -1
  56. package/dist/io/p5.TableRow.js +0 -1
  57. package/dist/io/p5.XML.js +0 -1
  58. package/dist/{ir_builders-Cd6rU9Vm.js → ir_builders-C2ebb6Lu.js} +234 -1
  59. package/dist/{main-H_nu4eDs.js → main-D2MtO721.js} +107 -136
  60. package/dist/math/Matrices/Matrix.js +1 -1
  61. package/dist/math/Matrices/MatrixNumjs.js +1 -1
  62. package/dist/math/calculation.js +0 -1
  63. package/dist/math/index.js +3 -1
  64. package/dist/math/math.js +3 -17
  65. package/dist/math/noise.js +0 -1
  66. package/dist/math/p5.Matrix.js +1 -2
  67. package/dist/math/p5.Vector.js +233 -279
  68. package/dist/math/patch-vector.js +75 -0
  69. package/dist/math/random.js +0 -1
  70. package/dist/math/trigonometry.js +3 -4
  71. package/dist/{p5.Renderer-BmD2P6Wv.js → p5.Renderer-C0Kzy71d.js} +31 -24
  72. package/dist/{rendering-CC8JNTwG.js → rendering-CvNr0bB8.js} +732 -44
  73. package/dist/shape/2d_primitives.js +1 -4
  74. package/dist/shape/attributes.js +43 -8
  75. package/dist/shape/curves.js +0 -1
  76. package/dist/shape/custom_shapes.js +260 -5
  77. package/dist/shape/index.js +2 -2
  78. package/dist/shape/vertex.js +0 -2
  79. package/dist/strands/ir_builders.js +1 -1
  80. package/dist/strands/ir_types.js +5 -1
  81. package/dist/strands/p5.strands.js +286 -31
  82. package/dist/strands/strands_api.js +179 -8
  83. package/dist/strands/strands_codegen.js +26 -8
  84. package/dist/strands/strands_conditionals.js +1 -1
  85. package/dist/strands/strands_for.js +1 -1
  86. package/dist/strands/strands_node.js +1 -1
  87. package/dist/strands/strands_ternary.js +56 -0
  88. package/dist/strands/strands_transpiler.js +416 -251
  89. package/dist/strands_glslBackend-i-ReKgZo.js +423 -0
  90. package/dist/type/index.js +3 -3
  91. package/dist/type/lib/Typr.js +1 -1
  92. package/dist/type/p5.Font.js +3 -3
  93. package/dist/type/textCore.js +31 -24
  94. package/dist/utilities/conversion.js +0 -1
  95. package/dist/utilities/time_date.js +0 -1
  96. package/dist/utilities/utility_functions.js +0 -1
  97. package/dist/webgl/3d_primitives.js +5 -5
  98. package/dist/webgl/GeometryBuilder.js +1 -1
  99. package/dist/webgl/ShapeBuilder.js +26 -1
  100. package/dist/webgl/enums.js +1 -1
  101. package/dist/webgl/index.js +8 -9
  102. package/dist/webgl/interaction.js +8 -4
  103. package/dist/webgl/light.js +5 -5
  104. package/dist/webgl/loading.js +60 -21
  105. package/dist/webgl/material.js +5 -5
  106. package/dist/webgl/p5.Camera.js +5 -5
  107. package/dist/webgl/p5.Framebuffer.js +5 -5
  108. package/dist/webgl/p5.Geometry.js +3 -5
  109. package/dist/webgl/p5.Quat.js +1 -1
  110. package/dist/webgl/p5.RendererGL.js +17 -21
  111. package/dist/webgl/p5.Shader.js +129 -36
  112. package/dist/webgl/p5.Texture.js +5 -5
  113. package/dist/webgl/strands_glslBackend.js +5 -386
  114. package/dist/webgl/text.js +5 -5
  115. package/dist/webgl/utils.js +5 -5
  116. package/dist/webgl2Compatibility-DA7DLMuq.js +7 -0
  117. package/dist/webgpu/index.js +7 -3
  118. package/dist/webgpu/p5.RendererWebGPU.js +1036 -180
  119. package/dist/webgpu/shaders/color.js +1 -1
  120. package/dist/webgpu/shaders/compute.js +32 -0
  121. package/dist/webgpu/shaders/functions/randomComputeWGSL.js +31 -0
  122. package/dist/webgpu/shaders/functions/randomVertWGSL.js +30 -0
  123. package/dist/webgpu/shaders/functions/randomWGSL.js +30 -0
  124. package/dist/webgpu/shaders/line.js +1 -1
  125. package/dist/webgpu/shaders/material.js +3 -3
  126. package/dist/webgpu/strands_wgslBackend.js +137 -15
  127. package/lib/p5.esm.js +4088 -1950
  128. package/lib/p5.esm.min.js +1 -1
  129. package/lib/p5.js +4088 -1950
  130. package/lib/p5.min.js +1 -1
  131. package/lib/p5.webgpu.esm.js +1638 -306
  132. package/lib/p5.webgpu.js +1637 -305
  133. package/lib/p5.webgpu.min.js +1 -1
  134. package/package.json +6 -1
  135. package/types/global.d.ts +4137 -2396
  136. package/types/p5.d.ts +2702 -1658
  137. package/dist/noise3DGLSL-Bwrdi4gi.js +0 -9
@@ -1,90 +1,93 @@
1
- import { q as TWO_PI } from '../constants-BdTiYOQI.js';
1
+ import { o as TWO_PI } from '../constants-CYF6mp5_.js';
2
2
 
3
3
  /**
4
4
  * @module Math
5
- * @requires constants
6
5
  */
7
6
 
8
7
 
9
- /// HELPERS FOR REMAINDER METHOD
10
- const calculateRemainder2D = function (xComponent, yComponent) {
11
- if (xComponent !== 0) {
12
- this.x = this.x % xComponent;
13
- }
14
- if (yComponent !== 0) {
15
- this.y = this.y % yComponent;
16
- }
17
- return this;
8
+ /**
9
+ * This function is used by binary vector operations to prioritize shorter vectors,
10
+ * and to emit a warning when lengths do not match.
11
+ */
12
+ const prioritizeSmallerDimension = function(currentVectorDimension, args) {
13
+ return Math.min(currentVectorDimension, args.length);
18
14
  };
19
15
 
20
- const calculateRemainder3D = function (xComponent, yComponent, zComponent) {
21
- if (xComponent !== 0) {
22
- this.x = this.x % xComponent;
23
- }
24
- if (yComponent !== 0) {
25
- this.y = this.y % yComponent;
26
- }
27
- if (zComponent !== 0) {
28
- this.z = this.z % zComponent;
29
- }
30
- return this;
31
- };
32
16
 
33
17
  class Vector {
18
+ /**
19
+ * The values of an N-dimensional vector.
20
+ *
21
+ * This array of numbers that represents the vector.
22
+ * Each number in the array corresponds to a different component of the vector,
23
+ * like its position in different directions (e.g., x, y, z).
24
+ *
25
+ * You can update the values of the entire vector to a new set of values.
26
+ * You need to provide an array of numbers, where each number represents a component
27
+ * of the vector (e.g., x, y, z). The length of the array will become the number of
28
+ * dimensions of the vector.
29
+ *
30
+ * You can add (`add()`), multiply (`mult()`), divide (`div()`), and subtract (`sub()`)
31
+ * vectors from each other, and calculate remainder (`rem()`). Only use these functions
32
+ * on vectors when they are the same size: both 2-dimensional, or both 3-dimensional.
33
+ * When an operation uses two vectors of different sizes, the smaller dimension will be
34
+ * used, any additional values of the longer vector will be ignored.
35
+ *
36
+ * You can multiply, divide, or calculate remainder of a vector with a single number. Then,
37
+ * the same operation will be done on each element of the vector.
38
+ *
39
+ * @type {Array<number>} The array of values representing the vector.
40
+ * @throws Will throw an error if provided no arguments, or if the arguments
41
+ * are not all finity numbers
42
+ */
43
+ values = [];
44
+
34
45
  // This is how it comes in with createVector()
35
46
  // This check if the first argument is a function
36
47
  constructor(...args) {
37
- let values = args; // .map(arg => arg || 0);
48
+
49
+ if (args.length === 0) {
50
+ this._friendlyError(
51
+ 'Requires valid arguments.', 'p5.Vector'
52
+ );
53
+ }
54
+
38
55
  if (typeof args[0] === 'function') {
39
56
  this.isPInst = true;
40
57
  this._fromRadians = args[0];
41
58
  this._toRadians = args[1];
42
- values = args.slice(2); // .map(arg => arg || 0);
59
+ args = args.slice(2);
43
60
  }
44
- let dimensions = values.length; // TODO: make default 3 if no arguments
45
- if (dimensions === 0) {
46
- this.dimensions = 2;
47
- this._values = [0, 0, 0];
61
+
62
+ this.values = [];
63
+ if(Array.isArray(args) && !args.every(v => typeof v === 'number' && Number.isFinite(v))){
64
+ this._friendlyError(
65
+ 'Arguments contain non-finite numbers',
66
+ 'p5.Vector'
67
+ );
48
68
  } else {
49
- this.dimensions = dimensions;
50
- this._values = values;
69
+ this.values = args;
51
70
  }
52
- }
53
71
 
54
- /**
55
- * Gets the values of the N-dimensional vector.
56
- *
57
- * This method returns an array of numbers that represent the vector.
58
- * Each number in the array corresponds to a different component of the vector,
59
- * like its position in different directions (e.g., x, y, z).
60
- *
61
- * @returns {Array<number>} The array of values representing the vector.
62
- */
63
- get values() {
64
- return this._values;
72
+ // This property is here where duck typing (checking if obj.isVector) needs
73
+ // to be used over more standard type checking (obj instanceof Vector). This
74
+ // needs to happen where we are building multiple files, such as in p5.webgpu.js,
75
+ // where if we `import { Vector }` directly, it will be a separate copy of the
76
+ // Vector class from the one imported in the main p5.js bundle.
77
+ this.isVector = true;
65
78
  }
66
79
 
80
+ // This will get overwritten when exported as part of p5.
81
+ _friendlyError(_e) {}
82
+
83
+
67
84
  /**
68
- * Sets the values of the vector.
69
- *
70
- * This method allows you to update the entire vector with a new set of values.
71
- * You need to provide an array of numbers, where each number represents a component
72
- * of the vector (e.g., x, y, z). The length of the array should match the number of
73
- * dimensions of the vector. If the array is shorter, the missing components will be
74
- * set to 0. If the array is longer, the extra values will be ignored.
75
- *
76
- * @param {Array<number>} newValues - An array of numbers representing the new values for the vector.
85
+ * Gets how many dimensions the vector has.
77
86
  *
87
+ * @returns {Number} The number of dimensions. Can be 1, 2, or 3.
78
88
  */
79
- set values(newValues) {
80
- let dimensions = newValues.length;
81
- if (dimensions === 0) {
82
- this.dimensions = 2;
83
- this._values = [0, 0, 0];
84
- } else {
85
- this.dimensions = dimensions;
86
- this._values = newValues.slice();
87
- }
89
+ get dimensions(){
90
+ return this.values.length;
88
91
  }
89
92
 
90
93
  /**
@@ -97,7 +100,7 @@ class Vector {
97
100
  * @returns {Number} The x component of the vector. Returns 0 if the value is not defined.
98
101
  */
99
102
  get x() {
100
- return this._values[0] || 0;
103
+ return this.values[0] || 0;
101
104
  }
102
105
 
103
106
  /**
@@ -118,10 +121,10 @@ class Vector {
118
121
  * get a value from a position that doesn't exist in the vector.
119
122
  */
120
123
  getValue(index) {
121
- if (index < this._values.length) {
122
- return this._values[index];
124
+ if (index < this.values.length) {
125
+ return this.values[index];
123
126
  } else {
124
- p5._friendlyError(
127
+ this._friendlyError(
125
128
  'The index parameter is trying to set a value outside the bounds of the vector',
126
129
  'p5.Vector.setValue'
127
130
  );
@@ -143,10 +146,10 @@ class Vector {
143
146
  * @throws Will throw an error if the index is outside the bounds of the vector, meaning if you try to set a value at a position that doesn't exist in the vector.
144
147
  */
145
148
  setValue(index, value) {
146
- if (index < this._values.length) {
147
- this._values[index] = value;
149
+ if (index < this.values.length) {
150
+ this.values[index] = value;
148
151
  } else {
149
- p5._friendlyError(
152
+ this._friendlyError(
150
153
  'The index parameter is trying to set a value outside the bounds of the vector',
151
154
  'p5.Vector.setValue'
152
155
  );
@@ -163,7 +166,7 @@ class Vector {
163
166
  * @returns {Number} The y component of the vector. Returns 0 if the value is not defined.
164
167
  */
165
168
  get y() {
166
- return this._values[1] || 0;
169
+ return this.values[1] || 0;
167
170
  }
168
171
 
169
172
  /**
@@ -176,7 +179,7 @@ class Vector {
176
179
  * @returns {Number} The z component of the vector. Returns 0 if the value is not defined.
177
180
  */
178
181
  get z() {
179
- return this._values[2] || 0;
182
+ return this.values[2] || 0;
180
183
  }
181
184
 
182
185
  /**
@@ -189,7 +192,7 @@ class Vector {
189
192
  * @returns {Number} The w component of the vector. Returns 0 if the value is not defined.
190
193
  */
191
194
  get w() {
192
- return this._values[3] || 0;
195
+ return this.values[3] || 0;
193
196
  }
194
197
 
195
198
  /**
@@ -202,8 +205,8 @@ class Vector {
202
205
  * @param {Number} xVal - The new value for the x component.
203
206
  */
204
207
  set x(xVal) {
205
- if (this._values.length > 1) {
206
- this._values[0] = xVal;
208
+ if (this.values.length > 1) {
209
+ this.values[0] = xVal;
207
210
  }
208
211
  }
209
212
 
@@ -217,8 +220,8 @@ class Vector {
217
220
  * @param {Number} yVal - The new value for the y component.
218
221
  */
219
222
  set y(yVal) {
220
- if (this._values.length > 1) {
221
- this._values[1] = yVal;
223
+ if (this.values.length > 1) {
224
+ this.values[1] = yVal;
222
225
  }
223
226
  }
224
227
 
@@ -232,8 +235,8 @@ class Vector {
232
235
  * @param {Number} zVal - The new value for the z component.
233
236
  */
234
237
  set z(zVal) {
235
- if (this._values.length > 2) {
236
- this._values[2] = zVal;
238
+ if (this.values.length > 2) {
239
+ this.values[2] = zVal;
237
240
  }
238
241
  }
239
242
 
@@ -247,8 +250,8 @@ class Vector {
247
250
  * @param {Number} wVal - The new value for the w component.
248
251
  */
249
252
  set w(wVal) {
250
- if (this._values.length > 3) {
251
- this._values[3] = wVal;
253
+ if (this.values.length > 3) {
254
+ this.values[3] = wVal;
252
255
  }
253
256
  }
254
257
 
@@ -270,7 +273,7 @@ class Vector {
270
273
  * }
271
274
  */
272
275
  toString() {
273
- return `vector[${this._values.join(', ')}]`;
276
+ return `vector[${this.values.join(', ')}]`;
274
277
  }
275
278
 
276
279
  /**
@@ -328,13 +331,12 @@ class Vector {
328
331
  */
329
332
  set(...args) {
330
333
  if (args[0] instanceof Vector) {
331
- this._values = args[0].values.slice();
334
+ this.values = args[0].values.slice();
332
335
  } else if (Array.isArray(args[0])) {
333
- this._values = args[0].map(arg => arg || 0);
336
+ this.values = args[0].map(arg => arg || 0);
334
337
  } else {
335
- this._values = args.map(arg => arg || 0);
338
+ this.values = args.map(arg => arg || 0);
336
339
  }
337
- this.dimensions = this._values.length;
338
340
  return this;
339
341
  }
340
342
 
@@ -364,12 +366,14 @@ class Vector {
364
366
  */
365
367
  copy() {
366
368
  if (this.isPInst) {
367
- return new Vector(this._fromRadians, this._toRadians, ...this._values);
369
+ return new Vector(this._fromRadians, this._toRadians, ...this.values);
368
370
  } else {
369
- return new Vector(...this._values);
371
+ return new Vector(...this.values);
370
372
  }
371
373
  }
372
374
 
375
+
376
+
373
377
  /**
374
378
  * Adds to a vector's components.
375
379
  *
@@ -377,8 +381,12 @@ class Vector {
377
381
  * another <a href="#/p5.Vector">p5.Vector</a> object, as in `v.add(v2)`, or
378
382
  * an array of numbers, as in `v.add([1, 2, 3])`.
379
383
  *
380
- * If a value isn't provided for a component, it won't change. For
381
- * example, `v.add(4, 5)` adds 4 to `v.x`, 5 to `v.y`, and 0 to `v.z`.
384
+ * Add vectors only when they are the same size: both 2-dimensional, or
385
+ * both 3-dimensional. When two vectors of different sizes are added, the
386
+ * smaller dimension will be used, any additional values of the longer
387
+ * vector will be ignored.
388
+ * For example, adding `[1, 2, 3]` and `[4, 5]` will result in `[5, 7]`.
389
+ *
382
390
  * Calling `add()` with no arguments, as in `v.add()`, has no effect.
383
391
  *
384
392
  * This method supports N-dimensional vectors.
@@ -495,17 +503,18 @@ class Vector {
495
503
  * @chainable
496
504
  */
497
505
  add(...args) {
498
- if (args[0] instanceof Vector) {
499
- args = args[0].values;
500
- } else if (Array.isArray(args[0])) {
501
- args = args[0];
502
- }
503
- args.forEach((value, index) => {
504
- this._values[index] = (this._values[index] || 0) + (value || 0);
505
- });
506
+ const minDimension = prioritizeSmallerDimension(this.dimensions, args);
507
+
508
+ this.values = this.values.reduce((acc, v, i) => {
509
+ if(i < minDimension) acc[i] = this.values[i] + Number(args[i]);
510
+ return acc;
511
+ }, new Array(minDimension));
512
+
506
513
  return this;
507
514
  }
508
515
 
516
+
517
+
509
518
  /**
510
519
  * Performs modulo (remainder) division with a vector's `x`, `y`, and `z`
511
520
  * components.
@@ -515,10 +524,14 @@ class Vector {
515
524
  * an array of numbers, as in `v.rem([1, 2, 3])`.
516
525
  *
517
526
  * If only one value is provided, as in `v.rem(2)`, then all the components
518
- * will be set to their values modulo 2. If two values are provided, as in
519
- * `v.rem(2, 3)`, then `v.z` won't change. Calling `rem()` with no
527
+ * will be set to their values modulo 2. Calling `rem()` with no
520
528
  * arguments, as in `v.rem()`, has no effect.
521
529
  *
530
+ * Modulo vectors only when they are the same size: both 2D, or both 3D.
531
+ * When two vectors of different sizes are used, the smaller dimension will be
532
+ * used, any additional values of the longer vector will be ignored.
533
+ * For example, taking `[3, 6, 9]` modulo `[2, 4]` will result in `[1, 2]`.
534
+ *
522
535
  * The static version of `rem()`, as in `p5.Vector.rem(v2, v1)`, returns a
523
536
  * new <a href="#/p5.Vector">p5.Vector</a> object and doesn't change the
524
537
  * originals.
@@ -613,59 +626,14 @@ class Vector {
613
626
  * @param {p5.Vector | Number[]} value divisor vector.
614
627
  * @chainable
615
628
  */
616
- rem(x, y, z) {
617
- if (x instanceof Vector) {
618
- if ([x.x, x.y, x.z].every(Number.isFinite)) {
619
- const xComponent = parseFloat(x.x);
620
- const yComponent = parseFloat(x.y);
621
- const zComponent = parseFloat(x.z);
622
- return calculateRemainder3D.call(
623
- this,
624
- xComponent,
625
- yComponent,
626
- zComponent
627
- );
628
- }
629
- } else if (Array.isArray(x)) {
630
- if (x.every(element => Number.isFinite(element))) {
631
- if (x.length === 2) {
632
- return calculateRemainder2D.call(this, x[0], x[1]);
633
- }
634
- if (x.length === 3) {
635
- return calculateRemainder3D.call(this, x[0], x[1], x[2]);
636
- }
637
- }
638
- } else if (arguments.length === 1) {
639
- if (Number.isFinite(arguments[0]) && arguments[0] !== 0) {
640
- this.x = this.x % arguments[0];
641
- this.y = this.y % arguments[0];
642
- this.z = this.z % arguments[0];
643
- return this;
644
- }
645
- } else if (arguments.length === 2) {
646
- const vectorComponents = [...arguments];
647
- if (vectorComponents.every(element => Number.isFinite(element))) {
648
- if (vectorComponents.length === 2) {
649
- return calculateRemainder2D.call(
650
- this,
651
- vectorComponents[0],
652
- vectorComponents[1]
653
- );
654
- }
655
- }
656
- } else if (arguments.length === 3) {
657
- const vectorComponents = [...arguments];
658
- if (vectorComponents.every(element => Number.isFinite(element))) {
659
- if (vectorComponents.length === 3) {
660
- return calculateRemainder3D.call(
661
- this,
662
- vectorComponents[0],
663
- vectorComponents[1],
664
- vectorComponents[2]
665
- );
666
- }
667
- }
668
- }
629
+ rem(...args) {
630
+ const minDimension = prioritizeSmallerDimension(this.dimensions, args);
631
+
632
+ this.values = Array.from({ length: minDimension }, (_, i) => {
633
+ return (args[i] > 0) ? this.values[i] % args[i] : this.values[i];
634
+ });
635
+
636
+ return this;
669
637
  }
670
638
 
671
639
  /**
@@ -675,10 +643,13 @@ class Vector {
675
643
  * <a href="#/p5.Vector">p5.Vector</a> object, as in `v.sub(v2)`, or an array
676
644
  * of numbers, as in `v.sub([1, 2, 3])`.
677
645
  *
678
- * If a value isn't provided for a component, it won't change. For
679
- * example, `v.sub(4, 5)` subtracts 4 from `v.x`, 5 from `v.y`, and 0 from `v.z`.
680
646
  * Calling `sub()` with no arguments, as in `v.sub()`, has no effect.
681
647
  *
648
+ * Subtract vectors only when they are the same size: both 2D, or both 3D.
649
+ * When two vectors of different sizes are used, the smaller dimension will be
650
+ * used, any additional values of the longer vector will be ignored.
651
+ * For example, subtracting `[1, 2]` from `[3, 5, 7]` will result in `[2, 3]`.
652
+ *
682
653
  * The static version of `sub()`, as in `p5.Vector.sub(v2, v1)`, returns a new
683
654
  * <a href="#/p5.Vector">p5.Vector</a> object and doesn't change the
684
655
  * originals.
@@ -788,19 +759,13 @@ class Vector {
788
759
  * @chainable
789
760
  */
790
761
  sub(...args) {
791
- if (args[0] instanceof Vector) {
792
- args[0].values.forEach((value, index) => {
793
- this._values[index] -= value || 0;
794
- });
795
- } else if (Array.isArray(args[0])) {
796
- args[0].forEach((value, index) => {
797
- this._values[index] -= value || 0;
798
- });
799
- } else {
800
- args.forEach((value, index) => {
801
- this._values[index] -= value || 0;
802
- });
803
- }
762
+ const minDimension = prioritizeSmallerDimension(this.dimensions, args);
763
+
764
+ this.values = this.values.reduce((acc, v, i) => {
765
+ if(i < minDimension) acc[i] = this.values[i] - args[i];
766
+ return acc;
767
+ }, new Array(minDimension));
768
+
804
769
  return this;
805
770
  }
806
771
 
@@ -812,11 +777,14 @@ class Vector {
812
777
  * of numbers, as in `v.mult([1, 2, 3])`.
813
778
  *
814
779
  * If only one value is provided, as in `v.mult(2)`, then all the components
815
- * will be multiplied by 2. If a value isn't provided for a component, it
816
- * won't change. For example, `v.mult(4, 5)` multiplies `v.x` by, `v.y` by 5,
817
- * and `v.z` by 1. Calling `mult()` with no arguments, as in `v.mult()`, has
780
+ * will be multiplied by 2. Calling `mult()` with no arguments, as in `v.mult()`, has
818
781
  * no effect.
819
782
  *
783
+ * Multiply vectors only when they are the same size: both 2D, or both 3D.
784
+ * When two vectors of different sizes are multiplied, the smaller dimension will be
785
+ * used, any additional values of the longer vector will be ignored.
786
+ * For example, multiplying `[1, 2, 3]` by `[4, 5]` will result in `[4, 10]`.
787
+ *
820
788
  * The static version of `mult()`, as in `p5.Vector.mult(v, 2)`, returns a new
821
789
  * <a href="#/p5.Vector">p5.Vector</a> object and doesn't change the
822
790
  * originals.
@@ -979,43 +947,51 @@ class Vector {
979
947
  * @chainable
980
948
  */
981
949
  mult(...args) {
982
- if (args.length === 1 && args[0] instanceof Vector) {
983
- const v = args[0];
984
- const maxLen = Math.min(this._values.length, v.values.length);
985
- for (let i = 0; i < maxLen; i++) {
986
- if (Number.isFinite(v.values[i]) && typeof v.values[i] === 'number') {
987
- this._values[i] *= v.values[i];
988
- } else {
989
- console.warn(
990
- 'p5.Vector.prototype.mult:',
991
- 'v contains components that are either undefined or not finite numbers'
992
- );
993
- return this;
994
- }
995
- }
996
- } else if (args.length === 1 && Array.isArray(args[0])) {
997
- const arr = args[0];
998
- const maxLen = Math.min(this._values.length, arr.length);
999
- for (let i = 0; i < maxLen; i++) {
1000
- if (Number.isFinite(arr[i]) && typeof arr[i] === 'number') {
1001
- this._values[i] *= arr[i];
1002
- } else {
1003
- console.warn(
1004
- 'p5.Vector.prototype.mult:',
1005
- 'arr contains elements that are either undefined or not finite numbers'
1006
- );
1007
- return this;
1008
- }
1009
- }
1010
- } else if (
1011
- args.length === 1 &&
1012
- typeof args[0] === 'number' &&
1013
- Number.isFinite(args[0])
1014
- ) {
1015
- for (let i = 0; i < this._values.length; i++) {
1016
- this._values[i] *= args[0];
1017
- }
1018
- }
950
+ const minDimension = prioritizeSmallerDimension(this.dimensions, args);
951
+
952
+ this.values = this.values.reduce((acc, v, i) => {
953
+ if(i < minDimension) acc[i] = this.values[i] * args[i];
954
+ return acc;
955
+ }, new Array(minDimension));
956
+
957
+ // if (args.length === 1 && args[0] instanceof Vector) {
958
+ // const v = args[0];
959
+ // const maxLen = Math.min(this.values.length, v.values.length);
960
+ // for (let i = 0; i < maxLen; i++) {
961
+ // if (Number.isFinite(v.values[i]) && typeof v.values[i] === 'number') {
962
+ // if(!this.values[i]) this.values[i] = 0;
963
+ // this.values[i] *= v.values[i];
964
+ // } else {
965
+ // console.warn(
966
+ // 'p5.Vector.prototype.mult:',
967
+ // 'v contains components that are either undefined or not finite numbers'
968
+ // );
969
+ // return this;
970
+ // }
971
+ // }
972
+ // } else if (args.length === 1 && Array.isArray(args[0])) {
973
+ // const arr = args[0];
974
+ // const maxLen = Math.min(this.values.length, arr.length);
975
+ // for (let i = 0; i < maxLen; i++) {
976
+ // if (Number.isFinite(arr[i]) && typeof arr[i] === 'number') {
977
+ // this.values[i] *= arr[i];
978
+ // } else {
979
+ // console.warn(
980
+ // 'p5.Vector.prototype.mult:',
981
+ // 'arr contains elements that are either undefined or not finite numbers'
982
+ // );
983
+ // return this;
984
+ // }
985
+ // }
986
+ // } else if (
987
+ // args.length === 1 &&
988
+ // typeof args[0] === 'number' &&
989
+ // Number.isFinite(args[0])
990
+ // ) {
991
+ // for (let i = 0; i < this.values.length; i++) {
992
+ // this.values[i] *= args[0];
993
+ // }
994
+ // }
1019
995
  return this;
1020
996
  }
1021
997
 
@@ -1027,11 +1003,14 @@ class Vector {
1027
1003
  * of numbers, as in `v.div([1, 2, 3])`.
1028
1004
  *
1029
1005
  * If only one value is provided, as in `v.div(2)`, then all the components
1030
- * will be divided by 2. If a value isn't provided for a component, it
1031
- * won't change. For example, `v.div(4, 5)` divides `v.x` by, `v.y` by 5,
1032
- * and `v.z` by 1. Calling `div()` with no arguments, as in `v.div()`, has
1006
+ * will be divided by 2. Calling `div()` with no arguments, as in `v.div()`, has
1033
1007
  * no effect.
1034
1008
  *
1009
+ * Divide vectors only when they are the same size: both 2D, or both 3D.
1010
+ * When two vectors of different sizes are divided, the smaller dimension will be
1011
+ * used, any additional values of the longer vector will be ignored.
1012
+ * For example, dividing `[8, 12, 21]` by `[2, 3]` will result in `[4, 4]`.
1013
+ *
1035
1014
  * The static version of `div()`, as in `p5.Vector.div(v, 2)`, returns a new
1036
1015
  * <a href="#/p5.Vector">p5.Vector</a> object and doesn't change the
1037
1016
  * originals.
@@ -1195,57 +1174,19 @@ class Vector {
1195
1174
  * @chainable
1196
1175
  */
1197
1176
  div(...args) {
1198
- if (args.length === 0) return this;
1199
- if (args.length === 1 && args[0] instanceof Vector) {
1200
- const v = args[0];
1201
- if (
1202
- v._values.every(
1203
- val => Number.isFinite(val) && typeof val === 'number'
1204
- )
1205
- ) {
1206
- if (v._values.some(val => val === 0)) {
1207
- console.warn('p5.Vector.prototype.div:', 'divide by 0');
1208
- return this;
1209
- }
1210
- this._values = this._values.map((val, i) => val / v._values[i]);
1211
- } else {
1212
- console.warn(
1213
- 'p5.Vector.prototype.div:',
1214
- 'vector contains components that are either undefined or not finite numbers'
1215
- );
1216
- }
1217
- return this;
1218
- }
1219
-
1220
- if (args.length === 1 && Array.isArray(args[0])) {
1221
- const arr = args[0];
1222
- if (arr.every(val => Number.isFinite(val) && typeof val === 'number')) {
1223
- if (arr.some(val => val === 0)) {
1224
- console.warn('p5.Vector.prototype.div:', 'divide by 0');
1225
- return this;
1226
- }
1227
- this._values = this._values.map((val, i) => val / arr[i]);
1228
- } else {
1229
- console.warn(
1230
- 'p5.Vector.prototype.div:',
1231
- 'array contains components that are either undefined or not finite numbers'
1232
- );
1233
- }
1234
- return this;
1235
- }
1177
+ const minDimension = prioritizeSmallerDimension(this.dimensions, args);
1236
1178
 
1237
- if (args.every(val => Number.isFinite(val) && typeof val === 'number')) {
1238
- if (args.some(val => val === 0)) {
1239
- console.warn('p5.Vector.prototype.div:', 'divide by 0');
1240
- return this;
1241
- }
1242
- this._values = this._values.map((val, i) => val / args[0]);
1243
- } else {
1179
+ if(!args.every(v => typeof v === 'number' && v !== 0)){
1244
1180
  console.warn(
1245
- 'p5.Vector.prototype.div:',
1246
- 'arguments contain components that are either undefined or not finite numbers'
1181
+ 'p5.Vector.prototype.div',
1182
+ 'Arguments contain components that are 0'
1247
1183
  );
1184
+ return this;
1248
1185
  }
1186
+ this.values = this.values.reduce((acc, v, i) => {
1187
+ if(i < minDimension) acc[i] = this.values[i] / args[i];
1188
+ return acc;
1189
+ }, new Array(minDimension));
1249
1190
 
1250
1191
  return this;
1251
1192
  }
@@ -1298,7 +1239,8 @@ class Vector {
1298
1239
  * // Create a p5.Vector object.
1299
1240
  * let p = createVector(30, 40);
1300
1241
  *
1301
- * // Draw a line from the origin.
1242
+ * // Draw a line from th
1243
+ * e origin.
1302
1244
  * line(0, 0, p.x, p.y);
1303
1245
  *
1304
1246
  * // Style the text.
@@ -1313,7 +1255,7 @@ class Vector {
1313
1255
  * }
1314
1256
  */
1315
1257
  magSq() {
1316
- return this._values.reduce(
1258
+ return this.values.reduce(
1317
1259
  (sum, component) => sum + component * component,
1318
1260
  0
1319
1261
  );
@@ -1417,9 +1359,9 @@ class Vector {
1417
1359
  */
1418
1360
  dot(...args) {
1419
1361
  if (args[0] instanceof Vector) {
1420
- return this.dot(...args[0]._values);
1362
+ return this.dot(...args[0].values);
1421
1363
  }
1422
- return this._values.reduce((sum, component, index) => {
1364
+ return this.values.reduce((sum, component, index) => {
1423
1365
  return sum + component * (args[index] || 0);
1424
1366
  }, 0);
1425
1367
  }
@@ -2038,8 +1980,18 @@ class Vector {
2038
1980
  * }
2039
1981
  */
2040
1982
  setHeading(a) {
2041
- if (this.isPInst) a = this._toRadians(a);
2042
- let m = this.mag();
1983
+ if (this.dimensions < 2 || (
1984
+ this._values instanceof Array && this._values.slice(2).some(v => v !== 0))
1985
+ ) {
1986
+ p5._friendlyError(
1987
+ 'p5.Vector.setHeading() only supports 2D vectors (z === 0). ' +
1988
+ 'For 3D or higher-dimensional vectors, use rotate() or another ' +
1989
+ 'appropriate method instead.',
1990
+ 'p5.Vector.setHeading'
1991
+ );
1992
+ return this;
1993
+ }
1994
+ const m = this.mag();
2043
1995
  this.x = m * Math.cos(a);
2044
1996
  this.y = m * Math.sin(a);
2045
1997
  return this;
@@ -2824,15 +2776,15 @@ class Vector {
2824
2776
  equals(...args) {
2825
2777
  let values;
2826
2778
  if (args[0] instanceof Vector) {
2827
- values = args[0]._values;
2779
+ values = args[0].values;
2828
2780
  } else if (Array.isArray(args[0])) {
2829
2781
  values = args[0];
2830
2782
  } else {
2831
2783
  values = args;
2832
2784
  }
2833
2785
 
2834
- for (let i = 0; i < this._values.length; i++) {
2835
- if (this._values[i] !== (values[i] || 0)) {
2786
+ for (let i = 0; i < this.values.length; i++) {
2787
+ if (this.values[i] !== (values[i] || 0)) {
2836
2788
  return false;
2837
2789
  }
2838
2790
  }
@@ -2852,8 +2804,8 @@ class Vector {
2852
2804
  * @chainable
2853
2805
  */
2854
2806
  clampToZero() {
2855
- for (let i = 0; i < this._values.length; i++) {
2856
- this._values[i] = this._clampToZero(this._values[i]);
2807
+ for (let i = 0; i < this.values.length; i++) {
2808
+ this.values[i] = this._clampToZero(this.values[i]);
2857
2809
  }
2858
2810
  return this;
2859
2811
  }
@@ -3110,7 +3062,7 @@ class Vector {
3110
3062
  if (!target) {
3111
3063
  target = v1.copy();
3112
3064
  if (arguments.length === 3) {
3113
- p5._friendlyError(
3065
+ this._friendlyError(
3114
3066
  'The target parameter is undefined, it should be of type p5.Vector',
3115
3067
  'p5.Vector.add'
3116
3068
  );
@@ -3157,7 +3109,7 @@ class Vector {
3157
3109
  if (!target) {
3158
3110
  target = v1.copy();
3159
3111
  if (arguments.length === 3) {
3160
- p5._friendlyError(
3112
+ this._friendlyError(
3161
3113
  'The target parameter is undefined, it should be of type p5.Vector',
3162
3114
  'p5.Vector.sub'
3163
3115
  );
@@ -3201,7 +3153,7 @@ class Vector {
3201
3153
  if (!target) {
3202
3154
  target = v.copy();
3203
3155
  if (arguments.length === 3) {
3204
- p5._friendlyError(
3156
+ this._friendlyError(
3205
3157
  'The target parameter is undefined, it should be of type p5.Vector',
3206
3158
  'p5.Vector.mult'
3207
3159
  );
@@ -3227,7 +3179,7 @@ class Vector {
3227
3179
  target = v.copy();
3228
3180
  } else {
3229
3181
  if (!(target instanceof Vector)) {
3230
- p5._friendlyError(
3182
+ this._friendlyError(
3231
3183
  'The target parameter should be of type p5.Vector',
3232
3184
  'p5.Vector.rotate'
3233
3185
  );
@@ -3271,7 +3223,7 @@ class Vector {
3271
3223
  target = v.copy();
3272
3224
 
3273
3225
  if (arguments.length === 3) {
3274
- p5._friendlyError(
3226
+ this._friendlyError(
3275
3227
  'The target parameter is undefined, it should be of type p5.Vector',
3276
3228
  'p5.Vector.div'
3277
3229
  );
@@ -3339,7 +3291,7 @@ class Vector {
3339
3291
  if (!target) {
3340
3292
  target = v1.copy();
3341
3293
  if (arguments.length === 4) {
3342
- p5._friendlyError(
3294
+ this._friendlyError(
3343
3295
  'The target parameter is undefined, it should be of type p5.Vector',
3344
3296
  'p5.Vector.lerp'
3345
3297
  );
@@ -3369,7 +3321,7 @@ class Vector {
3369
3321
  if (!target) {
3370
3322
  target = v1.copy();
3371
3323
  if (arguments.length === 4) {
3372
- p5._friendlyError(
3324
+ this._friendlyError(
3373
3325
  'The target parameter is undefined, it should be of type p5.Vector',
3374
3326
  'p5.Vector.slerp'
3375
3327
  );
@@ -3423,7 +3375,7 @@ class Vector {
3423
3375
  target = v.copy();
3424
3376
  } else {
3425
3377
  if (!(target instanceof Vector)) {
3426
- p5._friendlyError(
3378
+ this._friendlyError(
3427
3379
  'The target parameter should be of type p5.Vector',
3428
3380
  'p5.Vector.normalize'
3429
3381
  );
@@ -3449,7 +3401,7 @@ class Vector {
3449
3401
  target = v.copy();
3450
3402
  } else {
3451
3403
  if (!(target instanceof Vector)) {
3452
- p5._friendlyError(
3404
+ this._friendlyError(
3453
3405
  'The target parameter should be of type p5.Vector',
3454
3406
  'p5.Vector.limit'
3455
3407
  );
@@ -3475,7 +3427,7 @@ class Vector {
3475
3427
  target = v.copy();
3476
3428
  } else {
3477
3429
  if (!(target instanceof Vector)) {
3478
- p5._friendlyError(
3430
+ this._friendlyError(
3479
3431
  'The target parameter should be of type p5.Vector',
3480
3432
  'p5.Vector.setMag'
3481
3433
  );
@@ -3531,7 +3483,7 @@ class Vector {
3531
3483
  target = incidentVector.copy();
3532
3484
  } else {
3533
3485
  if (!(target instanceof Vector)) {
3534
- p5._friendlyError(
3486
+ this._friendlyError(
3535
3487
  'The target parameter should be of type p5.Vector',
3536
3488
  'p5.Vector.reflect'
3537
3489
  );
@@ -3572,7 +3524,7 @@ class Vector {
3572
3524
  } else if (v1 instanceof Array) {
3573
3525
  v = new Vector().set(v1);
3574
3526
  } else {
3575
- p5._friendlyError(
3527
+ this._friendlyError(
3576
3528
  'The v1 parameter should be of type Array or p5.Vector',
3577
3529
  'p5.Vector.equals'
3578
3530
  );
@@ -3660,6 +3612,8 @@ function vector(p5, fn) {
3660
3612
  */
3661
3613
  p5.Vector = Vector;
3662
3614
 
3615
+ Vector.prototype._friendlyError = p5._friendlyError;
3616
+
3663
3617
  /**
3664
3618
  * The x component of the vector
3665
3619
  * @type {Number}