diff --git a/src/color/creating_reading.js b/src/color/creating_reading.js index 457f6db07d..1d8adf4a59 100644 --- a/src/color/creating_reading.js +++ b/src/color/creating_reading.js @@ -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); }; /** diff --git a/src/core/p5.Renderer2D.js b/src/core/p5.Renderer2D.js index 42dcbf0275..a9be0a6211 100644 --- a/src/core/p5.Renderer2D.js +++ b/src/core/p5.Renderer2D.js @@ -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]'); } diff --git a/src/core/shape/2d_primitives.js b/src/core/shape/2d_primitives.js index f2b62203e3..e2ef3cf530 100644 --- a/src/core/shape/2d_primitives.js +++ b/src/core/shape/2d_primitives.js @@ -292,12 +292,11 @@ p5.prototype.ellipse = function(x, y, w, h, detailX) { * * */ -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) diff --git a/src/core/shim.js b/src/core/shim.js index 8bf38fdf57..e69de29bb2 100644 --- a/src/core/shim.js +++ b/src/core/shim.js @@ -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 - }); - } -})(); diff --git a/src/core/transform.js b/src/core/transform.js index 4ed2e0118e..2b747a8000 100644 --- a/src/core/transform.js +++ b/src/core/transform.js @@ -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; }; diff --git a/src/dom/dom.js b/src/dom/dom.js index b12a2750c8..5273d9d491 100644 --- a/src/dom/dom.js +++ b/src/dom/dom.js @@ -3243,7 +3243,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 */ diff --git a/src/image/filters.js b/src/image/filters.js index e178b9585a..6c746df32e 100644 --- a/src/image/filters.js +++ b/src/image/filters.js @@ -63,16 +63,16 @@ const Filters = { }, /** - * Returns a 32-bit number containing ARGB data at the ith pixel in the - * 1D array containing pixels data. - * - * @private - * - * @param {Uint8ClampedArray} data array returned by _toPixels() - * @param {Integer} i index of a 1D Image Array - * @return {Integer} 32-bit integer value representing - * ARGB value. - */ + * Returns a 32-bit number containing ARGB data at the ith pixel in the + * 1D array containing pixels data. + * + * @private + * + * @param {Uint8ClampedArray} data array returned by _toPixels() + * @param {Integer} i index of a 1D Image Array + * @return {Integer} 32-bit integer value representing + * ARGB value. + */ _getARGB(data, i) { // Determine the starting position in the 'data' array for the 'i'-th pixel. const offset = i * 4; @@ -86,14 +86,14 @@ const Filters = { }, /** - * Modifies pixels RGBA values to values contained in the data object. - * - * @private - * - * @param {Uint8ClampedArray} pixels array returned by _toPixels() - * @param {Int32Array} data source 1D array where each value - * represents ARGB values - */ + * Modifies pixels RGBA values to values contained in the data object. + * + * @private + * + * @param {Uint8ClampedArray} pixels array returned by _toPixels() + * @param {Int32Array} data source 1D array where each value + * represents ARGB values + */ _setPixels(pixels, data) { let offset = 0; for (let i = 0, al = pixels.length; i < al; i++) { @@ -107,15 +107,15 @@ const Filters = { /** - * Returns the ImageData object for a canvas. - * https://developer.mozilla.org/en-US/docs/Web/API/ImageData - * - * @private - * - * @param {Canvas|ImageData} canvas canvas to get image data from - * @return {ImageData} Holder of pixel data (and width and - * height) for a canvas - */ + * Returns the ImageData object for a canvas. + * https://developer.mozilla.org/en-US/docs/Web/API/ImageData + * + * @private + * + * @param {Canvas|ImageData} canvas canvas to get image data from + * @return {ImageData} Holder of pixel data (and width and + * height) for a canvas + */ _toImageData(canvas) { if (canvas instanceof ImageData) { return canvas; @@ -128,14 +128,14 @@ const Filters = { /** - * Returns a blank ImageData object. - * - * @private - * - * @param {Integer} width - * @param {Integer} height - * @return {ImageData} - */ + * Returns a blank ImageData object. + * + * @private + * + * @param {Integer} width + * @param {Integer} height + * @return {ImageData} + */ _createImageData(width, height) { Filters._tmpCanvas = document.createElement('canvas'); Filters._tmpCtx = Filters._tmpCanvas.getContext('2d'); @@ -143,24 +143,24 @@ const Filters = { }, /** - * Applys a filter function to a canvas. - * - * The difference between this and the actual filter functions defined below - * is that the filter functions generally modify the pixel buffer but do - * not actually put that data back to the canvas (where it would actually - * update what is visible). By contrast this method does make the changes - * actually visible in the canvas. - * - * The apply method is the method that callers of this module would generally - * use. It has been separated from the actual filters to support an advanced - * use case of creating a filter chain that executes without actually updating - * the canvas in between everystep. - * - * @private - * @param {HTMLCanvasElement} canvas The input canvas to apply the filter on. - * @param {function(ImageData,Object)} func The filter function to apply to the canvas's pixel data. - * @param {Object} filterParam An optional parameter to pass to the filter function. - */ + * Applys a filter function to a canvas. + * + * The difference between this and the actual filter functions defined below + * is that the filter functions generally modify the pixel buffer but do + * not actually put that data back to the canvas (where it would actually + * update what is visible). By contrast this method does make the changes + * actually visible in the canvas. + * + * The apply method is the method that callers of this module would generally + * use. It has been separated from the actual filters to support an advanced + * use case of creating a filter chain that executes without actually updating + * the canvas in between everystep. + * + * @private + * @param {HTMLCanvasElement} canvas The input canvas to apply the filter on. + * @param {function(ImageData,Object)} func The filter function to apply to the canvas's pixel data. + * @param {Object} filterParam An optional parameter to pass to the filter function. + */ apply(canvas, func, filterParam) { const pixelsState = canvas.getContext('2d'); const imageData = pixelsState.getImageData( @@ -194,26 +194,23 @@ const Filters = { }, /* - * Filters - */ + * Filters + */ /** - * Converts the image to black and white pixels depending if they are above or - * below the threshold defined by the level parameter. The parameter must be - * between 0.0 (black) and 1.0 (white). If no level is specified, 0.5 is used. - * - * Borrowed from http://www.html5rocks.com/en/tutorials/canvas/imagefilters/ - * - * @private - * @param {Canvas} canvas Canvas to apply thershold filter on. - * @param {Float} level Threshold level (0-1). - */ - threshold(canvas, level) { + * Converts the image to black and white pixels depending if they are above or + * below the threshold defined by the level parameter. The parameter must be + * between 0.0 (black) and 1.0 (white). If no level is specified, 0.5 is used. + * + * Borrowed from http://www.html5rocks.com/en/tutorials/canvas/imagefilters/ + * + * @private + * @param {Canvas} canvas Canvas to apply thershold filter on. + * @param {Float} level Threshold level (0-1). + */ + threshold(canvas, level = 0.5) { const pixels = Filters._toPixels(canvas); - if (level === undefined) { - level = 0.5; - } // Calculate threshold value on a (0-255) scale. const thresh = Math.floor(level * 255); @@ -234,14 +231,14 @@ const Filters = { }, /** - * Converts any colors in the image to grayscale equivalents. - * No parameter is used. - * - * Borrowed from http://www.html5rocks.com/en/tutorials/canvas/imagefilters/ - * - * @private - * @param {Canvas} canvas Canvas to apply gray filter on. - */ + * Converts any colors in the image to grayscale equivalents. + * No parameter is used. + * + * Borrowed from http://www.html5rocks.com/en/tutorials/canvas/imagefilters/ + * + * @private + * @param {Canvas} canvas Canvas to apply gray filter on. + */ gray(canvas) { const pixels = Filters._toPixels(canvas); @@ -257,11 +254,11 @@ const Filters = { }, /** - * Sets the alpha channel to entirely opaque. No parameter is used. - * - * @private - * @param {Canvas} canvas - */ + * Sets the alpha channel to entirely opaque. No parameter is used. + * + * @private + * @param {Canvas} canvas + */ opaque(canvas) { const pixels = Filters._toPixels(canvas); @@ -273,10 +270,10 @@ const Filters = { }, /** - * Sets each pixel to its inverse value. No parameter is used. - * @private - * @param {Canvas} canvas - */ + * Sets each pixel to its inverse value. No parameter is used. + * @private + * @param {Canvas} canvas + */ invert(canvas) { const pixels = Filters._toPixels(canvas); @@ -288,21 +285,18 @@ const Filters = { }, /** - * Limits each channel of the image to the number of colors specified as - * the parameter. The parameter can be set to values between 2 and 255, but - * results are most noticeable in the lower ranges. - * - * Adapted from java based processing implementation - * - * @private - * @param {Canvas} canvas - * @param {Integer} level - */ - posterize(canvas, level) { + * Limits each channel of the image to the number of colors specified as + * the parameter. The parameter can be set to values between 2 and 255, but + * results are most noticeable in the lower ranges. + * + * Adapted from java based processing implementation + * + * @private + * @param {Canvas} canvas + * @param {Integer} level + */ + posterize(canvas, level = 4) { const pixels = Filters._toPixels(canvas); - if (level === undefined) { - level = 4; - } if (level < 2 || level > 255) { throw new Error( 'Level must be greater than 2 and less than 255 for posterize' @@ -323,10 +317,10 @@ const Filters = { }, /** - * Increases the bright areas in an image. - * @private - * @param {Canvas} canvas - */ + * Increases the bright areas in an image. + * @private + * @param {Canvas} canvas + */ dilate(canvas) { const pixels = Filters._toPixels(canvas); let currIdx = 0; @@ -415,11 +409,11 @@ const Filters = { }, /** - * Reduces the bright areas in an image. - * Similar to `dilate()`, but updates the output color based on the lowest luminance value. - * @private - * @param {Canvas} canvas - */ + * Reduces the bright areas in an image. + * Similar to `dilate()`, but updates the output color based on the lowest luminance value. + * @private + * @param {Canvas} canvas + */ erode(canvas) { const pixels = Filters._toPixels(canvas); let currIdx = 0; diff --git a/src/image/image.js b/src/image/image.js index 2344c07257..3e82f6cbb4 100644 --- a/src/image/image.js +++ b/src/image/image.js @@ -170,21 +170,20 @@ p5.prototype.createImage = function(width, height) { * @param {String} [filename] * @param {String} [extension] */ -p5.prototype.saveCanvas = function() { - p5._validateParameters('saveCanvas', arguments); +p5.prototype.saveCanvas = function(...args) { + p5._validateParameters('saveCanvas', args); // copy arguments to array - const args = [].slice.call(arguments); let htmlCanvas, filename, extension, temporaryGraphics; - if (arguments[0] instanceof HTMLCanvasElement) { - htmlCanvas = arguments[0]; + if (args[0] instanceof HTMLCanvasElement) { + htmlCanvas = args[0]; args.shift(); - } else if (arguments[0] instanceof p5.Element) { - htmlCanvas = arguments[0].elt; + } else if (args[0] instanceof p5.Element) { + htmlCanvas = args[0].elt; args.shift(); - } else if (arguments[0] instanceof p5.Framebuffer) { - const framebuffer = arguments[0]; + } else if (args[0] instanceof p5.Framebuffer) { + const framebuffer = args[0]; temporaryGraphics = this.createGraphics(framebuffer.width, framebuffer.height); temporaryGraphics.pixelDensity(pixelDensity()); diff --git a/src/image/pixels.js b/src/image/pixels.js index 476c7ab5ec..b834501ade 100644 --- a/src/image/pixels.js +++ b/src/image/pixels.js @@ -568,7 +568,7 @@ p5.prototype.getFilterGraphicsLayer = function() { * frag shader using a `tex0` uniform. */ p5.prototype.filter = function(...args) { - p5._validateParameters('filter', arguments); + p5._validateParameters('filter', args); let { shader, operation, value, useWebGL } = parseFilterArgs(...args); diff --git a/src/io/files.js b/src/io/files.js index 8a0370c4f9..fe1367a90c 100644 --- a/src/io/files.js +++ b/src/io/files.js @@ -806,10 +806,9 @@ p5.prototype.loadBytes = function(file, callback, errorCallback) { * @param {function} [errorCallback] * @return {Promise} */ -p5.prototype.httpGet = function() { - p5._validateParameters('httpGet', arguments); +p5.prototype.httpGet = function(...args) { + p5._validateParameters('httpGet', args); - const args = Array.prototype.slice.call(arguments); args.splice(1, 0, 'GET'); return p5.prototype.httpDo.apply(this, args); }; @@ -896,10 +895,9 @@ p5.prototype.httpGet = function() { * @param {function} [errorCallback] * @return {Promise} */ -p5.prototype.httpPost = function() { - p5._validateParameters('httpPost', arguments); +p5.prototype.httpPost = function(...args) { + p5._validateParameters('httpPost', args); - const args = Array.prototype.slice.call(arguments); args.splice(1, 0, 'POST'); return p5.prototype.httpDo.apply(this, args); }; @@ -1880,8 +1878,7 @@ p5.prototype._checkFileExtension = _checkFileExtension; * @private */ p5.prototype._isSafari = function() { - const x = Object.prototype.toString.call(window.HTMLElement); - return x.indexOf('Constructor') > 0; + return window.HTMLElement.toString().includes('Constructor'); }; /** diff --git a/src/math/calculation.js b/src/math/calculation.js index 77d9239750..e23ca71d5d 100644 --- a/src/math/calculation.js +++ b/src/math/calculation.js @@ -470,15 +470,9 @@ p5.prototype.map = function(n, start1, stop1, start2, stop2, withinBounds) { * @return {Number} */ p5.prototype.max = function(...args) { - const findMax = arr => { - let max = -Infinity; - for (let x of arr) { - max = Math.max(max, x); - } - return max; - }; + const findMax = arr => Math.max(...arr); - if (args[0] instanceof Array) { + if (Array.isArray(args[0])) { return findMax(args[0]); } else { return findMax(args); @@ -548,15 +542,9 @@ p5.prototype.max = function(...args) { * @return {Number} */ p5.prototype.min = function(...args) { - const findMin = arr => { - let min = Infinity; - for (let x of arr) { - min = Math.min(min, x); - } - return min; - }; + const findMin = arr => Math.min(...arr); - if (args[0] instanceof Array) { + if (Array.isArray(args[0])) { return findMin(args[0]); } else { return findMin(args); diff --git a/src/math/p5.Vector.js b/src/math/p5.Vector.js index 2ccb5fc4f5..8257425f32 100644 --- a/src/math/p5.Vector.js +++ b/src/math/p5.Vector.js @@ -7,29 +7,6 @@ import p5 from '../core/main'; import * as constants from '../core/constants'; -/// HELPERS FOR REMAINDER METHOD -const calculateRemainder2D = function(xComponent, yComponent) { - if (xComponent !== 0) { - this.x = this.x % xComponent; - } - if (yComponent !== 0) { - this.y = this.y % yComponent; - } - return this; -}; - -const calculateRemainder3D = function(xComponent, yComponent, zComponent) { - if (xComponent !== 0) { - this.x = this.x % xComponent; - } - if (yComponent !== 0) { - this.y = this.y % yComponent; - } - if (zComponent !== 0) { - this.z = this.z % zComponent; - } - return this; -}; /** * A class to describe a two or three-dimensional vector. A vector is like an @@ -366,75 +343,105 @@ p5.Vector = class { } /** - * Performs modulo (remainder) division with a vector's `x`, `y`, and `z` - * components using separate numbers, another - * p5.Vector object, or an array of numbers. - * - * The static version of `rem()` as in `p5.Vector.rem(v2, v1)`, returns a new - * p5.Vector object and doesn't change the - * originals. - * - * @method rem - * @param {Number} x x component of divisor vector. - * @param {Number} y y component of divisor vector. - * @param {Number} z z component of divisor vector. - * @chainable - * @example - *
- * let v = createVector(3, 4, 5);
- * v.rem(2, 3, 4);
- * // Prints 'p5.Vector Object : [1, 1, 1]'.
- * print(v.toString());
- *
- *
- * let v1 = createVector(3, 4, 5);
- * let v2 = createVector(2, 3, 4);
- * v1.rem(v2);
- *
- * // Prints 'p5.Vector Object : [1, 1, 1]'.
- * print(v1.toString());
- *
- *
- * let v = createVector(3, 4, 5);
- * let arr = [2, 3, 4];
- * v.rem(arr);
- *
- * // Prints 'p5.Vector Object : [1, 1, 1]'.
- * print(v.toString());
- *
- *
- * let v1 = createVector(3, 4, 5);
- * let v2 = createVector(2, 3, 4);
- * let v3 = p5.Vector.rem(v1, v2);
- *
- * // Prints 'p5.Vector Object : [1, 1, 1]'.
- * print(v3.toString());
- *
- *
+ * let v = createVector(3, 4, 5);
+ * v.rem(2, 3, 4);
+ * // Prints 'p5.Vector Object : [1, 1, 1]'.
+ * print(v.toString());
+ *
+ *
+ * let v1 = createVector(3, 4, 5);
+ * let v2 = createVector(2, 3, 4);
+ * v1.rem(v2);
+ *
+ * // Prints 'p5.Vector Object : [1, 1, 1]'.
+ * print(v1.toString());
+ *
+ *
+ * let v = createVector(3, 4, 5);
+ * let arr = [2, 3, 4];
+ * v.rem(arr);
+ *
+ * // Prints 'p5.Vector Object : [1, 1, 1]'.
+ * print(v.toString());
+ *
+ *
+ * let v1 = createVector(3, 4, 5);
+ * let v2 = createVector(2, 3, 4);
+ * let v3 = p5.Vector.rem(v1, v2);
+ *
+ * // Prints 'p5.Vector Object : [1, 1, 1]'.
+ * print(v3.toString());
+ *
+ *