@@ -23,6 +23,13 @@ function math(p5, fn) {
2323 * This allows for flexibility in representing vectors in higher-dimensional
2424 * spaces.
2525 *
26+ * - Calling `createVector()` **with no arguments** is **deprecated** and will emit
27+ * a friendly warning. Use `createVector(0)`, `createVector(0, 0)`, or
28+ * `createVector(0, 0, 0)` instead.
29+ * - To prevent `NaN` appearing in subsequent operations, missing components are
30+ * padded with zeros: `createVector(x)` -> `[x, 0, 0]`, `createVector(x, y)` -> `[x, y, 0]`.
31+ * This is for backwards compatibility only; explicitly pass all intended components.
32+ *
2633 * <a href="#/p5.Vector">p5.Vector</a> objects are often used to program
2734 * motion because they simplify the math. For example, a moving ball has a
2835 * position and a velocity. Position describes where the ball is in space. The
@@ -96,14 +103,26 @@ function math(p5, fn) {
96103 * </div>
97104 */
98105 fn . createVector = function ( x , y , z ) {
106+
107+ if ( arguments . length === 0 && typeof p5 !== 'undefined' && p5 . _friendlyError ) {
108+ p5 . _friendlyError (
109+ 'Calling createVector() with no arguments is deprecated and will be removed in a future release. Pass zeros for the desired dimensionality.'
110+ ) ;
111+ }
112+
113+ const safeArgs =
114+ arguments . length === 1 ? [ x , 0 , 0 ]
115+ : arguments . length === 2 ? [ x , y , 0 ]
116+ : [ ...arguments ] ;
117+
99118 if ( this instanceof p5 ) {
100119 return new p5 . Vector (
101120 this . _fromRadians . bind ( this ) ,
102121 this . _toRadians . bind ( this ) ,
103- ...arguments
122+ ...safeArgs
104123 ) ;
105124 } else {
106- return new p5 . Vector ( x , y , z ) ;
125+ return new p5 . Vector ( ... safeArgs ) ;
107126 }
108127 } ;
109128
0 commit comments