Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default values of arguments && modernize some code #6807

Merged
Prev Previous commit
Next Next commit
Array.isArray
asukaminato0721 committed Feb 12, 2024
commit c5a59030d93ae691e084b063297e81630c1704f4
12 changes: 6 additions & 6 deletions src/color/creating_reading.js
Original file line number Diff line number Diff line change
@@ -290,14 +290,14 @@ p5.prototype.brightness = function(c) {
* @param {p5.Color} color
* @return {p5.Color}
*/
p5.prototype.color = function() {
p5._validateParameters('color', arguments);
if (arguments[0] instanceof p5.Color) {
return arguments[0]; // Do nothing if argument is already a color object.
p5.prototype.color = function(...args) {
p5._validateParameters('color', args);
if (args[0] instanceof p5.Color) {
return args[0]; // Do nothing if argument is already a color object.
}

const args = arguments[0] instanceof Array ? arguments[0] : arguments;
return new p5.Color(this, args);
const arg = Array.isArray(args[0]) ? args[0] : args;
return new p5.Color(this, arg);
};

/**
4 changes: 2 additions & 2 deletions src/core/p5.Renderer2D.js
Original file line number Diff line number Diff line change
@@ -452,7 +452,7 @@ class Renderer2D extends p5.Renderer {
(this.width * pixelsState._pixelDensity) +
x * pixelsState._pixelDensity);
if (!pixelsState.imageData) {
pixelsState.loadPixels.call(pixelsState);
pixelsState.loadPixels();
}
if (typeof imgOrCol === 'number') {
if (idx < pixelsState.pixels.length) {
@@ -462,7 +462,7 @@ class Renderer2D extends p5.Renderer {
a = 255;
//this.updatePixels.call(this);
}
} else if (imgOrCol instanceof Array) {
} else if (Array.isArray(imgOrCol)) {
if (imgOrCol.length < 4) {
throw new Error('pixel array must be of the form [R, G, B, A]');
}
4 changes: 2 additions & 2 deletions src/core/transform.js
Original file line number Diff line number Diff line change
@@ -431,7 +431,7 @@ p5.prototype.scale = function(x, y, z) {
x = v.x;
y = v.y;
z = v.z;
} else if (x instanceof Array) {
} else if (Array.isArray(x)) {
const rg = x;
x = rg[0];
y = rg[1];
@@ -443,7 +443,7 @@ p5.prototype.scale = function(x, y, z) {
z = 1;
}

this._renderer.scale.call(this._renderer, x, y, z);
this._renderer.scale(x, y, z);

return this;
};
16 changes: 4 additions & 12 deletions src/math/calculation.js
Original file line number Diff line number Diff line change
@@ -471,14 +471,10 @@ p5.prototype.map = function(n, start1, stop1, start2, stop2, withinBounds) {
*/
p5.prototype.max = function(...args) {
const findMax = arr => {
let max = -Infinity;
for (let x of arr) {
max = Math.max(max, x);
}
return max;
return Math.max(...arr);
};

if (args[0] instanceof Array) {
if (Array.isArray(args[0])) {
return findMax(args[0]);
} else {
return findMax(args);
@@ -549,14 +545,10 @@ p5.prototype.max = function(...args) {
*/
p5.prototype.min = function(...args) {
const findMin = arr => {
let min = Infinity;
for (let x of arr) {
min = Math.min(min, x);
}
return min;
Math.min(...arr);
};

if (args[0] instanceof Array) {
if (Array.isArray(args[0])) {
return findMin(args[0]);
} else {
return findMin(args);
2 changes: 1 addition & 1 deletion src/math/p5.Vector.js
Original file line number Diff line number Diff line change
@@ -3060,7 +3060,7 @@ p5.Vector = class {
let v;
if (v1 instanceof p5.Vector) {
v = v1;
} else if (v1 instanceof Array) {
} else if (Array.isArray(v1)) {
v = new p5.Vector().set(v1);
} else {
p5._friendlyError(
2 changes: 1 addition & 1 deletion src/math/random.js
Original file line number Diff line number Diff line change
@@ -174,7 +174,7 @@ p5.prototype.random = function(min, max) {
if (typeof min === 'undefined') {
return rand;
} else if (typeof max === 'undefined') {
if (min instanceof Array) {
if (Array.isArray(min)) {
return min[Math.floor(rand * min.length)];
} else {
return rand * min;