Skip to content

Commit

Permalink
Merge pull request #6807 from asukaminato0721/Default-values-of-argum…
Browse files Browse the repository at this point in the history
…ents

Default values of arguments && modernize some code
  • Loading branch information
limzykenneth committed Feb 29, 2024
2 parents b862aef + 5fc46d5 commit 7a808ae
Show file tree
Hide file tree
Showing 19 changed files with 305 additions and 507 deletions.
12 changes: 6 additions & 6 deletions src/color/creating_reading.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

/**
Expand Down
4 changes: 2 additions & 2 deletions src/core/p5.Renderer2D.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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]');
}
Expand Down
11 changes: 5 additions & 6 deletions src/core/shape/2d_primitives.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,12 +292,11 @@ p5.prototype.ellipse = function(x, y, w, h, detailX) {
* </div>
*
*/
p5.prototype.circle = function() {
p5._validateParameters('circle', arguments);
const args = Array.prototype.slice.call(arguments, 0, 2);
args.push(arguments[2]);
args.push(arguments[2]);
return this._renderEllipse(...args);
p5.prototype.circle = function(...args) {
p5._validateParameters('circle', args);
const argss = args.slice( 0, 2);
argss.push(args[2], args[2]);
return this._renderEllipse(...argss);
};

// internal method for drawing ellipses (without parameter validation)
Expand Down
90 changes: 0 additions & 90 deletions src/core/shim.js
Original file line number Diff line number Diff line change
@@ -1,90 +0,0 @@
// requestAnim shim layer by Paul Irish
// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
// http://my.opera.com/emoller/blog/2011/12/20/
// requestanimationframe-for-smart-er-animating
// requestAnimationFrame polyfill by Erik Möller
// fixes from Paul Irish and Tino Zijdel
window.requestAnimationFrame = (() =>
window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
((callback, element) => {
// should '60' here be framerate?
window.setTimeout(callback, 1000 / 60);
}))();

/**
* shim for Uint8ClampedArray.slice
* (allows arrayCopy to work with pixels[])
* with thanks to http://halfpapstudios.com/blog/tag/html5-canvas/
* Enumerable set to false to protect for...in from
* Uint8ClampedArray.prototype pollution.
*/
(() => {
if (
typeof Uint8ClampedArray !== 'undefined' &&
!Uint8ClampedArray.prototype.slice
) {
Object.defineProperty(Uint8ClampedArray.prototype, 'slice', {
value: Array.prototype.slice,
writable: true,
configurable: true,
enumerable: false
});
}
})();

/**
* this is implementation of Object.assign() which is unavailable in
* IE11 and (non-Chrome) Android browsers.
* The assign() method is used to copy the values of all enumerable
* own properties from one or more source objects to a target object.
* It will return the target object.
* Modified from https://github.com/ljharb/object.assign
*/
(() => {
if (!Object.assign) {
const keys = Object.keys;
const defineProperty = Object.defineProperty;
const canBeObject = obj => typeof obj !== 'undefined' && obj !== null;
const hasSymbols =
typeof Symbol === 'function' && typeof Symbol() === 'symbol';
const propIsEnumerable = Object.prototype.propertyIsEnumerable;
const isEnumerableOn = obj =>
(function isEnumerable(prop) {
return propIsEnumerable.call(obj, prop);
});

// per ES6 spec, this function has to have a length of 2
const assignShim = function assign(target, source1) {
if (!canBeObject(target)) {
throw new TypeError('target must be an object');
}
const objTarget = Object(target);
let s, source, i, props;
for (s = 1; s < arguments.length; ++s) {
source = Object(arguments[s]);
props = keys(source);
if (hasSymbols && Object.getOwnPropertySymbols) {
props.push(
...Object.getOwnPropertySymbols(source)
.filter(isEnumerableOn(source))
);
}
for (i = 0; i < props.length; ++i) {
objTarget[props[i]] = source[props[i]];
}
}
return objTarget;
};

defineProperty(Object, 'assign', {
value: assignShim,
configurable: true,
enumerable: false,
writable: true
});
}
})();
4 changes: 2 additions & 2 deletions src/core/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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;
};
Expand Down
2 changes: 1 addition & 1 deletion src/dom/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -3278,7 +3278,7 @@ p5.Element.prototype.hide = function () {
*/
/**
* @method size
* @param {Number|Constant} w width of the element, either AUTO, or a number.
* @param {Number|Constant} [w] width of the element, either AUTO, or a number.
* @param {Number|Constant} [h] height of the element, either AUTO, or a number.
* @chainable
*/
Expand Down
Loading

0 comments on commit 7a808ae

Please sign in to comment.