p5
Version:
[](https://www.npmjs.com/package/p5)
86 lines (85 loc) • 3.94 kB
JavaScript
import { Vector } from "./p5.Vector.js";
//#region src/math/patch-vector.js
function _vectorError(instance, message = "Requires valid arguments") {
if (!instance.friendlyErrorsDisabled()) instance._friendlyError(message, "p5.Vector");
return instance;
}
/**
* @private
* @internal
*/
function _defaultEmptyVector(target) {
return function(...args) {
if (args.length === 0) {
this.constructor._friendlyError("In 1.x, createVector() was a shortcut for createVector(0, 0, 0). In 2.x, p5.js has vectors of any dimension, so you must provide your desired number of zeros. Use createVector(0, 0) for a 2D vector and createVector(0, 0, 0) for a 3D vector.", "p5.createVector");
return target.call(this, 0, 0, 0);
} else {
if (Array.isArray(args[0])) args = args[0];
return target.call(this, ...args);
}
};
}
/**
* @private
* @internal
* @param {Boolean} expectsSoloNumberArgument whether a single number scales
* every component (mult/div/rem)
* @param {Number} [trailingArgCount=0] number of trailing args to peel off
* before treating the rest as vector components (e.g. 1 for lerp's amt)
* @param {Number[]} [trailingDefaults] if provided, used when only component
* values are passed (no trailing args), e.g. `[0]` so `v.lerp(other)` uses amt 0
*/
function _validatedVectorOperation(expectsSoloNumberArgument, trailingArgCount = 0, trailingDefaults) {
return function(target) {
return function(...args) {
const trailing = [];
if (trailingArgCount > 0) {
if (args.length === 1 && (args[0] instanceof Vector || Array.isArray(args[0])) && trailingDefaults) for (let i = 0; i < trailingDefaults.length; i++) trailing.push(trailingDefaults[i]);
else if (args.length < trailingArgCount) {
if (trailingDefaults && args.length === 0) return this;
return _vectorError(this);
} else {
for (let i = 0; i < trailingArgCount; i++) trailing.unshift(args.pop());
for (let i = 0; i < trailing.length; i++) {
const t = trailing[i];
if (typeof t !== "number" || !Number.isFinite(t)) return _vectorError(this, "Arguments contain non-finite numbers");
}
if (args.length === 0) return _vectorError(this);
}
} else if (args.length === 0) return this;
if (args[0] instanceof Vector) {
if (trailingArgCount > 0 && args.length > 1) return _vectorError(this);
args = args[0].values;
} else if (Array.isArray(args[0])) {
if (trailingArgCount > 0 && args.length > 1) return _vectorError(this);
args = args[0];
} else if (args.length === 1) {
if (expectsSoloNumberArgument) args = args[0];
}
if (Array.isArray(args)) for (let i = 0; i < args.length; i++) {
const v = args[i];
if (typeof v !== "number" || !Number.isFinite(v)) return _vectorError(this, "Arguments contain non-finite numbers");
}
else if (typeof args !== "number" || !Number.isFinite(args)) return _vectorError(this, "Arguments contain non-finite numbers");
return target.call(this, args, ...trailing);
};
};
}
/**
* @private
* @internal
* Each of the following decorators validates the data on vector operations.
* These ensure that the arguments are consistently formatted, and that
* pre-conditions are met.
*/
function vectorValidation(p5, fn, lifecycles) {
p5.registerDecorator("p5.prototype.createVector", _defaultEmptyVector);
p5.registerDecorator("p5.Vector.prototype.mult", _validatedVectorOperation(true));
p5.registerDecorator("p5.Vector.prototype.rem", _validatedVectorOperation(true));
p5.registerDecorator("p5.Vector.prototype.div", _validatedVectorOperation(true));
p5.registerDecorator("p5.Vector.prototype.add", _validatedVectorOperation(false));
p5.registerDecorator("p5.Vector.prototype.sub", _validatedVectorOperation(false));
p5.registerDecorator("p5.Vector.prototype.lerp", _validatedVectorOperation(false, 1, [0]));
}
//#endregion
export { _defaultEmptyVector, _validatedVectorOperation, vectorValidation as default };