1) {
message += ' ' + func + ' takes different numbers of parameters ' +
'depending on what you want to do. ' +
'Click this link to learn more:';
}
report(message, func, WRONG_TYPE);
}
}
}
};
/*
* NOTE THIS FUNCTION IS TEMPORARILY DISABLED UNTIL FURTHER WORK
* AND UPDATES ARE IMPLEMENTED. -LMCCART
*/
p5.prototype._validateParameters = function() {
return true;
};
var errorCases = {
'0': {
fileType: 'image',
method: 'loadImage',
message: ' hosting the image online,'
},
'1': {
fileType: 'XML file',
method: 'loadXML'
},
'2': {
fileType: 'table file',
method: 'loadTable'
},
'3': {
fileType: 'text file',
method: 'loadStrings'
}
};
p5._friendlyFileLoadError = function (errorType, filePath) {
var errorInfo = errorCases[ errorType ];
var message = 'It looks like there was a problem' +
' loading your ' + errorInfo.fileType + '.' +
' Try checking if the file path%c [' + filePath + '] %cis correct,' +
(errorInfo.message || '') + ' or running a local server.';
report(message, errorInfo.method, FILE_LOAD);
};
function friendlyWelcome() {
// p5.js brand - magenta: #ED225D
var astrixBgColor = 'transparent';
var astrixTxtColor = '#ED225D';
var welcomeBgColor = '#ED225D';
var welcomeTextColor = 'white';
console.log(
'%c _ \n'+
' /\\| |/\\ \n'+
' \\ ` \' / \n'+
' / , . \\ \n'+
' \\/|_|\\/ '+
'\n\n%c> p5.js says: Welcome! '+
'This is your friendly debugger. ' +
'To turn me off switch to using “p5.min.js”.',
'background-color:'+astrixBgColor+';color:' + astrixTxtColor +';',
'background-color:'+welcomeBgColor+';color:' + welcomeTextColor +';'
);
}
/**
* Prints out all the colors in the color pallete with white text.
* For color blindness testing.
*/
/* function testColors() {
var str = 'A box of biscuits, a box of mixed biscuits and a biscuit mixer';
report(str, 'println', '#ED225D'); // p5.js magenta
report(str, 'println', '#2D7BB6'); // p5.js blue
report(str, 'println', '#EE9900'); // p5.js orange
report(str, 'println', '#A67F59'); // p5.js light brown
report(str, 'println', '#704F21'); // p5.js gold
report(str, 'println', '#1CC581'); // auto cyan
report(str, 'println', '#FF6625'); // auto orange
report(str, 'println', '#79EB22'); // auto green
report(str, 'println', '#B40033'); // p5.js darkened magenta
report(str, 'println', '#084B7F'); // p5.js darkened blue
report(str, 'println', '#945F00'); // p5.js darkened orange
report(str, 'println', '#6B441D'); // p5.js darkened brown
report(str, 'println', '#2E1B00'); // p5.js darkened gold
report(str, 'println', '#008851'); // auto dark cyan
report(str, 'println', '#C83C00'); // auto dark orange
report(str, 'println', '#4DB200'); // auto dark green
} */
module.exports = p5;
},{"./core":48}],52:[function(_dereq_,module,exports){
/**
* @module DOM
* @submodule DOM
* @for p5.Element
*/
var p5 = _dereq_('./core');
/**
* Base class for all elements added to a sketch, including canvas,
* graphics buffers, and other HTML elements. Methods in blue are
* included in the core functionality, methods in brown are added
* with the p5.dom library.
* It is not called directly, but p5.Element
* objects are created by calling createCanvas, createGraphics,
* or in the p5.dom library, createDiv, createImg, createInput, etc.
*
* @class p5.Element
* @constructor
* @param {String} elt DOM node that is wrapped
* @param {Object} [pInst] pointer to p5 instance
*/
p5.Element = function(elt, pInst) {
/**
* Underlying HTML element. All normal HTML methods can be called on this.
*
* @property elt
*/
this.elt = elt;
this._pInst = pInst;
this._events = {};
this.width = this.elt.offsetWidth;
this.height = this.elt.offsetHeight;
};
/**
*
* Attaches the element to the parent specified. A way of setting
* the container for the element. Accepts either a string ID, DOM
* node, or p5.Element. If no arguments given, parent node is returned.
*
* @method parent
* @param {String|Object} parent the ID, DOM node, or p5.Element
* of desired parent element
* @return {p5.Element}
* @example
*
* // in the html file:
* <div id="myContainer"></div>
* // in the js file:
* var cnv = createCanvas(100, 100);
* cnv.parent("myContainer");
*
*
* var div0 = createDiv('this is the parent');
* var div1 = createDiv('this is the child');
* div1.parent(div0); // use p5.Element
*
*
* var div0 = createDiv('this is the parent');
* div0.id('apples');
* var div1 = createDiv('this is the child');
* div1.parent('apples'); // use id
*
*
* var elt = document.getElementById('myParentDiv');
* var div1 = createDiv('this is the child');
* div1.parent(elt); // use element from page
*
*/
p5.Element.prototype.parent = function(p) {
if (arguments.length === 0){
return this.elt.parentNode;
} else {
if (typeof p === 'string') {
if (p[0] === '#') {
p = p.substring(1);
}
p = document.getElementById(p);
} else if (p instanceof p5.Element) {
p = p.elt;
}
p.appendChild(this.elt);
return this;
}
};
/**
*
* Sets the ID of the element
*
* @method id
* @param {String} id ID of the element
* @return {p5.Element}
*/
p5.Element.prototype.id = function(id) {
this.elt.id = id;
return this;
};
/**
*
* Adds given class to the element
*
* @method class
* @param {String} class class to add
* @return {p5.Element}
*/
p5.Element.prototype.class = function(c) {
this.elt.className = c;
return this;
};
/**
* The .mousePressed() function is called once after every time a
* mouse button is pressed over the element. This can be used to
* attach element specific event listeners.
*
* @method mousePressed
* @param {Function} fxn function to be fired when mouse is
* pressed over the element.
* @return {p5.Element}
* @example
*
* var cnv;
* var d;
* var g;
* function setup() {
* cnv = createCanvas(100, 100);
* cnv.mousePressed(changeGray); // attach listener for
* // canvas click only
* d = 10;
* g = 100;
* }
*
* function draw() {
* background(g);
* ellipse(width/2, height/2, d, d);
* }
*
* // this function fires with any click anywhere
* function mousePressed() {
* d = d + 10;
* }
*
* // this function fires only when cnv is clicked
* function changeGray() {
* g = random(0, 255);
* }
*
*
*/
p5.Element.prototype.mousePressed = function (fxn) {
attachListener('mousedown', fxn, this);
attachListener('touchstart', fxn, this);
return this;
};
/**
* The .mouseWheel() function is called once after every time a
* mouse wheel is scrolled over the element. This can be used to
* attach element specific event listeners.
* The event.wheelDelta or event.detail property returns negative values if
* the mouse wheel if rotated up or away from the user and positive in the
* other direction. On OS X with "natural" scrolling enabled, the values are
* opposite.
*
* @method mouseWheel
* @param {Function} fxn function to be fired when mouse wheel is
* scrolled over the element.
* @return {p5.Element}
*/
p5.Element.prototype.mouseWheel = function (fxn) {
attachListener('mousewheel', fxn, this);
return this;
};
/**
* The .mouseReleased() function is called once after every time a
* mouse button is released over the element. This can be used to
* attach element specific event listeners.
*
* @method mouseReleased
* @param {Function} fxn function to be fired when mouse is
* released over the element.
* @return {p5.Element}
*/
p5.Element.prototype.mouseReleased = function (fxn) {
attachListener('mouseup', fxn, this);
attachListener('touchend', fxn, this);
return this;
};
/**
* The .mouseClicked() function is called once after a mouse button is
* pressed and released over the element. This can be used to
* attach element specific event listeners.
*
* @method mouseClicked
* @param {Function} fxn function to be fired when mouse is
* clicked over the element.
* @return {p5.Element}
*/
p5.Element.prototype.mouseClicked = function (fxn) {
attachListener('click', fxn, this);
return this;
};
/**
* The .mouseMoved() function is called once every time a
* mouse moves over the element. This can be used to attach an
* element specific event listener.
*
* @method mouseMoved
* @param {Function} fxn function to be fired when mouse is
* moved over the element.
* @return {p5.Element}
*/
p5.Element.prototype.mouseMoved = function (fxn) {
attachListener('mousemove', fxn, this);
attachListener('touchmove', fxn, this);
return this;
};
/**
* The .mouseOver() function is called once after every time a
* mouse moves onto the element. This can be used to attach an
* element specific event listener.
*
* @method mouseOver
* @param {Function} fxn function to be fired when mouse is
* moved over the element.
* @return {p5.Element}
*/
p5.Element.prototype.mouseOver = function (fxn) {
attachListener('mouseover', fxn, this);
return this;
};
/**
* The .changed() function is called when the value of an
* element is changed.
* This can be used to attach an element specific event listener.
*
* @method changed
* @param {Function} fxn function to be fired when the value of an
* element changes.
* @return {p5.Element}
*/
p5.Element.prototype.changed = function (fxn) {
attachListener('change', fxn, this);
return this;
};
/**
* The .input() function is called when any user input is
* detected with an element. The input event is often used
* to detect keystrokes in a input element, or changes on a
* slider element. This can be used to attach an element specific
* event listener.
*
* @method input
* @param {Function} fxn function to be fired on user input.
* @return {p5.Element}
*/
p5.Element.prototype.input = function (fxn) {
attachListener('input', fxn, this);
return this;
};
/**
* The .mouseOut() function is called once after every time a
* mouse moves off the element. This can be used to attach an
* element specific event listener.
*
* @method mouseOut
* @param {Function} fxn function to be fired when mouse is
* moved off the element.
* @return {p5.Element}
*/
p5.Element.prototype.mouseOut = function (fxn) {
attachListener('mouseout', fxn, this);
return this;
};
/**
* The .touchStarted() function is called once after every time a touch is
* registered. This can be used to attach element specific event listeners.
*
* @method touchStarted
* @param {Function} fxn function to be fired when touch is
* started over the element.
* @return {p5.Element}
* @example
*
* var cnv;
* var d;
* var g;
* function setup() {
* cnv = createCanvas(100, 100);
* cnv.touchStarted(changeGray); // attach listener for
* // canvas click only
* d = 10;
* g = 100;
* }
*
* function draw() {
* background(g);
* ellipse(width/2, height/2, d, d);
* }
*
* // this function fires with any touch anywhere
* function touchStarted() {
* d = d + 10;
* }
*
* // this function fires only when cnv is clicked
* function changeGray() {
* g = random(0, 255);
* }
*
*
*/
p5.Element.prototype.touchStarted = function (fxn) {
attachListener('touchstart', fxn, this);
attachListener('mousedown', fxn, this);
return this;
};
/**
* The .touchMoved() function is called once after every time a touch move is
* registered. This can be used to attach element specific event listeners.
*
* @method touchMoved
* @param {Function} fxn function to be fired when touch is moved
* over the element.
* @return {p5.Element}
* @example
*
* var cnv;
* var g;
* function setup() {
* cnv = createCanvas(100, 100);
* cnv.touchMoved(changeGray); // attach listener for
* // canvas click only
* g = 100;
* }
*
* function draw() {
* background(g);
* }
*
* // this function fires only when cnv is clicked
* function changeGray() {
* g = random(0, 255);
* }
*
*
*/
p5.Element.prototype.touchMoved = function (fxn) {
attachListener('touchmove', fxn, this);
attachListener('mousemove', fxn, this);
return this;
};
/**
* The .touchEnded() function is called once after every time a touch is
* registered. This can be used to attach element specific event listeners.
*
* @method touchEnded
* @param {Function} fxn function to be fired when touch is
* ended over the element.
* @return {p5.Element}
* @example
*
* var cnv;
* var d;
* var g;
* function setup() {
* cnv = createCanvas(100, 100);
* cnv.touchEnded(changeGray); // attach listener for
* // canvas click only
* d = 10;
* g = 100;
* }
*
* function draw() {
* background(g);
* ellipse(width/2, height/2, d, d);
* }
*
* // this function fires with any touch anywhere
* function touchEnded() {
* d = d + 10;
* }
*
* // this function fires only when cnv is clicked
* function changeGray() {
* g = random(0, 255);
* }
*
*
*/
p5.Element.prototype.touchEnded = function (fxn) {
attachListener('touchend', fxn, this);
attachListener('mouseup', fxn, this);
return this;
};
/**
* The .dragOver() function is called once after every time a
* file is dragged over the element. This can be used to attach an
* element specific event listener.
*
* @method dragOver
* @param {Function} fxn function to be fired when mouse is
* dragged over the element.
* @return {p5.Element}
*/
p5.Element.prototype.dragOver = function (fxn) {
attachListener('dragover', fxn, this);
return this;
};
/**
* The .dragLeave() function is called once after every time a
* dragged file leaves the element area. This can be used to attach an
* element specific event listener.
*
* @method dragLeave
* @param {Function} fxn function to be fired when mouse is
* dragged over the element.
* @return {p5.Element}
*/
p5.Element.prototype.dragLeave = function (fxn) {
attachListener('dragleave', fxn, this);
return this;
};
/**
* The .drop() function is called for each file dropped on the element.
* It requires a callback that is passed a p5.File object. You can
* optionally pass two callbacks, the first one (required) is triggered
* for each file dropped when the file is loaded. The second (optional)
* is triggered just once when a file (or files) are dropped.
*
* @method drop
* @param {Function} callback triggered when files are dropped.
* @param {Function} callback to receive loaded file.
* @return {p5.Element}
*/
p5.Element.prototype.drop = function (callback, fxn) {
// Make a file loader callback and trigger user's callback
function makeLoader(theFile) {
// Making a p5.File object
var p5file = new p5.File(theFile);
return function(e) {
p5file.data = e.target.result;
callback(p5file);
};
}
// Is the file stuff supported?
if (window.File && window.FileReader && window.FileList && window.Blob) {
// If you want to be able to drop you've got to turn off
// a lot of default behavior
attachListener('dragover',function(evt) {
evt.stopPropagation();
evt.preventDefault();
},this);
// If this is a drag area we need to turn off the default behavior
attachListener('dragleave',function(evt) {
evt.stopPropagation();
evt.preventDefault();
},this);
// If just one argument it's the callback for the files
if (arguments.length > 1) {
attachListener('drop', fxn, this);
}
// Deal with the files
attachListener('drop', function(evt) {
evt.stopPropagation();
evt.preventDefault();
// A FileList
var files = evt.dataTransfer.files;
// Load each one and trigger the callback
for (var i = 0; i < files.length; i++) {
var f = files[i];
var reader = new FileReader();
reader.onload = makeLoader(f);
// Text or data?
// This should likely be improved
if (f.type.indexOf('text') > -1) {
reader.readAsText(f);
} else {
reader.readAsDataURL(f);
}
}
}, this);
} else {
console.log('The File APIs are not fully supported in this browser.');
}
return this;
};
function attachListener(ev, fxn, ctx) {
// LM removing, not sure why we had this?
// var _this = ctx;
// var f = function (e) { fxn(e, _this); };
var f = fxn.bind(ctx);
ctx.elt.addEventListener(ev, f, false);
ctx._events[ev] = f;
}
/**
* Helper fxn for sharing pixel methods
*
*/
p5.Element.prototype._setProperty = function (prop, value) {
this[prop] = value;
};
module.exports = p5.Element;
},{"./core":48}],53:[function(_dereq_,module,exports){
/**
* @module Rendering
* @submodule Rendering
* @for p5
*/
var p5 = _dereq_('./core');
var constants = _dereq_('./constants');
/**
* Thin wrapper around a renderer, to be used for creating a
* graphics buffer object. Use this class if you need
* to draw into an off-screen graphics buffer. The two parameters define the
* width and height in pixels. The fields and methods for this class are
* extensive, but mirror the normal drawing API for p5.
*
* @class p5.Graphics
* @constructor
* @extends p5.Element
* @param {String} elt DOM node that is wrapped
* @param {Object} [pInst] pointer to p5 instance
* @param {Boolean} whether we're using it as main canvas
*/
p5.Graphics = function(w, h, renderer, pInst) {
var r = renderer || constants.P2D;
var c = document.createElement('canvas');
var node = this._userNode || document.body;
node.appendChild(c);
p5.Element.call(this, c, pInst, false);
this._styles = [];
this.width = w;
this.height = h;
this._pixelDensity = pInst._pixelDensity;
if (r === constants.WEBGL) {
this._renderer = new p5.Renderer3D(c, pInst, false);
} else {
this._renderer = new p5.Renderer2D(c, pInst, false);
}
this._renderer.resize(w, h);
this._renderer._applyDefaults();
pInst._elements.push(this);
// bind methods and props of p5 to the new object
for (var p in p5.prototype) {
if (!this[p]) {
if (typeof p5.prototype[p] === 'function') {
this[p] = p5.prototype[p].bind(this);
} else {
this[p] = p5.prototype[p];
}
}
}
return this;
};
p5.Graphics.prototype = Object.create(p5.Element.prototype);
module.exports = p5.Graphics;
},{"./constants":47,"./core":48}],54:[function(_dereq_,module,exports){
/**
* @module Rendering
* @submodule Rendering
* @for p5
*/
var p5 = _dereq_('./core');
var constants = _dereq_('../core/constants');
/**
* Main graphics and rendering context, as well as the base API
* implementation for p5.js "core". To be used as the superclass for
* Renderer2D and Renderer3D classes, respecitvely.
*
* @class p5.Renderer
* @constructor
* @extends p5.Element
* @param {String} elt DOM node that is wrapped
* @param {Object} [pInst] pointer to p5 instance
* @param {Boolean} whether we're using it as main canvas
*/
p5.Renderer = function(elt, pInst, isMainCanvas) {
p5.Element.call(this, elt, pInst);
this.canvas = elt;
this._pInst = pInst;
if (isMainCanvas) {
this._isMainCanvas = true;
// for pixel method sharing with pimage
this._pInst._setProperty('_curElement', this);
this._pInst._setProperty('canvas', this.canvas);
this._pInst._setProperty('width', this.width);
this._pInst._setProperty('height', this.height);
} else { // hide if offscreen buffer by default
this.canvas.style.display = 'none';
this._styles = []; // non-main elt styles stored in p5.Renderer
}
this._textSize = 12;
this._textLeading = 15;
this._textFont = 'sans-serif';
this._textStyle = constants.NORMAL;
this._textAscent = null;
this._textDescent = null;
this._rectMode = constants.CORNER;
this._ellipseMode = constants.CENTER;
this._curveTightness = 0;
this._imageMode = constants.CORNER;
this._tint = null;
this._doStroke = true;
this._doFill = true;
this._strokeSet = false;
this._fillSet = false;
this._colorMode = constants.RGB;
this._colorMaxes = {
rgb: [255, 255, 255, 255],
hsb: [360, 100, 100, 1],
hsl: [360, 100, 100, 1]
};
};
p5.Renderer.prototype = Object.create(p5.Element.prototype);
/**
* Resize our canvas element.
*/
p5.Renderer.prototype.resize = function(w, h) {
this.width = w;
this.height = h;
this.elt.width = w * this._pInst._pixelDensity;
this.elt.height = h * this._pInst._pixelDensity;
this.elt.style.width = w +'px';
this.elt.style.height = h + 'px';
if (this._isMainCanvas) {
this._pInst._setProperty('width', this.width);
this._pInst._setProperty('height', this.height);
}
};
p5.Renderer.prototype.textLeading = function(l) {
if (arguments.length && arguments[0]) {
this._setProperty('_textLeading', l);
return this;
}
return this._textLeading;
};
p5.Renderer.prototype.textSize = function(s) {
if (arguments.length && arguments[0]) {
this._setProperty('_textSize', s);
this._setProperty('_textLeading', s * constants._DEFAULT_LEADMULT);
return this._applyTextProperties();
}
return this._textSize;
};
p5.Renderer.prototype.textStyle = function(s) {
if (arguments.length && arguments[0]) {
if (s === constants.NORMAL ||
s === constants.ITALIC ||
s === constants.BOLD) {
this._setProperty('_textStyle', s);
}
return this._applyTextProperties();
}
return this._textStyle;
};
p5.Renderer.prototype.textAscent = function() {
if (this._textAscent === null) {
this._updateTextMetrics();
}
return this._textAscent;
};
p5.Renderer.prototype.textDescent = function() {
if (this._textDescent === null) {
this._updateTextMetrics();
}
return this._textDescent;
};
/**
* Helper fxn to check font type (system or otf)
*/
p5.Renderer.prototype._isOpenType = function(f) {
f = f || this._textFont;
return (typeof f === 'object' && f.font && f.font.supported);
};
p5.Renderer.prototype._updateTextMetrics = function() {
if (this._isOpenType()) {
this._setProperty('_textAscent', this._textFont._textAscent());
this._setProperty('_textDescent', this._textFont._textDescent());
return this;
}
// Adapted from http://stackoverflow.com/a/25355178
var text = document.createElement('span');
text.style.fontFamily = this._textFont;
text.style.fontSize = this._textSize + 'px';
text.innerHTML = 'ABCjgq|';
var block = document.createElement('div');
block.style.display = 'inline-block';
block.style.width = '1px';
block.style.height = '0px';
var container = document.createElement('div');
container.appendChild(text);
container.appendChild(block);
container.style.height = '0px';
container.style.overflow = 'hidden';
document.body.appendChild(container);
block.style.verticalAlign = 'baseline';
var blockOffset = calculateOffset(block);
var textOffset = calculateOffset(text);
var ascent = blockOffset[1] - textOffset[1];
block.style.verticalAlign = 'bottom';
blockOffset = calculateOffset(block);
textOffset = calculateOffset(text);
var height = blockOffset[1] - textOffset[1];
var descent = height - ascent;
document.body.removeChild(container);
this._setProperty('_textAscent', ascent);
this._setProperty('_textDescent', descent);
return this;
};
/**
* Helper fxn to measure ascent and descent.
* Adapted from http://stackoverflow.com/a/25355178
*/
function calculateOffset(object) {
var currentLeft = 0,
currentTop = 0;
if (object.offsetParent) {
do {
currentLeft += object.offsetLeft;
currentTop += object.offsetTop;
} while (object = object.offsetParent);
} else {
currentLeft += object.offsetLeft;
currentTop += object.offsetTop;
}
return [currentLeft, currentTop];
}
module.exports = p5.Renderer;
},{"../core/constants":47,"./core":48}],55:[function(_dereq_,module,exports){
var p5 = _dereq_('./core');
var canvas = _dereq_('./canvas');
var constants = _dereq_('./constants');
var filters = _dereq_('../image/filters');
_dereq_('./p5.Renderer');
/**
* 2D graphics renderer class. Can also be used as an off-screen
* graphics buffer. A p5.Renderer2D object can be constructed
* with the createRenderer2D() function. The fields and methods
* for this class are extensive, but mirror the normal drawing API for p5.
*
* @class p5.Renderer2D
* @constructor
* @extends p5.Renderer
* @param {String} elt DOM node that is wrapped
* @param {Object} [pInst] pointer to p5 instance
* @example
*
*
* var pg;
* function setup() {
* createCanvas(100, 100);
* pg = createRenderer2D(40, 40);
* }
* function draw() {
* background(200);
* pg.background(100);
* pg.noStroke();
* pg.ellipse(pg.width/2, pg.height/2, 50, 50);
* image(pg, 9, 30);
* image(pg, 51, 30);
* }
*
*
*/
var styleEmpty = 'rgba(0,0,0,0)';
// var alphaThreshold = 0.00125; // minimum visible
p5.Renderer2D = function(elt, pInst, isMainCanvas){
p5.Renderer.call(this, elt, pInst, isMainCanvas);
this.drawingContext = this.canvas.getContext('2d');
this._pInst._setProperty('drawingContext', this.drawingContext);
return this;
};
p5.Renderer2D.prototype = Object.create(p5.Renderer.prototype);
p5.Renderer2D.prototype._applyDefaults = function() {
this.drawingContext.fillStyle = constants._DEFAULT_FILL;
this.drawingContext.strokeStyle = constants._DEFAULT_STROKE;
this.drawingContext.lineCap = constants.ROUND;
this.drawingContext.font = 'normal 12px sans-serif';
};
p5.Renderer2D.prototype.resize = function(w,h) {
p5.Renderer.prototype.resize.call(this, w,h);
this.drawingContext.scale(this._pInst._pixelDensity,
this._pInst._pixelDensity);
};
//////////////////////////////////////////////
// COLOR | Setting
//////////////////////////////////////////////
p5.Renderer2D.prototype.background = function() {
this.drawingContext.save();
this.drawingContext.setTransform(1, 0, 0, 1, 0, 0);
this.drawingContext.scale(this._pInst._pixelDensity,
this._pInst._pixelDensity);
if (arguments[0] instanceof p5.Image) {
this._pInst.image(arguments[0], 0, 0, this.width, this.height);
} else {
var curFill = this.drawingContext.fillStyle;
// create background rect
var color = this._pInst.color.apply(this._pInst, arguments);
var newFill = color.toString();
this.drawingContext.fillStyle = newFill;
this.drawingContext.fillRect(0, 0, this.width, this.height);
// reset fill
this.drawingContext.fillStyle = curFill;
}
this.drawingContext.restore();
};
p5.Renderer2D.prototype.clear = function() {
this.drawingContext.clearRect(0, 0, this.width, this.height);
};
p5.Renderer2D.prototype.fill = function() {
var ctx = this.drawingContext;
var color = this._pInst.color.apply(this._pInst, arguments);
ctx.fillStyle = color.toString();
};
p5.Renderer2D.prototype.stroke = function() {
var ctx = this.drawingContext;
var color = this._pInst.color.apply(this._pInst, arguments);
ctx.strokeStyle = color.toString();
};
//////////////////////////////////////////////
// IMAGE | Loading & Displaying
//////////////////////////////////////////////
p5.Renderer2D.prototype.image =
function (img, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight) {
var frame = img.canvas || img.elt;
try {
if (this._tint && img.canvas) {
this.drawingContext.drawImage(this._getTintedImageCanvas(img), sx, sy,
sWidth, sHeight, dx, dy, dWidth, dHeight);
} else {
this.drawingContext.drawImage(frame, sx, sy, sWidth, sHeight, dx, dy,
dWidth, dHeight);
}
} catch (e) {
if (e.name !== 'NS_ERROR_NOT_AVAILABLE') {
throw e;
}
}
};
p5.Renderer2D.prototype._getTintedImageCanvas = function (img) {
if (!img.canvas) {
return img;
}
var pixels = filters._toPixels(img.canvas);
var tmpCanvas = document.createElement('canvas');
tmpCanvas.width = img.canvas.width;
tmpCanvas.height = img.canvas.height;
var tmpCtx = tmpCanvas.getContext('2d');
var id = tmpCtx.createImageData(img.canvas.width, img.canvas.height);
var newPixels = id.data;
for (var i = 0; i < pixels.length; i += 4) {
var r = pixels[i];
var g = pixels[i + 1];
var b = pixels[i + 2];
var a = pixels[i + 3];
newPixels[i] = r * this._tint[0] / 255;
newPixels[i + 1] = g * this._tint[1] / 255;
newPixels[i + 2] = b * this._tint[2] / 255;
newPixels[i + 3] = a * this._tint[3] / 255;
}
tmpCtx.putImageData(id, 0, 0);
return tmpCanvas;
};
//////////////////////////////////////////////
// IMAGE | Pixels
//////////////////////////////////////////////
p5.Renderer2D.prototype.blendMode = function(mode) {
this.drawingContext.globalCompositeOperation = mode;
};
p5.Renderer2D.prototype.blend = function() {
var currBlend = this.drawingContext.globalCompositeOperation;
var blendMode = arguments[arguments.length - 1];
var copyArgs = Array.prototype.slice.call(
arguments,
0,
arguments.length - 1
);
this.drawingContext.globalCompositeOperation = blendMode;
this._pInst.copy.apply(this._pInst, copyArgs);
this.drawingContext.globalCompositeOperation = currBlend;
};
p5.Renderer2D.prototype.copy = function () {
var srcImage, sx, sy, sw, sh, dx, dy, dw, dh;
if (arguments.length === 9) {
srcImage = arguments[0];
sx = arguments[1];
sy = arguments[2];
sw = arguments[3];
sh = arguments[4];
dx = arguments[5];
dy = arguments[6];
dw = arguments[7];
dh = arguments[8];
} else if (arguments.length === 8) {
srcImage = this._pInst;
sx = arguments[0];
sy = arguments[1];
sw = arguments[2];
sh = arguments[3];
dx = arguments[4];
dy = arguments[5];
dw = arguments[6];
dh = arguments[7];
} else {
throw new Error('Signature not supported');
}
p5.Renderer2D._copyHelper(srcImage, sx, sy, sw, sh, dx, dy, dw, dh);
};
p5.Renderer2D._copyHelper =
function (srcImage, sx, sy, sw, sh, dx, dy, dw, dh) {
var s = srcImage.canvas.width / srcImage.width;
this.drawingContext.drawImage(srcImage.canvas,
s * sx, s * sy, s * sw, s * sh, dx, dy, dw, dh);
};
p5.Renderer2D.prototype.get = function(x, y, w, h) {
if (x === undefined && y === undefined &&
w === undefined && h === undefined){
x = 0;
y = 0;
w = this.width;
h = this.height;
} else if (w === undefined && h === undefined) {
w = 1;
h = 1;
}
// if the section does not overlap the canvas
if(x + w < 0 || y + h < 0 || x > this.width || y > this.height){
return [0, 0, 0, 255];
}
var ctx = this._pInst || this;
var pd = ctx._pixelDensity;
this.loadPixels.call(ctx);
if (w === 1 && h === 1){
return [
ctx.pixels[pd*4*(y*this.width+x)],
ctx.pixels[pd*(4*(y*this.width+x)+1)],
ctx.pixels[pd*(4*(y*this.width+x)+2)],
ctx.pixels[pd*(4*(y*this.width+x)+3)]
];
} else {
var sx = x * pd;
var sy = y * pd;
//auto constrain the width and height to
//dimensions of the source image
var dw = Math.min(w, ctx.width);
var dh = Math.min(h, ctx.height);
var sw = dw * pd;
var sh = dh * pd;
var region = new p5.Image(dw, dh);
region.canvas.getContext('2d').drawImage(this.canvas, sx, sy, sw, sh,
0, 0, dw, dh);
return region;
}
};
p5.Renderer2D.prototype.loadPixels = function () {
var pd = this._pixelDensity || this._pInst._pixelDensity;
var w = this.width * pd;
var h = this.height * pd;
var imageData = this.drawingContext.getImageData(0, 0, w, h);
// @todo this should actually set pixels per object, so diff buffers can
// have diff pixel arrays.
if (this._pInst) {
this._pInst._setProperty('imageData', imageData);
this._pInst._setProperty('pixels', imageData.data);
} else { // if called by p5.Image
this._setProperty('imageData', imageData);
this._setProperty('pixels', imageData.data);
}
};
p5.Renderer2D.prototype.set = function (x, y, imgOrCol) {
if (imgOrCol instanceof p5.Image) {
this.drawingContext.save();
this.drawingContext.setTransform(1, 0, 0, 1, 0, 0);
this.drawingContext.scale(this._pInst._pixelDensity,
this._pInst._pixelDensity);
this.drawingContext.drawImage(imgOrCol.canvas, x, y);
this.loadPixels.call(this._pInst);
this.drawingContext.restore();
} else {
var ctx = this._pInst || this;
var r = 0, g = 0, b = 0, a = 0;
var idx = 4*((y * ctx._pixelDensity) *
(this.width * ctx._pixelDensity) + (x * ctx._pixelDensity));
if (!ctx.imageData) {
ctx.loadPixels.call(ctx);
}
if (typeof imgOrCol === 'number') {
if (idx < ctx.pixels.length) {
r = imgOrCol;
g = imgOrCol;
b = imgOrCol;
a = 255;
//this.updatePixels.call(this);
}
}
else if (imgOrCol instanceof Array) {
if (imgOrCol.length < 4) {
throw new Error('pixel array must be of the form [R, G, B, A]');
}
if (idx < ctx.pixels.length) {
r = imgOrCol[0];
g = imgOrCol[1];
b = imgOrCol[2];
a = imgOrCol[3];
//this.updatePixels.call(this);
}
} else if (imgOrCol instanceof p5.Color) {
if (idx < ctx.pixels.length) {
r = imgOrCol.levels[0];
g = imgOrCol.levels[1];
b = imgOrCol.levels[2];
a = imgOrCol.levels[3];
//this.updatePixels.call(this);
}
}
// loop over pixelDensity * pixelDensity
for (var i = 0; i < ctx._pixelDensity; i++) {
for (var j = 0; j < ctx._pixelDensity; j++) {
// loop over
idx = 4*((y * ctx._pixelDensity + j) * this.width *
ctx._pixelDensity + (x * ctx._pixelDensity + i));
ctx.pixels[idx] = r;
ctx.pixels[idx+1] = g;
ctx.pixels[idx+2] = b;
ctx.pixels[idx+3] = a;
}
}
}
};
p5.Renderer2D.prototype.updatePixels = function (x, y, w, h) {
var pd = this._pixelDensity || this._pInst._pixelDensity;
if (x === undefined &&
y === undefined &&
w === undefined &&
h === undefined) {
x = 0;
y = 0;
w = this.width;
h = this.height;
}
w *= pd;
h *= pd;
if (this._pInst) {
this.drawingContext.putImageData(this._pInst.imageData, x, y, 0, 0, w, h);
} else {
this.drawingContext.putImageData(this.imageData, x, y, 0, 0, w, h);
}
};
//////////////////////////////////////////////
// SHAPE | 2D Primitives
//////////////////////////////////////////////
/**
* Generate a cubic Bezier representing an arc on the unit circle of total
* angle `size` radians, beginning `start` radians above the x-axis. Up to
* four of these curves are combined to make a full arc.
*
* See www.joecridge.me/bezier.pdf for an explanation of the method.
*/
p5.Renderer2D.prototype._acuteArcToBezier =
function _acuteArcToBezier(start, size) {
// Evauate constants.
var alpha = size / 2.0,
cos_alpha = Math.cos(alpha),
sin_alpha = Math.sin(alpha),
cot_alpha = 1.0 / Math.tan(alpha),
phi = start + alpha, // This is how far the arc needs to be rotated.
cos_phi = Math.cos(phi),
sin_phi = Math.sin(phi),
lambda = (4.0 - cos_alpha) / 3.0,
mu = sin_alpha + (cos_alpha - lambda) * cot_alpha;
// Return rotated waypoints.
return {
ax: Math.cos(start),
ay: Math.sin(start),
bx: lambda * cos_phi + mu * sin_phi,
by: lambda * sin_phi - mu * cos_phi,
cx: lambda * cos_phi - mu * sin_phi,
cy: lambda * sin_phi + mu * cos_phi,
dx: Math.cos(start + size),
dy: Math.sin(start + size)
};
};
p5.Renderer2D.prototype.arc =
function(x, y, w, h, start, stop, mode) {
var ctx = this.drawingContext;
var vals = canvas.arcModeAdjust(x, y, w, h, this._ellipseMode);
var rx = vals.w / 2.0;
var ry = vals.h / 2.0;
var epsilon = 0.00001; // Smallest visible angle on displays up to 4K.
var arcToDraw = 0;
var curves = [];
// Create curves
while(stop - start > epsilon) {
arcToDraw = Math.min(stop - start, constants.HALF_PI);
curves.push(this._acuteArcToBezier(start, arcToDraw));
start += arcToDraw;
}
// Fill curves
if (this._doFill) {
ctx.beginPath();
curves.forEach(function (curve, index) {
if (index === 0) {
ctx.moveTo(vals.x + curve.ax * rx, vals.y + curve.ay * ry);
}
ctx.bezierCurveTo(vals.x + curve.bx * rx, vals.y + curve.by * ry,
vals.x + curve.cx * rx, vals.y + curve.cy * ry,
vals.x + curve.dx * rx, vals.y + curve.dy * ry);
});
if (mode === constants.PIE || mode == null) {
ctx.lineTo(vals.x, vals.y);
}
ctx.closePath();
ctx.fill();
}
// Stroke curves
if (this._doStroke) {
ctx.beginPath();
curves.forEach(function (curve, index) {
if (index === 0) {
ctx.moveTo(vals.x + curve.ax * rx, vals.y + curve.ay * ry);
}
ctx.bezierCurveTo(vals.x + curve.bx * rx, vals.y + curve.by * ry,
vals.x + curve.cx * rx, vals.y + curve.cy * ry,
vals.x + curve.dx * rx, vals.y + curve.dy * ry);
});
if (mode === constants.PIE) {
ctx.lineTo(vals.x, vals.y);
ctx.closePath();
} else if (mode === constants.CHORD) {
ctx.closePath();
}
ctx.stroke();
}
return this;
};
p5.Renderer2D.prototype.ellipse = function(x, y, w, h) {
var ctx = this.drawingContext;
var doFill = this._doFill, doStroke = this._doStroke;
if (doFill && !doStroke) {
if(ctx.fillStyle === styleEmpty) {
return this;
}
} else if (!doFill && doStroke) {
if(ctx.strokeStyle === styleEmpty) {
return this;
}
}
var vals = canvas.modeAdjust(x, y, w, h, this._ellipseMode);
var kappa = 0.5522847498,
ox = (vals.w / 2) * kappa, // control point offset horizontal
oy = (vals.h / 2) * kappa, // control point offset vertical
xe = vals.x + vals.w, // x-end
ye = vals.y + vals.h, // y-end
xm = vals.x + vals.w / 2, // x-middle
ym = vals.y + vals.h / 2; // y-middle
ctx.beginPath();
ctx.moveTo(vals.x, ym);
ctx.bezierCurveTo(vals.x, ym - oy, xm - ox, vals.y, xm, vals.y);
ctx.bezierCurveTo(xm + ox, vals.y, xe, ym - oy, xe, ym);
ctx.bezierCurveTo(xe, ym + oy, xm + ox, ye, xm, ye);
ctx.bezierCurveTo(xm - ox, ye, vals.x, ym + oy, vals.x, ym);
ctx.closePath();
if (doFill) {
ctx.fill();
}
if (doStroke) {
ctx.stroke();
}
};
p5.Renderer2D.prototype.line = function(x1, y1, x2, y2) {
var ctx = this.drawingContext;
if (!this._doStroke) {
return this;
} else if(ctx.strokeStyle === styleEmpty){
return this;
}
// Translate the line by (0.5, 0.5) to draw it crisp
if (ctx.lineWidth % 2 === 1) {
ctx.translate(0.5, 0.5);
}
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.stroke();
if (ctx.lineWidth % 2 === 1) {
ctx.translate(-0.5, -0.5);
}
return this;
};
p5.Renderer2D.prototype.point = function(x, y) {
var ctx = this.drawingContext;
var s = ctx.strokeStyle;
var f = ctx.fillStyle;
if (!this._doStroke) {
return this;
} else if(ctx.strokeStyle === styleEmpty){
return this;
}
x = Math.round(x);
y = Math.round(y);
ctx.fillStyle = s;
if (ctx.lineWidth > 1) {
ctx.beginPath();
ctx.arc(
x,
y,
ctx.lineWidth / 2,
0,
constants.TWO_PI,
false
);
ctx.fill();
} else {
ctx.fillRect(x, y, 1, 1);
}
ctx.fillStyle = f;
};
p5.Renderer2D.prototype.quad =
function(x1, y1, x2, y2, x3, y3, x4, y4) {
var ctx = this.drawingContext;
var doFill = this._doFill, doStroke = this._doStroke;
if (doFill && !doStroke) {
if(ctx.fillStyle === styleEmpty) {
return this;
}
} else if (!doFill && doStroke) {
if(ctx.strokeStyle === styleEmpty) {
return this;
}
}
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.lineTo(x3, y3);
ctx.lineTo(x4, y4);
ctx.closePath();
if (doFill) {
ctx.fill();
}
if (doStroke) {
ctx.stroke();
}
return this;
};
p5.Renderer2D.prototype.rect = function(x, y, w, h, tl, tr, br, bl) {
var ctx = this.drawingContext;
var doFill = this._doFill, doStroke = this._doStroke;
if (doFill && !doStroke) {
if(ctx.fillStyle === styleEmpty) {
return this;
}
} else if (!doFill && doStroke) {
if(ctx.strokeStyle === styleEmpty) {
return this;
}
}
var vals = canvas.modeAdjust(x, y, w, h, this._rectMode);
// Translate the line by (0.5, 0.5) to draw a crisp rectangle border
if (this._doStroke && ctx.lineWidth % 2 === 1) {
ctx.translate(0.5, 0.5);
}
ctx.beginPath();
if (typeof tl === 'undefined') {
// No rounded corners
ctx.rect(vals.x, vals.y, vals.w, vals.h);
} else {
// At least one rounded corner
// Set defaults when not specified
if (typeof tr === 'undefined') { tr = tl; }
if (typeof br === 'undefined') { br = tr; }
if (typeof bl === 'undefined') { bl = br; }
// Cache and compute several values
var _x = vals.x;
var _y = vals.y;
var _w = vals.w;
var _h = vals.h;
var hw = _w / 2;
var hh = _h / 2;
// Clip radii
if (_w < 2 * tl) { tl = hw; }
if (_h < 2 * tl) { tl = hh; }
if (_w < 2 * tr) { tr = hw; }
if (_h < 2 * tr) { tr = hh; }
if (_w < 2 * br) { br = hw; }
if (_h < 2 * br) { br = hh; }
if (_w < 2 * bl) { bl = hw; }
if (_h < 2 * bl) { bl = hh; }
// Draw shape
ctx.beginPath();
ctx.moveTo(_x + tl, _y);
ctx.arcTo(_x + _w, _y, _x + _w, _y + _h, tr);
ctx.arcTo(_x + _w, _y + _h, _x, _y + _h, br);
ctx.arcTo(_x, _y + _h, _x, _y, bl);
ctx.arcTo(_x, _y, _x + _w, _y, tl);
ctx.closePath();
}
if (this._doFill) {
ctx.fill();
}
if (this._doStroke) {
ctx.stroke();
}
if (this._doStroke && ctx.lineWidth % 2 === 1) {
ctx.translate(-0.5, -0.5);
}
return this;
};
p5.Renderer2D.prototype.triangle = function(x1, y1, x2, y2, x3, y3) {
var ctx = this.drawingContext;
var doFill = this._doFill, doStroke = this._doStroke;
if (doFill && !doStroke) {
if(ctx.fillStyle === styleEmpty) {
return this;
}
} else if (!doFill && doStroke) {
if(ctx.strokeStyle === styleEmpty) {
return this;
}
}
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.lineTo(x3, y3);
ctx.closePath();
if (doFill) {
ctx.fill();
}
if (doStroke) {
ctx.stroke();
}
};
p5.Renderer2D.prototype.endShape =
function (mode, vertices, isCurve, isBezier,
isQuadratic, isContour, shapeKind) {
if (vertices.length === 0) {
return this;
}
if (!this._doStroke && !this._doFill) {
return this;
}
var closeShape = mode === constants.CLOSE;
var v;
if (closeShape && !isContour) {
vertices.push(vertices[0]);
}
var i, j;
var numVerts = vertices.length;
if (isCurve && (shapeKind === constants.POLYGON || shapeKind === null)) {
if (numVerts > 3) {
var b = [], s = 1 - this._curveTightness;
this.drawingContext.beginPath();
this.drawingContext.moveTo(vertices[1][0], vertices[1][1]);
for (i = 1; i + 2 < numVerts; i++) {
v = vertices[i];
b[0] = [
v[0],
v[1]
];
b[1] = [
v[0] + (s * vertices[i + 1][0] - s * vertices[i - 1][0]) / 6,
v[1] + (s * vertices[i + 1][1] - s * vertices[i - 1][1]) / 6
];
b[2] = [
vertices[i + 1][0] +
(s * vertices[i][0]-s * vertices[i + 2][0]) / 6,
vertices[i + 1][1]+(s * vertices[i][1] - s*vertices[i + 2][1]) / 6
];
b[3] = [
vertices[i + 1][0],
vertices[i + 1][1]
];
this.drawingContext.bezierCurveTo(b[1][0],b[1][1],
b[2][0],b[2][1],b[3][0],b[3][1]);
}
if (closeShape) {
this.drawingContext.lineTo(vertices[i + 1][0], vertices[i + 1][1]);
}
this._doFillStrokeClose();
}
} else if (isBezier&&(shapeKind===constants.POLYGON ||shapeKind === null)) {
this.drawingContext.beginPath();
for (i = 0; i < numVerts; i++) {
if (vertices[i].isVert) {
if (vertices[i].moveTo) {
this.drawingContext.moveTo(vertices[i][0], vertices[i][1]);
} else {
this.drawingContext.lineTo(vertices[i][0], vertices[i][1]);
}
} else {
this.drawingContext.bezierCurveTo(vertices[i][0], vertices[i][1],
vertices[i][2], vertices[i][3], vertices[i][4], vertices[i][5]);
}
}
this._doFillStrokeClose();
} else if (isQuadratic &&
(shapeKind === constants.POLYGON || shapeKind === null)) {
this.drawingContext.beginPath();
for (i = 0; i < numVerts; i++) {
if (vertices[i].isVert) {
if (vertices[i].moveTo) {
this.drawingContext.moveTo([0], vertices[i][1]);
} else {
this.drawingContext.lineTo(vertices[i][0], vertices[i][1]);
}
} else {
this.drawingContext.quadraticCurveTo(vertices[i][0], vertices[i][1],
vertices[i][2], vertices[i][3]);
}
}
this._doFillStrokeClose();
} else {
if (shapeKind === constants.POINTS) {
for (i = 0; i < numVerts; i++) {
v = vertices[i];
if (this._doStroke) {
this._pInst.stroke(v[6]);
}
this._pInst.point(v[0], v[1]);
}
} else if (shapeKind === constants.LINES) {
for (i = 0; i + 1 < numVerts; i += 2) {
v = vertices[i];
if (this._doStroke) {
this._pInst.stroke(vertices[i + 1][6]);
}
this._pInst.line(v[0], v[1], vertices[i + 1][0], vertices[i + 1][1]);
}
} else if (shapeKind === constants.TRIANGLES) {
for (i = 0; i + 2 < numVerts; i += 3) {
v = vertices[i];
this.drawingContext.beginPath();
this.drawingContext.moveTo(v[0], v[1]);
this.drawingContext.lineTo(vertices[i + 1][0], vertices[i + 1][1]);
this.drawingContext.lineTo(vertices[i + 2][0], vertices[i + 2][1]);
this.drawingContext.lineTo(v[0], v[1]);
if (this._doFill) {
this._pInst.fill(vertices[i + 2][5]);
this.drawingContext.fill();
}
if (this._doStroke) {
this._pInst.stroke(vertices[i + 2][6]);
this.drawingContext.stroke();
}
this.drawingContext.closePath();
}
} else if (shapeKind === constants.TRIANGLE_STRIP) {
for (i = 0; i + 1 < numVerts; i++) {
v = vertices[i];
this.drawingContext.beginPath();
this.drawingContext.moveTo(vertices[i + 1][0], vertices[i + 1][1]);
this.drawingContext.lineTo(v[0], v[1]);
if (this._doStroke) {
this._pInst.stroke(vertices[i + 1][6]);
}
if (this._doFill) {
this._pInst.fill(vertices[i + 1][5]);
}
if (i + 2 < numVerts) {
this.drawingContext.lineTo(vertices[i + 2][0], vertices[i + 2][1]);
if (this._doStroke) {
this._pInst.stroke(vertices[i + 2][6]);
}
if (this._doFill) {
this._pInst.fill(vertices[i + 2][5]);
}
}
this._doFillStrokeClose();
}
} else if (shapeKind === constants.TRIANGLE_FAN) {
if (numVerts > 2) {
this.drawingContext.beginPath();
this.drawingContext.moveTo(vertices[0][0], vertices[0][1]);
this.drawingContext.lineTo(vertices[1][0], vertices[1][1]);
this.drawingContext.lineTo(vertices[2][0], vertices[2][1]);
if (this._doFill) {
this._pInst.fill(vertices[2][5]);
}
if (this._doStroke) {
this._pInst.stroke(vertices[2][6]);
}
this._doFillStrokeClose();
for (i = 3; i < numVerts; i++) {
v = vertices[i];
this.drawingContext.beginPath();
this.drawingContext.moveTo(vertices[0][0], vertices[0][1]);
this.drawingContext.lineTo(vertices[i - 1][0], vertices[i - 1][1]);
this.drawingContext.lineTo(v[0], v[1]);
if (this._doFill) {
this._pInst.fill(v[5]);
}
if (this._doStroke) {
this._pInst.stroke(v[6]);
}
this._doFillStrokeClose();
}
}
} else if (shapeKind === constants.QUADS) {
for (i = 0; i + 3 < numVerts; i += 4) {
v = vertices[i];
this.drawingContext.beginPath();
this.drawingContext.moveTo(v[0], v[1]);
for (j = 1; j < 4; j++) {
this.drawingContext.lineTo(vertices[i + j][0], vertices[i + j][1]);
}
this.drawingContext.lineTo(v[0], v[1]);
if (this._doFill) {
this._pInst.fill(vertices[i + 3][5]);
}
if (this._doStroke) {
this._pInst.stroke(vertices[i + 3][6]);
}
this._doFillStrokeClose();
}
} else if (shapeKind === constants.QUAD_STRIP) {
if (numVerts > 3) {
for (i = 0; i + 1 < numVerts; i += 2) {
v = vertices[i];
this.drawingContext.beginPath();
if (i + 3 < numVerts) {
this.drawingContext.moveTo(vertices[i + 2][0], vertices[i+2][1]);
this.drawingContext.lineTo(v[0], v[1]);
this.drawingContext.lineTo(vertices[i + 1][0], vertices[i+1][1]);
this.drawingContext.lineTo(vertices[i + 3][0], vertices[i+3][1]);
if (this._doFill) {
this._pInst.fill(vertices[i + 3][5]);
}
if (this._doStroke) {
this._pInst.stroke(vertices[i + 3][6]);
}
} else {
this.drawingContext.moveTo(v[0], v[1]);
this.drawingContext.lineTo(vertices[i + 1][0], vertices[i+1][1]);
}
this._doFillStrokeClose();
}
}
} else {
this.drawingContext.beginPath();
this.drawingContext.moveTo(vertices[0][0], vertices[0][1]);
for (i = 1; i < numVerts; i++) {
v = vertices[i];
if (v.isVert) {
if (v.moveTo) {
this.drawingContext.moveTo(v[0], v[1]);
} else {
this.drawingContext.lineTo(v[0], v[1]);
}
}
}
this._doFillStrokeClose();
}
}
isCurve = false;
isBezier = false;
isQuadratic = false;
isContour = false;
if (closeShape) {
vertices.pop();
}
return this;
};
//////////////////////////////////////////////
// SHAPE | Attributes
//////////////////////////////////////////////
p5.Renderer2D.prototype.noSmooth = function() {
if ('imageSmoothingEnabled' in this.drawingContext) {
this.drawingContext.imageSmoothingEnabled = false;
}
else if ('mozImageSmoothingEnabled' in this.drawingContext) {
this.drawingContext.mozImageSmoothingEnabled = false;
}
else if ('webkitImageSmoothingEnabled' in this.drawingContext) {
this.drawingContext.webkitImageSmoothingEnabled = false;
}
else if ('msImageSmoothingEnabled' in this.drawingContext) {
this.drawingContext.msImageSmoothingEnabled = false;
}
return this;
};
p5.Renderer2D.prototype.smooth = function() {
if ('imageSmoothingEnabled' in this.drawingContext) {
this.drawingContext.imageSmoothingEnabled = true;
}
else if ('mozImageSmoothingEnabled' in this.drawingContext) {
this.drawingContext.mozImageSmoothingEnabled = true;
}
else if ('webkitImageSmoothingEnabled' in this.drawingContext) {
this.drawingContext.webkitImageSmoothingEnabled = true;
}
else if ('msImageSmoothingEnabled' in this.drawingContext) {
this.drawingContext.msImageSmoothingEnabled = true;
}
return this;
};
p5.Renderer2D.prototype.strokeCap = function(cap) {
if (cap === constants.ROUND ||
cap === constants.SQUARE ||
cap === constants.PROJECT) {
this.drawingContext.lineCap = cap;
}
return this;
};
p5.Renderer2D.prototype.strokeJoin = function(join) {
if (join === constants.ROUND ||
join === constants.BEVEL ||
join === constants.MITER) {
this.drawingContext.lineJoin = join;
}
return this;
};
p5.Renderer2D.prototype.strokeWeight = function(w) {
if (typeof w === 'undefined' || w === 0) {
// hack because lineWidth 0 doesn't work
this.drawingContext.lineWidth = 0.0001;
} else {
this.drawingContext.lineWidth = w;
}
return this;
};
p5.Renderer2D.prototype._getFill = function(){
return this.drawingContext.fillStyle;
};
p5.Renderer2D.prototype._getStroke = function(){
return this.drawingContext.strokeStyle;
};
//////////////////////////////////////////////
// SHAPE | Curves
//////////////////////////////////////////////
p5.Renderer2D.prototype.bezier = function (x1, y1, x2, y2, x3, y3, x4, y4) {
this._pInst.beginShape();
this._pInst.vertex(x1, y1);
this._pInst.bezierVertex(x2, y2, x3, y3, x4, y4);
this._pInst.endShape();
return this;
};
p5.Renderer2D.prototype.curve = function (x1, y1, x2, y2, x3, y3, x4, y4) {
this._pInst.beginShape();
this._pInst.curveVertex(x1, y1);
this._pInst.curveVertex(x2, y2);
this._pInst.curveVertex(x3, y3);
this._pInst.curveVertex(x4, y4);
this._pInst.endShape();
return this;
};
//////////////////////////////////////////////
// SHAPE | Vertex
//////////////////////////////////////////////
p5.Renderer2D.prototype._doFillStrokeClose = function () {
if (this._doFill) {
this.drawingContext.fill();
}
if (this._doStroke) {
this.drawingContext.stroke();
}
this.drawingContext.closePath();
};
//////////////////////////////////////////////
// TRANSFORM
//////////////////////////////////////////////
p5.Renderer2D.prototype.applyMatrix =
function(n00, n01, n02, n10, n11, n12) {
this.drawingContext.transform(n00, n01, n02, n10, n11, n12);
};
p5.Renderer2D.prototype.resetMatrix = function() {
this.drawingContext.setTransform(1, 0, 0, 1, 0, 0);
this.drawingContext.scale(this._pInst._pixelDensity,
this._pInst._pixelDensity);
return this;
};
p5.Renderer2D.prototype.rotate = function(r) {
this.drawingContext.rotate(r);
};
p5.Renderer2D.prototype.scale = function(x,y) {
this.drawingContext.scale(x, y);
return this;
};
p5.Renderer2D.prototype.shearX = function(angle) {
if (this._pInst._angleMode === constants.DEGREES) {
angle = this._pInst.radians(angle);
}
this.drawingContext.transform(1, 0, this._pInst.tan(angle), 1, 0, 0);
return this;
};
p5.Renderer2D.prototype.shearY = function(angle) {
if (this._pInst._angleMode === constants.DEGREES) {
angle = this._pInst.radians(angle);
}
this.drawingContext.transform(1, this._pInst.tan(angle), 0, 1, 0, 0);
return this;
};
p5.Renderer2D.prototype.translate = function(x, y) {
this.drawingContext.translate(x, y);
return this;
};
//////////////////////////////////////////////
// TYPOGRAPHY
//
//////////////////////////////////////////////
p5.Renderer2D.prototype.text = function (str, x, y, maxWidth, maxHeight) {
var p = this._pInst, cars, n, ii, jj, line, testLine,
testWidth, words, totalHeight, baselineHacked,
finalMaxHeight = Number.MAX_VALUE;
// baselineHacked: (HACK)
// A temporary fix to conform to Processing's implementation
// of BASELINE vertical alignment in a bounding box
if (!(this._doFill || this._doStroke)) {
return;
}
if (typeof str !== 'string') {
str = str.toString();
}
str = str.replace(/(\t)/g, ' ');
cars = str.split('\n');
if (typeof maxWidth !== 'undefined') {
totalHeight = 0;
for (ii = 0; ii < cars.length; ii++) {
line = '';
words = cars[ii].split(' ');
for (n = 0; n < words.length; n++) {
testLine = line + words[n] + ' ';
testWidth = this.textWidth(testLine);
if (testWidth > maxWidth) {
line = words[n] + ' ';
totalHeight += p.textLeading();
} else {
line = testLine;
}
}
}
if (this._rectMode === constants.CENTER) {
x -= maxWidth / 2;
y -= maxHeight / 2;
}
switch (this.drawingContext.textAlign) {
case constants.CENTER:
x += maxWidth / 2;
break;
case constants.RIGHT:
x += maxWidth;
break;
}
if (typeof maxHeight !== 'undefined') {
switch (this.drawingContext.textBaseline) {
case constants.BOTTOM:
y += (maxHeight - totalHeight);
break;
case constants._CTX_MIDDLE:
y += (maxHeight - totalHeight) / 2;
break;
case constants.BASELINE:
baselineHacked = true;
this.drawingContext.textBaseline = constants.TOP;
break;
}
// remember the max-allowed y-position for any line (fix to #928)
finalMaxHeight = (y + maxHeight) - p.textAscent();
}
for (ii = 0; ii < cars.length; ii++) {
line = '';
words = cars[ii].split(' ');
for (n = 0; n < words.length; n++) {
testLine = line + words[n] + ' ';
testWidth = this.textWidth(testLine);
if (testWidth > maxWidth && line.length > 0) {
this._renderText(p, line, x, y, finalMaxHeight);
line = words[n] + ' ';
y += p.textLeading();
} else {
line = testLine;
}
}
this._renderText(p, line, x, y, finalMaxHeight);
y += p.textLeading();
}
}
else {
for (jj = 0; jj < cars.length; jj++) {
this._renderText(p, cars[jj], x, y, finalMaxHeight);
y += p.textLeading();
}
}
if (baselineHacked) {
this.drawingContext.textBaseline = constants.BASELINE;
}
return p;
};
p5.Renderer2D.prototype._renderText = function(p, line, x, y, maxY) {
if (y >= maxY) {
return; // don't render lines beyond our maxY position
}
p.push(); // fix to #803
if (!this._isOpenType()) { // a system/browser font
// no stroke unless specified by user
if (this._doStroke && this._strokeSet) {
this.drawingContext.strokeText(line, x, y);
}
if (this._doFill) {
// if fill hasn't been set by user, use default text fill
this.drawingContext.fillStyle = this._fillSet ?
this.drawingContext.fillStyle : constants._DEFAULT_TEXT_FILL;
this.drawingContext.fillText(line, x, y);
}
}
else { // an opentype font, let it handle the rendering
this._textFont._renderPath(line, x, y);
}
p.pop();
return p;
};
p5.Renderer2D.prototype.textWidth = function(s) {
if (this._isOpenType()) {
return this._textFont._textWidth(s);
}
return this.drawingContext.measureText(s).width;
};
p5.Renderer2D.prototype.textAlign = function(h, v) {
if (arguments.length) {
if (h === constants.LEFT ||
h === constants.RIGHT ||
h === constants.CENTER) {
this.drawingContext.textAlign = h;
}
if (v === constants.TOP ||
v === constants.BOTTOM ||
v === constants.CENTER ||
v === constants.BASELINE) {
if (v === constants.CENTER) {
this.drawingContext.textBaseline = constants._CTX_MIDDLE;
} else {
this.drawingContext.textBaseline = v;
}
}
return this._pInst;
} else {
var valign = this.drawingContext.textBaseline;
if (valign === constants._CTX_MIDDLE) {
valign = constants.CENTER;
}
return {
horizontal: this.drawingContext.textAlign,
vertical: valign
};
}
};
p5.Renderer2D.prototype._applyTextProperties = function() {
var font, p = this._pInst;
this._setProperty('_textAscent', null);
this._setProperty('_textDescent', null);
font = this._textFont;
if (this._isOpenType()) {
font = this._textFont.font.familyName;
this._setProperty('_textStyle', this._textFont.font.styleName);
}
this.drawingContext.font = this._textStyle + ' ' +
this._textSize + 'px ' + font;
return p;
};
//////////////////////////////////////////////
// STRUCTURE
//////////////////////////////////////////////
p5.Renderer2D.prototype.push = function() {
this.drawingContext.save();
};
p5.Renderer2D.prototype.pop = function() {
this.drawingContext.restore();
};
module.exports = p5.Renderer2D;
},{"../image/filters":65,"./canvas":46,"./constants":47,"./core":48,"./p5.Renderer":54}],56:[function(_dereq_,module,exports){
/**
* @module Rendering
* @submodule Rendering
* @for p5
*/
var p5 = _dereq_('./core');
var constants = _dereq_('./constants');
_dereq_('./p5.Graphics');
_dereq_('./p5.Renderer2D');
_dereq_('../3d/p5.Renderer3D');
var defaultId = 'defaultCanvas0'; // this gets set again in createCanvas
/**
* Creates a canvas element in the document, and sets the dimensions of it
* in pixels. This method should be called only once at the start of setup.
* Calling createCanvas more than once in a sketch will result in very
* unpredicable behavior. If you want more than one drawing canvas
* you could use createGraphics (hidden by default but it can be shown).
* The system variables width and height are set by the parameters passed
* to this function. If createCanvas() is not used, the window will be
* given a default size of 100x100 pixels.
*
* @method createCanvas
* @param {Number} w width of the canvas
* @param {Number} h height of the canvas
* @param optional:{String} renderer 'p2d' | 'webgl'
* @return {Object} canvas generated
* @example
*
*
* function setup() {
* createCanvas(100, 50);
* background(153);
* line(0, 0, width, height);
* }
*
*
*/
p5.prototype.createCanvas = function(w, h, renderer) {
//optional: renderer, otherwise defaults to p2d
var r = renderer || constants.P2D;
var isDefault, c;
//4th arg (isDefault) used when called onLoad,
//otherwise hidden to the public api
if(arguments[3]){
isDefault =
(typeof arguments[3] === 'boolean') ? arguments[3] : false;
}
if(r === constants.WEBGL){
c = document.getElementById(defaultId);
if(c){ //if defaultCanvas already exists
c.parentNode.removeChild(c); //replace the existing defaultCanvas
}
c = document.createElement('canvas');
c.id = defaultId;
}
else {
if (isDefault) {
c = document.createElement('canvas');
var i = 0;
while (document.getElementById('defaultCanvas'+i)) {
i++;
}
defaultId = 'defaultCanvas'+i;
c.id = defaultId;
} else { // resize the default canvas if new one is created
c = this.canvas;
}
}
// set to invisible if still in setup (to prevent flashing with manipulate)
if (!this._setupDone) {
c.className += ' p5_hidden'; // tag to show later
c.style.visibility='hidden';
}
if (this._userNode) { // user input node case
this._userNode.appendChild(c);
} else {
document.body.appendChild(c);
}
// Init our graphics renderer
//webgl mode
if (r === constants.WEBGL) {
this._setProperty('_renderer', new p5.Renderer3D(c, this, true));
this._isdefaultGraphics = true;
}
//P2D mode
else {
if (!this._isdefaultGraphics) {
this._setProperty('_renderer', new p5.Renderer2D(c, this, true));
this._isdefaultGraphics = true;
}
}
this._renderer.resize(w, h);
this._renderer._applyDefaults();
if (isDefault) { // only push once
this._elements.push(this._renderer);
}
return this._renderer;
};
/**
* Resizes the canvas to given width and height. Note that the
* canvas will be cleared so anything drawn previously in setup
* or draw will disappear on resize. Setup will not be called
* again.
* @method resizeCanvas
* @example
*
* function setup() {
* createCanvas(windowWidth, windowHeight);
* }
*
* function draw() {
* background(0, 100, 200);
* }
*
* function windowResized() {
* resizeCanvas(windowWidth, windowHeight);
* }
*
*/
p5.prototype.resizeCanvas = function (w, h, noRedraw) {
if (this._renderer) {
this._renderer.resize(w, h);
this._renderer._applyDefaults();
if (!noRedraw) {
this.redraw();
}
}
};
/**
* Removes the default canvas for a p5 sketch that doesn't
* require a canvas
* @method noCanvas
* @example
*
*
* function setup() {
* noCanvas();
* }
*
*
*/
p5.prototype.noCanvas = function() {
if (this.canvas) {
this.canvas.parentNode.removeChild(this.canvas);
}
};
/**
* Creates and returns a new p5.Renderer object. Use this class if you need
* to draw into an off-screen graphics buffer. The two parameters define the
* width and height in pixels.
*
* @method createGraphics
* @param {Number} w width of the offscreen graphics buffer
* @param {Number} h height of the offscreen graphics buffer
* @param {String} renderer either 'p2d' or 'webgl'.
* undefined defaults to p2d
* @return {Object} offscreen graphics buffer
* @example
*
*
* var pg;
* function setup() {
* createCanvas(100, 100);
* pg = createGraphics(100, 100);
* }
* function draw() {
* background(200);
* pg.background(100);
* pg.noStroke();
* pg.ellipse(pg.width/2, pg.height/2, 50, 50);
* image(pg, 50, 50);
* image(pg, 0, 0, 50, 50);
* }
*
*
*/
p5.prototype.createGraphics = function(w, h, renderer){
return new p5.Graphics(w, h, renderer, this);
};
/**
* Blends the pixels in the display window according to the defined mode.
* There is a choice of the following modes to blend the source pixels (A)
* with the ones of pixels already in the display window (B):
*
* BLEND - linear interpolation of colours: C =
* A*factor + B. This is the default blending mode.
* ADD - sum of A and B
* DARKEST - only the darkest colour succeeds: C =
* min(A*factor, B).
* LIGHTEST - only the lightest colour succeeds: C =
* max(A*factor, B).
* DIFFERENCE - subtract colors from underlying image.
* EXCLUSION - similar to DIFFERENCE, but less
* extreme.
* MULTIPLY - multiply the colors, result will always be
* darker.
* SCREEN - opposite multiply, uses inverse values of the
* colors.
* REPLACE - the pixels entirely replace the others and
* don't utilize alpha (transparency) values.
* OVERLAY - mix of MULTIPLY and SCREEN
* . Multiplies dark values, and screens light values.
* HARD_LIGHT - SCREEN when greater than 50%
* gray, MULTIPLY when lower.
* SOFT_LIGHT - mix of DARKEST and
* LIGHTEST. Works like OVERLAY, but not as harsh.
*
* DODGE - lightens light tones and increases contrast,
* ignores darks.
* BURN - darker areas are applied, increasing contrast,
* ignores lights.
*
*
* @method blendMode
* @param {String/Constant} mode blend mode to set for canvas
* @example
*
*
* blendMode(LIGHTEST);
* strokeWeight(30);
* stroke(80, 150, 255);
* line(25, 25, 75, 75);
* stroke(255, 50, 50);
* line(75, 25, 25, 75);
*
*
*
*
* blendMode(MULTIPLY);
* strokeWeight(30);
* stroke(80, 150, 255);
* line(25, 25, 75, 75);
* stroke(255, 50, 50);
* line(75, 25, 25, 75);
*
*
*/
p5.prototype.blendMode = function(mode) {
if (mode === constants.BLEND || mode === constants.DARKEST ||
mode === constants.LIGHTEST || mode === constants.DIFFERENCE ||
mode === constants.MULTIPLY || mode === constants.EXCLUSION ||
mode === constants.SCREEN || mode === constants.REPLACE ||
mode === constants.OVERLAY || mode === constants.HARD_LIGHT ||
mode === constants.SOFT_LIGHT || mode === constants.DODGE ||
mode === constants.BURN || mode === constants.ADD ||
mode === constants.NORMAL) {
this._renderer.blendMode(mode);
} else {
throw new Error('Mode '+mode+' not recognized.');
}
};
module.exports = p5;
},{"../3d/p5.Renderer3D":36,"./constants":47,"./core":48,"./p5.Graphics":53,"./p5.Renderer2D":55}],57:[function(_dereq_,module,exports){
// requestAnim shim layer by Paul Irish
window.requestAnimationFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(callback, element){
// should '60' here be framerate?
window.setTimeout(callback, 1000 / 60);
};
})();
// use window.performance() to get max fast and accurate time in milliseconds
window.performance = window.performance || {};
window.performance.now = (function(){
var load_date = Date.now();
return window.performance.now ||
window.performance.mozNow ||
window.performance.msNow ||
window.performance.oNow ||
window.performance.webkitNow ||
function () {
return Date.now() - load_date;
};
})();
/*
// 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
(function() {
var lastTime = 0;
var vendors = ['ms', 'moz', 'webkit', 'o'];
for (var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame =
window[vendors[x]+'RequestAnimationFrame'];
window.cancelAnimationFrame =
window[vendors[x]+'CancelAnimationFrame'] ||
window[vendors[x]+'CancelRequestAnimationFrame'];
}
if (!window.requestAnimationFrame) {
window.requestAnimationFrame = function(callback, element) {
var currTime = new Date().getTime();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = window.setTimeout(function()
{ callback(currTime + timeToCall); }, timeToCall);
lastTime = currTime + timeToCall;
return id;
};
}
if (!window.cancelAnimationFrame) {
window.cancelAnimationFrame = function(id) {
clearTimeout(id);
};
}
}());
*/
/**
* shim for Uint8ClampedArray.slice
* (allows arrayCopy to work with pixels[])
* with thanks to http://halfpapstudios.com/blog/tag/html5-canvas/
*/
(function () {
'use strict';
if (typeof Uint8ClampedArray !== 'undefined') {
//Firefox and Chrome
Uint8ClampedArray.prototype.slice = Array.prototype.slice;
}
}());
},{}],58:[function(_dereq_,module,exports){
/**
* @module Structure
* @submodule Structure
* @for p5
* @requires core
*/
'use strict';
var p5 = _dereq_('./core');
p5.prototype.exit = function() {
throw 'exit() not implemented, see remove()';
};
/**
* Stops p5.js from continuously executing the code within draw().
* If loop() is called, the code in draw() begins to run continuously again.
* If using noLoop() in setup(), it should be the last line inside the block.
*
*
* When noLoop() is used, it's not possible to manipulate or access the
* screen inside event handling functions such as mousePressed() or
* keyPressed(). Instead, use those functions to call redraw() or loop(),
* which will run draw(), which can update the screen properly. This means
* that when noLoop() has been called, no drawing can happen, and functions
* like saveFrame() or loadPixels() may not be used.
*
* Note that if the sketch is resized, redraw() will be called to update
* the sketch, even after noLoop() has been specified. Otherwise, the sketch
* would enter an odd state until loop() was called.
*
* @method noLoop
* @example
*
* function setup() {
* createCanvas(100, 100);
* background(200);
* noLoop();
* }
* function draw() {
* line(10, 10, 90, 90);
* }
*
*
*
* var x = 0;
* function setup() {
* createCanvas(100, 100);
* }
*
* function draw() {
* background(204);
* x = x + 0.1;
* if (x > width) {
* x = 0;
* }
* line(x, 0, x, height);
* }
*
* function mousePressed() {
* noLoop();
* }
*
* function mouseReleased() {
* loop();
* }
*
*/
p5.prototype.noLoop = function() {
this._loop = false;
};
/**
* By default, p5.js loops through draw() continuously, executing the code
* within it. However, the draw() loop may be stopped by calling noLoop().
* In that case, the draw() loop can be resumed with loop().
*
* @method loop
* @example
*
* var x = 0;
* function setup() {
* createCanvas(100, 100);
* noLoop();
* }
*
* function draw() {
* background(204);
* x = x + 0.1;
* if (x > width) {
* x = 0;
* }
* line(x, 0, x, height);
* }
*
* function mousePressed() {
* loop();
* }
*
* function mouseReleased() {
* noLoop();
* }
*
*/
p5.prototype.loop = function() {
this._loop = true;
this._draw();
};
/**
* The push() function saves the current drawing style settings and
* transformations, while pop() restores these settings. Note that these
* functions are always used together. They allow you to change the style
* and transformation settings and later return to what you had. When a new
* state is started with push(), it builds on the current style and transform
* information. The push() and pop() functions can be embedded to provide
* more control. (See the second example for a demonstration.)
*
* push() stores information related to the current transformation state
* and style settings controlled by the following functions: fill(),
* stroke(), tint(), strokeWeight(), strokeCap(), strokeJoin(),
* imageMode(), rectMode(), ellipseMode(), colorMode(), textAlign(),
* textFont(), textMode(), textSize(), textLeading().
*
* @method push
* @example
*
*
* ellipse(0, 50, 33, 33); // Left circle
*
* push(); // Start a new drawing state
* strokeWeight(10);
* fill(204, 153, 0);
* translate(50, 0);
* ellipse(0, 50, 33, 33); // Middle circle
* pop(); // Restore original state
*
* ellipse(100, 50, 33, 33); // Right circle
*
*
*
*
* ellipse(0, 50, 33, 33); // Left circle
*
* push(); // Start a new drawing state
* strokeWeight(10);
* fill(204, 153, 0);
* ellipse(33, 50, 33, 33); // Left-middle circle
*
* push(); // Start another new drawing state
* stroke(0, 102, 153);
* ellipse(66, 50, 33, 33); // Right-middle circle
* pop(); // Restore previous state
*
* pop(); // Restore original state
*
* ellipse(100, 50, 33, 33); // Right circle
*
*
*/
p5.prototype.push = function () {
this._renderer.push();
this._styles.push({
_doStroke: this._renderer._doStroke,
_doFill: this._renderer._doFill,
_tint: this._renderer._tint,
_imageMode: this._renderer._imageMode,
_rectMode: this._renderer._rectMode,
_ellipseMode: this._renderer._ellipseMode,
_colorMode: this._renderer._colorMode,
_textFont: this._renderer._textFont,
_textLeading: this._renderer._textLeading,
_textSize: this._renderer._textSize,
_textStyle: this._renderer._textStyle
});
};
/**
* The push() function saves the current drawing style settings and
* transformations, while pop() restores these settings. Note that these
* functions are always used together. They allow you to change the style
* and transformation settings and later return to what you had. When a new
* state is started with push(), it builds on the current style and transform
* information. The push() and pop() functions can be embedded to provide
* more control. (See the second example for a demonstration.)
*
* push() stores information related to the current transformation state
* and style settings controlled by the following functions: fill(),
* stroke(), tint(), strokeWeight(), strokeCap(), strokeJoin(),
* imageMode(), rectMode(), ellipseMode(), colorMode(), textAlign(),
* textFont(), textMode(), textSize(), textLeading().
*
* @method pop
* @example
*
*
* ellipse(0, 50, 33, 33); // Left circle
*
* push(); // Start a new drawing state
* translate(50, 0);
* strokeWeight(10);
* fill(204, 153, 0);
* ellipse(0, 50, 33, 33); // Middle circle
* pop(); // Restore original state
*
* ellipse(100, 50, 33, 33); // Right circle
*
*
*
*
* ellipse(0, 50, 33, 33); // Left circle
*
* push(); // Start a new drawing state
* strokeWeight(10);
* fill(204, 153, 0);
* ellipse(33, 50, 33, 33); // Left-middle circle
*
* push(); // Start another new drawing state
* stroke(0, 102, 153);
* ellipse(66, 50, 33, 33); // Right-middle circle
* pop(); // Restore previous state
*
* pop(); // Restore original state
*
* ellipse(100, 50, 33, 33); // Right circle
*
*
*/
p5.prototype.pop = function () {
this._renderer.pop();
var lastS = this._styles.pop();
for(var prop in lastS){
this._renderer[prop] = lastS[prop];
}
};
p5.prototype.pushStyle = function() {
throw new Error('pushStyle() not used, see push()');
};
p5.prototype.popStyle = function() {
throw new Error('popStyle() not used, see pop()');
};
/**
*
* Executes the code within draw() one time. This functions allows the
* program to update the display window only when necessary, for example
* when an event registered by mousePressed() or keyPressed() occurs.
*
* In structuring a program, it only makes sense to call redraw() within
* events such as mousePressed(). This is because redraw() does not run
* draw() immediately (it only sets a flag that indicates an update is
* needed).
*
* The redraw() function does not work properly when called inside draw().
* To enable/disable animations, use loop() and noLoop().
*
* @method redraw
* @example
*
* var x = 0;
*
* function setup() {
* createCanvas(100, 100);
* noLoop();
* }
*
* function draw() {
* background(204);
* line(x, 0, x, height);
* }
*
* function mousePressed() {
* x += 1;
* redraw();
* }
*
*/
p5.prototype.redraw = function () {
var userSetup = this.setup || window.setup;
var userDraw = this.draw || window.draw;
if (typeof userDraw === 'function') {
this.push();
if (typeof userSetup === 'undefined') {
this.scale(this._pixelDensity, this._pixelDensity);
}
var self = this;
this._registeredMethods.pre.forEach(function (f) {
f.call(self);
});
this.pop();
userDraw();
this._registeredMethods.post.forEach(function (f) {
f.call(self);
});
}
};
p5.prototype.size = function() {
var s = 'size() is not a valid p5 function, to set the size of the ';
s += 'drawing canvas, please use createCanvas() instead';
throw s;
};
module.exports = p5;
},{"./core":48}],59:[function(_dereq_,module,exports){
/**
* @module Transform
* @submodule Transform
* @for p5
* @requires core
* @requires constants
*/
'use strict';
var p5 = _dereq_('./core');
var constants = _dereq_('./constants');
/**
* Multiplies the current matrix by the one specified through the parameters.
* This is very slow because it will try to calculate the inverse of the
* transform, so avoid it whenever possible.
*
* @method applyMatrix
* @param {Number} n00 numbers which define the 3x2 matrix to be multiplied
* @param {Number} n01 numbers which define the 3x2 matrix to be multiplied
* @param {Number} n02 numbers which define the 3x2 matrix to be multiplied
* @param {Number} n10 numbers which define the 3x2 matrix to be multiplied
* @param {Number} n11 numbers which define the 3x2 matrix to be multiplied
* @param {Number} n12 numbers which define the 3x2 matrix to be multiplied
* @return {p5} the p5 object
* @example
*
*
* // Example in the works.
*
*
*/
p5.prototype.applyMatrix = function(n00, n01, n02, n10, n11, n12) {
this._renderer.applyMatrix(n00, n01, n02, n10, n11, n12);
return this;
};
p5.prototype.popMatrix = function() {
throw new Error('popMatrix() not used, see pop()');
};
p5.prototype.printMatrix = function() {
throw new Error('printMatrix() not implemented');
};
p5.prototype.pushMatrix = function() {
throw new Error('pushMatrix() not used, see push()');
};
/**
* Replaces the current matrix with the identity matrix.
*
* @method resetMatrix
* @return {p5} the p5 object
* @example
*
*
* // Example in the works.
*
*
*/
p5.prototype.resetMatrix = function() {
this._renderer.resetMatrix();
return this;
};
/**
* Rotates a shape the amount specified by the angle parameter. This
* function accounts for angleMode, so angles can be entered in either
* RADIANS or DEGREES.
*
* Objects are always rotated around their relative position to the
* origin and positive numbers rotate objects in a clockwise direction.
* Transformations apply to everything that happens after and subsequent
* calls to the function accumulates the effect. For example, calling
* rotate(HALF_PI) and then rotate(HALF_PI) is the same as rotate(PI).
* All tranformations are reset when draw() begins again.
*
* Technically, rotate() multiplies the current transformation matrix
* by a rotation matrix. This function can be further controlled by
* the push() and pop().
*
* @method rotate
* @param {Number} angle the angle of rotation, specified in radians
* or degrees, depending on current angleMode
* @return {p5} the p5 object
* @example
*
*
* translate(width/2, height/2);
* rotate(PI/3.0);
* rect(-26, -26, 52, 52);
*
*
*/
p5.prototype.rotate = function() {
var r = arguments[0];
if (this._angleMode === constants.DEGREES) {
r = this.radians(r);
}
//in webgl mode
if(arguments.length > 1){
this._renderer.rotate(r, arguments[1]);
}
else {
this._renderer.rotate(r);
}
return this;
};
/**
* [rotateX description]
* @param {[type]} rad [description]
* @return {[type]} [description]
*/
p5.prototype.rotateX = function(rad) {
var args = new Array(arguments.length);
for (var i = 0; i < args.length; ++i) {
args[i] = arguments[i];
}
if (this._renderer.isP3D) {
this._validateParameters(
'rotateX',
args,
[
['Number']
]
);
this._renderer.rotateX(rad);
} else {
throw 'not supported in p2d. Please use webgl mode';
}
return this;
};
/**
* [rotateY description]
* @param {[type]} rad [description]
* @return {[type]} [description]
*/
p5.prototype.rotateY = function(rad) {
if (this._renderer.isP3D) {
var args = new Array(arguments.length);
for (var i = 0; i < args.length; ++i) {
args[i] = arguments[i];
}
this._validateParameters(
'rotateY',
args,
[
['Number']
]
);
this._renderer.rotateY(rad);
} else {
throw 'not supported in p2d. Please use webgl mode';
}
return this;
};
/**
* [rotateZ description]
* @param {[type]} rad [description]
* @return {[type]} [description]
*/
p5.prototype.rotateZ = function(rad) {
if (this._renderer.isP3D) {
var args = new Array(arguments.length);
for (var i = 0; i < args.length; ++i) {
args[i] = arguments[i];
}
this._validateParameters(
'rotateZ',
args,
[
['Number']
]
);
this._renderer.rotateZ(rad);
} else {
throw 'not supported in p2d. Please use webgl mode';
}
return this;
};
/**
* Increases or decreases the size of a shape by expanding and contracting
* vertices. Objects always scale from their relative origin to the
* coordinate system. Scale values are specified as decimal percentages.
* For example, the function call scale(2.0) increases the dimension of a
* shape by 200%.
*
* Transformations apply to everything that happens after and subsequent
* calls to the function multiply the effect. For example, calling scale(2.0)
* and then scale(1.5) is the same as scale(3.0). If scale() is called
* within draw(), the transformation is reset when the loop begins again.
*
* Using this fuction with the z parameter requires using P3D as a
* parameter for size(), as shown in the third example above. This function
* can be further controlled with push() and pop().
*
* @method scale
* @param {Number | p5.Vector | Array} s
* percent to scale the object, or percentage to
* scale the object in the x-axis if multiple arguments
* are given
* @param {Number} [y] percent to scale the object in the y-axis
* @param {Number} [z] percent to scale the object in the z-axis (webgl only)
* @return {p5} the p5 object
* @example
*
*
* translate(width/2, height/2);
* rotate(PI/3.0);
* rect(-26, -26, 52, 52);
*
*
*
*
*
* rect(30, 20, 50, 50);
* scale(0.5, 1.3);
* rect(30, 20, 50, 50);
*
*
*/
p5.prototype.scale = function() {
var x,y,z;
var args = new Array(arguments.length);
for(var i = 0; i < args.length; i++) {
args[i] = arguments[i];
}
if(args[0] instanceof p5.Vector){
x = args[0].x;
y = args[0].y;
z = args[0].z;
}
else if(args[0] instanceof Array){
x = args[0][0];
y = args[0][1];
z = args[0][2] || 1;
}
else {
if(args.length === 1){
x = y = z = args[0];
}
else {
x = args[0];
y = args[1];
z = args[2] || 1;
}
}
if(this._renderer.isP3D){
this._renderer.scale.call(this._renderer, x,y,z);
}
else {
this._renderer.scale.call(this._renderer, x,y);
}
return this;
};
/**
* Shears a shape around the x-axis the amount specified by the angle
* parameter. Angles should be specified in the current angleMode.
* Objects are always sheared around their relative position to the origin
* and positive numbers shear objects in a clockwise direction.
*
* Transformations apply to everything that happens after and subsequent
* calls to the function accumulates the effect. For example, calling
* shearX(PI/2) and then shearX(PI/2) is the same as shearX(PI).
* If shearX() is called within the draw(), the transformation is reset when
* the loop begins again.
*
* Technically, shearX() multiplies the current transformation matrix by a
* rotation matrix. This function can be further controlled by the
* push() and pop() functions.
*
* @method shearX
* @param {Number} angle angle of shear specified in radians or degrees,
* depending on current angleMode
* @return {p5} the p5 object
* @example
*
*
* translate(width/4, height/4);
* shearX(PI/4.0);
* rect(0, 0, 30, 30);
*
*
*/
p5.prototype.shearX = function(angle) {
if (this._angleMode === constants.DEGREES) {
angle = this.radians(angle);
}
this._renderer.shearX(angle);
return this;
};
/**
* Shears a shape around the y-axis the amount specified by the angle
* parameter. Angles should be specified in the current angleMode. Objects
* are always sheared around their relative position to the origin and
* positive numbers shear objects in a clockwise direction.
*
* Transformations apply to everything that happens after and subsequent
* calls to the function accumulates the effect. For example, calling
* shearY(PI/2) and then shearY(PI/2) is the same as shearY(PI). If
* shearY() is called within the draw(), the transformation is reset when
* the loop begins again.
*
* Technically, shearY() multiplies the current transformation matrix by a
* rotation matrix. This function can be further controlled by the
* push() and pop() functions.
*
* @method shearY
* @param {Number} angle angle of shear specified in radians or degrees,
* depending on current angleMode
* @return {p5} the p5 object
* @example
*
*
* translate(width/4, height/4);
* shearY(PI/4.0);
* rect(0, 0, 30, 30);
*
*
*/
p5.prototype.shearY = function(angle) {
if (this._angleMode === constants.DEGREES) {
angle = this.radians(angle);
}
this._renderer.shearY(angle);
return this;
};
/**
* Specifies an amount to displace objects within the display window.
* The x parameter specifies left/right translation, the y parameter
* specifies up/down translation.
*
* Transformations are cumulative and apply to everything that happens after
* and subsequent calls to the function accumulates the effect. For example,
* calling translate(50, 0) and then translate(20, 0) is the same as
* translate(70, 0). If translate() is called within draw(), the
* transformation is reset when the loop begins again. This function can be
* further controlled by using push() and pop().
*
* @method translate
* @param {Number} x left/right translation
* @param {Number} y up/down translation
* @return {p5} the p5 object
* @example
*
*
* translate(30, 20);
* rect(0, 0, 55, 55);
*
*
*
*
*
* rect(0, 0, 55, 55); // Draw rect at original 0,0
* translate(30, 20);
* rect(0, 0, 55, 55); // Draw rect at new 0,0
* translate(14, 14);
* rect(0, 0, 55, 55); // Draw rect at new 0,0
*
*
*/
p5.prototype.translate = function(x, y, z) {
var args = new Array(arguments.length);
for (var i = 0; i < args.length; ++i) {
args[i] = arguments[i];
}
if (this._renderer.isP3D) {
this._validateParameters(
'translate',
args,
[
//p3d
['Number', 'Number', 'Number']
]
);
this._renderer.translate(x, y, z);
} else {
this._validateParameters(
'translate',
args,
[
//p2d
['Number', 'Number']
]
);
this._renderer.translate(x, y);
}
return this;
};
module.exports = p5;
},{"./constants":47,"./core":48}],60:[function(_dereq_,module,exports){
/**
* @module Shape
* @submodule Vertex
* @for p5
* @requires core
* @requires constants
*/
'use strict';
var p5 = _dereq_('./core');
var constants = _dereq_('./constants');
var shapeKind = null;
var vertices = [];
var contourVertices = [];
var isBezier = false;
var isCurve = false;
var isQuadratic = false;
var isContour = false;
/**
* Use the beginContour() and endContour() functions to create negative
* shapes within shapes such as the center of the letter 'O'. beginContour()
* begins recording vertices for the shape and endContour() stops recording.
* The vertices that define a negative shape must "wind" in the opposite
* direction from the exterior shape. First draw vertices for the exterior
* clockwise order, then for internal shapes, draw vertices
* shape in counter-clockwise.
*
* These functions can only be used within a beginShape()/endShape() pair and
* transformations such as translate(), rotate(), and scale() do not work
* within a beginContour()/endContour() pair. It is also not possible to use
* other shapes, such as ellipse() or rect() within.
*
* @method beginContour
* @return {Object} the p5 object
* @example
*
*
* translate(50, 50);
* stroke(255, 0, 0);
* beginShape();
* // Exterior part of shape, clockwise winding
* vertex(-40, -40);
* vertex(40, -40);
* vertex(40, 40);
* vertex(-40, 40);
* // Interior part of shape, counter-clockwise winding
* beginContour();
* vertex(-20, -20);
* vertex(-20, 20);
* vertex(20, 20);
* vertex(20, -20);
* endContour();
* endShape(CLOSE);
*
*
*/
p5.prototype.beginContour = function() {
contourVertices = [];
isContour = true;
return this;
};
/**
* Using the beginShape() and endShape() functions allow creating more
* complex forms. beginShape() begins recording vertices for a shape and
* endShape() stops recording. The value of the kind parameter tells it which
* types of shapes to create from the provided vertices. With no mode
* specified, the shape can be any irregular polygon. The parameters
* available for beginShape() are POINTS, LINES, TRIANGLES, TRIANGLE_FAN,
* TRIANGLE_STRIP, QUADS, and QUAD_STRIP. After calling the beginShape()
* function, a series of vertex() commands must follow. To stop drawing the
* shape, call endShape(). Each shape will be outlined with the current
* stroke color and filled with the fill color.
*
* Transformations such as translate(), rotate(), and scale() do not work
* within beginShape(). It is also not possible to use other shapes, such as
* ellipse() or rect() within beginShape().
*
* @method beginShape
* @param {Number/Constant} kind either POINTS, LINES, TRIANGLES,
* TRIANGLE_FAN, TRIANGLE_STRIP, QUADS,
* or QUAD_STRIP
* @return {Object} the p5 object
* @example
*
*
* beginShape();
* vertex(30, 20);
* vertex(85, 20);
* vertex(85, 75);
* vertex(30, 75);
* endShape(CLOSE);
*
*
*
*
*
* // currently not working
* beginShape(POINTS);
* vertex(30, 20);
* vertex(85, 20);
* vertex(85, 75);
* vertex(30, 75);
* endShape();
*
*
*
*
*
* beginShape(LINES);
* vertex(30, 20);
* vertex(85, 20);
* vertex(85, 75);
* vertex(30, 75);
* endShape();
*
*
*
*
*
* noFill();
* beginShape();
* vertex(30, 20);
* vertex(85, 20);
* vertex(85, 75);
* vertex(30, 75);
* endShape();
*
*
*
*
*
* noFill();
* beginShape();
* vertex(30, 20);
* vertex(85, 20);
* vertex(85, 75);
* vertex(30, 75);
* endShape(CLOSE);
*
*
*
*
*
* beginShape(TRIANGLES);
* vertex(30, 75);
* vertex(40, 20);
* vertex(50, 75);
* vertex(60, 20);
* vertex(70, 75);
* vertex(80, 20);
* endShape();
*
*
*
*
*
* beginShape(TRIANGLE_STRIP);
* vertex(30, 75);
* vertex(40, 20);
* vertex(50, 75);
* vertex(60, 20);
* vertex(70, 75);
* vertex(80, 20);
* vertex(90, 75);
* endShape();
*
*
*
*
*
* beginShape(TRIANGLE_FAN);
* vertex(57.5, 50);
* vertex(57.5, 15);
* vertex(92, 50);
* vertex(57.5, 85);
* vertex(22, 50);
* vertex(57.5, 15);
* endShape();
*
*
*
*
*
* beginShape(QUADS);
* vertex(30, 20);
* vertex(30, 75);
* vertex(50, 75);
* vertex(50, 20);
* vertex(65, 20);
* vertex(65, 75);
* vertex(85, 75);
* vertex(85, 20);
* endShape();
*
*
*
*
*
* beginShape(QUAD_STRIP);
* vertex(30, 20);
* vertex(30, 75);
* vertex(50, 20);
* vertex(50, 75);
* vertex(65, 20);
* vertex(65, 75);
* vertex(85, 20);
* vertex(85, 75);
* endShape();
*
*
*
*
*
* beginShape();
* vertex(20, 20);
* vertex(40, 20);
* vertex(40, 40);
* vertex(60, 40);
* vertex(60, 60);
* vertex(20, 60);
* endShape(CLOSE);
*
*
*/
p5.prototype.beginShape = function(kind) {
if (kind === constants.POINTS ||
kind === constants.LINES ||
kind === constants.TRIANGLES ||
kind === constants.TRIANGLE_FAN ||
kind === constants.TRIANGLE_STRIP ||
kind === constants.QUADS ||
kind === constants.QUAD_STRIP) {
shapeKind = kind;
} else {
shapeKind = null;
}
if(this._renderer.isP3D){
this._renderer.beginShape(kind);
} else {
vertices = [];
contourVertices = [];
}
return this;
};
/**
* Specifies vertex coordinates for Bezier curves. Each call to
* bezierVertex() defines the position of two control points and
* one anchor point of a Bezier curve, adding a new segment to a
* line or shape. The first time bezierVertex() is used within a
* beginShape() call, it must be prefaced with a call to vertex()
* to set the first anchor point. This function must be used between
* beginShape() and endShape() and only when there is no MODE
* parameter specified to beginShape().
*
* @method bezierVertex
* @param {Number} x2 x-coordinate for the first control point
* @param {Number} y2 y-coordinate for the first control point
* @param {Number} x3 x-coordinate for the second control point
* @param {Number} y3 y-coordinate for the second control point
* @param {Number} x4 x-coordinate for the anchor point
* @param {Number} y4 y-coordinate for the anchor point
* @return {Object} the p5 object
* @example
*
*
* noFill();
* beginShape();
* vertex(30, 20);
* bezierVertex(80, 0, 80, 75, 30, 75);
* endShape();
*
*
*
*
*
* beginShape();
* vertex(30, 20);
* bezierVertex(80, 0, 80, 75, 30, 75);
* bezierVertex(50, 80, 60, 25, 30, 20);
* endShape();
*
*
*/
p5.prototype.bezierVertex = function(x2, y2, x3, y3, x4, y4) {
if (vertices.length === 0) {
throw 'vertex() must be used once before calling bezierVertex()';
} else {
isBezier = true;
var vert = [];
for (var i = 0; i < arguments.length; i++) {
vert[i] = arguments[i];
}
vert.isVert = false;
if (isContour) {
contourVertices.push(vert);
} else {
vertices.push(vert);
}
}
return this;
};
/**
* Specifies vertex coordinates for curves. This function may only
* be used between beginShape() and endShape() and only when there
* is no MODE parameter specified to beginShape(). The first and
* last points in a series of curveVertex() lines will be used to
* guide the beginning and end of a the curve. A minimum of four
* points is required to draw a tiny curve between the second and
* third points. Adding a fifth point with curveVertex() will draw
* the curve between the second, third, and fourth points. The
* curveVertex() function is an implementation of Catmull-Rom
* splines.
*
* @method curveVertex
* @param {Number} x x-coordinate of the vertex
* @param {Number} y y-coordinate of the vertex
* @return {Object} the p5 object
* @example
*
*
* noFill();
* beginShape();
* curveVertex(84, 91);
* curveVertex(84, 91);
* curveVertex(68, 19);
* curveVertex(21, 17);
* curveVertex(32, 100);
* curveVertex(32, 100);
* endShape();
*
*
*/
p5.prototype.curveVertex = function(x,y) {
isCurve = true;
this.vertex(x, y);
return this;
};
/**
* Use the beginContour() and endContour() functions to create negative
* shapes within shapes such as the center of the letter 'O'. beginContour()
* begins recording vertices for the shape and endContour() stops recording.
* The vertices that define a negative shape must "wind" in the opposite
* direction from the exterior shape. First draw vertices for the exterior
* clockwise order, then for internal shapes, draw vertices
* shape in counter-clockwise.
*
* These functions can only be used within a beginShape()/endShape() pair and
* transformations such as translate(), rotate(), and scale() do not work
* within a beginContour()/endContour() pair. It is also not possible to use
* other shapes, such as ellipse() or rect() within.
*
* @method endContour
* @return {Object} the p5 object
* @example
*
*
* translate(50, 50);
* stroke(255, 0, 0);
* beginShape();
* // Exterior part of shape, clockwise winding
* vertex(-40, -40);
* vertex(40, -40);
* vertex(40, 40);
* vertex(-40, 40);
* // Interior part of shape, counter-clockwise winding
* beginContour();
* vertex(-20, -20);
* vertex(-20, 20);
* vertex(20, 20);
* vertex(20, -20);
* endContour();
* endShape(CLOSE);
*
*
*/
p5.prototype.endContour = function() {
var vert = contourVertices[0].slice(); // copy all data
vert.isVert = contourVertices[0].isVert;
vert.moveTo = false;
contourVertices.push(vert);
vertices.push(vertices[0]);
for (var i = 0; i < contourVertices.length; i++) {
vertices.push(contourVertices[i]);
}
return this;
};
/**
* The endShape() function is the companion to beginShape() and may only be
* called after beginShape(). When endshape() is called, all of image data
* defined since the previous call to beginShape() is written into the image
* buffer. The constant CLOSE as the value for the MODE parameter to close
* the shape (to connect the beginning and the end).
*
* @method endShape
* @param {Number/Constant} mode use CLOSE to close the shape
* @return {Object} the p5 object
* @example
*
*
* noFill();
*
* beginShape();
* vertex(20, 20);
* vertex(45, 20);
* vertex(45, 80);
* endShape(CLOSE);
*
* beginShape();
* vertex(50, 20);
* vertex(75, 20);
* vertex(75, 80);
* endShape();
*
*
*/
p5.prototype.endShape = function(mode) {
if(this._renderer.isP3D){
this._renderer.endShape();
}else{
if (vertices.length === 0) { return this; }
if (!this._renderer._doStroke && !this._renderer._doFill) { return this; }
var closeShape = mode === constants.CLOSE;
// if the shape is closed, the first element is also the last element
if (closeShape && !isContour) {
vertices.push(vertices[0]);
}
this._renderer.endShape(mode, vertices, isCurve, isBezier,
isQuadratic, isContour, shapeKind);
// Reset some settings
isCurve = false;
isBezier = false;
isQuadratic = false;
isContour = false;
// If the shape is closed, the first element was added as last element.
// We must remove it again to prevent the list of vertices from growing
// over successive calls to endShape(CLOSE)
if (closeShape) {
vertices.pop();
}
}
return this;
};
/**
* Specifies vertex coordinates for quadratic Bezier curves. Each call to
* quadraticVertex() defines the position of one control points and one
* anchor point of a Bezier curve, adding a new segment to a line or shape.
* The first time quadraticVertex() is used within a beginShape() call, it
* must be prefaced with a call to vertex() to set the first anchor point.
* This function must be used between beginShape() and endShape() and only
* when there is no MODE parameter specified to beginShape().
*
* @method quadraticVertex
* @param {Number} cx x-coordinate for the control point
* @param {Number} cy y-coordinate for the control point
* @param {Number} x3 x-coordinate for the anchor point
* @param {Number} y3 y-coordinate for the anchor point
* @return {Object} the p5 object
* @example
*
*
* noFill();
* strokeWeight(4);
* beginShape();
* vertex(20, 20);
* quadraticVertex(80, 20, 50, 50);
* endShape();
*
*
*
*
*
* noFill();
* strokeWeight(4);
* beginShape();
* vertex(20, 20);
* quadraticVertex(80, 20, 50, 50);
* quadraticVertex(20, 80, 80, 80);
* vertex(80, 60);
* endShape();
*
*
*/
p5.prototype.quadraticVertex = function(cx, cy, x3, y3) {
//if we're drawing a contour, put the points into an
// array for inside drawing
if(this._contourInited) {
var pt = {};
pt.x = cx;
pt.y = cy;
pt.x3 = x3;
pt.y3 = y3;
pt.type = constants.QUADRATIC;
this._contourVertices.push(pt);
return this;
}
if (vertices.length > 0) {
isQuadratic = true;
var vert = [];
for (var i = 0; i < arguments.length; i++) {
vert[i] = arguments[i];
}
vert.isVert = false;
if (isContour) {
contourVertices.push(vert);
} else {
vertices.push(vert);
}
} else {
throw 'vertex() must be used once before calling quadraticVertex()';
}
return this;
};
/**
* All shapes are constructed by connecting a series of vertices. vertex()
* is used to specify the vertex coordinates for points, lines, triangles,
* quads, and polygons. It is used exclusively within the beginShape() and
* endShape() functions.
*
* @method vertex
* @param {Number} x x-coordinate of the vertex
* @param {Number} y y-coordinate of the vertex
* @return {Object} the p5 object
* @example
*
*
* beginShape(POINTS);
* vertex(30, 20);
* vertex(85, 20);
* vertex(85, 75);
* vertex(30, 75);
* endShape();
*
*
*/
p5.prototype.vertex = function(x, y, moveTo) {
var args = new Array(arguments.length);
for (var i = 0; i < args.length; ++i) {
args[i] = arguments[i];
}
if(this._renderer.isP3D){
this._validateParameters(
'vertex',
args,
[
['Number', 'Number', 'Number']
]
);
this._renderer.vertex
(arguments[0], arguments[1], arguments[2]);
}else{
this._validateParameters(
'vertex',
args,
[
['Number', 'Number'],
['Number', 'Number', 'Number']
]
);
var vert = [];
vert.isVert = true;
vert[0] = x;
vert[1] = y;
vert[2] = 0;
vert[3] = 0;
vert[4] = 0;
vert[5] = this._renderer._getFill();
vert[6] = this._renderer._getStroke();
if (moveTo) {
vert.moveTo = moveTo;
}
if (isContour) {
if (contourVertices.length === 0) {
vert.moveTo = true;
}
contourVertices.push(vert);
} else {
vertices.push(vert);
}
}
return this;
};
module.exports = p5;
},{"./constants":47,"./core":48}],61:[function(_dereq_,module,exports){
/**
* @module Events
* @submodule Acceleration
* @for p5
* @requires core
*/
'use strict';
var p5 = _dereq_('../core/core');
/**
* The system variable deviceOrientation always contains the orientation of
* the device. The value of this variable will either be set 'landscape'
* or 'portrait'. If no data is available it will be set to 'undefined'.
*
* @property deviceOrientation
*/
p5.prototype.deviceOrientation = undefined;
/**
* The system variable accelerationX always contains the acceleration of the
* device along the x axis. Value is represented as meters per second squared.
*
* @property accelerationX
*/
p5.prototype.accelerationX = 0;
/**
* The system variable accelerationY always contains the acceleration of the
* device along the y axis. Value is represented as meters per second squared.
*
* @property accelerationY
*/
p5.prototype.accelerationY = 0;
/**
* The system variable accelerationZ always contains the acceleration of the
* device along the z axis. Value is represented as meters per second squared.
*
* @property accelerationZ
*/
p5.prototype.accelerationZ = 0;
/**
* The system variable pAccelerationX always contains the acceleration of the
* device along the x axis in the frame previous to the current frame. Value
* is represented as meters per second squared.
*
* @property pAccelerationX
*/
p5.prototype.pAccelerationX = 0;
/**
* The system variable pAccelerationY always contains the acceleration of the
* device along the y axis in the frame previous to the current frame. Value
* is represented as meters per second squared.
*
* @property pAccelerationY
*/
p5.prototype.pAccelerationY = 0;
/**
* The system variable pAccelerationZ always contains the acceleration of the
* device along the z axis in the frame previous to the current frame. Value
* is represented as meters per second squared.
*
* @property pAccelerationZ
*/
p5.prototype.pAccelerationZ = 0;
/**
* _updatePAccelerations updates the pAcceleration values
*
* @private
*/
p5.prototype._updatePAccelerations = function(){
this._setProperty('pAccelerationX', this.accelerationX);
this._setProperty('pAccelerationY', this.accelerationY);
this._setProperty('pAccelerationZ', this.accelerationZ);
};
/**
* The system variable rotationX always contains the rotation of the
* device along the x axis. Value is represented as 0 to +/-180 degrees.
*
* @property rotationX
*/
p5.prototype.rotationX = 0;
/**
* The system variable rotationY always contains the rotation of the
* device along the y axis. Value is represented as 0 to +/-180 degrees.
*
* @property rotationY
*/
p5.prototype.rotationY = 0;
/**
* The system variable rotationZ always contains the rotation of the
* device along the z axis. Value is represented as 0 to 359 degrees.
*
* Unlike rotationX and rotationY, this variable is available for devices
* with a built-in compass only.
*
* @property rotationZ
*/
p5.prototype.rotationZ = 0;
/**
* The system variable pRotationX always contains the rotation of the
* device along the x axis in the frame previous to the current frame. Value
* is represented as 0 to +/-180 degrees.
*
* pRotationX can also be used with rotationX to determine the rotate
* direction of the device along the X-axis.
* @example
*
*
* // A simple if statement looking at whether
* // rotationX - pRotationX < 0 is true or not will be
* // sufficient for determining the rotate direction
* // in most cases.
*
* // Some extra logic is needed to account for cases where
* // the angles wrap around.
* var rotateDirection = 'clockwise';
*
* // Simple range conversion to make things simpler.
* // This is not absolutely neccessary but the logic
* // will be different in that case.
*
* var rX = rotationX + 180;
* var pRX = pRotationX + 180;
*
* if ((rX - pRX > 0 && rX - pRX < 270)|| rX - pRX < -270){
* rotateDirection = 'clockwise';
* } else if (rX - pRX < 0 || rX - pRX > 270){
* rotateDirection = 'counter-clockwise';
* }
*
*
*
* @property pRotationX
*/
p5.prototype.pRotationX = 0;
/**
* The system variable pRotationY always contains the rotation of the
* device along the y axis in the frame previous to the current frame. Value
* is represented as 0 to +/-180 degrees.
*
* pRotationY can also be used with rotationY to determine the rotate
* direction of the device along the Y-axis.
* @example
*
*
* // A simple if statement looking at whether
* // rotationY - pRotationY < 0 is true or not will be
* // sufficient for determining the rotate direction
* // in most cases.
*
* // Some extra logic is needed to account for cases where
* // the angles wrap around.
* var rotateDirection = 'clockwise';
*
* // Simple range conversion to make things simpler.
* // This is not absolutely neccessary but the logic
* // will be different in that case.
*
* var rY = rotationY + 180;
* var pRY = pRotationY + 180;
*
* if ((rY - pRY > 0 && rY - pRY < 270)|| rY - pRY < -270){
* rotateDirection = 'clockwise';
* } else if (rY - pRY < 0 || rY - pRY > 270){
* rotateDirection = 'counter-clockwise';
* }
*
*
*
* @property pRotationY
*/
p5.prototype.pRotationY = 0;
/**
* The system variable pRotationZ always contains the rotation of the
* device along the z axis in the frame previous to the current frame. Value
* is represented as 0 to 359 degrees.
*
* pRotationZ can also be used with rotationZ to determine the rotate
* direction of the device along the Z-axis.
* @example
*
*
* // A simple if statement looking at whether
* // rotationZ - pRotationZ < 0 is true or not will be
* // sufficient for determining the rotate direction
* // in most cases.
*
* // Some extra logic is needed to account for cases where
* // the angles wrap around.
* var rotateDirection = 'clockwise';
*
* if ((rotationZ - pRotationZ > 0 &&
* rotationZ - pRotationZ < 270)||
* rotationZ - pRotationZ < -270){
*
* rotateDirection = 'clockwise';
*
* } else if (rotationZ - pRotationZ < 0 ||
* rotationZ - pRotationZ > 270){
*
* rotateDirection = 'counter-clockwise';
*
* }
*
*
*
* @property pRotationZ
*/
p5.prototype.pRotationZ = 0;
var startAngleX = 0;
var startAngleY = 0;
var startAngleZ = 0;
var rotateDirectionX = 'clockwise';
var rotateDirectionY = 'clockwise';
var rotateDirectionZ = 'clockwise';
var pRotateDirectionX;
var pRotateDirectionY;
var pRotateDirectionZ;
p5.prototype._updatePRotations = function(){
this._setProperty('pRotationX', this.rotationX);
this._setProperty('pRotationY', this.rotationY);
this._setProperty('pRotationZ', this.rotationZ);
};
p5.prototype.turnAxis = undefined;
var move_threshold = 0.5;
var shake_threshold = 30;
/**
* The setMoveThreshold() function is used to set the movement threshold for
* the deviceMoved() function. The default threshold is set to 0.5.
*
* @method setMoveThreshold
* @param {number} value The threshold value
*/
p5.prototype.setMoveThreshold = function(val){
if(typeof val === 'number'){
move_threshold = val;
}
};
/**
* The setShakeThreshold() function is used to set the movement threshold for
* the deviceShaken() function. The default threshold is set to 30.
*
* @method setShakeThreshold
* @param {number} value The threshold value
*/
p5.prototype.setShakeThreshold = function(val){
if(typeof val === 'number'){
shake_threshold = val;
}
};
/**
* The deviceMoved() function is called when the device is moved by more than
* the threshold value along X, Y or Z axis. The default threshold is set to
* 0.5.
* @method deviceMoved
* @example
*
*
* // Run this example on a mobile device
* // Move the device around
* // to change the value.
*
* var value = 0;
* function draw() {
* fill(value);
* rect(25, 25, 50, 50);
* }
* function deviceMoved() {
* value = value + 5;
* if (value > 255) {
* value = 0;
* }
* }
*
*
*/
/**
* The deviceTurned() function is called when the device rotates by
* more than 90 degrees continuously.
*
* The axis that triggers the deviceTurned() method is stored in the turnAxis
* variable. The deviceTurned() method can be locked to trigger on any axis:
* X, Y or Z by comparing the turnAxis variable to 'X', 'Y' or 'Z'.
*
* @method deviceTurned
* @example
*
*
* // Run this example on a mobile device
* // Rotate the device by 90 degrees
* // to change the value.
*
* var value = 0;
* function draw() {
* fill(value);
* rect(25, 25, 50, 50);
* }
* function deviceTurned() {
* if (value == 0){
* value = 255
* } else if (value == 255) {
* value = 0;
* }
* }
*
*
*
*
* // Run this example on a mobile device
* // Rotate the device by 90 degrees in the
* // X-axis to change the value.
*
* var value = 0;
* function draw() {
* fill(value);
* rect(25, 25, 50, 50);
* }
* function deviceTurned() {
* if (turnAxis == 'X'){
* if (value == 0){
* value = 255
* } else if (value == 255) {
* value = 0;
* }
* }
* }
*
*
*/
/**
* The deviceShaken() function is called when the device total acceleration
* changes of accelerationX and accelerationY values is more than
* the threshold value. The default threshold is set to 30.
* @method deviceShaken
* @example
*
*
* // Run this example on a mobile device
* // Shake the device to change the value.
*
* var value = 0;
* function draw() {
* fill(value);
* rect(25, 25, 50, 50);
* }
* function deviceShaken() {
* value = value + 5;
* if (value > 255) {
* value = 0;
* }
* }
*
*
*/
p5.prototype._ondeviceorientation = function (e) {
this._updatePRotations();
this._setProperty('rotationX', e.beta);
this._setProperty('rotationY', e.gamma);
this._setProperty('rotationZ', e.alpha);
this._handleMotion();
};
p5.prototype._ondevicemotion = function (e) {
this._updatePAccelerations();
this._setProperty('accelerationX', e.acceleration.x * 2);
this._setProperty('accelerationY', e.acceleration.y * 2);
this._setProperty('accelerationZ', e.acceleration.z * 2);
this._handleMotion();
};
p5.prototype._handleMotion = function() {
if (window.orientation === 90 || window.orientation === -90) {
this._setProperty('deviceOrientation', 'landscape');
} else if (window.orientation === 0) {
this._setProperty('deviceOrientation', 'portrait');
} else if (window.orientation === undefined) {
this._setProperty('deviceOrientation', 'undefined');
}
var deviceMoved = this.deviceMoved || window.deviceMoved;
if (typeof deviceMoved === 'function') {
if (Math.abs(this.accelerationX - this.pAccelerationX) > move_threshold ||
Math.abs(this.accelerationY - this.pAccelerationY) > move_threshold ||
Math.abs(this.accelerationZ - this.pAccelerationZ) > move_threshold) {
deviceMoved();
}
}
var deviceTurned = this.deviceTurned || window.deviceTurned;
if (typeof deviceTurned === 'function') {
// The angles given by rotationX etc is from range -180 to 180.
// The following will convert them to 0 to 360 for ease of calculation
// of cases when the angles wrapped around.
// _startAngleX will be converted back at the end and updated.
var wRX = this.rotationX + 180;
var wPRX = this.pRotationX + 180;
var wSAX = startAngleX + 180;
if ((wRX - wPRX > 0 && wRX - wPRX < 270)|| wRX - wPRX < -270){
rotateDirectionX = 'clockwise';
} else if (wRX - wPRX < 0 || wRX - wPRX > 270){
rotateDirectionX = 'counter-clockwise';
}
if (rotateDirectionX !== pRotateDirectionX){
wSAX = wRX;
}
if (Math.abs(wRX - wSAX) > 90 && Math.abs(wRX - wSAX) < 270){
wSAX = wRX;
this._setProperty('turnAxis', 'X');
deviceTurned();
}
pRotateDirectionX = rotateDirectionX;
startAngleX = wSAX - 180;
// Y-axis is identical to X-axis except for changing some names.
var wRY = this.rotationY + 180;
var wPRY = this.pRotationY + 180;
var wSAY = startAngleY + 180;
if ((wRY - wPRY > 0 && wRY - wPRY < 270)|| wRY - wPRY < -270){
rotateDirectionY = 'clockwise';
} else if (wRY - wPRY < 0 || wRY - this.pRotationY > 270){
rotateDirectionY = 'counter-clockwise';
}
if (rotateDirectionY !== pRotateDirectionY){
wSAY = wRY;
}
if (Math.abs(wRY - wSAY) > 90 && Math.abs(wRY - wSAY) < 270){
wSAY = wRY;
this._setProperty('turnAxis', 'Y');
deviceTurned();
}
pRotateDirectionY = rotateDirectionY;
startAngleY = wSAY - 180;
// Z-axis is already in the range 0 to 360
// so no conversion is needed.
if ((this.rotationZ - this.pRotationZ > 0 &&
this.rotationZ - this.pRotationZ < 270)||
this.rotationZ - this.pRotationZ < -270){
rotateDirectionZ = 'clockwise';
} else if (this.rotationZ - this.pRotationZ < 0 ||
this.rotationZ - this.pRotationZ > 270){
rotateDirectionZ = 'counter-clockwise';
}
if (rotateDirectionZ !== pRotateDirectionZ){
startAngleZ = this.rotationZ;
}
if (Math.abs(this.rotationZ - startAngleZ) > 90 &&
Math.abs(this.rotationZ - startAngleZ) < 270){
startAngleZ = this.rotationZ;
this._setProperty('turnAxis', 'Z');
deviceTurned();
}
pRotateDirectionZ = rotateDirectionZ;
this._setProperty('turnAxis', undefined);
}
var deviceShaken = this.deviceShaken || window.deviceShaken;
if (typeof deviceShaken === 'function') {
var accelerationChangeX;
var accelerationChangeY;
// Add accelerationChangeZ if acceleration change on Z is needed
if (this.pAccelerationX !== null) {
accelerationChangeX = Math.abs(this.accelerationX - this.pAccelerationX);
accelerationChangeY = Math.abs(this.accelerationY - this.pAccelerationY);
}
if (accelerationChangeX + accelerationChangeY > shake_threshold) {
deviceShaken();
}
}
};
module.exports = p5;
},{"../core/core":48}],62:[function(_dereq_,module,exports){
/**
* @module Events
* @submodule Keyboard
* @for p5
* @requires core
*/
'use strict';
var p5 = _dereq_('../core/core');
/**
* Holds the key codes of currently pressed keys.
* @private
*/
var downKeys = {};
/**
* The boolean system variable keyIsPressed is true if any key is pressed
* and false if no keys are pressed.
*
* @property keyIsPressed
* @example
*
*
* var value = 0;
* function draw() {
* if (keyIsPressed === true) {
* fill(0);
* } else {
* fill(255);
* }
* rect(25, 25, 50, 50);
* }
*
*
*/
p5.prototype.isKeyPressed = false;
p5.prototype.keyIsPressed = false; // khan
/**
* The system variable key always contains the value of the most recent
* key on the keyboard that was typed. To get the proper capitalization, it
* is best to use it within keyTyped(). For non-ASCII keys, use the keyCode
* variable.
*
* @property key
* @example
*
* // Click any key to display it!
* // (Not Guaranteed to be Case Sensitive)
* function setup() {
* fill(245, 123, 158);
* textSize(50);
* }
*
* function draw() {
* background(200);
* text(key, 33,65); // Display last key pressed.
* }
*
*/
p5.prototype.key = '';
/**
* The variable keyCode is used to detect special keys such as BACKSPACE,
* DELETE, ENTER, RETURN, TAB, ESCAPE, SHIFT, CONTROL, OPTION, ALT, UP_ARROW,
* DOWN_ARROW, LEFT_ARROW, RIGHT_ARROW.
*
* @property keyCode
* @example
*
* var fillVal = 126;
* function draw() {
* fill(fillVal);
* rect(25, 25, 50, 50);
* }
*
* function keyPressed() {
* if (keyCode == UP_ARROW) {
* fillVal = 255;
* } else if (keyCode == DOWN_ARROW) {
* fillVal = 0;
* }
* return false; // prevent default
* }
*
*/
p5.prototype.keyCode = 0;
/**
* The keyPressed() function is called once every time a key is pressed. The
* keyCode for the key that was pressed is stored in the keyCode variable.
*
* For non-ASCII keys, use the keyCode variable. You can check if the keyCode
* equals BACKSPACE, DELETE, ENTER, RETURN, TAB, ESCAPE, SHIFT, CONTROL,
* OPTION, ALT, UP_ARROW, DOWN_ARROW, LEFT_ARROW, RIGHT_ARROW.
*
* For ASCII keys that was pressed is stored in the key variable. However, it
* does not distinguish between uppercase and lowercase. For this reason, it
* is recommended to use keyTyped() to read the key variable, in which the
* case of the variable will be distinguished.
*
* Because of how operating systems handle key repeats, holding down a key
* may cause multiple calls to keyTyped() (and keyReleased() as well). The
* rate of repeat is set by the operating system and how each computer is
* configured.
* Browsers may have different default
* behaviors attached to various key events. To prevent any default
* behavior for this event, add "return false" to the end of the method.
*
* @method keyPressed
* @example
*
*
* var value = 0;
* function draw() {
* fill(value);
* rect(25, 25, 50, 50);
* }
* function keyPressed() {
* if (value === 0) {
* value = 255;
* } else {
* value = 0;
* }
* }
*
*
*
*
* var value = 0;
* function draw() {
* fill(value);
* rect(25, 25, 50, 50);
* }
* function keyPressed() {
* if (keyCode === LEFT_ARROW) {
* value = 255;
* } else if (keyCode === RIGHT_ARROW) {
* value = 0;
* }
* }
*
*
*
*
* function keyPressed(){
* // Do something
* return false; // prevent any default behaviour
* }
*
*
*/
p5.prototype._onkeydown = function (e) {
this._setProperty('isKeyPressed', true);
this._setProperty('keyIsPressed', true);
this._setProperty('keyCode', e.which);
downKeys[e.which] = true;
var key = String.fromCharCode(e.which);
if (!key) {
key = e.which;
}
this._setProperty('key', key);
var keyPressed = this.keyPressed || window.keyPressed;
if (typeof keyPressed === 'function' && !e.charCode) {
var executeDefault = keyPressed(e);
if(executeDefault === false) {
e.preventDefault();
}
}
};
/**
* The keyReleased() function is called once every time a key is released.
* See key and keyCode for more information.
* Browsers may have different default
* behaviors attached to various key events. To prevent any default
* behavior for this event, add "return false" to the end of the method.
*
* @method keyReleased
* @example
*
*
* var value = 0;
* function draw() {
* fill(value);
* rect(25, 25, 50, 50);
* }
* function keyReleased() {
* if (value === 0) {
* value = 255;
* } else {
* value = 0;
* }
* return false; // prevent any default behavior
* }
*
*
*/
p5.prototype._onkeyup = function (e) {
var keyReleased = this.keyReleased || window.keyReleased;
this._setProperty('isKeyPressed', false);
this._setProperty('keyIsPressed', false);
downKeys[e.which] = false;
//delete this._downKeys[e.which];
var key = String.fromCharCode(e.which);
if (!key) {
key = e.which;
}
this._setProperty('key', key);
this._setProperty('keyCode', e.which);
if (typeof keyReleased === 'function') {
var executeDefault = keyReleased(e);
if(executeDefault === false) {
e.preventDefault();
}
}
};
/**
* The keyTyped() function is called once every time a key is pressed, but
* action keys such as Ctrl, Shift, and Alt are ignored. The most recent
* key pressed will be stored in the key variable.
*
* Because of how operating systems handle key repeats, holding down a key
* will cause multiple calls to keyTyped(), the rate is set by the operating
* system and how each computer is configured.
* Browsers may have different default
* behaviors attached to various key events. To prevent any default
* behavior for this event, add "return false" to the end of the method.
*
* @method keyTyped
* @example
*
*
* var value = 0;
* function draw() {
* fill(value);
* rect(25, 25, 50, 50);
* }
* function keyTyped() {
* if (key === 'a') {
* value = 255;
* } else if (key === 'b') {
* value = 0;
* }
* return false; // prevent any default behavior
* }
*
*
*/
p5.prototype._onkeypress = function (e) {
this._setProperty('keyCode', e.which);
this._setProperty('key', String.fromCharCode(e.which));
var keyTyped = this.keyTyped || window.keyTyped;
if (typeof keyTyped === 'function') {
var executeDefault = keyTyped(e);
if(executeDefault === false) {
e.preventDefault();
}
}
};
/**
* The onblur function is called when the user is no longer focused
* on the p5 element. Because the keyup events will no fire if the user is
* not focused on the element we must assume all keys currently down have
* been released.
*/
p5.prototype._onblur = function (e) {
downKeys = {};
};
/**
* The keyIsDown function checks if the key is currently down, i.e. pressed.
* It can be used if you have an object that moves, and you want several keys
* to be able to affect its behaviour simultaneously, such as moving a
* sprite diagonally. You can put in any number representing the keyCode of
* the key, or use any of the variable keyCode names listed
* here.
*
* @method keyIsDown
* @param {Number} code The key to check for.
* @return {Boolean} whether key is down or not
* @example
*
* var x = 100;
* var y = 100;
*
* function setup() {
* createCanvas(512, 512);
* }
*
* function draw() {
* if (keyIsDown(LEFT_ARROW))
* x-=5;
*
* if (keyIsDown(RIGHT_ARROW))
* x+=5;
*
* if (keyIsDown(UP_ARROW))
* y-=5;
*
* if (keyIsDown(DOWN_ARROW))
* y+=5;
*
* clear();
* fill(255, 0, 0);
* ellipse(x, y, 50, 50);
* }
*
*/
p5.prototype.keyIsDown = function(code) {
return downKeys[code];
};
module.exports = p5;
},{"../core/core":48}],63:[function(_dereq_,module,exports){
/**
* @module Events
* @submodule Mouse
* @for p5
* @requires core
* @requires constants
*/
'use strict';
var p5 = _dereq_('../core/core');
var constants = _dereq_('../core/constants');
/**
* The system variable mouseX always contains the current horizontal
* position of the mouse, relative to (0, 0) of the canvas.
*
* @property mouseX
*
* @example
*
*
* // Move the mouse across the canvas
* function draw() {
* background(244, 248, 252);
* line(mouseX, 0, mouseX, 100);
* }
*
*
*/
p5.prototype.mouseX = 0;
/**
* The system variable mouseY always contains the current vertical position
* of the mouse, relative to (0, 0) of the canvas.
*
* @property mouseY
*
* @example
*
*
* // Move the mouse across the canvas
* function draw() {
* background(244, 248, 252);
* line(0, mouseY, 100, mouseY);
*}
*
*
*/
p5.prototype.mouseY = 0;
/**
* The system variable pmouseX always contains the horizontal position of
* the mouse in the frame previous to the current frame, relative to (0, 0)
* of the canvas.
*
* @property pmouseX
*
* @example
*
*
* // Move the mouse across the canvas to leave a trail
* function setup() {
* //slow down the frameRate to make it more visible
* frameRate(10);
* }
*
* function draw() {
* background(244, 248, 252);
* line(mouseX, mouseY, pmouseX, pmouseY);
* print(pmouseX + " -> " + mouseX);
* }
*
*
*
*/
p5.prototype.pmouseX = 0;
/**
* The system variable pmouseY always contains the vertical position of the
* mouse in the frame previous to the current frame, relative to (0, 0) of
* the canvas.
*
* @property pmouseY
*
* @example
*
*
* function draw() {
* background(237, 34, 93);
* fill(0);
* //draw a square only if the mouse is not moving
* if(mouseY == pmouseY && mouseX == pmouseX)
* rect(20,20,60,60);
*
* print(pmouseY + " -> " + mouseY);
* }
*
*
*
*/
p5.prototype.pmouseY = 0;
/**
* The system variable winMouseX always contains the current horizontal
* position of the mouse, relative to (0, 0) of the window.
*
* @property winMouseX
*
* @example
*
*
* var myCanvas;
*
* function setup() {
* //use a variable to store a pointer to the canvas
* myCanvas = createCanvas(100, 100);
* }
*
* function draw() {
* background(237, 34, 93);
* fill(0);
*
* //move the canvas to the horizontal mouse position
* //relative to the window
* myCanvas.position(winMouseX+1, windowHeight/2);
*
* //the y of the square is relative to the canvas
* rect(20,mouseY,60,60);
* }
*
*
*
*/
p5.prototype.winMouseX = 0;
/**
* The system variable winMouseY always contains the current vertical
* position of the mouse, relative to (0, 0) of the window.
*
* @property winMouseY
*
* @example
*
*
*var myCanvas;
*
* function setup() {
* //use a variable to store a pointer to the canvas
* myCanvas = createCanvas(100, 100);
* }
*
* function draw() {
* background(237, 34, 93);
* fill(0);
*
* //move the canvas to the vertical mouse position
* //relative to the window
* myCanvas.position(windowWidth/2, winMouseY+1);
*
* //the x of the square is relative to the canvas
* rect(mouseX,20,60,60);
* }
*
*
*
*/
p5.prototype.winMouseY = 0;
/**
* The system variable pwinMouseX always contains the horizontal position
* of the mouse in the frame previous to the current frame, relative to
* (0, 0) of the window.
*
* @property pwinMouseX
*
* @example
*
*
*
* var myCanvas;
*
* function setup() {
* //use a variable to store a pointer to the canvas
* myCanvas = createCanvas(100, 100);
* noStroke();
* fill(237, 34, 93);
* }
*
* function draw() {
* clear();
* //the difference between previous and
* //current x position is the horizontal mouse speed
* var speed = abs(winMouseX-pwinMouseX);
* //change the size of the circle
* //according to the horizontal speed
* ellipse(50, 50, 10+speed*5, 10+speed*5);
* //move the canvas to the mouse position
* myCanvas.position( winMouseX+1, winMouseY+1);
* }
*
*
*
*/
p5.prototype.pwinMouseX = 0;
/**
* The system variable pwinMouseY always contains the vertical position of
* the mouse in the frame previous to the current frame, relative to (0, 0)
* of the window.
*
* @property pwinMouseY
*
*
* @example
*
*
*
* var myCanvas;
*
* function setup() {
* //use a variable to store a pointer to the canvas
* myCanvas = createCanvas(100, 100);
* noStroke();
* fill(237, 34, 93);
* }
*
* function draw() {
* clear();
* //the difference between previous and
* //current y position is the vertical mouse speed
* var speed = abs(winMouseY-pwinMouseY);
* //change the size of the circle
* //according to the vertical speed
* ellipse(50, 50, 10+speed*5, 10+speed*5);
* //move the canvas to the mouse position
* myCanvas.position( winMouseX+1, winMouseY+1);
* }
*
*
*
*/
p5.prototype.pwinMouseY = 0;
/**
* Processing automatically tracks if the mouse button is pressed and which
* button is pressed. The value of the system variable mouseButton is either
* LEFT, RIGHT, or CENTER depending on which button was pressed last.
* Warning: different browsers may track mouseButton differently.
*
* @property mouseButton
*
* @example
*
*
* function draw() {
* background(237, 34, 93);
* fill(0);
*
* if (mouseIsPressed) {
* if (mouseButton == LEFT)
* ellipse(50, 50, 50, 50);
* if (mouseButton == RIGHT)
* rect(25, 25, 50, 50);
* if (mouseButton == CENTER)
* triangle(23, 75, 50, 20, 78, 75);
* }
*
* print(mouseButton);
* }
*
*
*/
p5.prototype.mouseButton = 0;
/**
* The boolean system variable mouseIsPressed is true if the mouse is pressed
* and false if not.
*
* @property mouseIsPressed
*
* @example
*
*
* function draw() {
* background(237, 34, 93);
* fill(0);
*
* if (mouseIsPressed)
* ellipse(50, 50, 50, 50);
* else
* rect(25, 25, 50, 50);
*
* print(mouseIsPressed);
* }
*
*
*/
p5.prototype.mouseIsPressed = false;
p5.prototype.isMousePressed = false; // both are supported
p5.prototype._updateMouseCoords = function(e) {
if(e.type === 'touchstart' ||
e.type === 'touchmove' ||
e.type === 'touchend') {
this._updatePTouchCoords();
this._setProperty('mouseX', this.touchX);
this._setProperty('mouseY', this.touchY);
} else {
if(this._curElement !== null) {
this._updatePMouseCoords();
var mousePos = getMousePos(this._curElement.elt, e);
this._setProperty('mouseX', mousePos.x);
this._setProperty('mouseY', mousePos.y);
}
}
this._setProperty('winMouseX', e.pageX);
this._setProperty('winMouseY', e.pageY);
};
p5.prototype._updatePMouseCoords = function() {
this._setProperty('pmouseX', this.mouseX);
this._setProperty('pmouseY', this.mouseY);
this._setProperty('pwinMouseX', this.winMouseX);
this._setProperty('pwinMouseY', this.winMouseY);
};
function getMousePos(canvas, evt) {
var rect = canvas.getBoundingClientRect();
return {
x: evt.clientX - rect.left,
y: evt.clientY - rect.top
};
}
p5.prototype._setMouseButton = function(e) {
if (e.button === 1) {
this._setProperty('mouseButton', constants.CENTER);
} else if (e.button === 2) {
this._setProperty('mouseButton', constants.RIGHT);
} else {
this._setProperty('mouseButton', constants.LEFT);
if(e.type === 'touchstart' || e.type === 'touchmove') {
this._setProperty('mouseX', this.touchX);
this._setProperty('mouseY', this.touchY);
}
}
};
/**
* The mouseMoved() function is called every time the mouse moves and a mouse
* button is not pressed.
* Browsers may have different default
* behaviors attached to various mouse events. To prevent any default
* behavior for this event, add `return false` to the end of the method.
*
* @method mouseMoved
* @example
*
*
* // Move the mouse across the page
* // to change its value
*
* var value = 0;
* function draw() {
* fill(value);
* rect(25, 25, 50, 50);
* }
* function mouseMoved() {
* value = value + 5;
* if (value > 255) {
* value = 0;
* }
* }
*
*
*
*
*
* function mouseMoved() {
* ellipse(mouseX, mouseY, 5, 5);
* // prevent default
* return false;
* }
*
*
*/
/**
* The mouseDragged() function is called once every time the mouse moves and
* a mouse button is pressed. If no mouseDragged() function is defined, the
* touchMoved() function will be called instead if it is defined.
* Browsers may have different default
* behaviors attached to various mouse events. To prevent any default
* behavior for this event, add `return false` to the end of the method.
*
* @method mouseDragged
* @example
*
*
* // Drag the mouse across the page
* // to change its value
*
* var value = 0;
* function draw() {
* fill(value);
* rect(25, 25, 50, 50);
* }
* function mouseDragged() {
* value = value + 5;
* if (value > 255) {
* value = 0;
* }
* }
*
*
*
*
*
* function mouseDragged() {
* ellipse(mouseX, mouseY, 5, 5);
* // prevent default
* return false;
* }
*
*
*/
p5.prototype._onmousemove = function(e){
var context = this._isGlobal ? window : this;
var executeDefault;
this._updateMouseCoords(e);
if (!this.isMousePressed) {
if (typeof context.mouseMoved === 'function') {
executeDefault = context.mouseMoved(e);
if(executeDefault === false) {
e.preventDefault();
}
}
}
else {
if (typeof context.mouseDragged === 'function') {
executeDefault = context.mouseDragged(e);
if(executeDefault === false) {
e.preventDefault();
}
} else if (typeof context.touchMoved === 'function') {
executeDefault = context.touchMoved(e);
if(executeDefault === false) {
e.preventDefault();
}
this._updateTouchCoords(e);
}
}
};
/**
* The mousePressed() function is called once after every time a mouse button
* is pressed. The mouseButton variable (see the related reference entry)
* can be used to determine which button has been pressed. If no
* mousePressed() function is defined, the touchStarted() function will be
* called instead if it is defined.
* Browsers may have different default
* behaviors attached to various mouse events. To prevent any default
* behavior for this event, add `return false` to the end of the method.
*
* @method mousePressed
* @example
*
*
* // Click within the image to change
* // the value of the rectangle
*
* var value = 0;
* function draw() {
* fill(value);
* rect(25, 25, 50, 50);
* }
* function mousePressed() {
* if (value == 0) {
* value = 255;
* } else {
* value = 0;
* }
* }
*
*
*
*
*
* function mousePressed() {
* ellipse(mouseX, mouseY, 5, 5);
* // prevent default
* return false;
* }
*
*
*/
p5.prototype._onmousedown = function(e) {
var context = this._isGlobal ? window : this;
var executeDefault;
this._setProperty('isMousePressed', true);
this._setProperty('mouseIsPressed', true);
this._setMouseButton(e);
this._updateMouseCoords(e);
if (typeof context.mousePressed === 'function') {
executeDefault = context.mousePressed(e);
if(executeDefault === false) {
e.preventDefault();
}
} else if (typeof context.touchStarted === 'function') {
executeDefault = context.touchStarted(e);
if(executeDefault === false) {
e.preventDefault();
}
this._updateTouchCoords(e);
}
};
/**
* The mouseReleased() function is called every time a mouse button is
* released. If no mouseReleased() function is defined, the touchEnded()
* function will be called instead if it is defined.
* Browsers may have different default
* behaviors attached to various mouse events. To prevent any default
* behavior for this event, add `return false` to the end of the method.
*
*
* @method mouseReleased
* @example
*
*
* // Click within the image to change
* // the value of the rectangle
* // after the mouse has been clicked
*
* var value = 0;
* function draw() {
* fill(value);
* rect(25, 25, 50, 50);
* }
* function mouseReleased() {
* if (value == 0) {
* value = 255;
* } else {
* value = 0;
* }
* }
*
*
*
*
*
* function mouseReleased() {
* ellipse(mouseX, mouseY, 5, 5);
* // prevent default
* return false;
* }
*
*
*/
p5.prototype._onmouseup = function(e) {
var context = this._isGlobal ? window : this;
var executeDefault;
this._setProperty('isMousePressed', false);
this._setProperty('mouseIsPressed', false);
if (typeof context.mouseReleased === 'function') {
executeDefault = context.mouseReleased(e);
if(executeDefault === false) {
e.preventDefault();
}
} else if (typeof context.touchEnded === 'function') {
executeDefault = context.touchEnded(e);
if(executeDefault === false) {
e.preventDefault();
}
this._updateTouchCoords(e);
}
};
p5.prototype._ondragend = p5.prototype._onmouseup;
p5.prototype._ondragover = p5.prototype._onmousemove;
/**
* The mouseClicked() function is called once after a mouse button has been
* pressed and then released.
* Browsers may have different default
* behaviors attached to various mouse events. To prevent any default
* behavior for this event, add `return false` to the end of the method.
*
* @method mouseClicked
* @example
*
*
* // Click within the image to change
* // the value of the rectangle
* // after the mouse has been clicked
*
* var value = 0;
* function draw() {
* fill(value);
* rect(25, 25, 50, 50);
* }
* function mouseClicked() {
* if (value == 0) {
* value = 255;
* } else {
* value = 0;
* }
* }
*
*
*
*
*
* function mouseClicked() {
* ellipse(mouseX, mouseY, 5, 5);
* // prevent default
* return false;
* }
*
*
*/
p5.prototype._onclick = function(e) {
var context = this._isGlobal ? window : this;
if (typeof context.mouseClicked === 'function') {
var executeDefault = context.mouseClicked(e);
if(executeDefault === false) {
e.preventDefault();
}
}
};
/**
* The function mouseWheel is executed every time a scroll event is detected
* either triggered by an actual mouse wheel or by a touchpad.
* The event.delta property returns -1 or +1 depending on the scroll
* direction and the user's settings. (on OS X with "natural" scrolling
* enabled, the values are inverted).
* Browsers may have different default behaviors attached to various
* mouse events. To prevent any default behavior for this event, add
* `return false` to the end of the method.
*
* The event.wheelDelta or event.detail properties can also be accessed but
* their behavior may differ depending on the browser.
* See
* mouse wheel event in JS.
*
* @method mouseWheel
*
* @example
*
*
* var pos = 25;
*
* function draw() {
* background(237, 34, 93);
* fill(0);
* rect(25, pos, 50, 50);
* }
*
* function mouseWheel(event) {
* //event.delta can be +1 or -1 depending
* //on the wheel/scroll direction
* print(event.delta);
* //move the square one pixel up or down
* pos += event.delta;
* //uncomment to block page scrolling
* //return false;
* }
*
*
*/
p5.prototype._onmousewheel = p5.prototype._onDOMMouseScroll = function(e) {
var context = this._isGlobal ? window : this;
if (typeof context.mouseWheel === 'function') {
//creating a delta property (either +1 or -1)
//for cross-browser compatibility
e.delta = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail)));
var executeDefault = context.mouseWheel(e);
if(executeDefault === false) {
e.preventDefault();
}
}
};
module.exports = p5;
},{"../core/constants":47,"../core/core":48}],64:[function(_dereq_,module,exports){
/**
* @module Events
* @submodule Touch
* @for p5
* @requires core
*/
'use strict';
var p5 = _dereq_('../core/core');
/**
* The system variable touchX always contains the horizontal position of
* one finger, relative to (0, 0) of the canvas. This is best used for
* single touch interactions. For multi-touch interactions, use the
* touches[] array.
*
* @property touchX
*/
p5.prototype.touchX = 0;
/**
* The system variable touchY always contains the vertical position of
* one finger, relative to (0, 0) of the canvas. This is best used for
* single touch interactions. For multi-touch interactions, use the
* touches[] array.
*
* @property touchY
*/
p5.prototype.touchY = 0;
/**
* The system variable ptouchX always contains the horizontal position of
* one finger, relative to (0, 0) of the canvas, in the frame previous to the
* current frame.
*
* @property ptouchX
*/
p5.prototype.ptouchX = 0;
/**
* The system variable ptouchY always contains the vertical position of
* one finger, relative to (0, 0) of the canvas, in the frame previous to the
* current frame.
*
* @property ptouchY
*/
p5.prototype.ptouchY = 0;
/**
* The system variable touches[] contains an array of the positions of all
* current touch points, relative to (0, 0) of the canvas, and IDs identifying a
* unique touch as it moves. Each element in the array is an object with x, y,
* and id properties.
*
* @property touches[]
*/
p5.prototype.touches = [];
/**
* The boolean system variable touchIsDown is true if the screen is
* touched and false if not.
*
* @property touchIsDown
*/
p5.prototype.touchIsDown = false;
p5.prototype._updateTouchCoords = function(e) {
if(e.type === 'mousedown' ||
e.type === 'mousemove' ||
e.type === 'mouseup'){
this._updatePMouseCoords();
this._setProperty('touchX', this.mouseX);
this._setProperty('touchY', this.mouseY);
} else {
this._updatePTouchCoords();
var touchInfo = getTouchInfo(this._curElement.elt, e, 0);
this._setProperty('touchX', touchInfo.x);
this._setProperty('touchY', touchInfo.y);
var touches = [];
for(var i = 0; i < e.touches.length; i++){
touches[i] = getTouchInfo(this._curElement.elt, e, i);
}
this._setProperty('touches', touches);
}
};
p5.prototype._updatePTouchCoords = function() {
this._setProperty('ptouchX', this.touchX);
this._setProperty('ptouchY', this.touchY);
};
function getTouchInfo(canvas, e, i) {
i = i || 0;
var rect = canvas.getBoundingClientRect();
var touch = e.touches[i] || e.changedTouches[i];
return {
x: touch.clientX - rect.left,
y: touch.clientY - rect.top,
id: touch.identifier
};
}
/**
* The touchStarted() function is called once after every time a touch is
* registered. If no touchStarted() function is defined, the mousePressed()
* function will be called instead if it is defined. Browsers may have
* different default
* behaviors attached to various touch events. To prevent any default
* behavior for this event, add `return false` to the end of the method.
*
* @method touchStarted
* @example
*
*
* // Touch within the image to change
* // the value of the rectangle
*
* var value = 0;
* function draw() {
* fill(value);
* rect(25, 25, 50, 50);
* }
* function touchStarted() {
* if (value == 0) {
* value = 255;
* } else {
* value = 0;
* }
* }
*
*
*
*
*
* function touchStarted() {
* ellipse(touchX, touchY, 5, 5);
* // prevent default
* return false;
* }
*
*
*/
p5.prototype._ontouchstart = function(e) {
var context = this._isGlobal ? window : this;
var executeDefault;
this._updateTouchCoords(e);
this._setProperty('touchIsDown', true);
if(typeof context.touchStarted === 'function') {
executeDefault = context.touchStarted(e);
if(executeDefault === false) {
e.preventDefault();
}
} else if (typeof context.mousePressed === 'function') {
executeDefault = context.mousePressed(e);
if(executeDefault === false) {
e.preventDefault();
}
//this._setMouseButton(e);
}
};
/**
* The touchMoved() function is called every time a touch move is registered.
* If no touchStarted() function is defined, the mouseDragged() function will
* be called instead if it is defined. Browsers may have different default
* behaviors attached to various touch events. To prevent any default
* behavior for this event, add `return false` to the end of the method.
*
* @method touchMoved
* @example
*
*
* // Move your finger across the page
* // to change its value
*
* var value = 0;
* function draw() {
* fill(value);
* rect(25, 25, 50, 50);
* }
* function touchMoved() {
* value = value + 5;
* if (value > 255) {
* value = 0;
* }
* }
*
*
*
*
*
* function touchMoved() {
* ellipse(touchX, touchY, 5, 5);
* // prevent default
* return false;
* }
*
*
*/
p5.prototype._ontouchmove = function(e) {
var context = this._isGlobal ? window : this;
var executeDefault;
this._updateTouchCoords(e);
if (typeof context.touchMoved === 'function') {
executeDefault = context.touchMoved(e);
if(executeDefault === false) {
e.preventDefault();
}
} else if (typeof context.mouseDragged === 'function') {
executeDefault = context.mouseDragged(e);
if(executeDefault === false) {
e.preventDefault();
}
this._updateMouseCoords(e);
}
};
/**
* The touchEnded() function is called every time a touch ends. If no
* touchStarted() function is defined, the mouseReleased() function will be
* called instead if it is defined. Browsers may have different default
* behaviors attached to various touch events. To prevent any default
* behavior for this event, add `return false` to the end of the method.
*
* @method touchEnded
* @example
*
*
* // Release touch within the image to
* // change the value of the rectangle
*
* var value = 0;
* function draw() {
* fill(value);
* rect(25, 25, 50, 50);
* }
* function touchEnded() {
* if (value == 0) {
* value = 255;
* } else {
* value = 0;
* }
* }
*
*
*
*
*
* function touchEnded() {
* ellipse(touchX, touchY, 5, 5);
* // prevent default
* return false;
* }
*
*
*/
p5.prototype._ontouchend = function(e) {
this._updateTouchCoords(e);
if (this.touches.length === 0) {
this._setProperty('touchIsDown', false);
}
var context = this._isGlobal ? window : this;
var executeDefault;
if (typeof context.touchEnded === 'function') {
executeDefault = context.touchEnded(e);
if(executeDefault === false) {
e.preventDefault();
}
} else if (typeof context.mouseReleased === 'function') {
executeDefault = context.mouseReleased(e);
if(executeDefault === false) {
e.preventDefault();
}
this._updateMouseCoords(e);
}
};
module.exports = p5;
},{"../core/core":48}],65:[function(_dereq_,module,exports){
/*global ImageData:false */
/**
* This module defines the filters for use with image buffers.
*
* This module is basically a collection of functions stored in an object
* as opposed to modules. The functions are destructive, modifying
* the passed in canvas rather than creating a copy.
*
* Generally speaking users of this module will use the Filters.apply method
* on a canvas to create an effect.
*
* A number of functions are borrowed/adapted from
* http://www.html5rocks.com/en/tutorials/canvas/imagefilters/
* or the java processing implementation.
*/
'use strict';
var Filters = {};
/*
* Helper functions
*/
/**
* Returns the pixel buffer for a canvas
*
* @private
*
* @param {Canvas|ImageData} canvas the canvas to get pixels from
* @return {Uint8ClampedArray} a one-dimensional array containing
* the data in thc RGBA order, with integer
* values between 0 and 255
*/
Filters._toPixels = function (canvas) {
if (canvas instanceof ImageData) {
return canvas.data;
} else {
return canvas.getContext('2d').getImageData(
0,
0,
canvas.width,
canvas.height
).data;
}
};
/**
* Returns a 32 bit number containing ARGB data at 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.
*/
Filters._getARGB = function (data, i) {
var offset = i * 4;
return (data[offset+3] << 24) & 0xff000000 |
(data[offset] << 16) & 0x00ff0000 |
(data[offset+1] << 8) & 0x0000ff00 |
data[offset+2] & 0x000000ff;
};
/**
* 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
*/
Filters._setPixels = function (pixels, data) {
var offset = 0;
for( var i = 0, al = pixels.length; i < al; i++) {
offset = i*4;
pixels[offset + 0] = (data[i] & 0x00ff0000)>>>16;
pixels[offset + 1] = (data[i] & 0x0000ff00)>>>8;
pixels[offset + 2] = (data[i] & 0x000000ff);
pixels[offset + 3] = (data[i] & 0xff000000)>>>24;
}
};
/**
* 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
*/
Filters._toImageData = function (canvas) {
if (canvas instanceof ImageData) {
return canvas;
} else {
return canvas.getContext('2d').getImageData(
0,
0,
canvas.width,
canvas.height
);
}
};
/**
* Returns a blank ImageData object.
*
* @private
*
* @param {Integer} width
* @param {Integer} height
* @return {ImageData}
*/
Filters._createImageData = function (width, height) {
Filters._tmpCanvas = document.createElement('canvas');
Filters._tmpCtx = Filters._tmpCanvas.getContext('2d');
return this._tmpCtx.createImageData(width, height);
};
/**
* 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.
*
* @param {[type]} func [description]
* @param {[type]} canvas [description]
* @param {[type]} level [description]
* @return {[type]} [description]
*/
Filters.apply = function (canvas, func, filterParam) {
var ctx = canvas.getContext('2d');
var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
//Filters can either return a new ImageData object, or just modify
//the one they received.
var newImageData = func(imageData, filterParam);
if (newImageData instanceof ImageData) {
ctx.putImageData(newImageData, 0, 0, 0, 0, canvas.width, canvas.height);
} else {
ctx.putImageData(imageData, 0, 0, 0, 0, canvas.width, canvas.height);
}
};
/*
* 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/
*
* @param {Canvas} canvas
* @param {Float} level
*/
Filters.threshold = function (canvas, level) {
var pixels = Filters._toPixels(canvas);
if (level === undefined) {
level = 0.5;
}
var thresh = Math.floor(level * 255);
for (var i = 0; i < pixels.length; i += 4) {
var r = pixels[i];
var g = pixels[i + 1];
var b = pixels[i + 2];
var gray = (0.2126 * r + 0.7152 * g + 0.0722 * b);
var val;
if (gray >= thresh) {
val = 255;
} else {
val = 0;
}
pixels[i] = pixels[i + 1] = pixels[i + 2] = val;
}
};
/**
* Converts any colors in the image to grayscale equivalents.
* No parameter is used.
*
* Borrowed from http://www.html5rocks.com/en/tutorials/canvas/imagefilters/
*
* @param {Canvas} canvas
*/
Filters.gray = function (canvas) {
var pixels = Filters._toPixels(canvas);
for (var i = 0; i < pixels.length; i += 4) {
var r = pixels[i];
var g = pixels[i + 1];
var b = pixels[i + 2];
// CIE luminance for RGB
var gray = (0.2126 * r + 0.7152 * g + 0.0722 * b);
pixels[i] = pixels[i + 1] = pixels[i + 2] = gray;
}
};
/**
* Sets the alpha channel to entirely opaque. No parameter is used.
*
* @param {Canvas} canvas
*/
Filters.opaque = function (canvas) {
var pixels = Filters._toPixels(canvas);
for (var i = 0; i < pixels.length; i += 4) {
pixels[i + 3] = 255;
}
return pixels;
};
/**
* Sets each pixel to its inverse value. No parameter is used.
* @param {Invert}
*/
Filters.invert = function (canvas) {
var pixels = Filters._toPixels(canvas);
for (var i = 0; i < pixels.length; i += 4) {
pixels[i] = 255 - pixels[i];
pixels[i + 1] = 255 - pixels[i + 1];
pixels[i + 2] = 255 - pixels[i + 2];
}
};
/**
* 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
*
* @param {Canvas} canvas
* @param {Integer} level
*/
Filters.posterize = function (canvas, level) {
var pixels = Filters._toPixels(canvas);
if ((level < 2) || (level > 255)) {
throw new Error(
'Level must be greater than 2 and less than 255 for posterize'
);
}
var levels1 = level - 1;
for (var i = 0; i < pixels.length; i+=4) {
var rlevel = pixels[i];
var glevel = pixels[i + 1];
var blevel = pixels[i + 2];
pixels[i] = (((rlevel * level) >> 8) * 255) / levels1;
pixels[i + 1] = (((glevel * level) >> 8) * 255) / levels1;
pixels[i + 2] = (((blevel * level) >> 8) * 255) / levels1;
}
};
/**
* reduces the bright areas in an image
* @param {Canvas} canvas
*
*/
Filters.dilate = function (canvas) {
var pixels = Filters._toPixels(canvas);
var currIdx = 0;
var maxIdx = pixels.length ? pixels.length/4 : 0;
var out = new Int32Array(maxIdx);
var currRowIdx, maxRowIdx, colOrig, colOut, currLum;
var idxRight, idxLeft, idxUp, idxDown,
colRight, colLeft, colUp, colDown,
lumRight, lumLeft, lumUp, lumDown;
while(currIdx < maxIdx) {
currRowIdx = currIdx;
maxRowIdx = currIdx + canvas.width;
while (currIdx < maxRowIdx) {
colOrig = colOut = Filters._getARGB(pixels, currIdx);
idxLeft = currIdx - 1;
idxRight = currIdx + 1;
idxUp = currIdx - canvas.width;
idxDown = currIdx + canvas.width;
if (idxLeft < currRowIdx) {
idxLeft = currIdx;
}
if (idxRight >= maxRowIdx) {
idxRight = currIdx;
}
if (idxUp < 0){
idxUp = 0;
}
if (idxDown >= maxIdx) {
idxDown = currIdx;
}
colUp = Filters._getARGB(pixels, idxUp);
colLeft = Filters._getARGB(pixels, idxLeft);
colDown = Filters._getARGB(pixels, idxDown);
colRight = Filters._getARGB(pixels, idxRight);
//compute luminance
currLum = 77*(colOrig>>16&0xff) +
151*(colOrig>>8&0xff) +
28*(colOrig&0xff);
lumLeft = 77*(colLeft>>16&0xff) +
151*(colLeft>>8&0xff) +
28*(colLeft&0xff);
lumRight = 77*(colRight>>16&0xff) +
151*(colRight>>8&0xff) +
28*(colRight&0xff);
lumUp = 77*(colUp>>16&0xff) +
151*(colUp>>8&0xff) +
28*(colUp&0xff);
lumDown = 77*(colDown>>16&0xff) +
151*(colDown>>8&0xff) +
28*(colDown&0xff);
if (lumLeft > currLum) {
colOut = colLeft;
currLum = lumLeft;
}
if (lumRight > currLum) {
colOut = colRight;
currLum = lumRight;
}
if (lumUp > currLum) {
colOut = colUp;
currLum = lumUp;
}
if (lumDown > currLum) {
colOut = colDown;
currLum = lumDown;
}
out[currIdx++]=colOut;
}
}
Filters._setPixels(pixels, out);
};
/**
* increases the bright areas in an image
* @param {Canvas} canvas
*
*/
Filters.erode = function(canvas) {
var pixels = Filters._toPixels(canvas);
var currIdx = 0;
var maxIdx = pixels.length ? pixels.length/4 : 0;
var out = new Int32Array(maxIdx);
var currRowIdx, maxRowIdx, colOrig, colOut, currLum;
var idxRight, idxLeft, idxUp, idxDown,
colRight, colLeft, colUp, colDown,
lumRight, lumLeft, lumUp, lumDown;
while(currIdx < maxIdx) {
currRowIdx = currIdx;
maxRowIdx = currIdx + canvas.width;
while (currIdx < maxRowIdx) {
colOrig = colOut = Filters._getARGB(pixels, currIdx);
idxLeft = currIdx - 1;
idxRight = currIdx + 1;
idxUp = currIdx - canvas.width;
idxDown = currIdx + canvas.width;
if (idxLeft < currRowIdx) {
idxLeft = currIdx;
}
if (idxRight >= maxRowIdx) {
idxRight = currIdx;
}
if (idxUp < 0) {
idxUp = 0;
}
if (idxDown >= maxIdx) {
idxDown = currIdx;
}
colUp = Filters._getARGB(pixels, idxUp);
colLeft = Filters._getARGB(pixels, idxLeft);
colDown = Filters._getARGB(pixels, idxDown);
colRight = Filters._getARGB(pixels, idxRight);
//compute luminance
currLum = 77*(colOrig>>16&0xff) +
151*(colOrig>>8&0xff) +
28*(colOrig&0xff);
lumLeft = 77*(colLeft>>16&0xff) +
151*(colLeft>>8&0xff) +
28*(colLeft&0xff);
lumRight = 77*(colRight>>16&0xff) +
151*(colRight>>8&0xff) +
28*(colRight&0xff);
lumUp = 77*(colUp>>16&0xff) +
151*(colUp>>8&0xff) +
28*(colUp&0xff);
lumDown = 77*(colDown>>16&0xff) +
151*(colDown>>8&0xff) +
28*(colDown&0xff);
if (lumLeft < currLum) {
colOut = colLeft;
currLum = lumLeft;
}
if (lumRight < currLum) {
colOut = colRight;
currLum = lumRight;
}
if (lumUp < currLum) {
colOut = colUp;
currLum = lumUp;
}
if (lumDown < currLum) {
colOut = colDown;
currLum = lumDown;
}
out[currIdx++]=colOut;
}
}
Filters._setPixels(pixels, out);
};
// BLUR
// internal kernel stuff for the gaussian blur filter
var blurRadius;
var blurKernelSize;
var blurKernel;
var blurMult;
/*
* Port of https://github.com/processing/processing/blob/
* master/core/src/processing/core/PImage.java#L1250
*
* Optimized code for building the blur kernel.
* further optimized blur code (approx. 15% for radius=20)
* bigger speed gains for larger radii (~30%)
* added support for various image types (ALPHA, RGB, ARGB)
* [toxi 050728]
*/
function buildBlurKernel(r) {
var radius = (r * 3.5)|0;
radius = (radius < 1) ? 1 : ((radius < 248) ? radius : 248);
if (blurRadius !== radius) {
blurRadius = radius;
blurKernelSize = 1 + blurRadius<<1;
blurKernel = new Int32Array(blurKernelSize);
blurMult = new Array(blurKernelSize);
for(var l = 0; l < blurKernelSize; l++){
blurMult[l] = new Int32Array(256);
}
var bk,bki;
var bm,bmi;
for (var i = 1, radiusi = radius - 1; i < radius; i++) {
blurKernel[radius+i] = blurKernel[radiusi] = bki = radiusi * radiusi;
bm = blurMult[radius+i];
bmi = blurMult[radiusi--];
for (var j = 0; j < 256; j++){
bm[j] = bmi[j] = bki * j;
}
}
bk = blurKernel[radius] = radius * radius;
bm = blurMult[radius];
for (var k = 0; k < 256; k++){
bm[k] = bk * k;
}
}
}
// Port of https://github.com/processing/processing/blob/
// master/core/src/processing/core/PImage.java#L1433
function blurARGB(canvas, radius) {
var pixels = Filters._toPixels(canvas);
var width = canvas.width;
var height = canvas.height;
var numPackedPixels = width * height;
var argb = new Int32Array(numPackedPixels);
for (var j = 0; j < numPackedPixels; j++) {
argb[j] = Filters._getARGB(pixels, j);
}
var sum, cr, cg, cb, ca;
var read, ri, ym, ymi, bk0;
var a2 = new Int32Array(numPackedPixels);
var r2 = new Int32Array(numPackedPixels);
var g2 = new Int32Array(numPackedPixels);
var b2 = new Int32Array(numPackedPixels);
var yi = 0;
buildBlurKernel(radius);
var x, y, i;
var bm;
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
cb = cg = cr = ca = sum = 0;
read = x - blurRadius;
if (read < 0) {
bk0 = -read;
read = 0;
} else {
if (read >= width) {
break;
}
bk0 = 0;
}
for (i = bk0; i < blurKernelSize; i++) {
if (read >= width) {
break;
}
var c = argb[read + yi];
bm = blurMult[i];
ca += bm[(c & -16777216) >>> 24];
cr += bm[(c & 16711680) >> 16];
cg += bm[(c & 65280) >> 8];
cb += bm[c & 255];
sum += blurKernel[i];
read++;
}
ri = yi + x;
a2[ri] = ca / sum;
r2[ri] = cr / sum;
g2[ri] = cg / sum;
b2[ri] = cb / sum;
}
yi += width;
}
yi = 0;
ym = -blurRadius;
ymi = ym * width;
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
cb = cg = cr = ca = sum = 0;
if (ym < 0) {
bk0 = ri = -ym;
read = x;
} else {
if (ym >= height) {
break;
}
bk0 = 0;
ri = ym;
read = x + ymi;
}
for (i = bk0; i < blurKernelSize; i++) {
if (ri >= height) {
break;
}
bm = blurMult[i];
ca += bm[a2[read]];
cr += bm[r2[read]];
cg += bm[g2[read]];
cb += bm[b2[read]];
sum += blurKernel[i];
ri++;
read += width;
}
argb[x + yi] = (ca/sum)<<24 | (cr/sum)<<16 | (cg/sum)<<8 | (cb/sum);
}
yi += width;
ymi += width;
ym++;
}
Filters._setPixels(pixels, argb);
}
Filters.blur = function(canvas, radius){
blurARGB(canvas, radius);
};
module.exports = Filters;
},{}],66:[function(_dereq_,module,exports){
/**
* @module Image
* @submodule Image
* @for p5
* @requires core
*/
/**
* This module defines the p5 methods for the p5.Image class
* for drawing images to the main display canvas.
*/
'use strict';
var p5 = _dereq_('../core/core');
/* global frames:true */// This is not global, but JSHint is not aware that
// this module is implicitly enclosed with Browserify: this overrides the
// redefined-global error and permits using the name "frames" for the array
// of saved animation frames.
var frames = [];
/**
* Creates a new p5.Image (the datatype for storing images). This provides a
* fresh buffer of pixels to play with. Set the size of the buffer with the
* width and height parameters.
*
* .pixels gives access to an array containing the values for all the pixels
* in the display window.
* These values are numbers. This array is the size (including an appropriate
* factor for the pixelDensity) of the display window x4,
* representing the R, G, B, A values in order for each pixel, moving from
* left to right across each row, then down each column. See .pixels for
* more info. It may also be simpler to use set() or get().
*
* Before accessing the pixels of an image, the data must loaded with the
* loadPixels()
* function. After the array data has been modified, the updatePixels()
* function must be run to update the changes.
*
* @method createImage
* @param {Integer} width width in pixels
* @param {Integer} height height in pixels
* @return {p5.Image} the p5.Image object
* @example
*
*
* img = createImage(66, 66);
* img.loadPixels();
* for (i = 0; i < img.width; i++) {
* for (j = 0; j < img.height; j++) {
* img.set(i, j, color(0, 90, 102));
* }
* }
* img.updatePixels();
* image(img, 17, 17);
*
*
*
*
*
* img = createImage(66, 66);
* img.loadPixels();
* for (i = 0; i < img.width; i++) {
* for (j = 0; j < img.height; j++) {
* img.set(i, j, color(0, 90, 102, i % img.width * 2));
* }
* }
* img.updatePixels();
* image(img, 17, 17);
* image(img, 34, 34);
*
*
*
*
*
* var pink = color(255, 102, 204);
* img = createImage(66, 66);
* img.loadPixels();
* var d = pixelDensity;
* var halfImage = 4 * (width * d) * (height/2 * d);
* for (var i = 0; i < halfImage; i+=4) {
* img.pixels[i] = red(pink);
* img.pixels[i+1] = green(pink);
* img.pixels[i+2] = blue(pink);
* img.pixels[i+3] = alpha(pink);
* }
* img.updatePixels();
* image(img, 17, 17);
*
*
*/
p5.prototype.createImage = function(width, height) {
return new p5.Image(width, height);
};
/**
* Save the current canvas as an image. In Safari, will open the
* image in the window and the user must provide their own
* filename on save-as. Other browsers will either save the
* file immediately, or prompt the user with a dialogue window.
*
* @method saveCanvas
* @param {[selectedCanvas]} canvas a variable representing a
* specific html5 canvas (optional)
* @param {[String]} filename
* @param {[String]} extension 'jpg' or 'png'
* @example
*
* function setup() {
* var c = createCanvas(100, 100);
* background(255, 0, 0);
* saveCanvas(c, 'myCanvas', 'jpg');
* }
*
*
* // note that this example has the same result as above
* // if no canvas is specified, defaults to main canvas
* function setup() {
* createCanvas(100, 100);
* background(255, 0, 0);
* saveCanvas('myCanvas', 'jpg');
* }
*
*
* // all of the following are valid
* saveCanvas(c, 'myCanvas', 'jpg');
* saveCanvas(c, 'myCanvas');
* saveCanvas(c);
* saveCanvas('myCanvas', 'png');
* saveCanvas('myCanvas');
* saveCanvas();
*
*/
p5.prototype.saveCanvas = function() {
var cnv, filename, extension;
if (arguments.length === 3) {
cnv = arguments[0];
filename = arguments[1];
extension = arguments[2];
} else if (arguments.length === 2) {
if (typeof arguments[0] === 'object') {
cnv = arguments[0];
filename = arguments[1];
} else {
filename = arguments[0];
extension = arguments[1];
}
} else if (arguments.length === 1) {
if (typeof arguments[0] === 'object') {
cnv = arguments[0];
} else {
filename = arguments[0];
}
}
if (cnv instanceof p5.Element) {
cnv = cnv.elt;
}
if (!(cnv instanceof HTMLCanvasElement)) {
cnv = null;
}
if (!extension) {
extension = p5.prototype._checkFileExtension(filename, extension)[1];
if (extension === '') {
extension = 'png';
}
}
if (!cnv) {
if (this._curElement && this._curElement.elt) {
cnv = this._curElement.elt;
}
}
if ( p5.prototype._isSafari() ) {
var aText = 'Hello, Safari user!\n';
aText += 'Now capturing a screenshot...\n';
aText += 'To save this image,\n';
aText += 'go to File --> Save As.\n';
alert(aText);
window.location.href = cnv.toDataURL();
} else {
var mimeType;
if (typeof(extension) === 'undefined') {
extension = 'png';
mimeType = 'image/png';
}
else {
switch(extension){
case 'png':
mimeType = 'image/png';
break;
case 'jpeg':
mimeType = 'image/jpeg';
break;
case 'jpg':
mimeType = 'image/jpeg';
break;
default:
mimeType = 'image/png';
break;
}
}
var downloadMime = 'image/octet-stream';
var imageData = cnv.toDataURL(mimeType);
imageData = imageData.replace(mimeType, downloadMime);
p5.prototype.downloadFile(imageData, filename, extension);
}
};
/**
* Capture a sequence of frames that can be used to create a movie.
* Accepts a callback. For example, you may wish to send the frames
* to a server where they can be stored or converted into a movie.
* If no callback is provided, the browser will attempt to download
* all of the images that have just been created.
*
* @method saveFrames
* @param {[type]} filename [description]
* @param {[type]} extension [description]
* @param {[type]} _duration [description]
* @param {[type]} _fps [description]
* @param {[Function]} callback [description]
* @return {[type]} [description]
*/
p5.prototype.saveFrames = function(fName, ext, _duration, _fps, callback) {
var duration = _duration || 3;
duration = p5.prototype.constrain(duration, 0, 15);
duration = duration * 1000;
var fps = _fps || 15;
fps = p5.prototype.constrain(fps, 0, 22);
var count = 0;
var makeFrame = p5.prototype._makeFrame;
var cnv = this._curElement.elt;
var frameFactory = setInterval(function(){
makeFrame(fName + count, ext, cnv);
count++;
},1000/fps);
setTimeout(function(){
clearInterval(frameFactory);
if (callback) {
callback(frames);
}
else {
for (var i = 0; i < frames.length; i++) {
var f = frames[i];
p5.prototype.downloadFile(f.imageData, f.filename, f.ext);
}
}
frames = []; // clear frames
}, duration + 0.01);
};
p5.prototype._makeFrame = function(filename, extension, _cnv) {
var cnv;
if (this) {
cnv = this._curElement.elt;
} else {
cnv = _cnv;
}
var mimeType;
if (!extension) {
extension = 'png';
mimeType = 'image/png';
}
else {
switch(extension.toLowerCase()){
case 'png':
mimeType = 'image/png';
break;
case 'jpeg':
mimeType = 'image/jpeg';
break;
case 'jpg':
mimeType = 'image/jpeg';
break;
default:
mimeType = 'image/png';
break;
}
}
var downloadMime = 'image/octet-stream';
var imageData = cnv.toDataURL(mimeType);
imageData = imageData.replace(mimeType, downloadMime);
var thisFrame = {};
thisFrame.imageData = imageData;
thisFrame.filename = filename;
thisFrame.ext = extension;
frames.push(thisFrame);
};
module.exports = p5;
},{"../core/core":48}],67:[function(_dereq_,module,exports){
/**
* @module Image
* @submodule Loading & Displaying
* @for p5
* @requires core
*/
'use strict';
var p5 = _dereq_('../core/core');
var Filters = _dereq_('./filters');
var canvas = _dereq_('../core/canvas');
var constants = _dereq_('../core/constants');
_dereq_('../core/error_helpers');
/**
* Loads an image from a path and creates a p5.Image from it.
*
* The image may not be immediately available for rendering
* If you want to ensure that the image is ready before doing
* anything with it, place the loadImage() call in preload().
* You may also supply a callback function to handle the image when it's ready.
*
* The path to the image should be relative to the HTML file
* that links in your sketch. Loading an from a URL or other
* remote location may be blocked due to your browser's built-in
* security.
*
* @method loadImage
* @param {String} path Path of the image to be loaded
* @param {Function(p5.Image)} [successCallback] Function to be called once
* the image is loaded. Will be passed the
* p5.Image.
* @param {Function(Event)} [failureCallback] called with event error if
* the image fails to load.
* @return {p5.Image} the p5.Image object
* @example
*
*
* var img;
* function preload() {
* img = loadImage("assets/laDefense.jpg");
* }
* function setup() {
* image(img, 0, 0);
* }
*
*
*
*
* function setup() {
* // here we use a callback to display the image after loading
* loadImage("assets/laDefense.jpg", function(img) {
* image(img, 0, 0);
* });
* }
*
*
*/
p5.prototype.loadImage = function(path, successCallback, failureCallback) {
var img = new Image();
var pImg = new p5.Image(1, 1, this);
var decrementPreload = p5._getDecrementPreload.apply(this, arguments);
img.onload = function() {
pImg.width = pImg.canvas.width = img.width;
pImg.height = pImg.canvas.height = img.height;
// Draw the image into the backing canvas of the p5.Image
pImg.drawingContext.drawImage(img, 0, 0);
if (typeof successCallback === 'function') {
successCallback(pImg);
}
if (decrementPreload && (successCallback !== decrementPreload)) {
decrementPreload();
}
};
img.onerror = function(e) {
p5._friendlyFileLoadError(0,img.src);
// don't get failure callback mixed up with decrementPreload
if ((typeof failureCallback === 'function') &&
(failureCallback !== decrementPreload)) {
failureCallback(e);
}
};
//set crossOrigin in case image is served which CORS headers
//this will let us draw to canvas without tainting it.
//see https://developer.mozilla.org/en-US/docs/HTML/CORS_Enabled_Image
// When using data-uris the file will be loaded locally
// so we don't need to worry about crossOrigin with base64 file types
if(path.indexOf('data:image/') !== 0) {
img.crossOrigin = 'Anonymous';
}
//start loading the image
img.src = path;
return pImg;
};
/**
* Validates clipping params. Per drawImage spec sWidth and sHight cannot be
* negative or greater than image intrinsic width and height
* @private
* @param {Number} sVal
* @param {Number} iVal
* @returns {Number}
* @private
*/
function _sAssign(sVal, iVal) {
if (sVal > 0 && sVal < iVal) {
return sVal;
}
else {
return iVal;
}
}
/**
* Draw an image to the main canvas of the p5js sketch
*
* @method image
* @param {p5.Image} img the image to display
* @param {Number} [sx=0] The X coordinate of the top left corner of the
* sub-rectangle of the source image to draw into
* the destination canvas.
* @param {Number} [sy=0] The Y coordinate of the top left corner of the
* sub-rectangle of the source image to draw into
* the destination canvas.
* @param {Number} [sWidth=img.width] The width of the sub-rectangle of the
* source image to draw into the destination
* canvas.
* @param {Number} [sHeight=img.height] The height of the sub-rectangle of the
* source image to draw into the
* destination context.
* @param {Number} [dx=0] The X coordinate in the destination canvas at
* which to place the top-left corner of the
* source image.
* @param {Number} [dy=0] The Y coordinate in the destination canvas at
* which to place the top-left corner of the
* source image.
* @param {Number} [dWidth] The width to draw the image in the destination
* canvas. This allows scaling of the drawn image.
* @param {Number} [dHeight] The height to draw the image in the destination
* canvas. This allows scaling of the drawn image.
* @example
*
*
* var img;
* function preload() {
* img = loadImage("assets/laDefense.jpg");
* }
* function setup() {
* image(img, 0, 0);
* image(img, 0, 0, 100, 100);
* image(img, 0, 0, 100, 100, 0, 0, 100, 100);
* }
*
*
*
*
* function setup() {
* // here we use a callback to display the image after loading
* loadImage("assets/laDefense.jpg", function(img) {
* image(img, 0, 0);
* });
* }
*
*
*/
p5.prototype.image =
function(img, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight) {
// Temporarily disabling until options for p5.Graphics are added.
// var args = new Array(arguments.length);
// for (var i = 0; i < args.length; ++i) {
// args[i] = arguments[i];
// }
// this._validateParameters(
// 'image',
// args,
// [
// ['p5.Image', 'Number', 'Number'],
// ['p5.Image', 'Number', 'Number', 'Number', 'Number']
// ]
// );
// set defaults per spec: https://goo.gl/3ykfOq
if (arguments.length <= 5) {
dx = sx || 0;
dy = sy || 0;
sx = 0;
sy = 0;
if (img.elt && img.elt.videoWidth && !img.canvas) { // video no canvas
var actualW = img.elt.videoWidth;
var actualH = img.elt.videoHeight;
dWidth = sWidth || img.width;
dHeight = sHeight || img.width*actualH/actualW;
sWidth = actualW;
sHeight = actualH;
} else {
dWidth = sWidth || img.width;
dHeight = sHeight || img.height;
sWidth = img.width;
sHeight = img.height;
}
} else if (arguments.length === 9) {
sx = sx || 0;
sy = sy || 0;
sWidth = _sAssign(sWidth, img.width);
sHeight = _sAssign(sHeight, img.height);
dx = dx || 0;
dy = dy || 0;
dWidth = dWidth || img.width;
dHeight = dHeight || img.height;
} else {
throw 'Wrong number of arguments to image()';
}
var vals = canvas.modeAdjust(dx, dy, dWidth, dHeight,
this._renderer._imageMode);
// tint the image if there is a tint
this._renderer.image(img, sx, sy, sWidth, sHeight, vals.x, vals.y, vals.w,
vals.h);
};
/**
* Sets the fill value for displaying images. Images can be tinted to
* specified colors or made transparent by including an alpha value.
*
* To apply transparency to an image without affecting its color, use
* white as the tint color and specify an alpha value. For instance,
* tint(255, 128) will make an image 50% transparent (assuming the default
* alpha range of 0-255, which can be changed with colorMode()).
*
* The value for the gray parameter must be less than or equal to the current
* maximum value as specified by colorMode(). The default maximum value is
* 255.
*
* @method tint
* @param {Number|Array} v1 gray value, red or hue value (depending on the
* current color mode), or color Array
* @param {Number|Array} [v2] green or saturation value (depending on the
* current color mode)
* @param {Number|Array} [v3] blue or brightness value (depending on the
* current color mode)
* @param {Number|Array} [a] opacity of the background
* @example
*
*
* var img;
* function preload() {
* img = loadImage("assets/laDefense.jpg");
* }
* function setup() {
* image(img, 0, 0);
* tint(0, 153, 204); // Tint blue
* image(img, 50, 0);
* }
*
*
*
*
*
* var img;
* function preload() {
* img = loadImage("assets/laDefense.jpg");
* }
* function setup() {
* image(img, 0, 0);
* tint(0, 153, 204, 126); // Tint blue and set transparency
* image(img, 50, 0);
* }
*
*
*
*
*
* var img;
* function preload() {
* img = loadImage("assets/laDefense.jpg");
* }
* function setup() {
* image(img, 0, 0);
* tint(255, 126); // Apply transparency without changing color
* image(img, 50, 0);
* }
*
*
*/
p5.prototype.tint = function () {
var c = this.color.apply(this, arguments);
this._renderer._tint = c.levels;
};
/**
* Removes the current fill value for displaying images and reverts to
* displaying images with their original hues.
*
* @method noTint
* @example
*
*
* var img;
* function preload() {
* img = loadImage("assets/bricks.jpg");
* }
* function setup() {
* tint(0, 153, 204); // Tint blue
* image(img, 0, 0);
* noTint(); // Disable tint
* image(img, 50, 0);
* }
*
*
*/
p5.prototype.noTint = function() {
this._renderer._tint = null;
};
/**
* Apply the current tint color to the input image, return the resulting
* canvas.
*
* @param {p5.Image} The image to be tinted
* @return {canvas} The resulting tinted canvas
*
*/
p5.prototype._getTintedImageCanvas = function(img) {
if (!img.canvas) {
return img;
}
var pixels = Filters._toPixels(img.canvas);
var tmpCanvas = document.createElement('canvas');
tmpCanvas.width = img.canvas.width;
tmpCanvas.height = img.canvas.height;
var tmpCtx = tmpCanvas.getContext('2d');
var id = tmpCtx.createImageData(img.canvas.width, img.canvas.height);
var newPixels = id.data;
for(var i = 0; i < pixels.length; i += 4) {
var r = pixels[i];
var g = pixels[i+1];
var b = pixels[i+2];
var a = pixels[i+3];
newPixels[i] = r*this._renderer._tint[0]/255;
newPixels[i+1] = g*this._renderer._tint[1]/255;
newPixels[i+2] = b*this._renderer._tint[2]/255;
newPixels[i+3] = a*this._renderer._tint[3]/255;
}
tmpCtx.putImageData(id, 0, 0);
return tmpCanvas;
};
/**
* Set image mode. Modifies the location from which images are drawn by
* changing the way in which parameters given to image() are interpreted.
* The default mode is imageMode(CORNER), which interprets the second and
* third parameters of image() as the upper-left corner of the image. If
* two additional parameters are specified, they are used to set the image's
* width and height.
*
* imageMode(CORNERS) interprets the second and third parameters of image()
* as the location of one corner, and the fourth and fifth parameters as the
* opposite corner.
* imageMode(CENTER) interprets the second and third parameters of image()
* as the image's center point. If two additional parameters are specified,
* they are used to set the image's width and height.
*
* @method imageMode
* @param {String} m The mode: either CORNER, CORNERS, or CENTER.
* @example
*
*
*
* var img;
* function preload() {
* img = loadImage("assets/bricks.jpg");
* }
* function setup() {
* imageMode(CORNER);
* image(img, 10, 10, 50, 50);
* }
*
*
*
*
*
* var img;
* function preload() {
* img = loadImage("assets/bricks.jpg");
* }
* function setup() {
* imageMode(CORNERS);
* image(img, 10, 10, 90, 40);
* }
*
*
*
*
*
* var img;
* function preload() {
* img = loadImage("assets/bricks.jpg");
* }
* function setup() {
* imageMode(CENTER);
* image(img, 50, 50, 80, 80);
* }
*
*
*/
p5.prototype.imageMode = function(m) {
if (m === constants.CORNER ||
m === constants.CORNERS ||
m === constants.CENTER) {
this._renderer._imageMode = m;
}
};
module.exports = p5;
},{"../core/canvas":46,"../core/constants":47,"../core/core":48,"../core/error_helpers":51,"./filters":65}],68:[function(_dereq_,module,exports){
/**
* @module Image
* @submodule Image
* @requires core
* @requires constants
* @requires filters
*/
/**
* This module defines the p5.Image class and P5 methods for
* drawing images to the main display canvas.
*/
'use strict';
var p5 = _dereq_('../core/core');
var Filters = _dereq_('./filters');
/*
* Class methods
*/
/**
* Creates a new p5.Image. A p5.Image is a canvas backed representation of an
* image. p5 can display .gif, .jpg and .png images. Images may be displayed
* in 2D and 3D space. Before an image is used, it must be loaded with the
* loadImage() function. The p5.Image class contains fields for the width and
* height of the image, as well as an array called pixels[] that contains the
* values for every pixel in the image. The methods described below allow
* easy access to the image's pixels and alpha channel and simplify the
* process of compositing.
*
* Before using the pixels[] array, be sure to use the loadPixels() method on
* the image to make sure that the pixel data is properly loaded.
*
* @class p5.Image
* @constructor
* @param {Number} width
* @param {Number} height
* @param {Object} pInst An instance of a p5 sketch.
*/
p5.Image = function(width, height){
/**
* Image width.
* @property width
*/
this.width = width;
/**
* Image height.
* @property height
*/
this.height = height;
this.canvas = document.createElement('canvas');
this.canvas.width = this.width;
this.canvas.height = this.height;
this.drawingContext = this.canvas.getContext('2d');
this._pixelDensity = 1;
//used for webgl texturing only
this.isTexture = false;
/**
* Array containing the values for all the pixels in the display window.
* These values are numbers. This array is the size (include an appropriate
* factor for pixelDensity) of the display window x4,
* representing the R, G, B, A values in order for each pixel, moving from
* left to right across each row, then down each column. Retina and other
* high denisty displays may have more pixels[] (by a factor of
* pixelDensity^2).
* For example, if the image is 100x100 pixels, there will be 40,000. With
* pixelDensity = 2, there will be 160,000. The first four values
* (indices 0-3) in the array will be the R, G, B, A values of the pixel at
* (0, 0). The second four values (indices 4-7) will contain the R, G, B, A
* values of the pixel at (1, 0). More generally, to set values for a pixel
* at (x, y):
* var d = pixelDensity;
* for (var i = 0; i < d; i++) {
* for (var j = 0; j < d; j++) {
* // loop over
* idx = 4*((y * d + j) * width * d + (x * d + i));
* pixels[idx] = r;
* pixels[idx+1] = g;
* pixels[idx+2] = b;
* pixels[idx+3] = a;
* }
* }
*
*
* Before accessing this array, the data must loaded with the loadPixels()
* function. After the array data has been modified, the updatePixels()
* function must be run to update the changes.
* @property pixels[]
* @example
*
*
* img = createImage(66, 66);
* img.loadPixels();
* for (i = 0; i < img.width; i++) {
* for (j = 0; j < img.height; j++) {
* img.set(i, j, color(0, 90, 102));
* }
* }
* img.updatePixels();
* image(img, 17, 17);
*
*
*
*
* var pink = color(255, 102, 204);
* img = createImage(66, 66);
* img.loadPixels();
* for (var i = 0; i < 4*(width*height/2); i+=4) {
* img.pixels[i] = red(pink);
* img.pixels[i+1] = green(pink);
* img.pixels[i+2] = blue(pink);
* img.pixels[i+3] = alpha(pink);
* }
* img.updatePixels();
* image(img, 17, 17);
*
*
*/
this.pixels = [];
};
/**
* Helper fxn for sharing pixel methods
*
*/
p5.Image.prototype._setProperty = function (prop, value) {
this[prop] = value;
};
/**
* Loads the pixels data for this image into the [pixels] attribute.
*
* @method loadPixels
*/
p5.Image.prototype.loadPixels = function(){
p5.Renderer2D.prototype.loadPixels.call(this);
};
/**
* Updates the backing canvas for this image with the contents of
* the [pixels] array.
*
* @method updatePixels
* @param {Integer|undefined} x x-offset of the target update area for the
* underlying canvas
* @param {Integer|undefined} y y-offset of the target update area for the
* underlying canvas
* @param {Integer|undefined} w height of the target update area for the
* underlying canvas
* @param {Integer|undefined} h height of the target update area for the
* underlying canvas
*/
p5.Image.prototype.updatePixels = function(x, y, w, h){
p5.Renderer2D.prototype.updatePixels.call(this, x, y, w, h);
};
/**
* Get a region of pixels from an image.
*
* If no params are passed, those whole image is returned,
* if x and y are the only params passed a single pixel is extracted
* if all params are passed a rectangle region is extracted and a p5.Image
* is returned.
*
* Returns undefined if the region is outside the bounds of the image
*
* @method get
* @param {Number} [x] x-coordinate of the pixel
* @param {Number} [y] y-coordinate of the pixel
* @param {Number} [w] width
* @param {Number} [h] height
* @return {Array/Color | p5.Image} color of pixel at x,y in array format
* [R, G, B, A] or p5.Image
*/
p5.Image.prototype.get = function(x, y, w, h){
return p5.Renderer2D.prototype.get.call(this, x, y, w, h);
};
/**
* Set the color of a single pixel or write an image into
* this p5.Image.
*
* Note that for a large number of pixels this will
* be slower than directly manipulating the pixels array
* and then calling updatePixels().
*
* @method set
* @param {Number} x x-coordinate of the pixel
* @param {Number} y y-coordinate of the pixel
* @param {Number|Array|Object} a grayscale value | pixel array |
* a p5.Color | image to copy
* @example
*
*
* img = createImage(66, 66);
* img.loadPixels();
* for (i = 0; i < img.width; i++) {
* for (j = 0; j < img.height; j++) {
* img.set(i, j, color(0, 90, 102, i % img.width * 2));
* }
* }
* img.updatePixels();
* image(img, 17, 17);
* image(img, 34, 34);
*
*
*/
p5.Image.prototype.set = function(x, y, imgOrCol){
p5.Renderer2D.prototype.set.call(this, x, y, imgOrCol);
};
/**
* Resize the image to a new width and height. To make the image scale
* proportionally, use 0 as the value for the wide or high parameter.
* For instance, to make the width of an image 150 pixels, and change
* the height using the same proportion, use resize(150, 0).
*
* @method resize
* @param {Number} width the resized image width
* @param {Number} height the resized image height
* @example
*
* var img;
*
* function setup() {
* img = loadImage("assets/rockies.jpg");
* }
* function draw() {
* image(img, 0, 0);
* }
*
* function mousePressed() {
* img.resize(50, 100);
* }
*
*/
p5.Image.prototype.resize = function(width, height){
// Copy contents to a temporary canvas, resize the original
// and then copy back.
//
// There is a faster approach that involves just one copy and swapping the
// this.canvas reference. We could switch to that approach if (as i think
// is the case) there an expectation that the user would not hold a
// reference to the backing canvas of a p5.Image. But since we do not
// enforce that at the moment, I am leaving in the slower, but safer
// implementation.
width = width || this.canvas.width;
height = height || this.canvas.height;
var tempCanvas = document.createElement('canvas');
tempCanvas.width = width;
tempCanvas.height = height;
tempCanvas.getContext('2d').drawImage(this.canvas,
0, 0, this.canvas.width, this.canvas.height,
0, 0, tempCanvas.width, tempCanvas.height
);
// Resize the original canvas, which will clear its contents
this.canvas.width = this.width = width;
this.canvas.height = this.height = height;
//Copy the image back
this.drawingContext.drawImage(tempCanvas,
0, 0, width, height,
0, 0, width, height
);
if(this.pixels.length > 0){
this.loadPixels();
}
};
/**
* Copies a region of pixels from one image to another. If no
* srcImage is specified this is used as the source. If the source
* and destination regions aren't the same size, it will
* automatically resize source pixels to fit the specified
* target region.
*
* @method copy
* @param {p5.Image|undefined} srcImage source image
* @param {Integer} sx X coordinate of the source's upper left corner
* @param {Integer} sy Y coordinate of the source's upper left corner
* @param {Integer} sw source image width
* @param {Integer} sh source image height
* @param {Integer} dx X coordinate of the destination's upper left corner
* @param {Integer} dy Y coordinate of the destination's upper left corner
* @param {Integer} dw destination image width
* @param {Integer} dh destination image height
*/
p5.Image.prototype.copy = function () {
p5.prototype.copy.apply(this, arguments);
};
/**
* Masks part of an image from displaying by loading another
* image and using it's alpha channel as an alpha channel for
* this image.
*
* @method mask
* @param {p5.Image|undefined} srcImage source image
*
* TODO: - Accept an array of alpha values.
* - Use other channels of an image. p5 uses the
* blue channel (which feels kind of arbitrary). Note: at the
* moment this method does not match native processings original
* functionality exactly.
*
* http://blogs.adobe.com/webplatform/2013/01/28/blending-features-in-canvas/
*
*/
p5.Image.prototype.mask = function(p5Image) {
if(p5Image === undefined){
p5Image = this;
}
var currBlend = this.drawingContext.globalCompositeOperation;
var scaleFactor = 1;
if (p5Image instanceof p5.Renderer) {
scaleFactor = p5Image._pInst._pixelDensity;
}
var copyArgs = [
p5Image,
0,
0,
scaleFactor*p5Image.width,
scaleFactor*p5Image.height,
0,
0,
this.width,
this.height
];
this.drawingContext.globalCompositeOperation = 'destination-in';
this.copy.apply(this, copyArgs);
this.drawingContext.globalCompositeOperation = currBlend;
};
/**
* Applies an image filter to a p5.Image
*
* @method filter
* @param {String} operation one of threshold, gray, invert, posterize and
* opaque see Filters.js for docs on each available
* filter
* @param {Number|undefined} value
*/
p5.Image.prototype.filter = function(operation, value) {
Filters.apply(this.canvas, Filters[operation.toLowerCase()], value);
};
/**
* Copies a region of pixels from one image to another, using a specified
* blend mode to do the operation.
*
* @method blend
* @param {p5.Image|undefined} srcImage source image
* @param {Integer} sx X coordinate of the source's upper left corner
* @param {Integer} sy Y coordinate of the source's upper left corner
* @param {Integer} sw source image width
* @param {Integer} sh source image height
* @param {Integer} dx X coordinate of the destination's upper left corner
* @param {Integer} dy Y coordinate of the destination's upper left corner
* @param {Integer} dw destination image width
* @param {Integer} dh destination image height
* @param {Integer} blendMode the blend mode
*
* Available blend modes are: normal | multiply | screen | overlay |
* darken | lighten | color-dodge | color-burn | hard-light |
* soft-light | difference | exclusion | hue | saturation |
* color | luminosity
*
*
* http://blogs.adobe.com/webplatform/2013/01/28/blending-features-in-canvas/
*
*/
p5.Image.prototype.blend = function() {
p5.prototype.blend.apply(this, arguments);
};
/**
* Saves the image to a file and force the browser to download it.
* Accepts two strings for filename and file extension
* Supports png (default) and jpg.
*
* @method save
* @param {String} filename give your file a name
* @param {String} extension 'png' or 'jpg'
*/
p5.Image.prototype.save = function(filename, extension) {
var mimeType;
if (!extension) {
extension = 'png';
mimeType = 'image/png';
}
else {
// en.wikipedia.org/wiki/Comparison_of_web_browsers#Image_format_support
switch(extension.toLowerCase()){
case 'png':
mimeType = 'image/png';
break;
case 'jpeg':
mimeType = 'image/jpeg';
break;
case 'jpg':
mimeType = 'image/jpeg';
break;
default:
mimeType = 'image/png';
break;
}
}
var downloadMime = 'image/octet-stream';
var imageData = this.canvas.toDataURL(mimeType);
imageData = imageData.replace(mimeType, downloadMime);
//Make the browser download the file
p5.prototype.downloadFile(imageData, filename, extension);
};
/**
* creates a gl texture
* used in WEBGL mode only
* @param {[type]} tex [description]
* @return {[type]} [description]
*/
p5.Image.prototype.createTexture = function(tex){
//this.texture = tex;
return this;
};
module.exports = p5.Image;
},{"../core/core":48,"./filters":65}],69:[function(_dereq_,module,exports){
/**
* @module Image
* @submodule Pixels
* @for p5
* @requires core
*/
'use strict';
var p5 = _dereq_('../core/core');
var Filters = _dereq_('./filters');
_dereq_('../color/p5.Color');
/**
* Uint8ClampedArray
* containing the values for all the pixels in the display window.
* These values are numbers. This array is the size (include an appropriate
* factor for pixelDensity) of the display window x4,
* representing the R, G, B, A values in order for each pixel, moving from
* left to right across each row, then down each column. Retina and other
* high denisty displays will have more pixels[] (by a factor of
* pixelDensity^2).
* For example, if the image is 100x100 pixels, there will be 40,000. On a
* retina display, there will be 160,000. The first four values
* (indices 0-3) in the array will be the R, G, B, A values of the pixel at
* (0, 0). The second four values (indices 4-7) will contain the R, G, B, A
* values of the pixel at (1, 0). More generally, to set values for a pixel
* at (x, y):
* var d = pixelDensity;
* for (var i = 0; i < d; i++) {
* for (var j = 0; j < d; j++) {
* // loop over
* idx = 4*((y * d + j) * width * d + (x * d + i));
* pixels[idx] = r;
* pixels[idx+1] = g;
* pixels[idx+2] = b;
* pixels[idx+3] = a;
* }
* }
*
* While the above method is complex, it is flexible enough to work with
* any pixelDensity. Note that set() will automatically take care of
* setting all the appropriate values in pixels[] for a given (x, y) at
* any pixelDensity, but the performance may not be as fast when lots of
* modifications are made to the pixel array.
*
* Before accessing this array, the data must loaded with the loadPixels()
* function. After the array data has been modified, the updatePixels()
* function must be run to update the changes.
*
* Note that this is not a standard javascript array. This means that
* standard javascript functions such as slice() or
* arrayCopy() do not
* work.
*
* @property pixels[]
* @example
*
*
* var pink = color(255, 102, 204);
* loadPixels();
* var d = pixelDensity;
* var halfImage = 4 * (width * d) * (height/2 * d);
* for (var i = 0; i < halfImage; i+=4) {
* pixels[i] = red(pink);
* pixels[i+1] = green(pink);
* pixels[i+2] = blue(pink);
* pixels[i+3] = alpha(pink);
* }
* updatePixels();
*
*
*/
p5.prototype.pixels = [];
/**
* Copies a region of pixels from one image to another, using a specified
* blend mode to do the operation.
* Available blend modes are: BLEND | DARKEST | LIGHTEST | DIFFERENCE |
* MULTIPLY| EXCLUSION | SCREEN | REPLACE | OVERLAY | HARD_LIGHT |
* SOFT_LIGHT | DODGE | BURN | ADD | NORMAL
*
*
* @method blend
* @param {p5.Image|undefined} srcImage source image
* @param {Integer} sx X coordinate of the source's upper left corner
* @param {Integer} sy Y coordinate of the source's upper left corner
* @param {Integer} sw source image width
* @param {Integer} sh source image height
* @param {Integer} dx X coordinate of the destination's upper left corner
* @param {Integer} dy Y coordinate of the destination's upper left corner
* @param {Integer} dw destination image width
* @param {Integer} dh destination image height
* @param {Integer} blendMode the blend mode
*
* @example
*
* var img0;
* var img1;
*
* function preload() {
* img0 = loadImage("assets/rockies.jpg");
* img1 = loadImage("assets/bricks_third.jpg");
* }
*
* function setup() {
* background(img0);
* image(img1, 0, 0);
* blend(img1, 0, 0, 33, 100, 67, 0, 33, 100, LIGHTEST);
* }
*
*
* var img0;
* var img1;
*
* function preload() {
* img0 = loadImage("assets/rockies.jpg");
* img1 = loadImage("assets/bricks_third.jpg");
* }
*
* function setup() {
* background(img0);
* image(img1, 0, 0);
* blend(img1, 0, 0, 33, 100, 67, 0, 33, 100, DARKEST);
* }
*
*
* var img0;
* var img1;
*
* function preload() {
* img0 = loadImage("assets/rockies.jpg");
* img1 = loadImage("assets/bricks_third.jpg");
* }
*
* function setup() {
* background(img0);
* image(img1, 0, 0);
* blend(img1, 0, 0, 33, 100, 67, 0, 33, 100, ADD);
* }
*
*/
p5.prototype.blend = function() {
this._renderer.blend.apply(this._renderer, arguments);
};
/**
* Copies a region of the canvas to another region of the canvas
* and copies a region of pixels from an image used as the srcImg parameter
* into the canvas srcImage is specified this is used as the source. If
* the source and destination regions aren't the same size, it will
* automatically resize source pixels to fit the specified
* target region.
*
* @method copy
* @param {p5.Image|undefined} srcImage source image
* @param {Integer} sx X coordinate of the source's upper left corner
* @param {Integer} sy Y coordinate of the source's upper left corner
* @param {Integer} sw source image width
* @param {Integer} sh source image height
* @param {Integer} dx X coordinate of the destination's upper left corner
* @param {Integer} dy Y coordinate of the destination's upper left corner
* @param {Integer} dw destination image width
* @param {Integer} dh destination image height
*
* @example
*
* var img;
*
* function preload() {
* img = loadImage("assets/rockies.jpg");
* }
*
* function setup() {
* background(img);
* copy(img, 7, 22, 10, 10, 35, 25, 50, 50);
* stroke(255);
* noFill();
* // Rectangle shows area being copied
* rect(7, 22, 10, 10);
* }
*
*/
p5.prototype.copy = function () {
p5.Renderer2D._copyHelper.apply(this, arguments);
};
/**
* Applies a filter to the canvas.
*
*
* The presets options are:
*
*
* THRESHOLD
* 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.
*
*
* GRAY
* Converts any colors in the image to grayscale equivalents. No parameter
* is used.
*
*
* OPAQUE
* Sets the alpha channel to entirely opaque. No parameter is used.
*
*
* INVERT
* Sets each pixel to its inverse value. No parameter is used.
*
*
* POSTERIZE
* 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.
*
*
* BLUR
* Executes a Guassian blur with the level parameter specifying the extent
* of the blurring. If no parameter is used, the blur is equivalent to
* Guassian blur of radius 1. Larger values increase the blur.
*
*
* ERODE
* Reduces the light areas. No parameter is used.
*
*
* DILATE
* Increases the light areas. No parameter is used.
*
* @method filter
* @param {String} filterType
* @param {Number} filterParam an optional parameter unique
* to each filter, see above
*
*
* @example
*
*
* var img;
* function preload() {
* img = loadImage("assets/bricks.jpg");
* }
* function setup() {
* image(img, 0, 0);
* filter(THRESHOLD);
* }
*
*
*
*
*
* var img;
* function preload() {
* img = loadImage("assets/bricks.jpg");
* }
* function setup() {
* image(img, 0, 0);
* filter(GRAY);
* }
*
*
*
*
*
* var img;
* function preload() {
* img = loadImage("assets/bricks.jpg");
* }
* function setup() {
* image(img, 0, 0);
* filter(OPAQUE);
* }
*
*
*
*
*
* var img;
* function preload() {
* img = loadImage("assets/bricks.jpg");
* }
* function setup() {
* image(img, 0, 0);
* filter(INVERT);
* }
*
*
*
*
*
* var img;
* function preload() {
* img = loadImage("assets/bricks.jpg");
* }
* function setup() {
* image(img, 0, 0);
* filter(POSTERIZE,3);
* }
*
*
*
*
*
* var img;
* function preload() {
* img = loadImage("assets/bricks.jpg");
* }
* function setup() {
* image(img, 0, 0);
* filter(DILATE);
* }
*
*
*
*
*
* var img;
* function preload() {
* img = loadImage("assets/bricks.jpg");
* }
* function setup() {
* image(img, 0, 0);
* filter(BLUR,3);
* }
*
*
*
*
*
* var img;
* function preload() {
* img = loadImage("assets/bricks.jpg");
* }
* function setup() {
* image(img, 0, 0);
* filter(ERODE);
* }
*
*
*/
p5.prototype.filter = function(operation, value) {
Filters.apply(this.canvas, Filters[operation.toLowerCase()], value);
};
/**
* Returns an array of [R,G,B,A] values for any pixel or grabs a section of
* an image. If no parameters are specified, the entire image is returned.
* Use the x and y parameters to get the value of one pixel. Get a section of
* the display window by specifying additional w and h parameters. When
* getting an image, the x and y parameters define the coordinates for the
* upper-left corner of the image, regardless of the current imageMode().
*
* If the pixel requested is outside of the image window, [0,0,0,255] is
* returned. To get the numbers scaled according to the current color ranges
* and taking into account colorMode, use getColor instead of get.
*
* Getting the color of a single pixel with get(x, y) is easy, but not as fast
* as grabbing the data directly from pixels[]. The equivalent statement to
* get(x, y) using pixels[] with pixel density d is
* [pixels[(y*width*d+x)*d],
* pixels[(y*width*d+x)*d+1],
* pixels[(y*width*d+x)*d+2],
* pixels[(y*width*d+x)*d+3] ].
* See the reference for pixels[] for more information.
*
* @method get
* @param {Number} [x] x-coordinate of the pixel
* @param {Number} [y] y-coordinate of the pixel
* @param {Number} [w] width
* @param {Number} [h] height
* @return {Array|p5.Image} values of pixel at x,y in array format
* [R, G, B, A] or p5.Image
* @example
*
*
* var img;
* function preload() {
* img = loadImage("assets/rockies.jpg");
* }
* function setup() {
* image(img, 0, 0);
* var c = get();
* image(c, width/2, 0);
* }
*
*
*
*
*
* var img;
* function preload() {
* img = loadImage("assets/rockies.jpg");
* }
* function setup() {
* image(img, 0, 0);
* var c = get(50, 90);
* fill(c);
* noStroke();
* rect(25, 25, 50, 50);
* }
*
*
*/
p5.prototype.get = function(x, y, w, h){
return this._renderer.get(x, y, w, h);
};
/**
* Loads the pixel data for the display window into the pixels[] array. This
* function must always be called before reading from or writing to pixels[].
*
* @method loadPixels
* @example
*
*
* var img;
* function preload() {
* img = loadImage("assets/rockies.jpg");
* }
*
* function setup() {
* image(img, 0, 0);
* var d = pixelDensity;
* var halfImage = 4 * (img.width * d) *
(img.height/2 * d);
* loadPixels();
* for (var i = 0; i < halfImage; i++) {
* pixels[i+halfImage] = pixels[i];
* }
* updatePixels();
* }
*
*
*/
p5.prototype.loadPixels = function() {
this._renderer.loadPixels();
};
/**
* Changes the color of any pixel, or writes an image directly to the
* display window.
* The x and y parameters specify the pixel to change and the c parameter
* specifies the color value. This can be a p5.Color object, or [R, G, B, A]
* pixel array. It can also be a single grayscale value.
* When setting an image, the x and y parameters define the coordinates for
* the upper-left corner of the image, regardless of the current imageMode().
*
*
* After using set(), you must call updatePixels() for your changes to
* appear. This should be called once all pixels have been set.
*
* Setting the color of a single pixel with set(x, y) is easy, but not as
* fast as putting the data directly into pixels[]. Setting the pixels[]
* values directly may be complicated when working with a retina display,
* but will perform better when lots of pixels need to be set directly on
* every loop.
* See the reference for pixels[] for more information.
*
* @method set
* @param {Number} x x-coordinate of the pixel
* @param {Number} y y-coordinate of the pixel
* @param {Number|Array|Object} c insert a grayscale value | a pixel array |
* a p5.Color object | a p5.Image to copy
* @example
*
*
* var black = color(0);
* set(30, 20, black);
* set(85, 20, black);
* set(85, 75, black);
* set(30, 75, black);
* updatePixels();
*
*
*
*
*
* for (var i = 30; i < width-15; i++) {
* for (var j = 20; j < height-25; j++) {
* var c = color(204-j, 153-i, 0);
* set(i, j, c);
* }
* }
* updatePixels();
*
*
*
*
*
* var img;
* function preload() {
* img = loadImage("assets/rockies.jpg");
* }
*
* function setup() {
* set(0, 0, img);
* updatePixels();
* line(0, 0, width, height);
* line(0, height, width, 0);
* }
*
*
*/
p5.prototype.set = function (x, y, imgOrCol) {
this._renderer.set(x, y, imgOrCol);
};
/**
* Updates the display window with the data in the pixels[] array.
* Use in conjunction with loadPixels(). If you're only reading pixels from
* the array, there's no need to call updatePixels() — updating is only
* necessary to apply changes. updatePixels() should be called anytime the
* pixels array is manipulated or set() is called.
*
* @method updatePixels
* @param {Number} [x] x-coordinate of the upper-left corner of region
* to update
* @param {Number} [y] y-coordinate of the upper-left corner of region
* to update
* @param {Number} [w] width of region to update
* @param {Number} [w] height of region to update
* @example
*
*
* var img;
* function preload() {
* img = loadImage("assets/rockies.jpg");
* }
*
* function setup() {
* image(img, 0, 0);
* var halfImage = 4 * (img.width * pixelDensity) *
* (img.height * pixelDensity/2);
* loadPixels();
* for (var i = 0; i < halfImage; i++) {
* pixels[i+halfImage] = pixels[i];
* }
* updatePixels();
* }
*
*
*/
p5.prototype.updatePixels = function (x, y, w, h) {
this._renderer.updatePixels(x, y, w, h);
};
module.exports = p5;
},{"../color/p5.Color":42,"../core/core":48,"./filters":65}],70:[function(_dereq_,module,exports){
/**
* @module IO
* @submodule Input
* @for p5
* @requires core
* @requires reqwest
*/
'use strict';
var p5 = _dereq_('../core/core');
var reqwest = _dereq_('reqwest');
var opentype = _dereq_('opentype.js');
_dereq_('../core/error_helpers');
/**
* Checks if we are in preload and returns the last arg which will be the
* _decrementPreload function if called from a loadX() function. Should
* only be used in loadX() functions.
* @private
*/
p5._getDecrementPreload = function() {
var decrementPreload = arguments[arguments.length - 1];
// when in preload decrementPreload will always be the last arg as it is set
// with args.push() before invocation in _wrapPreload
if ((window.preload || (this && this.preload)) &&
typeof decrementPreload === 'function') {
return decrementPreload;
} else {
return null;
}
};
/**
* Loads an opentype font file (.otf, .ttf) from a file or a URL,
* and returns a PFont Object. This method is asynchronous,
* meaning it may not finish before the next line in your sketch
* is executed.
*
* The path to the font should be relative to the HTML file
* that links in your sketch. Loading an from a URL or other
* remote location may be blocked due to your browser's built-in
* security.
*
* @method loadFont
* @param {String} path name of the file or url to load
* @param {Function} [callback] function to be executed after
* loadFont()
* completes
* @return {Object} p5.Font object
* @example
*
* Calling loadFont() inside preload() guarantees that the load
* operation will have completed before setup() and draw() are called.
*
*
* var myFont;
* function preload() {
* myFont = loadFont('assets/AvenirNextLTPro-Demi.otf');
* }
*
* function setup() {
* fill('#ED225D');
* textFont(myFont);
* textSize(36);
* text('p5*js', 10, 50);
* }
*
*
* Outside of preload(), you may supply a callback function to handle the
* object:
*
*
* function setup() {
* loadFont('assets/AvenirNextLTPro-Demi.otf', drawText);
* }
*
* function drawText(font) {
* fill('#ED225D');
* textFont(font, 36);
* text('p5*js', 10, 50);
* }
*
*
*
* You can also use the string name of the font to style other HTML
* elements.
*
*
* var myFont;
*
* function preload() {
* myFont = loadFont('assets/Avenir.otf');
* }
*
* function setup() {
* var myDiv = createDiv('hello there');
* myDiv.style('font-family', 'Avenir');
* }
*
*/
p5.prototype.loadFont = function(path, onSuccess, onError) {
var p5Font = new p5.Font(this);
var decrementPreload = p5._getDecrementPreload.apply(this, arguments);
opentype.load(path, function(err, font) {
if (err) {
if ((typeof onError !== 'undefined') && (onError !== decrementPreload)) {
return onError(err);
}
throw err;
}
p5Font.font = font;
if (typeof onSuccess !== 'undefined') {
onSuccess(p5Font);
}
if (decrementPreload && (onSuccess !== decrementPreload)) {
decrementPreload();
}
/*jshint multistr: true */
var exp =/\/[a-zA-Z]*((.ttf)|(.otf)|(.woff)|(.woff2))$/i;
if(!exp) {
return p5Font;
}
var i = (exp).exec( path ).index + 1;
var fontName = path.substring(i);
fontName = fontName.match(/[A-Za-z]*/);
var fontFamily = fontName[0];
var newStyle = document.createElement('style');
newStyle.appendChild(document.createTextNode('\n@font-face {\
\nfont-family: '+fontFamily+';\nsrc: url('+path+');\n}\n'));
document.head.appendChild(newStyle);
});
return p5Font;
};
//BufferedReader
p5.prototype.createInput = function() {
// TODO
throw 'not yet implemented';
};
p5.prototype.createReader = function() {
// TODO
throw 'not yet implemented';
};
p5.prototype.loadBytes = function() {
// TODO
throw 'not yet implemented';
};
/**
* Loads a JSON file from a file or a URL, and returns an Object or Array.
* This method is asynchronous, meaning it may not finish before the next
* line in your sketch is executed.
*
* @method loadJSON
* @param {String} path name of the file or url to load
* @param {Function} [callback] function to be executed after
* loadJSON() completes, data is passed
* in as first argument
* @param {Function} [errorCallback] function to be executed if
* there is an error, response is passed
* in as first argument
* @param {String} [datatype] "json" or "jsonp"
* @return {Object|Array} JSON data
* @example
*
* Calling loadJSON() inside preload() guarantees to complete the
* operation before setup() and draw() are called.
*
*
* var weather;
* function preload() {
* var url = 'http://api.openweathermap.org/data/2.5/weather?q=London,UK'+
* '&APPID=7bbbb47522848e8b9c26ba35c226c734';
* weather = loadJSON(url);
* }
*
* function setup() {
* noLoop();
* }
*
* function draw() {
* background(200);
* // get the humidity value out of the loaded JSON
* var humidity = weather.main.humidity;
* fill(0, humidity); // use the humidity value to set the alpha
* ellipse(width/2, height/2, 50, 50);
* }
*
*
*
* Outside of preload(), you may supply a callback function to handle the
* object:
*
* function setup() {
* noLoop();
* var url = 'http://api.openweathermap.org/data/2.5/weather?q=NewYork'+
* '&APPID=7bbbb47522848e8b9c26ba35c226c734';
* loadJSON(url, drawWeather);
* }
*
* function draw() {
* background(200);
* }
*
* function drawWeather(weather) {
* // get the humidity value out of the loaded JSON
* var humidity = weather.main.humidity;
* fill(0, humidity); // use the humidity value to set the alpha
* ellipse(width/2, height/2, 50, 50);
* }
*
*
*/
p5.prototype.loadJSON = function() {
var path = arguments[0];
var callback = arguments[1];
var errorCallback;
var decrementPreload = p5._getDecrementPreload.apply(this, arguments);
var ret = []; // array needed for preload
// assume jsonp for URLs
var t = 'json'; //= path.indexOf('http') === -1 ? 'json' : 'jsonp';
// check for explicit data type argument
for (var i=2; iCalling loadStrings() inside preload() guarantees to complete the
* operation before setup() and draw() are called.
*
*
* var result;
* function preload() {
* result = loadStrings('assets/test.txt');
* }
* function setup() {
* background(200);
* var ind = floor(random(result.length));
* text(result[ind], 10, 10, 80, 80);
* }
*
*
* Outside of preload(), you may supply a callback function to handle the
* object:
*
*
* function setup() {
* loadStrings('assets/test.txt', pickString);
* }
*
* function pickString(result) {
* background(200);
* var ind = floor(random(result.length));
* text(result[ind], 10, 10, 80, 80);
* }
*
*/
p5.prototype.loadStrings = function (path, callback, errorCallback) {
var ret = [];
var req = new XMLHttpRequest();
var decrementPreload = p5._getDecrementPreload.apply(this, arguments);
req.addEventListener('error', function (resp) {
if (errorCallback) {
errorCallback(resp);
} else {
console.log(resp.responseText);
}
});
req.open('GET', path, true);
req.onreadystatechange = function () {
if (req.readyState === 4) {
if (req.status === 200) {
var arr = req.responseText.match(/[^\r\n]+/g);
for (var k in arr) {
ret[k] = arr[k];
}
if (typeof callback !== 'undefined') {
callback(ret);
}
if (decrementPreload && (callback !== decrementPreload)) {
decrementPreload();
}
} else {
if (errorCallback) {
errorCallback(req);
} else {
console.log(req.statusText);
}
//p5._friendlyFileLoadError(3, path);
}
}
};
req.send(null);
return ret;
};
/**
* Reads the contents of a file or URL and creates a p5.Table object with
* its values. If a file is specified, it must be located in the sketch's
* "data" folder. The filename parameter can also be a URL to a file found
* online. By default, the file is assumed to be comma-separated (in CSV
* format). Table only looks for a header row if the 'header' option is
* included.
*
* Possible options include:
*
* - csv - parse the table as comma-separated values
* - tsv - parse the table as tab-separated values
* - header - this table has a header (title) row
*
*
*
* When passing in multiple options, pass them in as separate parameters,
* seperated by commas. For example:
*
*
* loadTable("my_csv_file.csv", "csv", "header")
*
*
*
* All files loaded and saved use UTF-8 encoding.
*
* This method is asynchronous, meaning it may not finish before the next
* line in your sketch is executed. Calling loadTable() inside preload()
* guarantees to complete the operation before setup() and draw() are called.
*
Outside of preload(), you may supply a callback function to handle the
* object:
*
*
* @method loadTable
* @param {String} filename name of the file or URL to load
* @param {String|Strings} [options] "header" "csv" "tsv"
* @param {Function} [callback] function to be executed after
* loadTable() completes, Table object is
* passed in as first argument
* @return {Object} Table object containing data
*
* @example
*
*
* // Given the following CSV file called "mammals.csv"
* // located in the project's "assets" folder:
* //
* // id,species,name
* // 0,Capra hircus,Goat
* // 1,Panthera pardus,Leopard
* // 2,Equus zebra,Zebra
*
* var table;
*
* function preload() {
* //my table is comma separated value "csv"
* //and has a header specifying the columns labels
* table = loadTable("assets/mammals.csv", "csv", "header");
* //the file can be remote
* //table = loadTable("http://p5js.org/reference/assets/mammals.csv",
* // "csv", "header");
* }
*
* function setup() {
* //count the columns
* print(table.getRowCount() + " total rows in table");
* print(table.getColumnCount() + " total columns in table");
*
* print(table.getColumn("name"));
* //["Goat", "Leopard", "Zebra"]
*
* //cycle through the table
* for (var r = 0; r < table.getRowCount(); r++)
* for (var c = 0; c < table.getColumnCount(); c++) {
* print(table.getString(r, c));
* }
* }
*
*
*/
p5.prototype.loadTable = function (path) {
var callback = null;
var options = [];
var header = false;
var sep = ',';
var separatorSet = false;
var decrementPreload = p5._getDecrementPreload.apply(this, arguments);
for (var i = 1; i < arguments.length; i++) {
if ((typeof(arguments[i]) === 'function') &&
(arguments[i] !== decrementPreload)) {
callback = arguments[i];
}
else if (typeof(arguments[i]) === 'string') {
options.push(arguments[i]);
if (arguments[i] === 'header') {
header = true;
}
if (arguments[i] === 'csv') {
if (separatorSet) {
throw new Error('Cannot set multiple separator types.');
}
else {
sep = ',';
separatorSet = true;
}
}
else if (arguments[i] === 'tsv') {
if (separatorSet) {
throw new Error('Cannot set multiple separator types.');
}
else {
sep = '\t';
separatorSet = true;
}
}
}
}
var t = new p5.Table();
reqwest({url: path, crossOrigin: true, type: 'csv'})
.then(function(resp) {
resp = resp.responseText;
var state = {};
// define constants
var PRE_TOKEN = 0,
MID_TOKEN = 1,
POST_TOKEN = 2,
POST_RECORD = 4;
var QUOTE = '\"',
CR = '\r',
LF = '\n';
var records = [];
var offset = 0;
var currentRecord = null;
var currentChar;
var recordBegin = function () {
state.escaped = false;
currentRecord = [];
tokenBegin();
};
var recordEnd = function () {
state.currentState = POST_RECORD;
records.push(currentRecord);
currentRecord = null;
};
var tokenBegin = function() {
state.currentState = PRE_TOKEN;
state.token = '';
};
var tokenEnd = function() {
currentRecord.push(state.token);
tokenBegin();
};
while(true) {
currentChar = resp[offset++];
// EOF
if(currentChar == null) {
if (state.escaped) {
throw new Error('Unclosed quote in file.');
}
if (currentRecord){
tokenEnd();
recordEnd();
break;
}
}
if(currentRecord === null) {
recordBegin();
}
// Handle opening quote
if (state.currentState === PRE_TOKEN) {
if (currentChar === QUOTE) {
state.escaped = true;
state.currentState = MID_TOKEN;
continue;
}
state.currentState = MID_TOKEN;
}
// mid-token and escaped, look for sequences and end quote
if (state.currentState === MID_TOKEN && state.escaped) {
if (currentChar === QUOTE) {
if (resp[offset] === QUOTE) {
state.token += QUOTE;
offset++;
}
else {
state.escaped = false;
state.currentState = POST_TOKEN;
}
}
else {
state.token += currentChar;
}
continue;
}
// fall-through: mid-token or post-token, not escaped
if (currentChar === CR ) {
if( resp[offset] === LF ) {
offset++;
}
tokenEnd();
recordEnd();
}
else if (currentChar === LF) {
tokenEnd();
recordEnd();
}
else if (currentChar === sep) {
tokenEnd();
}
else if( state.currentState === MID_TOKEN ){
state.token += currentChar;
}
}
// set up column names
if (header) {
t.columns = records.shift();
}
else {
for (i = 0; i < records.length; i++){
t.columns[i] = i.toString();
}
}
var row;
for (i =0; iOutside of preload(), you may supply a callback function to handle the
* object:
*
* @method loadXML
* @param {String} filename name of the file or URL to load
* @param {Function} [callback] function to be executed after loadXML()
* completes, XML object is passed in as
* first argument
* @param {Function} [errorCallback] function to be executed if
* there is an error, response is passed
* in as first argument
* @return {Object} XML object containing data
*/
p5.prototype.loadXML = function(path, callback, errorCallback) {
var ret = document.implementation.createDocument(null, null);
var decrementPreload = p5._getDecrementPreload.apply(this, arguments);
reqwest({
url: path,
type: 'xml',
crossOrigin: true,
error: function(resp){
// pass to error callback if defined
if (errorCallback) {
errorCallback(resp);
} else { // otherwise log error msg
console.log(resp.statusText);
}
//p5._friendlyFileLoadError(1,path);
}
})
.then(function(resp){
var x = resp.documentElement;
ret.appendChild(x);
if (typeof callback !== 'undefined') {
callback(ret);
}
if (decrementPreload && (callback !== decrementPreload)) {
decrementPreload();
}
});
return ret;
};
// name clash with window.open
// p5.prototype.open = function() {
// // TODO
// };
p5.prototype.parseXML = function() {
// TODO
throw 'not yet implemented';
};
p5.prototype.selectFolder = function() {
// TODO
throw 'not yet implemented';
};
p5.prototype.selectInput = function() {
// TODO
throw 'not yet implemented';
};
/**
* Method for executing an HTTP GET request. If data type is not specified,
* p5 will try to guess based on the URL, defaulting to text.
*
* @method httpGet
* @param {String} path name of the file or url to load
* @param {Object} [data] param data passed sent with request
* @param {String} [datatype] "json", "jsonp", "xml", or "text"
* @param {Function} [callback] function to be executed after
* httpGet() completes, data is passed in
* as first argument
* @param {Function} [errorCallback] function to be executed if
* there is an error, response is passed
* in as first argument
*/
p5.prototype.httpGet = function () {
var args = Array.prototype.slice.call(arguments);
args.push('GET');
p5.prototype.httpDo.apply(this, args);
};
/**
* Method for executing an HTTP POST request. If data type is not specified,
* p5 will try to guess based on the URL, defaulting to text.
*
* @method httpPost
* @param {String} path name of the file or url to load
* @param {Object} [data] param data passed sent with request
* @param {String} [datatype] "json", "jsonp", "xml", or "text"
* @param {Function} [callback] function to be executed after
* httpGet() completes, data is passed in
* as first argument
* @param {Function} [errorCallback] function to be executed if
* there is an error, response is passed
* in as first argument
*/
p5.prototype.httpPost = function () {
var args = Array.prototype.slice.call(arguments);
args.push('POST');
p5.prototype.httpDo.apply(this, args);
};
/**
* Method for executing an HTTP request. If data type is not specified,
* p5 will try to guess based on the URL, defaulting to text.
* You may also pass a single object specifying all parameters for the
* request following the examples inside the reqwest() calls here:
* https://github.com/ded/reqwest#api
*
* @method httpDo
* @param {String} path name of the file or url to load
* @param {String} [method] either "GET", "POST", or "PUT",
* defaults to "GET"
* @param {Object} [data] param data passed sent with request
* @param {String} [datatype] "json", "jsonp", "xml", or "text"
* @param {Function} [callback] function to be executed after
* httpGet() completes, data is passed in
* as first argument
* @param {Function} [errorCallback] function to be executed if
* there is an error, response is passed
* in as first argument
*/
p5.prototype.httpDo = function() {
if (typeof arguments[0] === 'object') {
reqwest(arguments[0]);
} else {
var method = 'GET';
var path = arguments[0];
var data = {};
var type = '';
var callback;
var errorCallback;
for (var i=1; i saveJSON, saveStrings, saveTable
// filename, [extension] [canvas] --> saveImage
/**
* Save an image, text, json, csv, wav, or html. Prompts download to
* the client's computer. Note that it is not recommended to call save()
* within draw if it's looping, as the save() function will open a new save
* dialog every frame.
* The default behavior is to save the canvas as an image. You can
* optionally specify a filename.
* For example:
*
* save();
* save('myCanvas.jpg'); // save a specific canvas with a filename
*
*
* Alternately, the first parameter can be a pointer to a canvas
* p5.Element, an Array of Strings,
* an Array of JSON, a JSON object, a p5.Table, a p5.Image, or a
* p5.SoundFile (requires p5.sound). The second parameter is a filename
* (including extension). The third parameter is for options specific
* to this type of object. This method will save a file that fits the
* given paramaters. For example:
*
*
*
* save('myCanvas.jpg'); // Saves canvas as an image
*
* var cnv = createCanvas(100, 100);
* save(cnv, 'myCanvas.jpg'); // Saves canvas as an image
*
* var gb = createGraphics(100, 100);
* save(gb, 'myGraphics.jpg'); // Saves p5.Renderer object as an image
*
* save(myTable, 'myTable.html'); // Saves table as html file
* save(myTable, 'myTable.csv',); // Comma Separated Values
* save(myTable, 'myTable.tsv'); // Tab Separated Values
*
* save(myJSON, 'my.json'); // Saves pretty JSON
* save(myJSON, 'my.json', true); // Optimizes JSON filesize
*
* save(img, 'my.png'); // Saves pImage as a png image
*
* save(arrayOfStrings, 'my.txt'); // Saves strings to a text file with line
* // breaks after each item in the array
*
*
* @method save
* @param {[Object|String]} objectOrFilename If filename is provided, will
* save canvas as an image with
* either png or jpg extension
* depending on the filename.
* If object is provided, will
* save depending on the object
* and filename (see examples
* above).
* @param {[String]} filename If an object is provided as the first
* parameter, then the second parameter
* indicates the filename,
* and should include an appropriate
* file extension (see examples above).
* @param {[Boolean/String]} options Additional options depend on
* filetype. For example, when saving JSON,
* true indicates that the
* output will be optimized for filesize,
* rather than readability.
*/
p5.prototype.save = function(object, _filename, _options) {
// parse the arguments and figure out which things we are saving
var args = arguments;
// =================================================
// OPTION 1: saveCanvas...
// if no arguments are provided, save canvas
var cnv = this._curElement.elt;
if (args.length === 0) {
p5.prototype.saveCanvas(cnv);
return;
}
// otherwise, parse the arguments
// if first param is a p5Graphics, then saveCanvas
else if (args[0] instanceof p5.Renderer ||
args[0] instanceof p5.Graphics) {
p5.prototype.saveCanvas(args[0].elt, args[1], args[2]);
return;
}
// if 1st param is String and only one arg, assume it is canvas filename
else if (args.length === 1 && typeof(args[0]) === 'string') {
p5.prototype.saveCanvas(cnv, args[0]);
}
// =================================================
// OPTION 2: extension clarifies saveStrings vs. saveJSON
else {
var extension = _checkFileExtension(args[1], args[2])[1];
switch(extension){
case 'json':
p5.prototype.saveJSON(args[0], args[1], args[2]);
return;
case 'txt':
p5.prototype.saveStrings(args[0], args[1], args[2]);
return;
// =================================================
// OPTION 3: decide based on object...
default:
if (args[0] instanceof Array) {
p5.prototype.saveStrings(args[0], args[1], args[2]);
}
else if (args[0] instanceof p5.Table) {
p5.prototype.saveTable(args[0], args[1], args[2], args[3]);
}
else if (args[0] instanceof p5.Image) {
p5.prototype.saveCanvas(args[0].canvas, args[1]);
}
else if (args[0] instanceof p5.SoundFile) {
p5.prototype.saveSound(args[0], args[1], args[2], args[3]);
}
}
}
};
/**
* Writes the contents of an Array or a JSON object to a .json file.
* The file saving process and location of the saved file will
* vary between web browsers.
*
* @method saveJSON
* @param {Array|Object} json
* @param {String} filename
* @param {Boolean} [optimize] If true, removes line breaks
* and spaces from the output
* file to optimize filesize
* (but not readability).
* @example
*
* var json;
*
* function setup() {
*
* json = {}; // new JSON Object
*
* json.id = 0;
* json.species = 'Panthera leo';
* json.name = 'Lion';
*
* // To save, un-comment the line below, then click 'run'
* // saveJSONObject(json, 'lion.json');
* }
*
* // Saves the following to a file called "lion.json":
* // {
* // "id": 0,
* // "species": "Panthera leo",
* // "name": "Lion"
* // }
*
*/
p5.prototype.saveJSON = function(json, filename, opt) {
var stringify;
if (opt){
stringify = JSON.stringify( json );
} else {
stringify = JSON.stringify( json, undefined, 2);
}
console.log(stringify);
this.saveStrings(stringify.split('\n'), filename, 'json');
};
p5.prototype.saveJSONObject = p5.prototype.saveJSON;
p5.prototype.saveJSONArray = p5.prototype.saveJSON;
p5.prototype.saveStream = function() {
// TODO
throw 'not yet implemented';
};
/**
* Writes an array of Strings to a text file, one line per String.
* The file saving process and location of the saved file will
* vary between web browsers.
*
* @method saveStrings
* @param {Array} list string array to be written
* @param {String} filename filename for output
* @example
*
* var words = 'apple bear cat dog';
*
* // .split() outputs an Array
* var list = split(words, ' ');
*
* // To save the file, un-comment next line and click 'run'
* // saveStrings(list, 'nouns.txt');
*
* // Saves the following to a file called 'nouns.txt':
* //
* // apple
* // bear
* // cat
* // dog
*
*/
p5.prototype.saveStrings = function(list, filename, extension) {
var ext = extension || 'txt';
var pWriter = this.createWriter(filename, ext);
for (var i = 0; i < list.length; i++) {
if (i < list.length - 1) {
pWriter.println(list[i]);
} else {
pWriter.print(list[i]);
}
}
pWriter.close();
pWriter.flush();
};
p5.prototype.saveXML = function() {
// TODO
throw 'not yet implemented';
};
p5.prototype.selectOutput = function() {
// TODO
throw 'not yet implemented';
};
// =======
// HELPERS
// =======
function escapeHelper(content) {
return content
.replace(/&/g, '&')
.replace(//g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
/**
* Writes the contents of a Table object to a file. Defaults to a
* text file with comma-separated-values ('csv') but can also
* use tab separation ('tsv'), or generate an HTML table ('html').
* The file saving process and location of the saved file will
* vary between web browsers.
*
* @method saveTable
* @param {p5.Table} Table the Table object to save to a file
* @param {String} filename the filename to which the Table should be saved
* @param {[String]} options can be one of "tsv", "csv", or "html"
* @example
*
* var table;
*
* function setup() {
* table = new p5.Table();
*
* table.addColumn('id');
* table.addColumn('species');
* table.addColumn('name');
*
* var newRow = table.addRow();
* newRow.setNum('id', table.getRowCount() - 1);
* newRow.setString('species', 'Panthera leo');
* newRow.setString('name', 'Lion');
*
* // To save, un-comment next line then click 'run'
* // saveTable(table, 'new.csv');
* }
*
* // Saves the following to a file called 'new.csv':
* // id,species,name
* // 0,Panthera leo,Lion
*
*/
p5.prototype.saveTable = function(table, filename, options) {
var pWriter = this.createWriter(filename, options);
var header = table.columns;
var sep = ','; // default to CSV
if (options === 'tsv') {
sep = '\t';
}
if (options !== 'html') {
// make header if it has values
if (header[0] !== '0') {
for (var h = 0; h < header.length; h++ ) {
if (h < header.length - 1){
pWriter.print(header[h] + sep);
} else {
pWriter.println(header[h]);
}
}
}
// make rows
for (var i = 0; i < table.rows.length; i++ ) {
var j;
for (j = 0; j < table.rows[i].arr.length; j++) {
if (j < table.rows[i].arr.length - 1) {
pWriter.print(table.rows[i].arr[j] + sep);
}
else if (i < table.rows.length - 1) {
pWriter.println(table.rows[i].arr[j]);
} else {
pWriter.print(table.rows[i].arr[j]); // no line break
}
}
}
}
// otherwise, make HTML
else {
pWriter.println('');
pWriter.println('');
var str = ' ';
pWriter.println(str);
pWriter.println('');
pWriter.println('');
pWriter.println(' ');
// make header if it has values
if (header[0] !== '0') {
pWriter.println(' ');
for (var k = 0; k < header.length; k++ ) {
var e = escapeHelper(header[k]);
pWriter.println(' | ' +e);
pWriter.println(' | ');
}
pWriter.println('
');
}
// make rows
for (var row = 0; row < table.rows.length; row++) {
pWriter.println(' ');
for (var col = 0; col < table.columns.length; col++) {
var entry = table.rows[row].getString(col);
var htmlEntry = escapeHelper(entry);
pWriter.println(' | ' +htmlEntry);
pWriter.println(' | ');
}
pWriter.println('
');
}
pWriter.println('
');
pWriter.println('');
pWriter.print('');
}
// close and flush the pWriter
pWriter.close();
pWriter.flush();
}; // end saveTable()
/**
* Generate a blob of file data as a url to prepare for download.
* Accepts an array of data, a filename, and an extension (optional).
* This is a private function because it does not do any formatting,
* but it is used by saveStrings, saveJSON, saveTable etc.
*
* @param {Array} dataToDownload
* @param {String} filename
* @param {[String]} extension
* @private
*/
p5.prototype.writeFile = function(dataToDownload, filename, extension) {
var type = 'application\/octet-stream';
if (p5.prototype._isSafari() ) {
type = 'text\/plain';
}
var blob = new Blob(dataToDownload, {'type': type});
var href = window.URL.createObjectURL(blob);
p5.prototype.downloadFile(href, filename, extension);
};
/**
* Forces download. Accepts a url to filedata/blob, a filename,
* and an extension (optional).
* This is a private function because it does not do any formatting,
* but it is used by saveStrings, saveJSON, saveTable etc.
*
* @param {String} href i.e. an href generated by createObjectURL
* @param {[String]} filename
* @param {[String]} extension
*/
p5.prototype.downloadFile = function(href, fName, extension) {
var fx = _checkFileExtension(fName, extension);
var filename = fx[0];
var ext = fx[1];
var a = document.createElement('a');
a.href = href;
a.download = filename;
// Firefox requires the link to be added to the DOM before click()
a.onclick = destroyClickedElement;
a.style.display = 'none';
document.body.appendChild(a);
// Safari will open this file in the same page as a confusing Blob.
if (p5.prototype._isSafari() ) {
var aText = 'Hello, Safari user! To download this file...\n';
aText += '1. Go to File --> Save As.\n';
aText += '2. Choose "Page Source" as the Format.\n';
aText += '3. Name it with this extension: .\"' + ext+'\"';
alert(aText);
}
a.click();
href = null;
};
/**
* Returns a file extension, or another string
* if the provided parameter has no extension.
*
* @param {String} filename
* @return {Array} [fileName, fileExtension]
*
* @private
*/
function _checkFileExtension(filename, extension) {
if (!extension || extension === true || extension === 'true') {
extension = '';
}
if (!filename) {
filename = 'untitled';
}
var ext = '';
// make sure the file will have a name, see if filename needs extension
if (filename && filename.indexOf('.') > -1) {
ext = filename.split('.').pop();
}
// append extension if it doesn't exist
if (extension) {
if (ext !== extension) {
ext = extension;
filename = filename + '.' + ext;
}
}
return [filename, ext];
}
p5.prototype._checkFileExtension = _checkFileExtension;
/**
* Returns true if the browser is Safari, false if not.
* Safari makes trouble for downloading files.
*
* @return {Boolean} [description]
* @private
*/
p5.prototype._isSafari = function() {
var x = Object.prototype.toString.call(window.HTMLElement);
return x.indexOf('Constructor') > 0;
};
/**
* Helper function, a callback for download that deletes
* an invisible anchor element from the DOM once the file
* has been automatically downloaded.
*
* @private
*/
function destroyClickedElement(event) {
document.body.removeChild(event.target);
}
module.exports = p5;
},{"../core/core":48,"../core/error_helpers":51,"opentype.js":8,"reqwest":27}],71:[function(_dereq_,module,exports){
/**
* @module IO
* @submodule Table
* @requires core
*/
'use strict';
var p5 = _dereq_('../core/core');
/**
* Table Options
* Generic class for handling tabular data, typically from a
* CSV, TSV, or other sort of spreadsheet file.
* CSV files are
*
* comma separated values, often with the data in quotes. TSV
* files use tabs as separators, and usually don't bother with the
* quotes.
* File names should end with .csv if they're comma separated.
* A rough "spec" for CSV can be found
* here.
* To load files, use the loadTable method.
* To save tables to your computer, use the save method
* or the saveTable method.
*
* Possible options include:
*
* - csv - parse the table as comma-separated values
*
- tsv - parse the table as tab-separated values
*
- header - this table has a header (title) row
*
*/
/**
* Table objects store data with multiple rows and columns, much
* like in a traditional spreadsheet. Tables can be generated from
* scratch, dynamically, or using data from an existing file.
*
* @class p5.Table
* @constructor
* @param {Array} [rows] An array of p5.TableRow objects
* @return {p5.Table} p5.Table generated
*/
p5.Table = function (rows) {
/**
* @property columns
* @type {Array}
*/
this.columns = [];
/**
* @property rows
* @type {Array}
*/
this.rows = [];
};
/**
* Use addRow() to add a new row of data to a p5.Table object. By default,
* an empty row is created. Typically, you would store a reference to
* the new row in a TableRow object (see newRow in the example above),
* and then set individual values using set().
*
* If a p5.TableRow object is included as a parameter, then that row is
* duplicated and added to the table.
*
* @method addRow
* @param {p5.TableRow} [row] row to be added to the table
*
* @example
*
*
* // Given the CSV file "mammals.csv"
* // in the project's "assets" folder:
* //
* // id,species,name
* // 0,Capra hircus,Goat
* // 1,Panthera pardus,Leopard
* // 2,Equus zebra,Zebra
*
* var table;
*
* function preload() {
* //my table is comma separated value "csv"
* //and has a header specifying the columns labels
* table = loadTable("assets/mammals.csv", "csv", "header");
* }
*
* function setup() {
* //add a row
* var newRow = table.addRow();
* newRow.setString("id", table.getRowCount() - 1);
* newRow.setString("species", "Canis Lupus");
* newRow.setString("name", "Wolf");
*
* //print the results
* for (var r = 0; r < table.getRowCount(); r++)
* for (var c = 0; c < table.getColumnCount(); c++)
* print(table.getString(r, c));
* }
*
*
*/
p5.Table.prototype.addRow = function(row) {
// make sure it is a valid TableRow
var r = row || new p5.TableRow();
if (typeof(r.arr) === 'undefined' || typeof(r.obj) === 'undefined') {
//r = new p5.prototype.TableRow(r);
throw 'invalid TableRow: ' + r;
}
r.table = this;
this.rows.push(r);
return r;
};
/**
* Removes a row from the table object.
*
* @method removeRow
* @param {Number} id ID number of the row to remove
*
* @example
*
*
* // Given the CSV file "mammals.csv"
* // in the project's "assets" folder:
* //
* // id,species,name
* // 0,Capra hircus,Goat
* // 1,Panthera pardus,Leopard
* // 2,Equus zebra,Zebra
*
* var table;
*
* function preload() {
* //my table is comma separated value "csv"
* //and has a header specifying the columns labels
* table = loadTable("assets/mammals.csv", "csv", "header");
* }
*
* function setup() {
* //remove the first row
* var r = table.removeRow(0);
*
* //print the results
* for (var r = 0; r < table.getRowCount(); r++)
* for (var c = 0; c < table.getColumnCount(); c++)
* print(table.getString(r, c));
* }
*
*
*/
p5.Table.prototype.removeRow = function(id) {
this.rows[id].table = null; // remove reference to table
var chunk = this.rows.splice(id+1, this.rows.length);
this.rows.pop();
this.rows = this.rows.concat(chunk);
};
/**
* Returns a reference to the specified p5.TableRow. The reference
* can then be used to get and set values of the selected row.
*
* @method getRow
* @param {Number} rowID ID number of the row to get
* @return {TableRow} p5.TableRow object
*
* @example
*
*
* // Given the CSV file "mammals.csv"
* // in the project's "assets" folder:
* //
* // id,species,name
* // 0,Capra hircus,Goat
* // 1,Panthera pardus,Leopard
* // 2,Equus zebra,Zebra
*
* var table;
*
* function preload() {
* //my table is comma separated value "csv"
* //and has a header specifying the columns labels
* table = loadTable("assets/mammals.csv", "csv", "header");
* }
*
* function setup() {
* var row = table.getRow(1);
* //print it column by column
* //note: a row is an object, not an array
* for (var c = 0; c < table.getColumnCount(); c++)
* print(row.getString(c));
* }
*
*
*/
p5.Table.prototype.getRow = function(r) {
return this.rows[r];
};
/**
* Gets all rows from the table. Returns an array of p5.TableRows.
*
* @method getRows
* @return {Array} Array of p5.TableRows
*
* @example
*
*
* // Given the CSV file "mammals.csv"
* // in the project's "assets" folder:
* //
* // id,species,name
* // 0,Capra hircus,Goat
* // 1,Panthera pardus,Leopard
* // 2,Equus zebra,Zebra
*
* var table;
*
* function preload() {
* //my table is comma separated value "csv"
* //and has a header specifying the columns labels
* table = loadTable("assets/mammals.csv", "csv", "header");
* }
*
* function setup() {
* var rows = table.getRows();
*
* //warning: rows is an array of objects
* for (var r = 0; r < rows.length; r++)
* rows[r].set("name", "Unicorn");
*
* //print the results
* for (var r = 0; r < table.getRowCount(); r++)
* for (var c = 0; c < table.getColumnCount(); c++)
* print(table.getString(r, c));
* }
*
*
*/
p5.Table.prototype.getRows = function() {
return this.rows;
};
/**
* Finds the first row in the Table that contains the value
* provided, and returns a reference to that row. Even if
* multiple rows are possible matches, only the first matching
* row is returned. The column to search may be specified by
* either its ID or title.
*
* @method findRow
* @param {String} value The value to match
* @param {Number|String} column ID number or title of the
* column to search
* @return {TableRow}
*
* @example
*
*
* // Given the CSV file "mammals.csv"
* // in the project's "assets" folder:
* //
* // id,species,name
* // 0,Capra hircus,Goat
* // 1,Panthera pardus,Leopard
* // 2,Equus zebra,Zebra
*
* var table;
*
* function preload() {
* //my table is comma separated value "csv"
* //and has a header specifying the columns labels
* table = loadTable("assets/mammals.csv", "csv", "header");
* }
*
* function setup() {
* //find the animal named zebra
* var row = table.findRow("Zebra", "name");
* //find the corresponding species
* print(row.getString("species"));
* }
*
*
*/
p5.Table.prototype.findRow = function(value, column) {
// try the Object
if (typeof(column) === 'string') {
for (var i = 0; i < this.rows.length; i++){
if (this.rows[i].obj[column] === value) {
return this.rows[i];
}
}
}
// try the Array
else {
for (var j = 0; j < this.rows.length; j++){
if (this.rows[j].arr[column] === value) {
return this.rows[j];
}
}
}
// otherwise...
return null;
};
/**
* Finds the rows in the Table that contain the value
* provided, and returns references to those rows. Returns an
* Array, so for must be used to iterate through all the rows,
* as shown in the example above. The column to search may be
* specified by either its ID or title.
*
* @method findRows
* @param {String} value The value to match
* @param {Number|String} column ID number or title of the
* column to search
* @return {Array} An Array of TableRow objects
*
* @example
*
*
* // Given the CSV file "mammals.csv"
* // in the project's "assets" folder:
* //
* // id,species,name
* // 0,Capra hircus,Goat
* // 1,Panthera pardus,Leopard
* // 2,Equus zebra,Zebra
*
* var table;
*
* function preload() {
* //my table is comma separated value "csv"
* //and has a header specifying the columns labels
* table = loadTable("assets/mammals.csv", "csv", "header");
* }
*
* function setup() {
* //add another goat
* var newRow = table.addRow();
* newRow.setString("id", table.getRowCount() - 1);
* newRow.setString("species", "Scape Goat");
* newRow.setString("name", "Goat");
*
* //find the rows containing animals named Goat
* var rows = table.findRows("Goat", "name");
* print(rows.length + " Goats found");
* }
*
*
*/
p5.Table.prototype.findRows = function(value, column) {
var ret = [];
if (typeof(column) === 'string') {
for (var i = 0; i < this.rows.length; i++){
if (this.rows[i].obj[column] === value) {
ret.push( this.rows[i] );
}
}
}
// try the Array
else {
for (var j = 0; j < this.rows.length; j++){
if (this.rows[j].arr[column] === value) {
ret.push( this.rows[j] );
}
}
}
return ret;
};
/**
* Finds the first row in the Table that matches the regular
* expression provided, and returns a reference to that row.
* Even if multiple rows are possible matches, only the first
* matching row is returned. The column to search may be
* specified by either its ID or title.
*
* @method matchRow
* @param {String} regexp The regular expression to match
* @param {String|Number} column The column ID (number) or
* title (string)
* @return {TableRow} TableRow object
*/
p5.Table.prototype.matchRow = function(regexp, column) {
if (typeof(column) === 'number') {
for (var j = 0; j < this.rows.length; j++) {
if ( this.rows[j].arr[column].match(regexp) ) {
return this.rows[j];
}
}
}
else {
for (var i = 0; i < this.rows.length; i++) {
if ( this.rows[i].obj[column].match(regexp) ) {
return this.rows[i];
}
}
}
return null;
};
/**
* Finds the first row in the Table that matches the regular
* expression provided, and returns a reference to that row.
* Even if multiple rows are possible matches, only the first
* matching row is returned. The column to search may be specified
* by either its ID or title.
*
* @method matchRows
* @param {String} regexp The regular expression to match
* @param {String|Number} [column] The column ID (number) or
* title (string)
* @return {Array} An Array of TableRow objects
*/
p5.Table.prototype.matchRows = function(regexp, column) {
var ret = [];
if (typeof(column) === 'number') {
for (var j = 0; j < this.rows.length; j++) {
if ( this.rows[j].arr[column].match(regexp) ) {
ret.push( this.rows[j] );
}
}
}
else {
for (var i = 0; i < this.rows.length; i++) {
if ( this.rows[i].obj[column].match(regexp) ) {
ret.push( this.rows[i] );
}
}
}
return ret;
};
/**
* Retrieves all values in the specified column, and returns them
* as an array. The column may be specified by either its ID or title.
*
* @method getColumn
* @param {String|Number} column String or Number of the column to return
* @return {Array} Array of column values
*
* @example
*
*
* // Given the CSV file "mammals.csv"
* // in the project's "assets" folder:
* //
* // id,species,name
* // 0,Capra hircus,Goat
* // 1,Panthera pardus,Leopard
* // 2,Equus zebra,Zebra
*
* var table;
*
* function preload() {
* //my table is comma separated value "csv"
* //and has a header specifying the columns labels
* table = loadTable("assets/mammals.csv", "csv", "header");
* }
*
* function setup() {
* //getColumn returns an array that can be printed directly
* print(table.getColumn("species"));
* //outputs ["Capra hircus", "Panthera pardus", "Equus zebra"]
* }
*
*
*/
p5.Table.prototype.getColumn = function(value) {
var ret = [];
if (typeof(value) === 'string'){
for (var i = 0; i < this.rows.length; i++){
ret.push (this.rows[i].obj[value]);
}
} else {
for (var j = 0; j < this.rows.length; j++){
ret.push (this.rows[j].arr[value]);
}
}
return ret;
};
/**
* Removes all rows from a Table. While all rows are removed,
* columns and column titles are maintained.
*
* @method clearRows
*
* @example
*
*
* // Given the CSV file "mammals.csv"
* // in the project's "assets" folder:
* //
* // id,species,name
* // 0,Capra hircus,Goat
* // 1,Panthera pardus,Leopard
* // 2,Equus zebra,Zebra
*
* var table;
*
* function preload() {
* //my table is comma separated value "csv"
* //and has a header specifying the columns labels
* table = loadTable("assets/mammals.csv", "csv", "header");
* }
*
* function setup() {
* table.clearRows();
* print(table.getRowCount() + " total rows in table");
* print(table.getColumnCount() + " total columns in table");
* }
*
*
*/
p5.Table.prototype.clearRows = function() {
delete this.rows;
this.rows = [];
};
/**
* Use addColumn() to add a new column to a Table object.
* Typically, you will want to specify a title, so the column
* may be easily referenced later by name. (If no title is
* specified, the new column's title will be null.)
*
* @method addColumn
* @param {String} [title] title of the given column
*
* @example
*
*
* // Given the CSV file "mammals.csv"
* // in the project's "assets" folder:
* //
* // id,species,name
* // 0,Capra hircus,Goat
* // 1,Panthera pardus,Leopard
* // 2,Equus zebra,Zebra
*
* var table;
*
* function preload() {
* //my table is comma separated value "csv"
* //and has a header specifying the columns labels
* table = loadTable("assets/mammals.csv", "csv", "header");
* }
*
* function setup() {
* table.addColumn("carnivore");
* table.set(0, "carnivore", "no");
* table.set(1, "carnivore", "yes");
* table.set(2, "carnivore", "no");
*
* //print the results
* for (var r = 0; r < table.getRowCount(); r++)
* for (var c = 0; c < table.getColumnCount(); c++)
* print(table.getString(r, c));
* }
*
*
*/
p5.Table.prototype.addColumn = function(title) {
var t = title || null;
this.columns.push(t);
};
/**
* Returns the total number of columns in a Table.
*
* @return {Number} Number of columns in this table
*/
p5.Table.prototype.getColumnCount = function() {
return this.columns.length;
};
/**
* Returns the total number of rows in a Table.
*
* @method getRowCount
* @return {Number} Number of rows in this table
*/
p5.Table.prototype.getRowCount = function() {
return this.rows.length;
};
/**
* Removes any of the specified characters (or "tokens").
*
* If no column is specified, then the values in all columns and
* rows are processed. A specific column may be referenced by
* either its ID or title.
*
* @method removeTokens
* @param {String} chars String listing characters to be removed
* @param {String|Number} [column] Column ID (number)
* or name (string)
*/
p5.Table.prototype.removeTokens = function(chars, column) {
var escape= function(s) {
return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
};
var charArray = [];
for (var i = 0; i < chars.length; i++) {
charArray.push( escape( chars.charAt(i) ) );
}
var regex = new RegExp(charArray.join('|'), 'g');
if (typeof(column) === 'undefined'){
for (var c = 0; c < this.columns.length; c++) {
for (var d = 0; d < this.rows.length; d++) {
var s = this.rows[d].arr[c];
s = s.replace(regex, '');
this.rows[d].arr[c] = s;
this.rows[d].obj[this.columns[c]] = s;
}
}
}
else if (typeof(column) === 'string'){
for (var j = 0; j < this.rows.length; j++) {
var val = this.rows[j].obj[column];
val = val.replace(regex, '');
this.rows[j].obj[column] = val;
var pos = this.columns.indexOf(column);
this.rows[j].arr[pos] = val;
}
}
else {
for (var k = 0; k < this.rows.length; k++) {
var str = this.rows[k].arr[column];
str = str.replace(regex, '');
this.rows[k].arr[column] = str;
this.rows[k].obj[this.columns[column]] = str;
}
}
};
/**
* Trims leading and trailing whitespace, such as spaces and tabs,
* from String table values. If no column is specified, then the
* values in all columns and rows are trimmed. A specific column
* may be referenced by either its ID or title.
*
* @method trim
* @param {String|Number} column Column ID (number)
* or name (string)
*/
p5.Table.prototype.trim = function(column) {
var regex = new RegExp( (' '), 'g');
if (typeof(column) === 'undefined'){
for (var c = 0; c < this.columns.length; c++) {
for (var d = 0; d < this.rows.length; d++) {
var s = this.rows[d].arr[c];
s = s.replace(regex, '');
this.rows[d].arr[c] = s;
this.rows[d].obj[this.columns[c]] = s;
}
}
}
else if (typeof(column) === 'string'){
for (var j = 0; j < this.rows.length; j++) {
var val = this.rows[j].obj[column];
val = val.replace(regex, '');
this.rows[j].obj[column] = val;
var pos = this.columns.indexOf(column);
this.rows[j].arr[pos] = val;
}
}
else {
for (var k = 0; k < this.rows.length; k++) {
var str = this.rows[k].arr[column];
str = str.replace(regex, '');
this.rows[k].arr[column] = str;
this.rows[k].obj[this.columns[column]] = str;
}
}
};
/**
* Use removeColumn() to remove an existing column from a Table
* object. The column to be removed may be identified by either
* its title (a String) or its index value (an int).
* removeColumn(0) would remove the first column, removeColumn(1)
* would remove the second column, and so on.
*
* @method removeColumn
* @param {String|Number} column columnName (string) or ID (number)
*
* @example
*
*
* // Given the CSV file "mammals.csv"
* // in the project's "assets" folder:
* //
* // id,species,name
* // 0,Capra hircus,Goat
* // 1,Panthera pardus,Leopard
* // 2,Equus zebra,Zebra
*
* var table;
*
* function preload() {
* //my table is comma separated value "csv"
* //and has a header specifying the columns labels
* table = loadTable("assets/mammals.csv", "csv", "header");
* }
*
* function setup() {
* table.removeColumn("id");
* print(table.getColumnCount());
* }
*
*
*/
p5.Table.prototype.removeColumn = function(c) {
var cString;
var cNumber;
if (typeof(c) === 'string') {
// find the position of c in the columns
cString = c;
cNumber = this.columns.indexOf(c);
console.log('string');
}
else{
cNumber = c;
cString = this.columns[c];
}
var chunk = this.columns.splice(cNumber+1, this.columns.length);
this.columns.pop();
this.columns = this.columns.concat(chunk);
for (var i = 0; i < this.rows.length; i++){
var tempR = this.rows[i].arr;
var chip = tempR.splice(cNumber+1, tempR.length);
tempR.pop();
this.rows[i].arr = tempR.concat(chip);
delete this.rows[i].obj[cString];
}
};
/**
* Stores a value in the Table's specified row and column.
* The row is specified by its ID, while the column may be specified
* by either its ID or title.
*
* @method set
* @param {String|Number} column column ID (Number)
* or title (String)
* @param {String|Number} value value to assign
*
* @example
*
*
* // Given the CSV file "mammals.csv"
* // in the project's "assets" folder:
* //
* // id,species,name
* // 0,Capra hircus,Goat
* // 1,Panthera pardus,Leopard
* // 2,Equus zebra,Zebra
*
* var table;
*
* function preload() {
* //my table is comma separated value "csv"
* //and has a header specifying the columns labels
* table = loadTable("assets/mammals.csv", "csv", "header");
* }
*
* function setup() {
* table.set(0, "species", "Canis Lupus");
* table.set(0, "name", "Wolf");
*
* //print the results
* for (var r = 0; r < table.getRowCount(); r++)
* for (var c = 0; c < table.getColumnCount(); c++)
* print(table.getString(r, c));
* }
*
*
*/
p5.Table.prototype.set = function(row, column, value) {
this.rows[row].set(column, value);
};
/**
* Stores a Float value in the Table's specified row and column.
* The row is specified by its ID, while the column may be specified
* by either its ID or title.
*
* @method setNum
* @param {Number} row row ID
* @param {String|Number} column column ID (Number)
* or title (String)
* @param {Number} value value to assign
*
* @example
*
*
* // Given the CSV file "mammals.csv"
* // in the project's "assets" folder:
* //
* // id,species,name
* // 0,Capra hircus,Goat
* // 1,Panthera pardus,Leopard
* // 2,Equus zebra,Zebra
*
* var table;
*
* function preload() {
* //my table is comma separated value "csv"
* //and has a header specifying the columns labels
* table = loadTable("assets/mammals.csv", "csv", "header");
* }
*
* function setup() {
* table.setNum(1, "id", 1);
*
* print(table.getColumn(0));
* //["0", 1, "2"]
* }
*
*
*/
p5.Table.prototype.setNum = function(row, column, value){
this.rows[row].setNum(column, value);
};
/**
* Stores a String value in the Table's specified row and column.
* The row is specified by its ID, while the column may be specified
* by either its ID or title.
*
* @method setString
* @param {Number} row row ID
* @param {String|Number} column column ID (Number)
* or title (String)
* @param {String} value value to assign
*/
p5.Table.prototype.setString = function(row, column, value){
this.rows[row].setString(column, value);
};
/**
* Retrieves a value from the Table's specified row and column.
* The row is specified by its ID, while the column may be specified by
* either its ID or title.
*
* @method get
* @param {Number} row row ID
* @param {String|Number} column columnName (string) or
* ID (number)
* @return {String|Number}
*
* @example
*
*
* // Given the CSV file "mammals.csv"
* // in the project's "assets" folder:
* //
* // id,species,name
* // 0,Capra hircus,Goat
* // 1,Panthera pardus,Leopard
* // 2,Equus zebra,Zebra
*
* var table;
*
* function preload() {
* //my table is comma separated value "csv"
* //and has a header specifying the columns labels
* table = loadTable("assets/mammals.csv", "csv", "header");
* }
*
* function setup() {
* print(table.get(0, 1));
* //Capra hircus
* print(table.get(0, "species"));
* //Capra hircus
* }
*
*
*/
p5.Table.prototype.get = function(row, column) {
return this.rows[row].get(column);
};
/**
* Retrieves a Float value from the Table's specified row and column.
* The row is specified by its ID, while the column may be specified by
* either its ID or title.
*
* @method getNum
* @param {Number} row row ID
* @param {String|Number} column columnName (string) or
* ID (number)
* @return {Number}
*
* @example
*
*
* // Given the CSV file "mammals.csv"
* // in the project's "assets" folder:
* //
* // id,species,name
* // 0,Capra hircus,Goat
* // 1,Panthera pardus,Leopard
* // 2,Equus zebra,Zebra
*
* var table;
*
* function preload() {
* //my table is comma separated value "csv"
* //and has a header specifying the columns labels
* table = loadTable("assets/mammals.csv", "csv", "header");
* }
*
* function setup() {
* print(table.getNum(1, 0) + 100);
* //id 1 + 100 = 101
* }
*
*
*/
p5.Table.prototype.getNum = function(row, column) {
return this.rows[row].getNum(column);
};
/**
* Retrieves a String value from the Table's specified row and column.
* The row is specified by its ID, while the column may be specified by
* either its ID or title.
*
* @method getString
* @param {Number} row row ID
* @param {String|Number} column columnName (string) or
* ID (number)
* @return {String}
*
* @example
*
*
* // Given the CSV file "mammals.csv"
* // in the project's "assets" folder:
* //
* // id,species,name
* // 0,Capra hircus,Goat
* // 1,Panthera pardus,Leopard
* // 2,Equus zebra,Zebra
*
* var table;
*
* function preload() {
* //my table is comma separated value "csv"
* //and has a header specifying the columns labels
* table = loadTable("assets/mammals.csv", "csv", "header");
* }
*
* function setup() {
* var tableArray = table.getArray();
*
* //output each row as array
* for (var i = 0; i < tableArray.length; i++)
* print(tableArray[i]);
* }
*
*
*/
p5.Table.prototype.getString = function(row, column) {
return this.rows[row].getString(column);
};
/**
* Retrieves all table data and returns as an object. If a column name is
* passed in, each row object will be stored with that attribute as its
* title.
*
* @method getObject
* @param {String} headerColumn Name of the column which should be used to
* title each row object (optional)
* @return {Object}
*
* @example
*
*
* // Given the CSV file "mammals.csv"
* // in the project's "assets" folder:
* //
* // id,species,name
* // 0,Capra hircus,Goat
* // 1,Panthera pardus,Leopard
* // 2,Equus zebra,Zebra
*
* var table;
*
* function preload() {
* //my table is comma separated value "csv"
* //and has a header specifying the columns labels
* table = loadTable("assets/mammals.csv", "csv", "header");
* }
*
* function setup() {
* var tableObject = table.getObject();
*
* print(tableObject);
* //outputs an object
* }
*
*
*/
p5.Table.prototype.getObject = function (headerColumn) {
var tableObject = {};
var obj, cPos, index;
for(var i = 0; i < this.rows.length; i++) {
obj = this.rows[i].obj;
if (typeof(headerColumn) === 'string'){
cPos = this.columns.indexOf(headerColumn); // index of columnID
if (cPos >= 0) {
index = obj[headerColumn];
tableObject[index] = obj;
} else {
throw 'This table has no column named "' + headerColumn +'"';
}
} else {
tableObject[i] = this.rows[i].obj;
}
}
return tableObject;
};
/**
* Retrieves all table data and returns it as a multidimensional array.
*
* @method getArray
* @return {Array}
*/
p5.Table.prototype.getArray = function () {
var tableArray = [];
for(var i = 0; i < this.rows.length; i++) {
tableArray.push(this.rows[i].arr);
}
return tableArray;
};
module.exports = p5.Table;
},{"../core/core":48}],72:[function(_dereq_,module,exports){
/**
* @module IO
* @submodule Table
* @requires core
*/
'use strict';
var p5 = _dereq_('../core/core');
/**
* A TableRow object represents a single row of data values,
* stored in columns, from a table.
*
* A Table Row contains both an ordered array, and an unordered
* JSON object.
*
* @class p5.TableRow
* @constructor
* @param {String} [str] optional: populate the row with a
* string of values, separated by the
* separator
* @param {String} [separator] comma separated values (csv) by default
*/
p5.TableRow = function (str, separator) {
var arr = [];
var obj = {};
if (str){
separator = separator || ',';
arr = str.split(separator);
}
for (var i = 0; i < arr.length; i++){
var key = i;
var val = arr[i];
obj[key] = val;
}
this.arr = arr;
this.obj = obj;
this.table = null;
};
/**
* Stores a value in the TableRow's specified column.
* The column may be specified by either its ID or title.
*
* @method set
* @param {String|Number} column Column ID (Number)
* or Title (String)
* @param {String|Number} value The value to be stored
*/
p5.TableRow.prototype.set = function(column, value) {
// if typeof column is string, use .obj
if (typeof(column) === 'string'){
var cPos = this.table.columns.indexOf(column); // index of columnID
if (cPos >= 0) {
this.obj[column] = value;
this.arr[cPos] = value;
}
else {
throw 'This table has no column named "' + column +'"';
}
}
// if typeof column is number, use .arr
else {
if (column < this.table.columns.length) {
this.arr[column] = value;
var cTitle = this.table.columns[column];
this.obj[cTitle] = value;
}
else {
throw 'Column #' + column + ' is out of the range of this table';
}
}
};
/**
* Stores a Float value in the TableRow's specified column.
* The column may be specified by either its ID or title.
*
* @method setNum
* @param {String|Number} column Column ID (Number)
* or Title (String)
* @param {Number} value The value to be stored
* as a Float
*/
p5.TableRow.prototype.setNum = function(column, value){
var floatVal = parseFloat(value, 10);
this.set(column, floatVal);
};
/**
* Stores a String value in the TableRow's specified column.
* The column may be specified by either its ID or title.
*
* @method setString
* @param {String|Number} column Column ID (Number)
* or Title (String)
* @param {String} value The value to be stored
* as a String
*/
p5.TableRow.prototype.setString = function(column, value){
var stringVal = value.toString();
this.set(column, stringVal);
};
/**
* Retrieves a value from the TableRow's specified column.
* The column may be specified by either its ID or title.
*
* @method get
* @param {String|Number} column columnName (string) or
* ID (number)
* @return {String|Number}
*/
p5.TableRow.prototype.get = function(column) {
if (typeof(column) === 'string'){
return this.obj[column];
} else {
return this.arr[column];
}
};
/**
* Retrieves a Float value from the TableRow's specified
* column. The column may be specified by either its ID or
* title.
*
* @method getNum
* @param {String|Number} column columnName (string) or
* ID (number)
* @return {Number} Float Floating point number
*/
p5.TableRow.prototype.getNum = function(column) {
var ret;
if (typeof(column) === 'string'){
ret = parseFloat(this.obj[column], 10);
} else {
ret = parseFloat(this.arr[column], 10);
}
if (ret.toString() === 'NaN') {
throw 'Error: ' + this.obj[column]+ ' is NaN (Not a Number)';
}
return ret;
};
/**
* Retrieves an String value from the TableRow's specified
* column. The column may be specified by either its ID or
* title.
*
* @method getString
* @param {String|Number} column columnName (string) or
* ID (number)
* @return {String} String
*/
p5.TableRow.prototype.getString = function(column) {
if (typeof(column) === 'string'){
return this.obj[column].toString();
} else {
return this.arr[column].toString();
}
};
module.exports = p5.TableRow;
},{"../core/core":48}],73:[function(_dereq_,module,exports){
/**
* @module Math
* @submodule Calculation
* @for p5
* @requires core
*/
'use strict';
var p5 = _dereq_('../core/core');
/**
* Calculates the absolute value (magnitude) of a number. Maps to Math.abs().
* The absolute value of a number is always positive.
*
* @method abs
* @param {Number} n number to compute
* @return {Number} absolute value of given number
* @example
*
* function setup() {
* var x = -3;
* var y = abs(x);
*
* print(x); // -3
* print(y); // 3
* }
*
*/
p5.prototype.abs = Math.abs;
/**
* Calculates the closest int value that is greater than or equal to the
* value of the parameter. Maps to Math.ceil(). For example, ceil(9.03)
* returns the value 10.
*
* @method ceil
* @param {Number} n number to round up
* @return {Number} rounded up number
* @example
*
* function draw() {
* background(200);
* // map, mouseX between 0 and 5.
* var ax = map(mouseX, 0, 100, 0, 5);
* var ay = 66;
*
* //Get the ceiling of the mapped number.
* var bx = ceil(map(mouseX, 0, 100, 0,5));
* var by = 33;
*
* // Multiply the mapped numbers by 20 to more easily
* // see the changes.
* stroke(0);
* fill(0);
* line(0, ay, ax * 20, ay);
* line(0, by, bx * 20, by);
*
* // Reformat the float returned by map and draw it.
* noStroke();
* text(nfc(ax, 2,2), ax, ay - 5);
* text(nfc(bx,1,1), bx, by - 5);
* }
*
*/
p5.prototype.ceil = Math.ceil;
/**
* Constrains a value between a minimum and maximum value.
*
* @method constrain
* @param {Number} n number to constrain
* @param {Number} low minimum limit
* @param {Number} high maximum limit
* @return {Number} constrained number
* @example
*
* function draw() {
* background(200);
*
* var leftWall = 25;
* var rightWall = 75;
*
* // xm is just the mouseX, while
* // xc is the mouseX, but constrained
* // between the leftWall and rightWall!
* var xm = mouseX;
* var xc = constrain(mouseX, leftWall, rightWall);
*
* // Draw the walls.
* stroke(150);
* line(leftWall, 0, leftWall, height);
* line(rightWall, 0, rightWall, height);
*
* // Draw xm and xc as circles.
* noStroke();
* fill(150);
* ellipse(xm, 33, 9,9); // Not Constrained
* fill(0);
* ellipse(xc, 66, 9,9); // Constrained
* }
*
*/
p5.prototype.constrain = function(n, low, high) {
return Math.max(Math.min(n, high), low);
};
/**
* Calculates the distance between two points.
*
* @method dist
* @param {Number} x1 x-coordinate of the first point
* @param {Number} y1 y-coordinate of the first point
* @param {Number} [z1] z-coordinate of the first point
* @param {Number} x2 x-coordinate of the second point
* @param {Number} y2 y-coordinate of the second point
* @param {Number} [z2] z-coordinate of the second point
* @return {Number} distance between the two points
* @example
*
* // Move your mouse inside the canvas to see the
* // change in distance between two points!
* function draw() {
* background(200);
* fill(0);
*
* var x1 = 10;
* var y1 = 90;
* var x2 = mouseX;
* var y2 = mouseY;
*
* line(x1, y1, x2, y2);
* ellipse(x1, y1, 7, 7);
* ellipse(x2, y2, 7, 7);
*
* // d is the length of the line
* // the distance from point 1 to point 2.
* var d = int(dist(x1, y1, x2, y2));
*
* // Let's write d along the line we are drawing!
* push();
* translate( (x1+x2)/2, (y1+y2)/2 );
* rotate( atan2(y2-y1,x2-x1) );
* text(nfc(d,1,1), 0, -5);
* pop();
* // Fancy!
* }
*
*/
p5.prototype.dist = function(x1, y1, z1, x2, y2, z2) {
if (arguments.length === 4) {
// In the case of 2d: z1 means x2 and x2 means y2
return Math.sqrt( (z1-x1)*(z1-x1) + (x2-y1)*(x2-y1) );
} else if (arguments.length === 6) {
return Math.sqrt( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) + (z2-z1)*(z2-z1) );
}
};
/**
* Returns Euler's number e (2.71828...) raised to the power of the n
* parameter. Maps to Math.exp().
*
* @method exp
* @param {Number} n exponent to raise
* @return {Number} e^n
* @example
*
* function draw() {
* background(200);
*
* // Compute the exp() function with a value between 0 and 2
* var xValue = map(mouseX, 0, width, 0, 2);
* var yValue = exp(xValue);
*
* var y = map(yValue, 0, 8, height, 0);
*
* var legend = "exp (" + nfc(xValue, 3) +")\n= " + nf(yValue, 1, 4);
* stroke(150);
* line(mouseX, y, mouseX, height);
* fill(0);
* text(legend, 5, 15);
* noStroke();
* ellipse (mouseX,y, 7, 7);
*
* // Draw the exp(x) curve,
* // over the domain of x from 0 to 2
* noFill();
* stroke(0);
* beginShape();
* for (var x = 0; x < width; x++) {
* xValue = map(x, 0, width, 0, 2);
* yValue = exp(xValue);
* y = map(yValue, 0, 8, height, 0);
* vertex(x, y);
* }
*
* endShape();
* line(0, 0, 0, height);
* line(0, height-1, width, height-1);
* }
*
*/
p5.prototype.exp = Math.exp;
/**
* Calculates the closest int value that is less than or equal to the
* value of the parameter. Maps to Math.floor().
*
* @method floor
* @param {Number} n number to round down
* @return {Number} rounded down number
* @example
*
* function draw() {
* background(200);
* //map, mouseX between 0 and 5.
* var ax = map(mouseX, 0, 100, 0, 5);
* var ay = 66;
*
* //Get the floor of the mapped number.
* var bx = floor(map(mouseX, 0, 100, 0,5));
* var by = 33;
*
* // Multiply the mapped numbers by 20 to more easily
* // see the changes.
* stroke(0);
* fill(0);
* line(0, ay, ax * 20, ay);
* line(0, by, bx * 20, by);
*
* // Reformat the float returned by map and draw it.
* noStroke();
* text(nfc(ax, 2,2), ax, ay - 5);
* text(nfc(bx,1,1), bx, by - 5);
* }
*
*/
p5.prototype.floor = Math.floor;
/**
* Calculates a number between two numbers at a specific increment. The amt
* parameter is the amount to interpolate between the two values where 0.0
* equal to the first point, 0.1 is very near the first point, 0.5 is
* half-way in between, etc. The lerp function is convenient for creating
* motion along a straight path and for drawing dotted lines.
*
* @method lerp
* @param {Number} start first value
* @param {Number} stop second value
* @param {Number} amt number between 0.0 and 1.0
* @return {Number} lerped value
* @example
*
* function setup() {
* background(200);
* var a = 20;
* var b = 80;
* var c = lerp(a,b, .2);
* var d = lerp(a,b, .5);
* var e = lerp(a,b, .8);
*
* var y = 50
*
* strokeWeight(5);
* stroke(0); // Draw the original points in black
* point(a, y);
* point(b, y);
*
* stroke(100); // Draw the lerp points in gray
* point(c, y);
* point(d, y);
* point(e, y);
* }
*
*/
p5.prototype.lerp = function(start, stop, amt) {
return amt*(stop-start)+start;
};
/**
* Calculates the natural logarithm (the base-e logarithm) of a number. This
* function expects the n parameter to be a value greater than 0.0. Maps to
* Math.log().
*
* @method log
* @param {Number} n number greater than 0
* @return {Number} natural logarithm of n
* @example
*
* function draw() {
* background(200);
* var maxX = 2.8;
* var maxY = 1.5;
*
* // Compute the natural log of a value between 0 and maxX
* var xValue = map(mouseX, 0, width, 0, maxX);
* if (xValue > 0) { // Cannot take the log of a negative number.
* var yValue = log(xValue);
* var y = map(yValue, -maxY, maxY, height, 0);
*
* // Display the calculation occurring.
* var legend = "log(" + nf(xValue, 1, 2) + ")\n= " + nf(yValue, 1, 3);
* stroke(150);
* line(mouseX, y, mouseX, height);
* fill(0);
* text (legend, 5, 15);
* noStroke();
* ellipse (mouseX, y, 7, 7);
* }
*
* // Draw the log(x) curve,
* // over the domain of x from 0 to maxX
* noFill();
* stroke(0);
* beginShape();
* for(var x=0; x < width; x++) {
* xValue = map(x, 0, width, 0, maxX);
* yValue = log(xValue);
* y = map(yValue, -maxY, maxY, height, 0);
* vertex(x, y);
* }
* endShape();
* line(0,0,0,height);
* line(0,height/2,width, height/2);
* }
*
*/
p5.prototype.log = Math.log;
/**
* Calculates the magnitude (or length) of a vector. A vector is a direction
* in space commonly used in computer graphics and linear algebra. Because it
* has no "start" position, the magnitude of a vector can be thought of as
* the distance from the coordinate 0,0 to its x,y value. Therefore, mag() is
* a shortcut for writing dist(0, 0, x, y).
*
* @method mag
* @param {Number} a first value
* @param {Number} b second value
* @return {Number} magnitude of vector from (0,0) to (a,b)
* @example
*
* function setup() {
* var x1 = 20;
* var x2 = 80;
* var y1 = 30;
* var y2 = 70;
*
* line(0, 0, x1, y1);
* print(mag(x1, y1)); // Prints "36.05551"
* line(0, 0, x2, y1);
* print(mag(x2, y1)); // Prints "85.44004"
* line(0, 0, x1, y2);
* print(mag(x1, y2)); // Prints "72.8011"
* line(0, 0, x2, y2);
* print(mag(x2, y2)); // Prints "106.30146"
* }
*
*/
p5.prototype.mag = function(x, y) {
return Math.sqrt(x*x+y*y);
};
/**
* Re-maps a number from one range to another.
* In the first example above, the number 25 is converted from a value in the
* range of 0 to 100 into a value that ranges from the left edge of the
* window (0) to the right edge (width).
*
* @method map
* @param {Number} value the incoming value to be converted
* @param {Number} start1 lower bound of the value's current range
* @param {Number} stop1 upper bound of the value's current range
* @param {Number} start2 lower bound of the value's target range
* @param {Number} stop upper bound of the value's target range
* @return {Number} remapped number
* @example
*
* createCanvas(200, 200);
* var value = 25;
* var m = map(value, 0, 100, 0, width);
* ellipse(m, 200, 10, 10);
*
*
*
* function setup() {
* createCanvs(200, 200);
* noStroke();
* }
*
* function draw() {
* background(204);
* var x1 = map(mouseX, 0, width, 50, 150);
* ellipse(x1, 75, 50, 50);
* var x2 = map(mouseX, 0, width, 0, 200);
* ellipse(x2, 125, 50, 50);
* }
*
*/
p5.prototype.map = function(n, start1, stop1, start2, stop2) {
return ((n-start1)/(stop1-start1))*(stop2-start2)+start2;
};
/**
* Determines the largest value in a sequence of numbers, and then returns
* that value. max() accepts any number of Number parameters, or an Array
* of any length.
*
* @method max
* @param {Number|Array} n0 Numbers to compare
* @return {Number} maximum Number
* @example
*
* function setup() {
* // Change the elements in the array and run the sketch
* // to show how max() works!
* numArray = new Array(2,1,5,4,8,9);
* fill(0);
* noStroke();
* text("Array Elements", 0, 10);
* // Draw all numbers in the array
* var spacing = 15;
* var elemsY = 25;
* for(var i = 0; i < numArray.length; i++) {
* text(numArray[i], i * spacing, elemsY);
* }
* maxX = 33;
* maxY = 80;
* // Draw the Maximum value in the array.
* textSize(32);
* text(max(numArray), maxX, maxY);
* }
*
*/
p5.prototype.max = function() {
if (arguments[0] instanceof Array) {
return Math.max.apply(null,arguments[0]);
} else {
return Math.max.apply(null,arguments);
}
};
/**
* Determines the smallest value in a sequence of numbers, and then returns
* that value. min() accepts any number of Number parameters, or an Array
* of any length.
*
* @method min
* @param {Number|Array} n0 Numbers to compare
* @return {Number} minimum Number
* @example
*
* function setup() {
* // Change the elements in the array and run the sketch
* // to show how min() works!
* numArray = new Array(2,1,5,4,8,9);
* fill(0);
* noStroke();
* text("Array Elements", 0, 10);
* // Draw all numbers in the array
* var spacing = 15;
* var elemsY = 25;
* for(var i = 0; i < numArray.length; i++) {
* text(numArray[i], i * spacing, elemsY);
* }
* maxX = 33;
* maxY = 80;
* // Draw the Minimum value in the array.
* textSize(32);
* text(min(numArray), maxX, maxY);
* }
*
*/
p5.prototype.min = function() {
if (arguments[0] instanceof Array) {
return Math.min.apply(null,arguments[0]);
} else {
return Math.min.apply(null,arguments);
}
};
/**
* Normalizes a number from another range into a value between 0 and 1.
* Identical to map(value, low, high, 0, 1).
* Numbers outside of the range are not clamped to 0 and 1, because
* out-of-range values are often intentional and useful. (See the second
* example above.)
*
* @method norm
* @param {Number} value incoming value to be normalized
* @param {Number} start lower bound of the value's current range
* @param {Number} stop upper bound of the value's current range
* @return {Number} normalized number
* @example
*
* function draw() {
* background(200);
* currentNum = mouseX;
* lowerBound = 0;
* upperBound = width; //100;
* normalized = norm(currentNum, lowerBound, upperBound);
* lineY = 70
* line(0, lineY, width, lineY);
* //Draw an ellipse mapped to the non-normalized value.
* noStroke();
* fill(50)
* var s = 7; // ellipse size
* ellipse(currentNum, lineY, s, s);
*
* // Draw the guide
* guideY = lineY + 15;
* text("0", 0, guideY);
* textAlign(RIGHT);
* text("100", width, guideY);
*
* // Draw the normalized value
* textAlign(LEFT);
* fill(0);
* textSize(32);
* normalY = 40;
* normalX = 20;
* text(normalized, normalX, normalY);
* }
*
*/
p5.prototype.norm = function(n, start, stop) {
return this.map(n, start, stop, 0, 1);
};
/**
* Facilitates exponential expressions. The pow() function is an efficient
* way of multiplying numbers by themselves (or their reciprocals) in large
* quantities. For example, pow(3, 5) is equivalent to the expression
* 3*3*3*3*3 and pow(3, -5) is equivalent to 1 / 3*3*3*3*3. Maps to
* Math.pow().
*
* @method pow
* @param {Number} n base of the exponential expression
* @param {Number} e power by which to raise the base
* @return {Number} n^e
* @example
*
* function setup() {
* //Exponentially increase the size of an ellipse.
* eSize = 3; // Original Size
* eLoc = 10; // Original Location
*
* ellipse(eLoc, eLoc, eSize, eSize);
*
* ellipse(eLoc*2, eLoc*2, pow(eSize, 2), pow(eSize, 2));
*
* ellipse(eLoc*4, eLoc*4, pow(eSize, 3), pow(eSize, 3));
*
* ellipse(eLoc*8, eLoc*8, pow(eSize, 4), pow(eSize, 4));
* }
*
*/
p5.prototype.pow = Math.pow;
/**
* Calculates the integer closest to the n parameter. For example,
* round(133.8) returns the value 134. Maps to Math.round().
*
* @method round
* @param {Number} n number to round
* @return {Number} rounded number
* @example
*
* function draw() {
* background(200);
* //map, mouseX between 0 and 5.
* var ax = map(mouseX, 0, 100, 0, 5);
* var ay = 66;
*
* // Round the mapped number.
* var bx = round(map(mouseX, 0, 100, 0,5));
* var by = 33;
*
* // Multiply the mapped numbers by 20 to more easily
* // see the changes.
* stroke(0);
* fill(0);
* line(0, ay, ax * 20, ay);
* line(0, by, bx * 20, by);
*
* // Reformat the float returned by map and draw it.
* noStroke();
* text(nfc(ax, 2,2), ax, ay - 5);
* text(nfc(bx,1,1), bx, by - 5);
* }
*
*/
p5.prototype.round = Math.round;
/**
* Squares a number (multiplies a number by itself). The result is always a
* positive number, as multiplying two negative numbers always yields a
* positive result. For example, -1 * -1 = 1.
*
* @method sq
* @param {Number} n number to square
* @return {Number} squared number
* @example
*
* function draw() {
* background(200);
* eSize = 7;
* x1 = map(mouseX, 0, width, 0, 10);
* y1 = 80;
* x2 = sq(x1);
* y2 = 20;
*
* // Draw the non-squared.
* line(0, y1, width, y1);
* ellipse(x1, y1, eSize, eSize);
*
* // Draw the squared.
* line(0, y2, width, y2);
* ellipse(x2, y2, eSize, eSize);
*
* // Draw dividing line.
* stroke(100)
* line(0, height/2, width, height/2);
*
* // Draw text.
* noStroke();
* fill(0);
* text("x = " + x1, 0, y1 + spacing);
* text("sqrt(x) = " + x2, 0, y2 + spacing);
* }
*
*/
p5.prototype.sq = function(n) { return n*n; };
/**
* Calculates the square root of a number. The square root of a number is
* always positive, even though there may be a valid negative root. The
* square root s of number a is such that s*s = a. It is the opposite of
* squaring. Maps to Math.sqrt().
*
* @method sqrt
* @param {Number} n non-negative number to square root
* @return {Number} square root of number
* @example
*
* function draw() {
* background(200);
* eSize = 7;
* x1 = mouseX;
* y1 = 80;
* x2 = sqrt(x1);
* y2 = 20;
*
* // Draw the non-squared.
* line(0, y1, width, y1);
* ellipse(x1, y1, eSize, eSize);
*
* // Draw the squared.
* line(0, y2, width, y2);
* ellipse(x2, y2, eSize, eSize);
*
* // Draw dividing line.
* stroke(100)
* line(0, height/2, width, height/2);
*
* // Draw text.
* noStroke();
* fill(0);
* var spacing = 15;
* text("x = " + x1, 0, y1 + spacing);
* text("sqrt(x) = " + x2, 0, y2 + spacing);
* }
*
*/
p5.prototype.sqrt = Math.sqrt;
module.exports = p5;
},{"../core/core":48}],74:[function(_dereq_,module,exports){
/**
* @module Math
* @submodule Math
* @for p5
* @requires core
*/
'use strict';
var p5 = _dereq_('../core/core');
/**
* Creates a new p5.Vector (the datatype for storing vectors). This provides a
* two or three dimensional vector, specifically a Euclidean (also known as
* geometric) vector. A vector is an entity that has both magnitude and
* direction.
*
* @method createVector
* @param {Number} [x] x component of the vector
* @param {Number} [y] y component of the vector
* @param {Number} [z] z component of the vector
*/
p5.prototype.createVector = function (x, y, z) {
if (this instanceof p5) {
return new p5.Vector(this, arguments);
} else {
return new p5.Vector(x, y, z);
}
};
module.exports = p5;
},{"../core/core":48}],75:[function(_dereq_,module,exports){
//////////////////////////////////////////////////////////////
// http://mrl.nyu.edu/~perlin/noise/
// Adapting from PApplet.java
// which was adapted from toxi
// which was adapted from the german demo group farbrausch
// as used in their demo "art": http://www.farb-rausch.de/fr010src.zip
// someday we might consider using "improved noise"
// http://mrl.nyu.edu/~perlin/paper445.pdf
// See: https://github.com/shiffman/The-Nature-of-Code-Examples-p5.js/
// blob/master/introduction/Noise1D/noise.js
/**
* @module Math
* @submodule Noise
* @for p5
* @requires core
*/
'use strict';
var p5 = _dereq_('../core/core');
var PERLIN_YWRAPB = 4;
var PERLIN_YWRAP = 1<random() function.
* It was invented by Ken Perlin in the 1980s and been used since in
* graphical applications to produce procedural textures, natural motion,
* shapes, terrains etc.
The main difference to the
* random() function is that Perlin noise is defined in an infinite
* n-dimensional space where each pair of coordinates corresponds to a
* fixed semi-random value (fixed only for the lifespan of the program; see
* the noiseSeed() function). p5.js can compute 1D, 2D and 3D noise,
* depending on the number of coordinates given. The resulting value will
* always be between 0.0 and 1.0. The noise value can be animated by moving
* through the noise space as demonstrated in the example above. The 2nd
* and 3rd dimension can also be interpreted as time.
The actual
* noise is structured similar to an audio signal, in respect to the
* function's use of frequencies. Similar to the concept of harmonics in
* physics, perlin noise is computed over several octaves which are added
* together for the final result.
Another way to adjust the
* character of the resulting sequence is the scale of the input
* coordinates. As the function works within an infinite space the value of
* the coordinates doesn't matter as such, only the distance between
* successive coordinates does (eg. when using noise() within a
* loop). As a general rule the smaller the difference between coordinates,
* the smoother the resulting noise sequence will be. Steps of 0.005-0.03
* work best for most applications, but this will differ depending on use.
*
*
* @method noise
* @param {Number} x x-coordinate in noise space
* @param {Number} y y-coordinate in noise space
* @param {Number} z z-coordinate in noise space
* @return {Number} Perlin noise value (between 0 and 1) at specified
* coordinates
* @example
*
* var xoff = 0.0;
*
* function draw() {
* background(204);
* xoff = xoff + .01;
* var n = noise(xoff) * width;
* line(n, 0, n, height);
* }
*
*
*
* var noiseScale=0.02;
*
* function draw() {
* background(0);
* for (var x=0; x < width; x++) {
* var noiseVal = noise((mouseX+x)*noiseScale, mouseY*noiseScale);
* stroke(noiseVal*255);
* line(x, mouseY+noiseVal*80, x, height);
* }
* }
*
*
*/
p5.prototype.noise = function(x,y,z) {
y = y || 0;
z = z || 0;
if (perlin == null) {
perlin = new Array(PERLIN_SIZE + 1);
for (var i = 0; i < PERLIN_SIZE + 1; i++) {
perlin[i] = Math.random();
}
}
if (x<0) { x=-x; }
if (y<0) { y=-y; }
if (z<0) { z=-z; }
var xi=Math.floor(x), yi=Math.floor(y), zi=Math.floor(z);
var xf = x - xi;
var yf = y - yi;
var zf = z - zi;
var rxf, ryf;
var r=0;
var ampl=0.5;
var n1,n2,n3;
for (var o=0; o=1.0) { xi++; xf--; }
if (yf>=1.0) { yi++; yf--; }
if (zf>=1.0) { zi++; zf--; }
}
return r;
};
/**
*
* Adjusts the character and level of detail produced by the Perlin noise
* function. Similar to harmonics in physics, noise is computed over
* several octaves. Lower octaves contribute more to the output signal and
* as such define the overall intensity of the noise, whereas higher octaves
* create finer grained details in the noise sequence. By default, noise is
* computed over 4 octaves with each octave contributing exactly half than
* its predecessor, starting at 50% strength for the 1st octave. This
* falloff amount can be changed by adding an additional function
* parameter. Eg. a falloff factor of 0.75 means each octave will now have
* 75% impact (25% less) of the previous lower octave. Any value between
* 0.0 and 1.0 is valid, however note that values greater than 0.5 might
* result in greater than 1.0 values returned by noise().
By changing these parameters, the signal created by the noise()
* function can be adapted to fit very specific needs and characteristics.
*
* @method noiseDetail
* @param {Number} lod number of octaves to be used by the noise
* @param {Number} falloff falloff factor for each octave
* @example
*
*
*
* var noiseVal;
* var noiseScale=0.02;
*
* function setup() {
* createCanvas(100,100);
* }
*
* function draw() {
* background(0);
* for (var y = 0; y < height; y++) {
* for (var x = 0; x < width/2; x++) {
* noiseDetail(2,0.2);
* noiseVal = noise((mouseX+x) * noiseScale,
* (mouseY+y) * noiseScale);
* stroke(noiseVal*255);
* point(x,y);
* noiseDetail(8,0.65);
* noiseVal = noise((mouseX + x + width/2) * noiseScale,
* (mouseY + y) * noiseScale);
* stroke(noiseVal*255);
* point(x + width/2, y);
* }
* }
* }
*
*
*/
p5.prototype.noiseDetail = function(lod, falloff) {
if (lod>0) { perlin_octaves=lod; }
if (falloff>0) { perlin_amp_falloff=falloff; }
};
/**
* Sets the seed value for noise(). By default, noise()
* produces different results each time the program is run. Set the
* value parameter to a constant to return the same pseudo-random
* numbers each time the software is run.
*
* @method noiseSeed
* @param {Number} seed the seed value
* @example
*
* var xoff = 0.0;
*
* function setup() {
* noiseSeed(99);
* stroke(0, 10);
* }
*
* function draw() {
* xoff = xoff + .01;
* var n = noise(xoff) * width;
* line(n, 0, n, height);
* }
*
*
*/
p5.prototype.noiseSeed = function(seed) {
// Linear Congruential Generator
// Variant of a Lehman Generator
var lcg = (function() {
// Set to values from http://en.wikipedia.org/wiki/Numerical_Recipes
// m is basically chosen to be large (as it is the max period)
// and for its relationships to a and c
var m = 4294967296,
// a - 1 should be divisible by m's prime factors
a = 1664525,
// c and m should be co-prime
c = 1013904223,
seed, z;
return {
setSeed : function(val) {
// pick a random seed if val is undefined or null
// the >>> 0 casts the seed to an unsigned 32-bit integer
z = seed = (val == null ? Math.random() * m : val) >>> 0;
},
getSeed : function() {
return seed;
},
rand : function() {
// define the recurrence relationship
z = (a * z + c) % m;
// return a float in [0, 1)
// if z = m then z / m = 0 therefore (z % m) / m < 1 always
return z / m;
}
};
}());
lcg.setSeed(seed);
perlin = new Array(PERLIN_SIZE + 1);
for (var i = 0; i < PERLIN_SIZE + 1; i++) {
perlin[i] = lcg.rand();
}
};
module.exports = p5;
},{"../core/core":48}],76:[function(_dereq_,module,exports){
/**
* @module Math
* @submodule Math
* @requires constants
*/
'use strict';
var p5 = _dereq_('../core/core');
var polarGeometry = _dereq_('./polargeometry');
var constants = _dereq_('../core/constants');
/**
* A class to describe a two or three dimensional vector, specifically
* a Euclidean (also known as geometric) vector. A vector is an entity
* that has both magnitude and direction. The datatype, however, stores
* the components of the vector (x,y for 2D, and x,y,z for 3D). The magnitude
* and direction can be accessed via the methods mag() and heading(). In many
* of the p5.js examples, you will see p5.Vector used to describe a position,
* velocity, or acceleration. For example, if you consider a rectangle moving
* across the screen, at any given instant it has a position (a vector that
* points from the origin to its location), a velocity (the rate at which the
* object's position changes per time unit, expressed as a vector), and
* acceleration (the rate at which the object's velocity changes per time
* unit, expressed as a vector). Since vectors represent groupings of values,
* we cannot simply use traditional addition/multiplication/etc. Instead,
* we'll need to do some "vector" math, which is made easy by the methods
* inside the p5.Vector class.
*
* @class p5.Vector
* @constructor
* @param {Number} [x] x component of the vector
* @param {Number} [y] y component of the vector
* @param {Number} [z] z component of the vector
* @example
*
*
* var v1 = createVector(40, 50);
* var v2 = createVector(40, 50);
*
* ellipse(v1.x, v1.y, 50, 50);
* ellipse(v2.x, v2.y, 50, 50);
* v1.add(v2);
* ellipse(v1.x, v1.y, 50, 50);
*
*
*/
p5.Vector = function() {
var x,y,z;
// This is how it comes in with createVector()
if(arguments[0] instanceof p5) {
// save reference to p5 if passed in
this.p5 = arguments[0];
x = arguments[1][0] || 0;
y = arguments[1][1] || 0;
z = arguments[1][2] || 0;
// This is what we'll get with new p5.Vector()
} else {
x = arguments[0] || 0;
y = arguments[1] || 0;
z = arguments[2] || 0;
}
/**
* The x component of the vector
* @property x
* @type {Number}
*/
this.x = x;
/**
* The y component of the vector
* @property y
* @type {Number}
*/
this.y = y;
/**
* The z component of the vector
* @property z
* @type {Number}
*/
this.z = z;
};
/**
* Returns a string representation of a vector v by calling String(v)
* or v.toString(). This method is useful for logging vectors in the
* console.
* @method toString
* @example
*
* function setup() {
* var v = createVector(20,30);
* print(String(v)); // prints "p5.Vector Object : [20, 30, 0]"
* }
*
*
*/
p5.Vector.prototype.toString = function p5VectorToString() {
return 'p5.Vector Object : ['+ this.x +', '+ this.y +', '+ this.z + ']';
};
/**
* Sets the x, y, and z component of the vector using two or three separate
* variables, the data from a p5.Vector, or the values from a float array.
* @method set
*
* @param {Number|p5.Vector|Array} [x] the x component of the vector or a
* p5.Vector or an Array
* @param {Number} [y] the y component of the vector
* @param {Number} [z] the z component of the vector
* @example
*
*
* function setup() {
* var v = createVector(1, 2, 3);
* v.set(4,5,6); // Sets vector to [4, 5, 6]
*
* var v1 = createVector(0, 0, 0);
* var arr = [1, 2, 3];
* v1.set(arr); // Sets vector to [1, 2, 3]
* }
*
*
*/
p5.Vector.prototype.set = function (x, y, z) {
if (x instanceof p5.Vector) {
this.x = x.x || 0;
this.y = x.y || 0;
this.z = x.z || 0;
return this;
}
if (x instanceof Array) {
this.x = x[0] || 0;
this.y = x[1] || 0;
this.z = x[2] || 0;
return this;
}
this.x = x || 0;
this.y = y || 0;
this.z = z || 0;
return this;
};
/**
* Gets a copy of the vector, returns a p5.Vector object.
*
* @method copy
* @return {p5.Vector} the copy of the p5.Vector object
* @example
*
*
* var v1 = createVector(1, 2, 3);
* var v2 = v.copy();
* print(v1.x == v2.x && v1.y == v2.y && v1.z == v2.z);
* // Prints "true"
*
*
*/
p5.Vector.prototype.copy = function () {
if (this.p5) {
return new p5.Vector(this.p5,[this.x, this.y, this.z]);
} else {
return new p5.Vector(this.x,this.y,this.z);
}
};
/**
* Adds x, y, and z components to a vector, adds one vector to another, or
* adds two independent vectors together. The version of the method that adds
* two vectors together is a static method and returns a p5.Vector, the others
* acts directly on the vector. See the examples for more context.
*
* @method add
* @chainable
* @param {Number|p5.Vector|Array} x the x component of the vector to be
* added or a p5.Vector or an Array
* @param {Number} [y] the y component of the vector to be
* added
* @param {Number} [z] the z component of the vector to be
* added
* @return {p5.Vector} the p5.Vector object.
* @example
*
*
* var v = createVector(1, 2, 3);
* v.add(4,5,6);
* // v's compnents are set to [5, 7, 9]
*
*
*
*
* // Static method
* var v1 = createVector(1, 2, 3);
* var v2 = createVector(2, 3, 4);
*
* var v3 = p5.Vector.add(v1, v2);
* // v3 has components [3, 5, 7]
*
*
*/
p5.Vector.prototype.add = function (x, y, z) {
if (x instanceof p5.Vector) {
this.x += x.x || 0;
this.y += x.y || 0;
this.z += x.z || 0;
return this;
}
if (x instanceof Array) {
this.x += x[0] || 0;
this.y += x[1] || 0;
this.z += x[2] || 0;
return this;
}
this.x += x || 0;
this.y += y || 0;
this.z += z || 0;
return this;
};
/**
* Subtracts x, y, and z components from a vector, subtracts one vector from
* another, or subtracts two independent vectors. The version of the method
* that subtracts two vectors is a static method and returns a p5.Vector, the
* other acts directly on the vector. See the examples for more context.
*
* @method sub
* @chainable
* @param {Number|p5.Vector|Array} x the x component of the vector or a
* p5.Vector or an Array
* @param {Number} [y] the y component of the vector
* @param {Number} [z] the z component of the vector
* @return {p5.Vector} p5.Vector object.
* @example
*
*
* var v = createVector(4, 5, 6);
* v.sub(1, 1, 1);
* // v's compnents are set to [3, 4, 5]
*
*
*
*
*
* // Static method
* var v1 = createVector(2, 3, 4);
* var v2 = createVector(1, 2, 3);
*
* var v3 = p5.Vector.sub(v1, v2);
* // v3 has compnents [1, 1, 1]
*
*
*/
p5.Vector.prototype.sub = function (x, y, z) {
if (x instanceof p5.Vector) {
this.x -= x.x || 0;
this.y -= x.y || 0;
this.z -= x.z || 0;
return this;
}
if (x instanceof Array) {
this.x -= x[0] || 0;
this.y -= x[1] || 0;
this.z -= x[2] || 0;
return this;
}
this.x -= x || 0;
this.y -= y || 0;
this.z -= z || 0;
return this;
};
/**
* Multiply the vector by a scalar. The static version of this method
* creates a new p5.Vector while the non static version acts on the vector
* directly. See the examples for more context.
*
* @method mult
* @chainable
* @param {Number} n the number to multiply with the vector
* @return {p5.Vector} a reference to the p5.Vector object (allow chaining)
* @example
*
*
* var v = createVector(1, 2, 3);
* v.mult(2);
* // v's compnents are set to [2, 4, 6]
*
*
*
*
*
* // Static method
* var v1 = createVector(1, 2, 3);
* var v2 = p5.Vector.mult(v1, 2);
* // v2 has compnents [2, 4, 6]
*
*
*/
p5.Vector.prototype.mult = function (n) {
this.x *= n || 0;
this.y *= n || 0;
this.z *= n || 0;
return this;
};
/**
* Divide the vector by a scalar. The static version of this method creates a
* new p5.Vector while the non static version acts on the vector directly.
* See the examples for more context.
*
* @method div
* @chainable
* @param {number} n the number to divide the vector by
* @return {p5.Vector} a reference to the p5.Vector object (allow chaining)
* @example
*
*
* var v = createVector(6, 4, 2);
* v.div(2); //v's compnents are set to [3, 2, 1]
*
*
*
*
*
* // Static method
* var v1 = createVector(6, 4, 2);
* var v2 = p5.Vector.div(v, 2);
* // v2 has compnents [3, 2, 1]
*
*
*/
p5.Vector.prototype.div = function (n) {
this.x /= n;
this.y /= n;
this.z /= n;
return this;
};
/**
* Calculates the magnitude (length) of the vector and returns the result as
* a float (this is simply the equation sqrt(x*x + y*y + z*z).)
*
* @method mag
* @return {Number} magnitude of the vector
* @example
*
*
* var v = createVector(20.0, 30.0, 40.0);
* var m = v.mag(10);
* print(m); // Prints "53.85164807134504"
*
*
*/
p5.Vector.prototype.mag = function () {
return Math.sqrt(this.magSq());
};
/**
* Calculates the squared magnitude of the vector and returns the result
* as a float (this is simply the equation (x*x + y*y + z*z).)
* Faster if the real length is not required in the
* case of comparing vectors, etc.
*
* @method magSq
* @return {number} squared magnitude of the vector
* @example
*
*
* // Static method
* var v1 = createVector(6, 4, 2);
* print(v1.magSq()); // Prints "56"
*
*
*/
p5.Vector.prototype.magSq = function () {
var x = this.x, y = this.y, z = this.z;
return (x * x + y * y + z * z);
};
/**
* Calculates the dot product of two vectors. The version of the method
* that computes the dot product of two independent vectors is a static
* method. See the examples for more context.
*
*
* @method dot
* @param {Number|p5.Vector} x x component of the vector or a p5.Vector
* @param {Number} [y] y component of the vector
* @param {Number} [z] z component of the vector
* @return {Number} the dot product
*
* @example
*
*
* var v1 = createVector(1, 2, 3);
* var v2 = createVector(2, 3, 4);
*
* print(v1.dot(v2)); // Prints "20"
*
*
*
*
*
* //Static method
* var v1 = createVector(1, 2, 3);
* var v2 = createVector(3, 2, 1);
* print (p5.Vector.dot(v1, v2)); // Prints "10"
*
*
*/
p5.Vector.prototype.dot = function (x, y, z) {
if (x instanceof p5.Vector) {
return this.dot(x.x, x.y, x.z);
}
return this.x * (x || 0) +
this.y * (y || 0) +
this.z * (z || 0);
};
/**
* Calculates and returns a vector composed of the cross product between
* two vectors. Both the static and non static methods return a new p5.Vector.
* See the examples for more context.
*
* @method cross
* @param {p5.Vector} v p5.Vector to be crossed
* @return {p5.Vector} p5.Vector composed of cross product
* @example
*
*
* var v1 = createVector(1, 2, 3);
* var v2 = createVector(1, 2, 3);
*
* v1.cross(v2); // v's components are [0, 0, 0]
*
*
*
*
*
* // Static method
* var v1 = createVector(1, 0, 0);
* var v2 = createVector(0, 1, 0);
*
* var crossProduct = p5.Vector.cross(v1, v2);
* // crossProduct has components [0, 0, 1]
*
*
*/
p5.Vector.prototype.cross = function (v) {
var x = this.y * v.z - this.z * v.y;
var y = this.z * v.x - this.x * v.z;
var z = this.x * v.y - this.y * v.x;
if (this.p5) {
return new p5.Vector(this.p5,[x,y,z]);
} else {
return new p5.Vector(x,y,z);
}
};
/**
* Calculates the Euclidean distance between two points (considering a
* point as a vector object).
*
* @method dist
* @param {p5.Vector} v the x, y, and z coordinates of a p5.Vector
* @return {Number} the distance
* @example
*
*
* var v1 = createVector(1, 0, 0);
* var v2 = createVector(0, 1, 0);
*
* var distance = v1.dist(v2); // distance is 1.4142...
*
*
*
*
* // Static method
* var v1 = createVector(1, 0, 0);
* var v2 = createVector(0, 1, 0);
*
* var distance = p5.Vector.dist(v1,v2);
* // distance is 1.4142...
*
*
*/
p5.Vector.prototype.dist = function (v) {
var d = v.copy().sub(this);
return d.mag();
};
/**
* Normalize the vector to length 1 (make it a unit vector).
*
* @method normalize
* @return {p5.Vector} normalized p5.Vector
* @example
*
*
* var v = createVector(10, 20, 2);
* // v has compnents [10.0, 20.0, 2.0]
* v.normalize();
* // v's compnents are set to
* // [0.4454354, 0.8908708, 0.089087084]
*
*
*
*/
p5.Vector.prototype.normalize = function () {
return this.div(this.mag());
};
/**
* Limit the magnitude of this vector to the value used for the max
* parameter.
*
* @method limit
* @param {Number} max the maximum magnitude for the vector
* @return {p5.Vector} the modified p5.Vector
* @example
*
*
* var v = createVector(10, 20, 2);
* // v has compnents [10.0, 20.0, 2.0]
* v.limit(5);
* // v's compnents are set to
* // [2.2271771, 4.4543543, 0.4454354]
*
*
*/
p5.Vector.prototype.limit = function (l) {
var mSq = this.magSq();
if(mSq > l*l) {
this.div(Math.sqrt(mSq)); //normalize it
this.mult(l);
}
return this;
};
/**
* Set the magnitude of this vector to the value used for the len
* parameter.
*
* @method setMag
* @param {number} len the new length for this vector
* @return {p5.Vector} the modified p5.Vector
* @example
*
*
* var v1 = createVector(10, 20, 2);
* // v has compnents [10.0, 20.0, 2.0]
* v1.setMag(10);
* // v's compnents are set to [6.0, 8.0, 0.0]
*
*
*/
p5.Vector.prototype.setMag = function (n) {
return this.normalize().mult(n);
};
/**
* Calculate the angle of rotation for this vector (only 2D vectors)
*
* @method heading
* @return {Number} the angle of rotation
* @example
*
* function setup() {
* var v1 = createVector(30,50);
* print(v1.heading()); // 1.0303768265243125
*
* var v1 = createVector(40,50);
* print(v1.heading()); // 0.8960553845713439
*
* var v1 = createVector(30,70);
* print(v1.heading()); // 1.1659045405098132
* }
*
*/
p5.Vector.prototype.heading = function () {
var h = Math.atan2(this.y, this.x);
if (this.p5) {
if (this.p5._angleMode === constants.RADIANS) {
return h;
} else {
return polarGeometry.radiansToDegrees(h);
}
} else {
return h;
}
};
/**
* Rotate the vector by an angle (only 2D vectors), magnitude remains the
* same
*
* @method rotate
* @param {number} angle the angle of rotation
* @return {p5.Vector} the modified p5.Vector
* @example
*
*
* var v = createVector(10.0, 20.0);
* // v has compnents [10.0, 20.0, 0.0]
* v.rotate(HALF_PI);
* // v's compnents are set to [-20.0, 9.999999, 0.0]
*
*
*/
p5.Vector.prototype.rotate = function (a) {
if (this.p5) {
if (this.p5._angleMode === constants.DEGREES) {
a = polarGeometry.degreesToRadians(a);
}
}
var newHeading = this.heading() + a;
var mag = this.mag();
this.x = Math.cos(newHeading) * mag;
this.y = Math.sin(newHeading) * mag;
return this;
};
/**
* Linear interpolate the vector to another vector
*
* @method lerp
* @param {p5.Vector} x the x component or the p5.Vector to lerp to
* @param {p5.Vector} [y] y the y component
* @param {p5.Vector} [z] z the z component
* @param {Number} amt the amount of interpolation; some value between 0.0
* (old vector) and 1.0 (new vector). 0.1 is very near
* the new vector. 0.5 is halfway in between.
* @return {p5.Vector} the modified p5.Vector
* @example
*
*
* var v = createVector(1, 1, 0);
*
* v.lerp(3, 3, 0, 0.5); // v now has components [2,2,0]
*
*
*
*
*
* var v1 = createVector(0, 0, 0);
* var v2 = createVector(100, 100, 0);
*
* var v3 = p5.Vector.lerp(v1, v2, 0.5);
* // v3 has components [50,50,0]
*
*
*/
p5.Vector.prototype.lerp = function (x, y, z, amt) {
if (x instanceof p5.Vector) {
return this.lerp(x.x, x.y, x.z, y);
}
this.x += (x - this.x) * amt || 0;
this.y += (y - this.y) * amt || 0;
this.z += (z - this.z) * amt || 0;
return this;
};
/**
* Return a representation of this vector as a float array. This is only
* for temporary use. If used in any other fashion, the contents should be
* copied by using the p5.Vector.copy() method to copy into your own
* array.
*
* @method array
* @return {Array} an Array with the 3 values
* @example
*
* function setup() {
* var v = createVector(20,30);
* print(v.array()); // Prints : Array [20, 30, 0]
* }
*
*
*
* var v = createVector(10.0, 20.0, 30.0);
* var f = v.array();
* print(f[0]); // Prints "10.0"
* print(f[1]); // Prints "20.0"
* print(f[2]); // Prints "30.0"
*
*
*/
p5.Vector.prototype.array = function () {
return [this.x || 0, this.y || 0, this.z || 0];
};
/**
* Equality check against a p5.Vector
*
* @method equals
* @param {Number|p5.Vector|Array} [x] the x component of the vector or a
* p5.Vector or an Array
* @param {Number} [y] the y component of the vector
* @param {Number} [z] the z component of the vector
* @return {Boolean} whether the vectors are equals
* @example
*
* v1 = createVector(5,10,20);
* v2 = createVector(5,10,20);
* v3 = createVector(13,10,19);
*
* print(v1.equals(v2.x,v2.y,v2.z)); // true
* print(v1.equals(v3.x,v3.y,v3.z)); // false
*
*
*
* var v1 = createVector(10.0, 20.0, 30.0);
* var v2 = createVector(10.0, 20.0, 30.0);
* var v3 = createVector(0.0, 0.0, 0.0);
* print (v1.equals(v2)) // true
* print (v1.equals(v3)) // false
*
*
*/
p5.Vector.prototype.equals = function (x, y, z) {
var a, b, c;
if (x instanceof p5.Vector) {
a = x.x || 0;
b = x.y || 0;
c = x.z || 0;
} else if (x instanceof Array) {
a = x[0] || 0;
b = x[1] || 0;
c = x[2] || 0;
} else {
a = x || 0;
b = y || 0;
c = z || 0;
}
return this.x === a && this.y === b && this.z === c;
};
// Static Methods
/**
* Make a new 2D unit vector from an angle
*
* @method fromAngle
* @static
* @param {Number} angle the desired angle
* @return {p5.Vector} the new p5.Vector object
* @example
*
*
* function draw() {
* background (200);
*
* // Create a variable, proportional to the mouseX,
* // varying from 0-360, to represent an angle in degrees.
* angleMode(DEGREES);
* var myDegrees = map(mouseX, 0,width, 0,360);
*
* // Display that variable in an onscreen text.
* // (Note the nfc() function to truncate additional decimal places,
* // and the "\xB0" character for the degree symbol.)
* var readout = "angle = " + nfc(myDegrees,1,1) + "\xB0"
* noStroke();
* fill (0);
* text (readout, 5, 15);
*
* // Create a p5.Vector using the fromAngle function,
* // and extract its x and y components.
* var v = p5.Vector.fromAngle(radians(myDegrees));
* var vx = v.x;
* var vy = v.y;
*
* push();
* translate (width/2, height/2);
* noFill();
* stroke (150);
* line (0,0, 30,0);
* stroke (0);
* line (0,0, 30*vx, 30*vy);
* pop()
* }
*
*
*/
p5.Vector.fromAngle = function(angle) {
if (this.p5) {
if (this.p5._angleMode === constants.DEGREES) {
angle = polarGeometry.degreesToRadians(angle);
}
}
if (this.p5) {
return new p5.Vector(this.p5,[Math.cos(angle),Math.sin(angle),0]);
} else {
return new p5.Vector(Math.cos(angle),Math.sin(angle),0);
}
};
/**
* Make a new 2D unit vector from a random angle
*
* @method random2D
* @static
* @return {p5.Vector} the new p5.Vector object
* @example
*
*
* var v = p5.Vector.random2D();
* // May make v's attributes something like:
* // [0.61554617, -0.51195765, 0.0] or
* // [-0.4695841, -0.14366731, 0.0] or
* // [0.6091097, -0.22805278, 0.0]
*
*
*/
p5.Vector.random2D = function () {
var angle;
// A lot of nonsense to determine if we know about a
// p5 sketch and whether we should make a random angle in degrees or radians
if (this.p5) {
if (this.p5._angleMode === constants.DEGREES) {
angle = this.p5.random(360);
} else {
angle = this.p5.random(constants.TWO_PI);
}
} else {
angle = Math.random()*Math.PI*2;
}
return this.fromAngle(angle);
};
/**
* Make a new random 3D unit vector.
*
* @method random3D
* @static
* @return {p5.Vector} the new p5.Vector object
* @example
*
*
* var v = p5.Vector.random3D();
* // May make v's attributes something like:
* // [0.61554617, -0.51195765, 0.599168] or
* // [-0.4695841, -0.14366731, -0.8711202] or
* // [0.6091097, -0.22805278, -0.7595902]
*
*
*/
p5.Vector.random3D = function () {
var angle,vz;
// If we know about p5
if (this.p5) {
angle = this.p5.random(0,constants.TWO_PI);
vz = this.p5.random(-1,1);
} else {
angle = Math.random()*Math.PI*2;
vz = Math.random()*2-1;
}
var vx = Math.sqrt(1-vz*vz)*Math.cos(angle);
var vy = Math.sqrt(1-vz*vz)*Math.sin(angle);
if (this.p5) {
return new p5.Vector(this.p5,[vx,vy,vz]);
} else {
return new p5.Vector(vx,vy,vz);
}
};
/**
* Adds two vectors together and returns a new one.
*
* @static
* @param {p5.Vector} v1 a p5.Vector to add
* @param {p5.Vector} v2 a p5.Vector to add
* @param {p5.Vector} target if undefined a new vector will be created
* @return {p5.Vector} the resulting p5.Vector
*
*/
p5.Vector.add = function (v1, v2, target) {
if (!target) {
target = v1.copy();
} else {
target.set(v1);
}
target.add(v2);
return target;
};
/**
* Subtracts one p5.Vector from another and returns a new one. The second
* vector (v2) is subtracted from the first (v1), resulting in v1-v2.
*
* @static
* @param {p5.Vector} v1 a p5.Vector to subtract from
* @param {p5.Vector} v2 a p5.Vector to subtract
* @param {p5.Vector} target if undefined a new vector will be created
* @return {p5.Vector} the resulting p5.Vector
*/
p5.Vector.sub = function (v1, v2, target) {
if (!target) {
target = v1.copy();
} else {
target.set(v1);
}
target.sub(v2);
return target;
};
/**
* Multiplies a vector by a scalar and returns a new vector.
*
* @static
* @param {p5.Vector} v the p5.Vector to multiply
* @param {Number} n the scalar
* @param {p5.Vector} target if undefined a new vector will be created
* @return {p5.Vector} the resulting new p5.Vector
*/
p5.Vector.mult = function (v, n, target) {
if (!target) {
target = v.copy();
} else {
target.set(v);
}
target.mult(n);
return target;
};
/**
* Divides a vector by a scalar and returns a new vector.
*
* @static
* @param {p5.Vector} v the p5.Vector to divide
* @param {Number} n the scalar
* @param {p5.Vector} target if undefined a new vector will be created
* @return {p5.Vector} the resulting new p5.Vector
*/
p5.Vector.div = function (v, n, target) {
if (!target) {
target = v.copy();
} else {
target.set(v);
}
target.div(n);
return target;
};
/**
* Calculates the dot product of two vectors.
*
* @static
* @param {p5.Vector} v1 the first p5.Vector
* @param {p5.Vector} v2 the second p5.Vector
* @return {Number} the dot product
*/
p5.Vector.dot = function (v1, v2) {
return v1.dot(v2);
};
/**
* Calculates the cross product of two vectors.
*
* @static
* @param {p5.Vector} v1 the first p5.Vector
* @param {p5.Vector} v2 the second p5.Vector
* @return {Number} the cross product
*/
p5.Vector.cross = function (v1, v2) {
return v1.cross(v2);
};
/**
* Calculates the Euclidean distance between two points (considering a
* point as a vector object).
*
* @static
* @param {p5.Vector} v1 the first p5.Vector
* @param {p5.Vector} v2 the second p5.Vector
* @return {Number} the distance
*/
p5.Vector.dist = function (v1,v2) {
return v1.dist(v2);
};
/**
* Linear interpolate a vector to another vector and return the result as a
* new vector.
*
* @static
* @param {p5.Vector} v1 a starting p5.Vector
* @param {p5.Vector} v2 the p5.Vector to lerp to
* @param {Number} the amount of interpolation; some value between 0.0
* (old vector) and 1.0 (new vector). 0.1 is very near
* the new vector. 0.5 is halfway in between.
*/
p5.Vector.lerp = function (v1, v2, amt, target) {
if (!target) {
target = v1.copy();
} else {
target.set(v1);
}
target.lerp(v2, amt);
return target;
};
/**
* Calculates and returns the angle (in radians) between two vectors.
* @method angleBetween
* @static
* @param {p5.Vector} v1 the x, y, and z components of a p5.Vector
* @param {p5.Vector} v2 the x, y, and z components of a p5.Vector
* @return {Number} the angle between (in radians)
* @example
*
*
* var v1 = createVector(1, 0, 0);
* var v2 = createVector(0, 1, 0);
*
* var angle = p5.Vector.angleBetween(v1, v2);
* // angle is PI/2
*
*
*/
p5.Vector.angleBetween = function (v1, v2) {
var angle = Math.acos(v1.dot(v2) / (v1.mag() * v2.mag()));
if (this.p5) {
if (this.p5._angleMode === constants.DEGREES) {
angle = polarGeometry.radiansToDegrees(angle);
}
}
return angle;
};
module.exports = p5.Vector;
},{"../core/constants":47,"../core/core":48,"./polargeometry":77}],77:[function(_dereq_,module,exports){
module.exports = {
degreesToRadians: function(x) {
return 2 * Math.PI * x / 360;
},
radiansToDegrees: function(x) {
return 360 * x / (2 * Math.PI);
}
};
},{}],78:[function(_dereq_,module,exports){
/**
* @module Math
* @submodule Random
* @for p5
* @requires core
*/
'use strict';
var p5 = _dereq_('../core/core');
var seeded = false;
// Linear Congruential Generator
// Variant of a Lehman Generator
var lcg = (function() {
// Set to values from http://en.wikipedia.org/wiki/Numerical_Recipes
// m is basically chosen to be large (as it is the max period)
// and for its relationships to a and c
var m = 4294967296,
// a - 1 should be divisible by m's prime factors
a = 1664525,
// c and m should be co-prime
c = 1013904223,
seed, z;
return {
setSeed : function(val) {
// pick a random seed if val is undefined or null
// the >>> 0 casts the seed to an unsigned 32-bit integer
z = seed = (val == null ? Math.random() * m : val) >>> 0;
},
getSeed : function() {
return seed;
},
rand : function() {
// define the recurrence relationship
z = (a * z + c) % m;
// return a float in [0, 1)
// if z = m then z / m = 0 therefore (z % m) / m < 1 always
return z / m;
}
};
}());
/**
* Sets the seed value for random().
*
* By default, random() produces different results each time the program
* is run. Set the seed parameter to a constant to return the same
* pseudo-random numbers each time the software is run.
*
* @method randomSeed
* @param {Number} seed the seed value
* @example
*
*
* randomSeed(99);
* for (var i=0; i < 100; i++) {
* var r = random(0, 255);
* stroke(r);
* line(i, 0, i, 100);
* }
*
*
*/
p5.prototype.randomSeed = function(seed) {
lcg.setSeed(seed);
seeded = true;
};
/**
* Return a random number.
*
* Takes either 0, 1 or 2 arguments.
* If no argument is given, returns a random number between 0 and 1.
* If one argument is given, returns a random number between 0 and the number.
* If two arguments are given, returns a random number between them,
* inclusive.
*
* @method random
* @param {Number} min the lower bound
* @param {Number} max the upper bound
* @return {Number} the random number
* @example
*
*
* for (var i = 0; i < 100; i++) {
* var r = random(50);
* stroke(r*5);
* line(50, i, 50+r, i);
* }
*
*
*
*
* for (var i = 0; i < 100; i++) {
* var r = random(-50, 50);
* line(50,i,50+r,i);
* }
*
*
*
*
* // Get a random element from an array
* var words = [ "apple", "bear", "cat", "dog" ];
* var index = floor(random(words.length)); // Convert to integer
* text(words[index],10,50); // Displays one of the four words
*
*
*/
p5.prototype.random = function (min, max) {
var rand;
if (seeded) {
rand = lcg.rand();
} else {
rand = Math.random();
}
if (arguments.length === 0) {
return rand;
} else
if (arguments.length === 1) {
return rand * min;
} else {
if (min > max) {
var tmp = min;
min = max;
max = tmp;
}
return rand * (max-min) + min;
}
};
/**
*
* Returns a random number fitting a Gaussian, or
* normal, distribution. There is theoretically no minimum or maximum
* value that randomGaussian() might return. Rather, there is
* just a very low probability that values far from the mean will be
* returned; and a higher probability that numbers near the mean will
* be returned.
* Takes either 0, 1 or 2 arguments.
* If no args, returns a mean of 0 and standard deviation of 1
* If one arg, that arg is the mean (standard deviation is 1)
* If two args, first is mean, second is standard deviation
*
* @method randomGaussian
* @param {Number} mean the mean
* @param {Number} sd the standard deviation
* @return {Number} the random number
* @example
*
* for (var y = 0; y < 100; y++) {
* var x = randomGaussian(50,15);
* line(50, y, x, y);
*}
*
*
*
*
*var distribution = new Array(360);
*
*function setup() {
* createCanvas(100, 100);
* for (var i = 0; i < distribution.length; i++) {
* distribution[i] = floor(randomGaussian(0,15));
* }
*}
*
*function draw() {
* background(204);
*
* translate(width/2, width/2);
*
* for (var i = 0; i < distribution.length; i++) {
* rotate(TWO_PI/distribution.length);
* stroke(0);
* var dist = abs(distribution[i]);
* line(0, 0, dist, 0);
* }
*}
*
*
*/
var y2;
var previous = false;
p5.prototype.randomGaussian = function(mean, sd) {
var y1,x1,x2,w;
if (previous) {
y1 = y2;
previous = false;
} else {
do {
x1 = this.random(2) - 1;
x2 = this.random(2) - 1;
w = x1 * x1 + x2 * x2;
} while (w >= 1);
w = Math.sqrt((-2 * Math.log(w))/w);
y1 = x1 * w;
y2 = x2 * w;
previous = true;
}
var m = mean || 0;
var s = sd || 1;
return y1*s + m;
};
module.exports = p5;
},{"../core/core":48}],79:[function(_dereq_,module,exports){
/**
* @module Math
* @submodule Trigonometry
* @for p5
* @requires core
* @requires polargeometry
* @requires constants
*/
'use strict';
var p5 = _dereq_('../core/core');
var polarGeometry = _dereq_('./polargeometry');
var constants = _dereq_('../core/constants');
p5.prototype._angleMode = constants.RADIANS;
/**
* The inverse of cos(), returns the arc cosine of a value. This function
* expects the values in the range of -1 to 1 and values are returned in
* the range 0 to PI (3.1415927).
*
* @method acos
* @param {Number} value the value whose arc cosine is to be returned
* @return {Number} the arc cosine of the given value
*
* @example
*
*
* var a = PI;
* var c = cos(a);
* var ac = acos(c);
* // Prints: "3.1415927 : -1.0 : 3.1415927"
* println(a + " : " + c + " : " + ac);
*
*
*
*
*
* var a = PI + PI/4.0;
* var c = cos(a);
* var ac = acos(c);
* // Prints: "3.926991 : -0.70710665 : 2.3561943"
* println(a + " : " + c + " : " + ac);
*
*
*/
p5.prototype.acos = function(ratio) {
if (this._angleMode === constants.RADIANS) {
return Math.acos(ratio);
} else {
return polarGeometry.radiansToDegrees(Math.acos(ratio));
}
};
/**
* The inverse of sin(), returns the arc sine of a value. This function
* expects the values in the range of -1 to 1 and values are returned
* in the range -PI/2 to PI/2.
*
* @method asin
* @param {Number} value the value whose arc sine is to be returned
* @return {Number} the arc sine of the given value
*
* @example
*
*
* var a = PI + PI/3;
* var s = sin(a);
* var as = asin(s);
* // Prints: "1.0471976 : 0.86602545 : 1.0471976"
* println(a + " : " + s + " : " + as);
*
*
*
*
*
* var a = PI + PI/3.0;
* var s = sin(a);
* var as = asin(s);
* // Prints: "4.1887903 : -0.86602545 : -1.0471976"
* println(a + " : " + s + " : " + as);
*
*
*
*/
p5.prototype.asin = function(ratio) {
if (this._angleMode === constants.RADIANS) {
return Math.asin(ratio);
} else {
return polarGeometry.radiansToDegrees(Math.asin(ratio));
}
};
/**
* The inverse of tan(), returns the arc tangent of a value. This function
* expects the values in the range of -Infinity to Infinity (exclusive) and
* values are returned in the range -PI/2 to PI/2.
*
* @method atan
* @param {Number} value the value whose arc tangent is to be returned
* @return {Number} the arc tangent of the given value
*
* @example
*
*
* var a = PI + PI/3;
* var t = tan(a);
* var at = atan(t);
* // Prints: "1.0471976 : 1.7320509 : 1.0471976"
* println(a + " : " + t + " : " + at);
*
*
*
*
*
* var a = PI + PI/3.0;
* var t = tan(a);
* var at = atan(t);
* // Prints: "4.1887903 : 1.7320513 : 1.0471977"
* println(a + " : " + t + " : " + at);
*
*
*
*/
p5.prototype.atan = function(ratio) {
if (this._angleMode === constants.RADIANS) {
return Math.atan(ratio);
} else {
return polarGeometry.radiansToDegrees(Math.atan(ratio));
}
};
/**
* Calculates the angle (in radians) from a specified point to the coordinate
* origin as measured from the positive x-axis. Values are returned as a
* float in the range from PI to -PI. The atan2() function is most often used
* for orienting geometry to the position of the cursor. Note: The
* y-coordinate of the point is the first parameter, and the x-coordinate is
* the second parameter, due the the structure of calculating the tangent.
*
* @method atan2
* @param {Number} y y-coordinate of the point
* @param {Number} x x-coordinate of the point
* @return {Number} the arc tangent of the given point
*
* @example
*
*
* function draw() {
* background(204);
* translate(width/2, height/2);
* var a = atan2(mouseY-height/2, mouseX-width/2);
* rotate(a);
* rect(-30, -5, 60, 10);
* }
*
*
*/
p5.prototype.atan2 = function (y, x) {
if (this._angleMode === constants.RADIANS) {
return Math.atan2(y, x);
} else {
return polarGeometry.radiansToDegrees(Math.atan2(y, x));
}
};
/**
* Calculates the cosine of an angle. This function takes into account the
* current angleMode. Values are returned in the range -1 to 1.
*
* @method cos
* @param {Number} angle the angle
* @return {Number} the cosine of the angle
*
* @example
*
*
* var a = 0.0;
* var inc = TWO_PI/25.0;
* for (var i = 0; i < 25; i++) {
* line(i*4, 50, i*4, 50+cos(a)*40.0);
* a = a + inc;
* }
*
*
*
*/
p5.prototype.cos = function(angle) {
if (this._angleMode === constants.RADIANS) {
return Math.cos(angle);
} else {
return Math.cos(this.radians(angle));
}
};
/**
* Calculates the sine of an angle. This function takes into account the
* current angleMode. Values are returned in the range -1 to 1.
*
* @method sin
* @param {Number} angle the angle
* @return {Number} the sine of the angle
*
* @example
*
*
* var a = 0.0;
* var inc = TWO_PI/25.0;
* for (var i = 0; i < 25; i++) {
* line(i*4, 50, i*4, 50+sin(a)*40.0);
* a = a + inc;
* }
*
*
*/
p5.prototype.sin = function(angle) {
if (this._angleMode === constants.RADIANS) {
return Math.sin(angle);
} else {
return Math.sin(this.radians(angle));
}
};
/**
* Calculates the tangent of an angle. This function takes into account
* the current angleMode. Values are returned in the range -1 to 1.
*
* @method tan
* @param {Number} angle the angle
* @return {Number} the tangent of the angle
*
* @example
*
*
* var a = 0.0;
* var inc = TWO_PI/50.0;
* for (var i = 0; i < 100; i = i+2) {
* line(i, 50, i, 50+tan(a)*2.0);
* a = a + inc;
* }
*
*
*
*/
p5.prototype.tan = function(angle) {
if (this._angleMode === constants.RADIANS) {
return Math.tan(angle);
} else {
return Math.tan(this.radians(angle));
}
};
/**
* Converts a radian measurement to its corresponding value in degrees.
* Radians and degrees are two ways of measuring the same thing. There are
* 360 degrees in a circle and 2*PI radians in a circle. For example,
* 90° = PI/2 = 1.5707964.
*
* @method degrees
* @param {Number} radians the radians value to convert to degrees
* @return {Number} the converted angle
*
*
* @example
*
*
* var rad = PI/4;
* var deg = degrees(rad);
* println(rad + " radians is " + deg + " degrees");
* // Prints: 45 degrees is 0.7853981633974483 radians
*
*
*
*/
p5.prototype.degrees = function(angle) {
return polarGeometry.radiansToDegrees(angle);
};
/**
* Converts a degree measurement to its corresponding value in radians.
* Radians and degrees are two ways of measuring the same thing. There are
* 360 degrees in a circle and 2*PI radians in a circle. For example,
* 90° = PI/2 = 1.5707964.
*
* @method radians
* @param {Number} degrees the degree value to convert to radians
* @return {Number} the converted angle
*
* @example
*
*
* var deg = 45.0;
* var rad = radians(deg);
* println(deg + " degrees is " + rad + " radians");
* // Prints: 45 degrees is 0.7853981633974483 radians
*
*
*/
p5.prototype.radians = function(angle) {
return polarGeometry.degreesToRadians(angle);
};
/**
* Sets the current mode of p5 to given mode. Default mode is RADIANS.
*
* @method angleMode
* @param {Number/Constant} mode either RADIANS or DEGREES
*
* @example
*
*
* function draw(){
* background(204);
* angleMode(DEGREES); // Change the mode to DEGREES
* var a = atan2(mouseY-height/2, mouseX-width/2);
* translate(width/2, height/2);
* push();
* rotate(a);
* rect(-20, -5, 40, 10); // Larger rectangle is rotating in degrees
* pop();
* angleMode(RADIANS); // Change the mode to RADIANS
* rotate(a); // var a stays the same
* rect(-40, -5, 20, 10); // Smaller rectangle is rotating in radians
* }
*
*
*
*/
p5.prototype.angleMode = function(mode) {
if (mode === constants.DEGREES || mode === constants.RADIANS) {
this._angleMode = mode;
}
};
module.exports = p5;
},{"../core/constants":47,"../core/core":48,"./polargeometry":77}],80:[function(_dereq_,module,exports){
/**
* @module Typography
* @submodule Attributes
* @for p5
* @requires core
* @requires constants
*/
'use strict';
var p5 = _dereq_('../core/core');
/**
* Sets the current alignment for drawing text. The parameters LEFT, CENTER,
* and RIGHT set the alignment of text in relation to the values for
* the x and y parameters of the text() function.
*
* @method textAlign
* @param {Number/Constant} horizAlign horizontal alignment, either LEFT,
* CENTER, or RIGHT
* @param {Number/Constant} vertAlign vertical alignment, either TOP,
* BOTTOM, CENTER, or BASELINE
* @return {Number}
* @example
*
*
* textSize(16);
* textAlign(RIGHT);
* text("ABCD", 50, 30);
* textAlign(CENTER);
* text("EFGH", 50, 50);
* textAlign(LEFT);
* text("IJKL", 50, 70);
*
*
*/
p5.prototype.textAlign = function(horizAlign, vertAlign) {
return this._renderer.textAlign.apply(this._renderer, arguments);
};
/**
* Sets/gets the spacing, in pixels, between lines of text. This
* setting will be used in all subsequent calls to the text() function.
*
* @method textLeading
* @param {Number} leading the size in pixels for spacing between lines
* @return {Object|Number}
* @example
*
*
* // Text to display. The "\n" is a "new line" character
* lines = "L1\nL2\nL3";
* textSize(12);
*
* textLeading(10); // Set leading to 10
* text(lines, 10, 25);
*
* textLeading(20); // Set leading to 20
* text(lines, 40, 25);
*
* textLeading(30); // Set leading to 30
* text(lines, 70, 25);
*
*
*/
p5.prototype.textLeading = function(theLeading) {
return this._renderer.textLeading.apply(this._renderer, arguments);
};
/**
* Sets/gets the current font size. This size will be used in all subsequent
* calls to the text() function. Font size is measured in pixels.
*
* @method textSize
* @param {Number} theSize the size of the letters in units of pixels
* @return {Object|Number}
* @example
*
*
* textSize(12);
* text("Font Size 12", 10, 30);
* textSize(14);
* text("Font Size 14", 10, 60);
* textSize(16);
* text("Font Size 16", 10, 90);
*
*
*/
p5.prototype.textSize = function(theSize) {
return this._renderer.textSize.apply(this._renderer, arguments);
};
/**
* Sets/gets the style of the text for system fonts to NORMAL, ITALIC, or BOLD.
* Note: this may be is overridden by CSS styling. For non-system fonts
* (opentype, truetype, etc.) please load styled fonts instead.
*
* @method textStyle
* @param {Number/Constant} theStyle styling for text, either NORMAL,
* ITALIC, or BOLD
* @return {Object|String}
* @example
*
*
* strokeWeight(0);
* textSize(12);
* textStyle(NORMAL);
* text("Font Style Normal", 10, 30);
* textStyle(ITALIC);
* text("Font Style Italic", 10, 60);
* textStyle(BOLD);
* text("Font Style Bold", 10, 90);
*
*
*/
p5.prototype.textStyle = function(theStyle) {
return this._renderer.textStyle.apply(this._renderer, arguments);
};
/**
* Calculates and returns the width of any character or text string.
*
* @method textWidth
* @param {String} theText the String of characters to measure
* @return {Number}
* @example
*
*
* textSize(28);
*
* var aChar = 'P';
* var cWidth = textWidth(aChar);
* text(aChar, 0, 40);
* line(cWidth, 0, cWidth, 50);
*
* var aString = "p5.js";
* var sWidth = textWidth(aString);
* text(aString, 0, 85);
* line(sWidth, 50, sWidth, 100);
*
*
*/
p5.prototype.textWidth = function(theText) {
return this._renderer.textWidth.apply(this._renderer, arguments);
};
/**
* Returns the ascent of the current font at its current size. The ascent
* represents the distance, in pixels, of the tallest character above
* the baseline.
*
* @return {Number}
* @example
*
*
* var base = height * 0.75;
* var scalar = 0.8; // Different for each font
*
* textSize(32); // Set initial text size
* var asc = textAscent() * scalar; // Calc ascent
* line(0, base - asc, width, base - asc);
* text("dp", 0, base); // Draw text on baseline
*
* textSize(64); // Increase text size
* asc = textAscent() * scalar; // Recalc ascent
* line(40, base - asc, width, base - asc);
* text("dp", 40, base); // Draw text on baseline
*
*
*/
p5.prototype.textAscent = function() {
return this._renderer.textAscent();
};
/**
* Returns the descent of the current font at its current size. The descent
* represents the distance, in pixels, of the character with the longest
* descender below the baseline.
*
* @return {Number}
* @example
*
*
* var base = height * 0.75;
* var scalar = 0.8; // Different for each font
*
* textSize(32); // Set initial text size
* var desc = textDescent() * scalar; // Calc ascent
* line(0, base+desc, width, base+desc);
* text("dp", 0, base); // Draw text on baseline
*
* textSize(64); // Increase text size
* desc = textDescent() * scalar; // Recalc ascent
* line(40, base + desc, width, base + desc);
* text("dp", 40, base); // Draw text on baseline
*
*
*/
p5.prototype.textDescent = function() {
return this._renderer.textDescent();
};
/**
* Helper function to measure ascent and descent.
*/
p5.prototype._updateTextMetrics = function() {
return this._renderer._updateTextMetrics();
};
module.exports = p5;
},{"../core/core":48}],81:[function(_dereq_,module,exports){
/**
* @module Typography
* @submodule Loading & Displaying
* @for p5
* @requires core
*/
'use strict';
var p5 = _dereq_('../core/core');
var constants = _dereq_('../core/constants');
_dereq_('../core/error_helpers');
/**
* Draws text to the screen. Displays the information specified in the first
* parameter on the screen in the position specified by the additional
* parameters. A default font will be used unless a font is set with the
* textFont() function and a default size will be used unless a font is set
* with textSize(). Change the color of the text with the fill() function.
* Change the outline of the text with the stroke() and strokeWeight()
* functions.
*
* The text displays in relation to the textAlign() function, which gives the
* option to draw to the left, right, and center of the coordinates.
*
* The x2 and y2 parameters define a rectangular area to display within and
* may only be used with string data. When these parameters are specified,
* they are interpreted based on the current rectMode() setting. Text that
* does not fit completely within the rectangle specified will not be drawn
* to the screen.
*
* @method text
* @param {String} str the alphanumeric symbols to be displayed
* @param {Number} x x-coordinate of text
* @param {Number} y y-coordinate of text
* @param {Number} x2 by default, the width of the text box,
* see rectMode() for more info
* @param {Number} y2 by default, the height of the text box,
* see rectMode() for more info
* @return {Object} this
* @example
*
*
* textSize(32);
* text("word", 10, 30);
* fill(0, 102, 153);
* text("word", 10, 60);
* fill(0, 102, 153, 51);
* text("word", 10, 90);
*
*
*
*
* s = "The quick brown fox jumped over the lazy dog.";
* fill(50);
* text(s, 10, 10, 70, 80); // Text wraps within text box
*
*
*/
p5.prototype.text = function(str, x, y, maxWidth, maxHeight) {
var args = new Array(arguments.length);
for (var i = 0; i < args.length; ++i) {
args[i] = arguments[i];
}
this._validateParameters(
'text',
args,
[
['*', 'Number', 'Number'],
['*', 'Number', 'Number', 'Number', 'Number']
]
);
return (!(this._renderer._doFill || this._renderer._doStroke)) ? this :
this._renderer.text.apply(this._renderer, arguments);
};
/**
* Sets the current font that will be drawn with the text() function.
*
* @method textFont
* @param {Object|String} f a font loaded via loadFont(), or a String
* representing a browser-based dfault font.
* @return {Object} this
* @example
*
*
* fill(0);
* textSize(12);
* textFont("Georgia");
* text("Georgia", 12, 30);
* textFont("Helvetica");
* text("Helvetica", 12, 60);
*
*
*
*
* var fontRegular, fontItalic, fontBold;
* function preload() {
* fontRegular = loadFont("assets/Regular.otf");
* fontItalic = loadFont("assets/Italic.ttf");
* fontBold = loadFont("assets/Bold.ttf");
* }
* function setup() {
* background(210);
* fill(0).strokeWeight(0).textSize(10);
* textFont(fontRegular);
* text("Font Style Normal", 10, 30);
* textFont(fontItalic);
* text("Font Style Italic", 10, 50);
* textFont(fontBold);
* text("Font Style Bold", 10, 70);
* }
*
*
*/
p5.prototype.textFont = function(theFont, theSize) {
if (arguments.length) {
if (!theFont) {
throw Error('null font passed to textFont');
}
this._renderer._setProperty('_textFont', theFont);
if (theSize) {
this._renderer._setProperty('_textSize', theSize);
this._renderer._setProperty('_textLeading',
theSize * constants._DEFAULT_LEADMULT);
}
return this._renderer._applyTextProperties();
}
return this;
};
module.exports = p5;
},{"../core/constants":47,"../core/core":48,"../core/error_helpers":51}],82:[function(_dereq_,module,exports){
/**
* This module defines the p5.Font class and functions for
* drawing text to the display canvas.
* @module Typography
* @submodule Font
* @requires core
* @requires constants
*/
'use strict';
var p5 = _dereq_('../core/core');
var constants = _dereq_('../core/constants');
/*
* TODO:
*
* API:
* -- textBounds()
* -- getPath()
* -- getPoints()
*
* ===========================================
* -- PFont functions:
* PFont.list()
*
* -- kerning
* -- alignment: justified?
* -- integrate p5.dom? (later)
*/
/**
* Base class for font handling
* @class p5.Font
* @constructor
* @param {Object} [pInst] pointer to p5 instance
*/
p5.Font = function(p) {
this.parent = p;
this.cache = {};
/**
* Underlying opentype font implementation
* @property font
*/
this.font = undefined;
};
p5.Font.prototype.list = function() {
// TODO
throw 'not yet implemented';
};
/**
* Returns a tight bounding box for the given text string using this
* font (currently only supports single lines)
*
* @method textBounds
* @param {String} line a line of text
* @param {Number} x x-position
* @param {Number} y y-position
* @param {Number} fontSize font size to use (optional)
* @param {Object} options opentype options (optional)
*
* @return {Object} a rectangle object with properties: x, y, w, h
*
* @example
*
*
* var font;
* var text = 'Lorem ipsum dolor sit amet.';
* function preload() {
* font = loadFont('./assets/fonts/Regular.otf');
* };
* function setup() {
* background(210);
* var bbox = font.textBounds(text, 10, 30, 12);
* fill(255);
* stroke(0);
* rect(bbox.x, bbox.y, bbox.w, bbox.h);
* fill(0);
* noStroke();
* * textFont(font);
* textSize(12);
* text(text, 10, 30);
* };
*
*
*/
p5.Font.prototype.textBounds = function(str, x, y, fontSize, options) {
x = x !== undefined ? x : 0;
y = y !== undefined ? y : 0;
fontSize = fontSize || this.parent._renderer._textSize;
var result = this.cache[cacheKey('textBounds', str, x, y, fontSize)];
if (!result) {
var xCoords = [], yCoords = [], self = this,
scale = this._scale(fontSize), minX, minY, maxX, maxY;
this.font.forEachGlyph(str, x, y, fontSize, options,
function(glyph, gX, gY, gFontSize) {
xCoords.push(gX);
yCoords.push(gY);
var gm = glyph.getMetrics();
if (glyph.name !== 'space') {
xCoords.push(gX + (gm.xMax * scale));
yCoords.push(gY + (-gm.yMin * scale));
yCoords.push(gY + (-gm.yMax * scale));
} else { // NOTE: deals with broken metrics for spaces in opentype.js
xCoords.push(gX + self.font.charToGlyph(' ').advanceWidth *
self._scale(fontSize));
}
});
minX = Math.max(0, Math.min.apply(null, xCoords));
minY = Math.max(0, Math.min.apply(null, yCoords));
maxX = Math.max(0, Math.max.apply(null, xCoords));
maxY = Math.max(0, Math.max.apply(null, yCoords));
result = {
x: minX,
y: minY,
h: maxY - minY,
w: maxX - minX,
advance: minX - x
};
this.cache[cacheKey('textBounds', str, x, y, fontSize)] = result;
}
//else console.log('cache-hit');
return result;
};
// ----------------------------- End API ------------------------------
/**
* Returns the set of opentype glyphs for the supplied string.
*
* Note that there is not a strict one-to-one mapping between characters
* and glyphs, so the list of returned glyphs can be larger or smaller
* than the length of the given string.
*
* @param {String} str the string to be converted
* @return {array} the opentype glyphs
*/
p5.Font.prototype._getGlyphs = function(str) {
return this.font.stringToGlyphs(str);
};
/**
* Returns an opentype path for the supplied string and position.
*
* @param {String} line a line of text
* @param {Number} x x-position
* @param {Number} y y-position
* @param {Object} options opentype options (optional)
* @return {Object} the opentype path
*/
p5.Font.prototype._getPath = function(line, x, y, options) {
var p = this.parent,
ctx = p._renderer.drawingContext,
pos = this._handleAlignment(p, ctx, line, x, y);
return this.font.getPath(line, pos.x, pos.y, p._renderer._textSize, options);
};
/*
* Creates an SVG-formatted path-data string
* (See http://www.w3.org/TR/SVG/paths.html#PathData)
* from the given opentype path or string/position
*
* @param {Object} path an opentype path, OR the following:
*
* @param {String} line a line of text
* @param {Number} x x-position
* @param {Number} y y-position
* @param {Object} options opentype options (optional), set options.decimals
* to set the decimal precision of the path-data
*
* @return {Object} this p5.Font object
*/
p5.Font.prototype._getPathData = function(line, x, y, options) {
var decimals = 3;
// create path from string/position
if (typeof line === 'string' && arguments.length > 2) {
line = this._getPath(line, x, y, options);
}
// handle options specified in 2nd arg
else if (typeof x === 'object') {
options = x;
}
// handle svg arguments
if (options && typeof options.decimals === 'number') {
decimals = options.decimals;
}
return line.toPathData(decimals);
};
/*
* Creates an SVG element, as a string,
* from the given opentype path or string/position
*
* @param {Object} path an opentype path, OR the following:
*
* @param {String} line a line of text
* @param {Number} x x-position
* @param {Number} y y-position
* @param {Object} options opentype options (optional), set options.decimals
* to set the decimal precision of the path-data in the element,
* options.fill to set the fill color for the element,
* options.stroke to set the stroke color for the element,
* options.strokeWidth to set the strokeWidth for the element.
*
* @return {Object} this p5.Font object
*/
p5.Font.prototype._getSVG = function(line, x, y, options) {
var decimals = 3;
// create path from string/position
if (typeof line === 'string' && arguments.length > 2) {
line = this._getPath(line, x, y, options);
}
// handle options specified in 2nd arg
else if (typeof x === 'object') {
options = x;
}
// handle svg arguments
if (options) {
if (typeof options.decimals === 'number') {
decimals = options.decimals;
}
if (typeof options.strokeWidth === 'number') {
line.strokeWidth = options.strokeWidth;
}
if (typeof options.fill !== 'undefined') {
line.fill = options.fill;
}
if (typeof options.stroke !== 'undefined') {
line.stroke = options.stroke;
}
}
return line.toSVG(decimals);
};
/*
* Renders an opentype path or string/position
* to the current graphics context
*
* @param {Object} path an opentype path, OR the following:
*
* @param {String} line a line of text
* @param {Number} x x-position
* @param {Number} y y-position
* @param {Object} options opentype options (optional)
*
* @return {Object} this p5.Font object
*/
p5.Font.prototype._renderPath = function(line, x, y, options) {
// /console.log('_renderPath', typeof line);
var pdata, pg = this.parent._renderer,
ctx = pg.drawingContext;
if (typeof line === 'object' && line.commands) {
pdata = line.commands;
} else {
//pos = handleAlignment(p, ctx, line, x, y);
pdata = this._getPath(line, x, y, pg._textSize, options).commands;
}
ctx.beginPath();
for (var i = 0; i < pdata.length; i += 1) {
var cmd = pdata[i];
if (cmd.type === 'M') {
ctx.moveTo(cmd.x, cmd.y);
} else if (cmd.type === 'L') {
ctx.lineTo(cmd.x, cmd.y);
} else if (cmd.type === 'C') {
ctx.bezierCurveTo(cmd.x1, cmd.y1, cmd.x2, cmd.y2, cmd.x, cmd.y);
} else if (cmd.type === 'Q') {
ctx.quadraticCurveTo(cmd.x1, cmd.y1, cmd.x, cmd.y);
} else if (cmd.type === 'Z') {
ctx.closePath();
}
}
// only draw stroke if manually set by user
if (pg._doStroke && pg._strokeSet) {
ctx.stroke();
}
if (pg._doFill) {
// if fill hasn't been set by user, use default-text-fill
ctx.fillStyle = pg._fillSet ? ctx.fillStyle : constants._DEFAULT_TEXT_FILL;
ctx.fill();
}
return this;
};
p5.Font.prototype._textWidth = function(str, fontSize) {
if (str === ' ') { // special case for now
return this.font.charToGlyph(' ').advanceWidth * this._scale(fontSize);
}
var bounds = this.textBounds(str, 0, 0, fontSize);
return bounds.w + bounds.advance;
};
p5.Font.prototype._textAscent = function(fontSize) {
return this.font.ascender * this._scale(fontSize);
};
p5.Font.prototype._textDescent = function(fontSize) {
return -this.font.descender * this._scale(fontSize);
};
p5.Font.prototype._scale = function(fontSize) {
return (1 / this.font.unitsPerEm) * (fontSize ||
this.parent._renderer._textSize);
};
p5.Font.prototype._handleAlignment = function(p, ctx, line, x, y) {
var textWidth = this._textWidth(line),
textAscent = this._textAscent(),
textDescent = this._textDescent(),
textHeight = textAscent + textDescent;
if (ctx.textAlign === constants.CENTER) {
x -= textWidth / 2;
} else if (ctx.textAlign === constants.RIGHT) {
x -= textWidth;
}
if (ctx.textBaseline === constants.TOP) {
y += textHeight;
} else if (ctx.textBaseline === constants._CTX_MIDDLE) {
y += textHeight / 2 - textDescent;
} else if (ctx.textBaseline === constants.BOTTOM) {
y -= textDescent;
}
return { x: x, y: y };
};
function cacheKey() {
var args = new Array(arguments.length);
for (var i = 0; i < args.length; ++i) {
args[i] = arguments[i];
}
i = args.length;
var hash = '';
while (i--) {
hash += (args[i] === Object(args[i])) ?
JSON.stringify(args[i]) : args[i];
}
return hash;
}
module.exports = p5.Font;
},{"../core/constants":47,"../core/core":48}],83:[function(_dereq_,module,exports){
/**
* @module Data
* @submodule Array Functions
* @for p5
* @requires core
*/
'use strict';
var p5 = _dereq_('../core/core');
/**
* Adds a value to the end of an array. Extends the length of
* the array by one. Maps to Array.push().
*
* @method append
* @param {Array} array Array to append
* @param {any} value to be added to the Array
* @example
*
* function setup() {
*
* var myArray = new Array("Mango", "Apple", "Papaya")
* print(myArray) // ["Mango", "Apple", "Papaya"]
*
* append(myArray, "Peach")
* print(myArray) // ["Mango", "Apple", "Papaya", "Peach"]
*
* }
*
*/
p5.prototype.append = function(array, value) {
array.push(value);
return array;
};
/**
* Copies an array (or part of an array) to another array. The src array is
* copied to the dst array, beginning at the position specified by
* srcPosition and into the position specified by dstPosition. The number of
* elements to copy is determined by length. Note that copying values
* overwrites existing values in the destination array. To append values
* instead of overwriting them, use concat().
*
* The simplified version with only two arguments — arrayCopy(src, dst) —
* copies an entire array to another of the same size. It is equivalent to
* arrayCopy(src, 0, dst, 0, src.length).
*
* Using this function is far more efficient for copying array data than
* iterating through a for() loop and copying each element individually.
*
* @method arrayCopy
* @param {Array} src the source Array
* @param {Number} [srcPosition] starting position in the source Array
* @param {Array} dst the destination Array
* @param {Number} [dstPosition] starting position in the destination Array
* @param {Nimber} [length] number of Array elements to be copied
*
* @example
*
* function setup() {
*
* var src = new Array("A", "B", "C");
* var dst = new Array( 1 , 2 , 3 );
* var srcPosition = 1;
* var dstPosition = 0;
* var length = 2;
*
* print(src); // ["A", "B", "C"]
* print(dst); // [ 1 , 2 , 3 ]
*
* arrayCopy(src, srcPosition, dst, dstPosition, length);
* print(dst); // ["B", "C", 3]
*
* }
*
*/
p5.prototype.arrayCopy = function(
src,
srcPosition,
dst,
dstPosition,
length) {
// the index to begin splicing from dst array
var start,
end;
if (typeof length !== 'undefined') {
end = Math.min(length, src.length);
start = dstPosition;
src = src.slice(srcPosition, end + srcPosition);
} else {
if (typeof dst !== 'undefined') { // src, dst, length
// rename so we don't get confused
end = dst;
end = Math.min(end, src.length);
} else { // src, dst
end = src.length;
}
start = 0;
// rename so we don't get confused
dst = srcPosition;
src = src.slice(0, end);
}
// Since we are not returning the array and JavaScript is pass by reference
// we must modify the actual values of the array
// instead of reassigning arrays
Array.prototype.splice.apply(dst, [start, end].concat(src));
};
/**
* Concatenates two arrays, maps to Array.concat(). Does not modify the
* input arrays.
*
* @method concat
* @param {Array} a first Array to concatenate
* @param {Array} b second Array to concatenate
* @return {Array} concatenated array
*
* @example
*
* function setup() {
* var arr1 = new Array("A", "B", "C");
* var arr2 = new Array( 1 , 2 , 3 );
*
* print(arr1); // ["A","B","C"]
* print(arr2); // [1,2,3]
*
* var arr3 = concat(arr1, arr2);
*
* print(arr1); // ["A","B","C"]
* print(arr2); // [1,2,3]
* print(arr3); // ["A","B","C",1,2,3]
*
* }
*
*/
p5.prototype.concat = function(list0, list1) {
return list0.concat(list1);
};
/**
* Reverses the order of an array, maps to Array.reverse()
*
* @method reverse
* @param {Array} list Array to reverse
* @example
*
* function setup() {
* var myArray = new Array("A", "B", "C");
* print(myArray); // ["A","B","C"]
*
* reverse(myArray);
* print(myArray); // ["C","B","A"]
* }
*
*/
p5.prototype.reverse = function(list) {
return list.reverse();
};
/**
* Decreases an array by one element and returns the shortened array,
* maps to Array.pop().
*
* @method shorten
* @param {Array} list Array to shorten
* @return {Array} shortened Array
* @example
*
* function setup() {
* var myArray = new Array("A", "B", "C");
* print(myArray); // ["A","B","C"]
*
* var newArray = shorten(myArray);
* print(myArray); // ["A","B","C"]
* print(newArray); // ["A","B"]
* }
*
*/
p5.prototype.shorten = function(list) {
list.pop();
return list;
};
/**
* Randomizes the order of the elements of an array.
* Implements Fisher-Yates Shuffle Algorithm
* http://Bost.Ocks.org/mike/shuffle/
* http://en.Wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
*
* @method shuffle
* @param {Array} array Array to shuffle
* @param {Boolean} [bool] modify passed array
* @return {Array} shuffled Array
* @example
*
* function setup() {
* var regularArr = ['ABC', 'def', createVector(), TAU, Math.E];
* print(regularArr);
* shuffle(regularArr, true); // force modifications to passed array
* print(regularArr);
*
* // By default shuffle() returns a shuffled cloned array:
* var newArr = shuffle(regularArr);
* print(regularArr);
* print(newArr);
* }
*
*/
p5.prototype.shuffle = function(arr, bool) {
arr = bool || ArrayBuffer.isView(arr)? arr : arr.slice();
var rnd, tmp, idx = arr.length;
while (idx > 1) {
rnd = Math.random()*idx | 0;
tmp = arr[--idx];
arr[idx] = arr[rnd];
arr[rnd] = tmp;
}
return arr;
};
/**
* Sorts an array of numbers from smallest to largest, or puts an array of
* words in alphabetical order. The original array is not modified; a
* re-ordered array is returned. The count parameter states the number of
* elements to sort. For example, if there are 12 elements in an array and
* count is set to 5, only the first 5 elements in the array will be sorted.
*
* @method sort
* @param {Array} list Array to sort
* @param {Number} [count] number of elements to sort, starting from 0
*
* @example
*
* function setup() {
* var words = new Array("banana", "apple", "pear","lime");
* print(words); // ["banana", "apple", "pear", "lime"]
* var count = 4; // length of array
*
* sort(words, count);
* print(words); // ["apple", "banana", "lime", "pear"]
* }
*
*
* function setup() {
* var numbers = new Array(2,6,1,5,14,9,8,12);
* print(numbers); // [2,6,1,5,14,9,8,12]
* var count = 5; // Less than the length of the array
*
* sort(numbers, count);
* print(numbers); // [1,2,5,6,14,9,8,12]
* }
*
*/
p5.prototype.sort = function(list, count) {
var arr = count ? list.slice(0, Math.min(count, list.length)) : list;
var rest = count ? list.slice(Math.min(count, list.length)) : [];
if (typeof arr[0] === 'string') {
arr = arr.sort();
} else {
arr = arr.sort(function(a,b){return a-b;});
}
return arr.concat(rest);
};
/**
* Inserts a value or an array of values into an existing array. The first
* parameter specifies the initial array to be modified, and the second
* parameter defines the data to be inserted. The third parameter is an index
* value which specifies the array position from which to insert data.
* (Remember that array index numbering starts at zero, so the first position
* is 0, the second position is 1, and so on.)
*
* @method splice
* @param {Array} list Array to splice into
* @param {any} value value to be spliced in
* @param {Number} position in the array from which to insert data
*
* @example
*
* function setup() {
* var myArray = new Array(0,1,2,3,4);
* var insArray = new Array("A","B","C");
* print(myArray); // [0,1,2,3,4]
* print(insArray); // ["A","B","C"]
*
* splice(myArray, insArray, 3);
* print(myArray); // [0,1,2,"A","B","C",3,4]
* }
*
*/
p5.prototype.splice = function(list, value, index) {
// note that splice returns spliced elements and not an array
Array.prototype.splice.apply(list, [index, 0].concat(value));
return list;
};
/**
* Extracts an array of elements from an existing array. The list parameter
* defines the array from which the elements will be copied, and the start
* and count parameters specify which elements to extract. If no count is
* given, elements will be extracted from the start to the end of the array.
* When specifying the start, remember that the first array element is 0.
* This function does not change the source array.
*
* @method subset
* @param {Array} list Array to extract from
* @param {Number} start position to begin
* @param {Number} [count] number of values to extract
* @return {Array} Array of extracted elements
*
* @example
*
* function setup() {
* var myArray = new Array(1,2,3,4,5);
* print(myArray); // [1,2,3,4,5]
*
* var sub1 = subset(myArray, 0, 3);
* var sub2 = subset(myArray, 2, 2);
* print(sub1); // [1,2,3]
* print(sub2); // [3,4]
* }
*
*/
p5.prototype.subset = function(list, start, count) {
if (typeof count !== 'undefined') {
return list.slice(start, start + count);
} else {
return list.slice(start, list.length);
}
};
module.exports = p5;
},{"../core/core":48}],84:[function(_dereq_,module,exports){
/**
* @module Data
* @submodule Conversion
* @for p5
* @requires core
*/
'use strict';
var p5 = _dereq_('../core/core');
/**
* Converts a string to its floating point representation. The contents of a
* string must resemble a number, or NaN (not a number) will be returned.
* For example, float("1234.56") evaluates to 1234.56, but float("giraffe")
* will return NaN.
*
* @method float
* @param {String} str float string to parse
* @return {Number} floating point representation of string
* @example
*
* var str = '20';
* var diameter = float(str);
* ellipse(width/2, height/2, diameter, diameter);
*
*/
p5.prototype.float = function(str) {
return parseFloat(str);
};
/**
* Converts a boolean, string, or float to its integer representation.
* When an array of values is passed in, then an int array of the same length
* is returned.
*
* @method int
* @param {String|Boolean|Number|Array} n value to parse
* @return {Number} integer representation of value
* @example
*
* print(int("10")); // 10
* print(int(10.31)); // 10
* print(int(-10)); // -10
* print(int(true)); // 1
* print(int(false)); // 0
* print(int([false, true, "10.3", 9.8])); // [0, 1, 10, 9]
*
*/
p5.prototype.int = function(n, radix) {
if (typeof n === 'string') {
radix = radix || 10;
return parseInt(n, radix);
} else if (typeof n === 'number') {
return n | 0;
} else if (typeof n === 'boolean') {
return n ? 1 : 0;
} else if (n instanceof Array) {
return n.map(function(n) { return p5.prototype.int(n, radix); });
}
};
/**
* Converts a boolean, string or number to its string representation.
* When an array of values is passed in, then an array of strings of the same
* length is returned.
*
* @method str
* @param {String|Boolean|Number|Array} n value to parse
* @return {String} string representation of value
* @example
*
* print(str("10")); // "10"
* print(str(10.31)); // "10.31"
* print(str(-10)); // "-10"
* print(str(true)); // "true"
* print(str(false)); // "false"
* print(str([true, "10.3", 9.8])); // [ "true", "10.3", "9.8" ]
*
*/
p5.prototype.str = function(n) {
if (n instanceof Array) {
return n.map(p5.prototype.str);
} else {
return String(n);
}
};
/**
* Converts a number or string to its boolean representation.
* For a number, any non-zero value (positive or negative) evaluates to true,
* while zero evaluates to false. For a string, the value "true" evaluates to
* true, while any other value evaluates to false. When an array of number or
* string values is passed in, then a array of booleans of the same length is
* returned.
*
* @method boolean
* @param {String|Boolean|Number|Array} n value to parse
* @return {Boolean} boolean representation of value
* @example
*
* print(boolean(0)); // false
* print(boolean(1)); // true
* print(boolean("true")); // true
* print(boolean("abcd")); // false
* print(boolean([0, 12, "true"])); // [false, true, false]
*
*/
p5.prototype.boolean = function(n) {
if (typeof n === 'number') {
return n !== 0;
} else if (typeof n === 'string') {
return n.toLowerCase() === 'true';
} else if (typeof n === 'boolean') {
return n;
} else if (n instanceof Array) {
return n.map(p5.prototype.boolean);
}
};
/**
* Converts a number, string or boolean to its byte representation.
* A byte can be only a whole number between -128 and 127, so when a value
* outside of this range is converted, it wraps around to the corresponding
* byte representation. When an array of number, string or boolean values is
* passed in, then an array of bytes the same length is returned.
*
* @method byte
* @param {String|Boolean|Number|Array} n value to parse
* @return {Number} byte representation of value
* @example
*
* print(byte(127)); // 127
* print(byte(128)); // -128
* print(byte(23.4)); // 23
* print(byte("23.4")); // 23
* print(byte(true)); // 1
* print(byte([0, 255, "100"])); // [0, -1, 100]
*
*/
p5.prototype.byte = function(n) {
var nn = p5.prototype.int(n, 10);
if (typeof nn === 'number') {
return ((nn + 128) % 256) - 128;
} else if (nn instanceof Array) {
return nn.map(p5.prototype.byte);
}
};
/**
* Converts a number or string to its corresponding single-character
* string representation. If a string parameter is provided, it is first
* parsed as an integer and then translated into a single-character string.
* When an array of number or string values is passed in, then an array of
* single-character strings of the same length is returned.
*
* @method char
* @param {String|Number|Array} n value to parse
* @return {String} string representation of value
* @example
*
* print(char(65)); // "A"
* print(char("65")); // "A"
* print(char([65, 66, 67])); // [ "A", "B", "C" ]
* print(join(char([65, 66, 67]), '')); // "ABC"
*
*/
p5.prototype.char = function(n) {
if (typeof n === 'number' && !isNaN(n)) {
return String.fromCharCode(n);
} else if (n instanceof Array) {
return n.map(p5.prototype.char);
} else if (typeof n === 'string') {
return p5.prototype.char(parseInt(n, 10));
}
};
/**
* Converts a single-character string to its corresponding integer
* representation. When an array of single-character string values is passed
* in, then an array of integers of the same length is returned.
*
* @method unchar
* @param {String|Array} n value to parse
* @return {Number} integer representation of value
* @example
*
* print(unchar("A")); // 65
* print(unchar(["A", "B", "C"])); // [ 65, 66, 67 ]
* print(unchar(split("ABC", ""))); // [ 65, 66, 67 ]
*
*/
p5.prototype.unchar = function(n) {
if (typeof n === 'string' && n.length === 1) {
return n.charCodeAt(0);
} else if (n instanceof Array) {
return n.map(p5.prototype.unchar);
}
};
/**
* Converts a number to a string in its equivalent hexadecimal notation. If a
* second parameter is passed, it is used to set the number of characters to
* generate in the hexadecimal notation. When an array is passed in, an
* array of strings in hexadecimal notation of the same length is returned.
*
* @method hex
* @param {Number|Array} n value to parse
* @return {String} hexadecimal string representation of value
* @example
*
* print(hex(255)); // "000000FF"
* print(hex(255, 6)); // "0000FF"
* print(hex([0, 127, 255], 6)); // [ "000000", "00007F", "0000FF" ]
*
*/
p5.prototype.hex = function(n, digits) {
digits = (digits === undefined || digits === null) ? digits = 8 : digits;
if (n instanceof Array) {
return n.map(function(n) { return p5.prototype.hex(n, digits); });
} else if (typeof n === 'number') {
if (n < 0) {
n = 0xFFFFFFFF + n + 1;
}
var hex = Number(n).toString(16).toUpperCase();
while (hex.length < digits) {
hex = '0' + hex;
}
if (hex.length >= digits) {
hex = hex.substring(hex.length - digits, hex.length);
}
return hex;
}
};
/**
* Converts a string representation of a hexadecimal number to its equivalent
* integer value. When an array of strings in hexadecimal notation is passed
* in, an array of integers of the same length is returned.
*
* @method unhex
* @param {String|Array} n value to parse
* @return {Number} integer representation of hexadecimal value
* @example
*
* print(unhex("A")); // 10
* print(unhex("FF")); // 255
* print(unhex(["FF", "AA", "00"])); // [ 255, 170, 0 ]
*
*/
p5.prototype.unhex = function(n) {
if (n instanceof Array) {
return n.map(p5.prototype.unhex);
} else {
return parseInt('0x' + n, 16);
}
};
module.exports = p5;
},{"../core/core":48}],85:[function(_dereq_,module,exports){
/**
* @module Data
* @submodule String Functions
* @for p5
* @requires core
*/
'use strict';
var p5 = _dereq_('../core/core');
//return p5; //LM is this a mistake?
/**
* Combines an array of Strings into one String, each separated by the
* character(s) used for the separator parameter. To join arrays of ints or
* floats, it's necessary to first convert them to Strings using nf() or
* nfs().
*
* @method join
* @param {Array} list array of Strings to be joined
* @param {String} separator String to be placed between each item
* @return {String} joined String
* @example
*
*
* var array = ["Hello", "world!"]
* var separator = " "
* var message = join(array, separator);
* text(message, 5, 50);
*
*
*/
p5.prototype.join = function(list, separator) {
return list.join(separator);
};
/**
* This function is used to apply a regular expression to a piece of text,
* and return matching groups (elements found inside parentheses) as a
* String array. If there are no matches, a null value will be returned.
* If no groups are specified in the regular expression, but the sequence
* matches, an array of length 1 (with the matched text as the first element
* of the array) will be returned.
*
* To use the function, first check to see if the result is null. If the
* result is null, then the sequence did not match at all. If the sequence
* did match, an array is returned.
*
* If there are groups (specified by sets of parentheses) in the regular
* expression, then the contents of each will be returned in the array.
* Element [0] of a regular expression match returns the entire matching
* string, and the match groups start at element [1] (the first group is [1],
* the second [2], and so on).
*
* @method match
* @param {String} str the String to be searched
* @param {String} regexp the regexp to be used for matching
* @return {Array} Array of Strings found
* @example
*
*
* var string = "Hello p5js*!"
* var regexp = "p5js\\*"
* var match = match(string, regexp);
* text(match, 5, 50);
*
*
*/
p5.prototype.match = function(str, reg) {
return str.match(reg);
};
/**
* This function is used to apply a regular expression to a piece of text,
* and return a list of matching groups (elements found inside parentheses)
* as a two-dimensional String array. If there are no matches, a null value
* will be returned. If no groups are specified in the regular expression,
* but the sequence matches, a two dimensional array is still returned, but
* the second dimension is only of length one.
*
* To use the function, first check to see if the result is null. If the
* result is null, then the sequence did not match at all. If the sequence
* did match, a 2D array is returned.
*
* If there are groups (specified by sets of parentheses) in the regular
* expression, then the contents of each will be returned in the array.
* Assuming a loop with counter variable i, element [i][0] of a regular
* expression match returns the entire matching string, and the match groups
* start at element [i][1] (the first group is [i][1], the second [i][2],
* and so on).
*
* @method matchAll
* @param {String} str the String to be searched
* @param {String} regexp the regexp to be used for matching
* @return {Array} 2d Array of Strings found
* @example
*
*
* var string = "Hello p5js*! Hello world!"
* var regexp = "Hello"
* matchAll(string, regexp);
*
*
*/
p5.prototype.matchAll = function(str, reg) {
var re = new RegExp(reg, 'g');
var match = re.exec(str);
var matches = [];
while (match !== null) {
matches.push(match);
// matched text: match[0]
// match start: match.index
// capturing group n: match[n]
match = re.exec(str);
}
return matches;
};
/**
* Utility function for formatting numbers into strings. There are two
* versions: one for formatting floats, and one for formatting ints.
* The values for the digits, left, and right parameters should always
* be positive integers.
*
* @method nf
* @param {Number|Array} num the Number to format
* @param {Number} [left] number of digits to the left of the
* decimal point
* @param {Number} [right] number of digits to the right of the
* decimal point
* @return {String|Array} formatted String
* @example
*
*
* function setup() {
* background(200);
* var num = 112.53106115;
*
* noStroke();
* fill(0);
* textSize(14);
* // Draw formatted numbers
* text(nf(num, 5, 2), 10, 20);
*
* text(nf(num, 4, 3), 10, 55);
*
* text(nf(num, 3, 6), 10, 85);
*
* // Draw dividing lines
* stroke(120);
* line(0, 30, width, 30);
* line(0, 65, width, 65);
* }
*
*
*/
p5.prototype.nf = function () {
if (arguments[0] instanceof Array) {
var a = arguments[1];
var b = arguments[2];
return arguments[0].map(function (x) {
return doNf(x, a, b);
});
}
else{
var typeOfFirst = Object.prototype.toString.call(arguments[0]);
if(typeOfFirst === '[object Arguments]'){
if(arguments[0].length===3){
return this.nf(arguments[0][0],arguments[0][1],arguments[0][2]);
}
else if(arguments[0].length===2){
return this.nf(arguments[0][0],arguments[0][1]);
}
else{
return this.nf(arguments[0][0]);
}
}
else {
return doNf.apply(this, arguments);
}
}
};
function doNf() {
var num = arguments[0];
var neg = num < 0;
var n = neg ? num.toString().substring(1) : num.toString();
var decimalInd = n.indexOf('.');
var intPart = decimalInd !== -1 ? n.substring(0, decimalInd) : n;
var decPart = decimalInd !== -1 ? n.substring(decimalInd + 1) : '';
var str = neg ? '-' : '';
if (arguments.length === 3) {
var decimal = '';
if(decimalInd !== -1 || arguments[2] - decPart.length > 0){
decimal = '.';
}
if (decPart.length > arguments[2]) {
decPart = decPart.substring(0, arguments[2]);
}
for (var i = 0; i < arguments[1] - intPart.length; i++) {
str += '0';
}
str += intPart;
str += decimal;
str += decPart;
for (var j = 0; j < arguments[2] - decPart.length; j++) {
str += '0';
}
return str;
}
else {
for (var k = 0; k < Math.max(arguments[1] - intPart.length, 0); k++) {
str += '0';
}
str += n;
return str;
}
}
/**
* Utility function for formatting numbers into strings and placing
* appropriate commas to mark units of 1000. There are two versions: one
* for formatting ints, and one for formatting an array of ints. The value
* for the right parameter should always be a positive integer.
*
* @method nfc
* @param {Number|Array} num the Number to format
* @param {Number} [right] number of digits to the right of the
* decimal point
* @return {String|Array} formatted String
* @example
*
*
* function setup() {
* background(200);
* var num = 11253106.115;
* var numArr = new Array(1,1,2);
*
* noStroke();
* fill(0);
* textSize(12);
*
* // Draw formatted numbers
* text(nfc(num, 4, 2), 10, 30);
* text(nfc(numArr, 2, 1), 10, 80);
*
* // Draw dividing line
* stroke(120);
* line(0, 50, width, 50);
* }
*
*
*/
p5.prototype.nfc = function () {
if (arguments[0] instanceof Array) {
var a = arguments[1];
return arguments[0].map(function (x) {
return doNfc(x, a);
});
} else {
return doNfc.apply(this, arguments);
}
};
function doNfc() {
var num = arguments[0].toString();
var dec = num.indexOf('.');
var rem = dec !== -1 ? num.substring(dec) : '';
var n = dec !== -1 ? num.substring(0, dec) : num;
n = n.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
if (arguments[1] === 0) {
rem = '';
}
else if(arguments[1] !== undefined){
if(arguments[1] > rem.length){
rem+= dec === -1 ? '.' : '';
var len = arguments[1] - rem.length + 1;
for(var i =0; i< len; i++){
rem += '0';
}
}
else{
rem = rem.substring(0, arguments[1] + 1);
}
}
return n + rem;
}
/**
* Utility function for formatting numbers into strings. Similar to nf() but
* puts a "+" in front of positive numbers and a "-" in front of negative
* numbers. There are two versions: one for formatting floats, and one for
* formatting ints. The values for left, and right parameters
* should always be positive integers.
*
* @method nfp
* @param {Number|Array} num the Number to format
* @param {Number} [left] number of digits to the left of the decimal
* point
* @param {Number} [right] number of digits to the right of the
* decimal point
* @return {String|Array} formatted String
* @example
*
*
* function setup() {
* background(200);
* var num1 = 11253106.115;
* var num2 = -11253106.115;
*
* noStroke();
* fill(0);
* textSize(12);
*
* // Draw formatted numbers
* text(nfp(num1, 4, 2), 10, 30);
* text(nfp(num2, 4, 2), 10, 80);
*
* // Draw dividing line
* stroke(120);
* line(0, 50, width, 50);
* }
*
*
*/
p5.prototype.nfp = function() {
var nfRes = this.nf.apply(this, arguments);
if (nfRes instanceof Array) {
return nfRes.map(addNfp);
} else {
return addNfp(nfRes);
}
};
function addNfp() {
return (
parseFloat(arguments[0]) > 0) ?
'+'+arguments[0].toString() :
arguments[0].toString();
}
/**
* Utility function for formatting numbers into strings. Similar to nf() but
* puts a " " (space) in front of positive numbers and a "-" in front of
* negative numbers. There are two versions: one for formatting floats, and
* one for formatting ints. The values for the digits, left, and right
* parameters should always be positive integers.
*
* @method nfs
* @param {Number|Array} num the Number to format
* @param {Number} [left] number of digits to the left of the decimal
* point
* @param {Number} [right] number of digits to the right of the
* decimal point
* @return {String|Array} formatted String
* @example
*
*
* function setup() {
* background(200);
* var num1 = 11253106.115;
* var num2 = -11253106.115;
*
* noStroke();
* fill(0);
* textSize(12);
* // Draw formatted numbers
* text(nfs(num1, 4, 2), 10, 30);
*
* text(nfs(num2, 4, 2), 10, 80);
*
* // Draw dividing line
* stroke(120);
* line(0, 50, width, 50);
* }
*
*
*/
p5.prototype.nfs = function() {
var nfRes = this.nf.apply(this, arguments);
if (nfRes instanceof Array) {
return nfRes.map(addNfs);
} else {
return addNfs(nfRes);
}
};
function addNfs() {
return (
parseFloat(arguments[0]) > 0) ?
' '+arguments[0].toString() :
arguments[0].toString();
}
/**
* The split() function maps to String.split(), it breaks a String into
* pieces using a character or string as the delimiter. The delim parameter
* specifies the character or characters that mark the boundaries between
* each piece. A String[] array is returned that contains each of the pieces.
*
* The splitTokens() function works in a similar fashion, except that it
* splits using a range of characters instead of a specific character or
* sequence.
*
* @method split
* @param {String} value the String to be split
* @param {String} delim the String used to separate the data
* @return {Array} Array of Strings
* @example
*
*
*var names = "Pat,Xio,Alex"
* var splitString = split(names, ",");
* text(splitString[0], 5, 30);
* text(splitString[1], 5, 50);
* text(splitString[2], 5, 70);
*
*
*/
p5.prototype.split = function(str, delim) {
return str.split(delim);
};
/**
* The splitTokens() function splits a String at one or many character
* delimiters or "tokens." The delim parameter specifies the character or
* characters to be used as a boundary.
*
* If no delim characters are specified, any whitespace character is used to
* split. Whitespace characters include tab (\t), line feed (\n), carriage
* return (\r), form feed (\f), and space.
*
* @method splitTokens
* @param {String} value the String to be split
* @param {String} [delim] list of individual Strings that will be used as
* separators
* @return {Array} Array of Strings
* @example
*
*
* function setup() {
* var myStr = "Mango, Banana, Lime";
* var myStrArr = splitTokens(myStr, ",");
*
* print(myStrArr); // prints : ["Mango"," Banana"," Lime"]
* }
*
*
*/
p5.prototype.splitTokens = function() {
var d,sqo,sqc,str;
str = arguments[1];
if (arguments.length > 1) {
sqc = /\]/g.exec(str);
sqo = /\[/g.exec(str);
if ( sqo && sqc ) {
str = str.slice(0, sqc.index) + str.slice(sqc.index+1);
sqo = /\[/g.exec(str);
str = str.slice(0, sqo.index) + str.slice(sqo.index+1);
d = new RegExp('[\\['+str+'\\]]','g');
} else if ( sqc ) {
str = str.slice(0, sqc.index) + str.slice(sqc.index+1);
d = new RegExp('[' + str + '\\]]', 'g');
} else if(sqo) {
str = str.slice(0, sqo.index) + str.slice(sqo.index+1);
d = new RegExp('[' + str + '\\[]', 'g');
} else {
d = new RegExp('[' + str + ']', 'g');
}
} else {
d = /\s/g;
}
return arguments[0].split(d).filter(function(n){return n;});
};
/**
* Removes whitespace characters from the beginning and end of a String. In
* addition to standard whitespace characters such as space, carriage return,
* and tab, this function also removes the Unicode "nbsp" character.
*
* @method trim
* @param {String|Array} [str] a String or Array of Strings to be trimmed
* @return {String|Array} a trimmed String or Array of Strings
* @example
*
*
* var string = trim(" No new lines\n ");
* text(string +" here", 2, 50);
*
*
*/
p5.prototype.trim = function(str) {
if (str instanceof Array) {
return str.map(this.trim);
} else {
return str.trim();
}
};
module.exports = p5;
},{"../core/core":48}],86:[function(_dereq_,module,exports){
/**
* @module Input
* @submodule Time & Date
* @for p5
* @requires core
*/
'use strict';
var p5 = _dereq_('../core/core');
/**
* p5.js communicates with the clock on your computer. The day() function
* returns the current day as a value from 1 - 31.
*
* @method day
* @return {Number} the current day
* @example
*
*
* var day = day();
* text("Current day: \n"+day, 5, 50);
*
*
*/
p5.prototype.day = function() {
return new Date().getDate();
};
/**
* p5.js communicates with the clock on your computer. The hour() function
* returns the current hour as a value from 0 - 23.
*
* @method hour
* @return {Number} the current hour
* @example
*
*
* var hour = hour();
* text("Current hour:\n"+hour, 5, 50);
*
*
*/
p5.prototype.hour = function() {
return new Date().getHours();
};
/**
* p5.js communicates with the clock on your computer. The minute() function
* returns the current minute as a value from 0 - 59.
*
* @method minute
* @return {Number} the current minute
* @example
*
*
* var minute = minute();
* text("Current minute: \n:"+minute, 5, 50);
*
*
*/
p5.prototype.minute = function() {
return new Date().getMinutes();
};
/**
* Returns the number of milliseconds (thousandths of a second) since
* starting the program. This information is often used for timing events and
* animation sequences.
*
* @method millis
* @return {Number} the number of milliseconds since starting the program
* @example
*
*
* var millisecond = millis();
* text("Milliseconds \nrunning: "+millisecond, 5, 50);
*
*
*/
p5.prototype.millis = function() {
return window.performance.now();
};
/**
* p5.js communicates with the clock on your computer. The month() function
* returns the current month as a value from 1 - 12.
*
* @method month
* @return {Number} the current month
* @example
*
*
* var month = month();
* text("Current month: \n"+month, 5, 50);
*
*
*/
p5.prototype.month = function() {
return new Date().getMonth() + 1; //January is 0!
};
/**
* p5.js communicates with the clock on your computer. The second() function
* returns the current second as a value from 0 - 59.
*
* @method second
* @return {Number} the current second
* @example
*
*
* var second = second();
* text("Current second: \n" +second, 5, 50);
*
*
*/
p5.prototype.second = function() {
return new Date().getSeconds();
};
/**
* p5.js communicates with the clock on your computer. The year() function
* returns the current year as an integer (2014, 2015, 2016, etc).
*
* @method year
* @return {Number} the current year
* @example
*
*
* var year = year();
* text("Current year: \n" +year, 5, 50);
*
*
*/
p5.prototype.year = function() {
return new Date().getFullYear();
};
module.exports = p5;
},{"../core/core":48}]},{},[39])(39)
});
================================================
FILE: p5-ableton/libraries/p5.sound.js
================================================
/*! p5.sound.js v0.2.16 2015-11-03 */
(function (root, factory) {
if (typeof define === 'function' && define.amd)
define('p5.sound', ['p5'], function (p5) { (factory(p5));});
else if (typeof exports === 'object')
factory(require('../p5'));
else
factory(root['p5']);
}(this, function (p5) {
/**
* p5.sound extends p5 with Web Audio functionality including audio input,
* playback, analysis and synthesis.
*
* p5.SoundFile: Load and play sound files.
* p5.Amplitude: Get the current volume of a sound.
* p5.AudioIn: Get sound from an input source, typically
* a computer microphone.
* p5.FFT: Analyze the frequency of sound. Returns
* results from the frequency spectrum or time domain (waveform).
* p5.Oscillator: Generate Sine,
* Triangle, Square and Sawtooth waveforms. Base class of
* p5.Noise and p5.Pulse.
*
* p5.Env: An Envelope is a series
* of fades over time. Often used to control an object's
* output gain level as an "ADSR Envelope" (Attack, Decay,
* Sustain, Release). Can also modulate other parameters.
* p5.Delay: A delay effect with
* parameters for feedback, delayTime, and lowpass filter.
* p5.Filter: Filter the frequency range of a
* sound.
*
* p5.Reverb: Add reverb to a sound by specifying
* duration and decay.
* p5.Convolver: Extends
* p5.Reverb to simulate the sound of real
* physical spaces through convolution.
* p5.SoundRecorder: Record sound for playback
* / save the .wav file.
* p5.Phrase, p5.Part and
* p5.Score: Compose musical sequences.
*
* p5.sound is on GitHub.
* Download the latest version
* here.
*
* @module p5.sound
* @submodule p5.sound
* @for p5.sound
* @main
*/
/**
* p5.sound developed by Jason Sigal for the Processing Foundation, Google Summer of Code 2014. The MIT License (MIT).
*
* http://github.com/therewasaguy/p5.sound
*
* Some of the many audio libraries & resources that inspire p5.sound:
* - TONE.js (c) Yotam Mann, 2014. Licensed under The MIT License (MIT). https://github.com/TONEnoTONE/Tone.js
* - buzz.js (c) Jay Salvat, 2013. Licensed under The MIT License (MIT). http://buzz.jaysalvat.com/
* - Boris Smus Web Audio API book, 2013. Licensed under the Apache License http://www.apache.org/licenses/LICENSE-2.0
* - wavesurfer.js https://github.com/katspaugh/wavesurfer.js
* - Web Audio Components by Jordan Santell https://github.com/web-audio-components
* - Wilm Thoben's Sound library for Processing https://github.com/processing/processing/tree/master/java/libraries/sound
*
* Web Audio API: http://w3.org/TR/webaudio/
*/
var sndcore;
sndcore = function () {
'use strict';
/* AudioContext Monkeypatch
Copyright 2013 Chris Wilson
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
(function (global, exports, perf) {
exports = exports || {};
'use strict';
function fixSetTarget(param) {
if (!param)
// if NYI, just return
return;
if (!param.setTargetAtTime)
param.setTargetAtTime = param.setTargetValueAtTime;
}
if (window.hasOwnProperty('webkitAudioContext') && !window.hasOwnProperty('AudioContext')) {
window.AudioContext = webkitAudioContext;
if (!AudioContext.prototype.hasOwnProperty('createGain'))
AudioContext.prototype.createGain = AudioContext.prototype.createGainNode;
if (!AudioContext.prototype.hasOwnProperty('createDelay'))
AudioContext.prototype.createDelay = AudioContext.prototype.createDelayNode;
if (!AudioContext.prototype.hasOwnProperty('createScriptProcessor'))
AudioContext.prototype.createScriptProcessor = AudioContext.prototype.createJavaScriptNode;
if (!AudioContext.prototype.hasOwnProperty('createPeriodicWave'))
AudioContext.prototype.createPeriodicWave = AudioContext.prototype.createWaveTable;
AudioContext.prototype.internal_createGain = AudioContext.prototype.createGain;
AudioContext.prototype.createGain = function () {
var node = this.internal_createGain();
fixSetTarget(node.gain);
return node;
};
AudioContext.prototype.internal_createDelay = AudioContext.prototype.createDelay;
AudioContext.prototype.createDelay = function (maxDelayTime) {
var node = maxDelayTime ? this.internal_createDelay(maxDelayTime) : this.internal_createDelay();
fixSetTarget(node.delayTime);
return node;
};
AudioContext.prototype.internal_createBufferSource = AudioContext.prototype.createBufferSource;
AudioContext.prototype.createBufferSource = function () {
var node = this.internal_createBufferSource();
if (!node.start) {
node.start = function (when, offset, duration) {
if (offset || duration)
this.noteGrainOn(when || 0, offset, duration);
else
this.noteOn(when || 0);
};
} else {
node.internal_start = node.start;
node.start = function (when, offset, duration) {
if (typeof duration !== 'undefined')
node.internal_start(when || 0, offset, duration);
else
node.internal_start(when || 0, offset || 0);
};
}
if (!node.stop) {
node.stop = function (when) {
this.noteOff(when || 0);
};
} else {
node.internal_stop = node.stop;
node.stop = function (when) {
node.internal_stop(when || 0);
};
}
fixSetTarget(node.playbackRate);
return node;
};
AudioContext.prototype.internal_createDynamicsCompressor = AudioContext.prototype.createDynamicsCompressor;
AudioContext.prototype.createDynamicsCompressor = function () {
var node = this.internal_createDynamicsCompressor();
fixSetTarget(node.threshold);
fixSetTarget(node.knee);
fixSetTarget(node.ratio);
fixSetTarget(node.reduction);
fixSetTarget(node.attack);
fixSetTarget(node.release);
return node;
};
AudioContext.prototype.internal_createBiquadFilter = AudioContext.prototype.createBiquadFilter;
AudioContext.prototype.createBiquadFilter = function () {
var node = this.internal_createBiquadFilter();
fixSetTarget(node.frequency);
fixSetTarget(node.detune);
fixSetTarget(node.Q);
fixSetTarget(node.gain);
return node;
};
if (AudioContext.prototype.hasOwnProperty('createOscillator')) {
AudioContext.prototype.internal_createOscillator = AudioContext.prototype.createOscillator;
AudioContext.prototype.createOscillator = function () {
var node = this.internal_createOscillator();
if (!node.start) {
node.start = function (when) {
this.noteOn(when || 0);
};
} else {
node.internal_start = node.start;
node.start = function (when) {
node.internal_start(when || 0);
};
}
if (!node.stop) {
node.stop = function (when) {
this.noteOff(when || 0);
};
} else {
node.internal_stop = node.stop;
node.stop = function (when) {
node.internal_stop(when || 0);
};
}
if (!node.setPeriodicWave)
node.setPeriodicWave = node.setWaveTable;
fixSetTarget(node.frequency);
fixSetTarget(node.detune);
return node;
};
}
}
if (window.hasOwnProperty('webkitOfflineAudioContext') && !window.hasOwnProperty('OfflineAudioContext')) {
window.OfflineAudioContext = webkitOfflineAudioContext;
}
return exports;
}(window));
// <-- end MonkeyPatch.
// Create the Audio Context
var audiocontext = new window.AudioContext();
/**
* Returns the Audio Context for this sketch. Useful for users
* who would like to dig deeper into the Web Audio API
* .
*
* @method getAudioContext
* @return {Object} AudioContext for this sketch
*/
p5.prototype.getAudioContext = function () {
return audiocontext;
};
// Polyfill for AudioIn, also handled by p5.dom createCapture
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
/**
* Determine which filetypes are supported (inspired by buzz.js)
* The audio element (el) will only be used to test browser support for various audio formats
*/
var el = document.createElement('audio');
p5.prototype.isSupported = function () {
return !!el.canPlayType;
};
var isOGGSupported = function () {
return !!el.canPlayType && el.canPlayType('audio/ogg; codecs="vorbis"');
};
var isMP3Supported = function () {
return !!el.canPlayType && el.canPlayType('audio/mpeg;');
};
var isWAVSupported = function () {
return !!el.canPlayType && el.canPlayType('audio/wav; codecs="1"');
};
var isAACSupported = function () {
return !!el.canPlayType && (el.canPlayType('audio/x-m4a;') || el.canPlayType('audio/aac;'));
};
var isAIFSupported = function () {
return !!el.canPlayType && el.canPlayType('audio/x-aiff;');
};
p5.prototype.isFileSupported = function (extension) {
switch (extension.toLowerCase()) {
case 'mp3':
return isMP3Supported();
case 'wav':
return isWAVSupported();
case 'ogg':
return isOGGSupported();
case 'aac', 'm4a', 'mp4':
return isAACSupported();
case 'aif', 'aiff':
return isAIFSupported();
default:
return false;
}
};
// if it is iOS, we have to have a user interaction to start Web Audio
// http://paulbakaus.com/tutorials/html5/web-audio-on-ios/
var iOS = navigator.userAgent.match(/(iPad|iPhone|iPod)/g) ? true : false;
if (iOS) {
var iosStarted = false;
var startIOS = function () {
if (iosStarted)
return;
// create empty buffer
var buffer = audiocontext.createBuffer(1, 1, 22050);
var source = audiocontext.createBufferSource();
source.buffer = buffer;
// connect to output (your speakers)
source.connect(audiocontext.destination);
// play the file
source.start(0);
console.log('start ios!');
if (audiocontext.state === 'running') {
iosStarted = true;
}
};
document.addEventListener('touchend', startIOS, false);
document.addEventListener('touchstart', startIOS, false);
}
}();
var master;
master = function () {
'use strict';
/**
* Master contains AudioContext and the master sound output.
*/
var Master = function () {
var audiocontext = p5.prototype.getAudioContext();
this.input = audiocontext.createGain();
this.output = audiocontext.createGain();
//put a hard limiter on the output
this.limiter = audiocontext.createDynamicsCompressor();
this.limiter.threshold.value = 0;
this.limiter.ratio.value = 100;
this.audiocontext = audiocontext;
this.output.disconnect();
// an array of input sources
this.inputSources = [];
// connect input to limiter
this.input.connect(this.limiter);
// connect limiter to output
this.limiter.connect(this.output);
// meter is just for global Amplitude / FFT analysis
this.meter = audiocontext.createGain();
this.fftMeter = audiocontext.createGain();
this.output.connect(this.meter);
this.output.connect(this.fftMeter);
// connect output to destination
this.output.connect(this.audiocontext.destination);
// an array of all sounds in the sketch
this.soundArray = [];
// an array of all musical parts in the sketch
this.parts = [];
// file extensions to search for
this.extensions = [];
};
// create a single instance of the p5Sound / master output for use within this sketch
var p5sound = new Master();
/**
* Returns a number representing the master amplitude (volume) for sound
* in this sketch.
*
* @method getMasterVolume
* @return {Number} Master amplitude (volume) for sound in this sketch.
* Should be between 0.0 (silence) and 1.0.
*/
p5.prototype.getMasterVolume = function () {
return p5sound.output.gain.value;
};
/**
* Scale the output of all sound in this sketch
* Scaled between 0.0 (silence) and 1.0 (full volume).
* 1.0 is the maximum amplitude of a digital sound, so multiplying
* by greater than 1.0 may cause digital distortion. To
* fade, provide a rampTime parameter. For more
* complex fades, see the Env class.
*
* Alternately, you can pass in a signal source such as an
* oscillator to modulate the amplitude with an audio signal.
*
* How This Works: When you load the p5.sound module, it
* creates a single instance of p5sound. All sound objects in this
* module output to p5sound before reaching your computer's output.
* So if you change the amplitude of p5sound, it impacts all of the
* sound in this module.
*
* If no value is provided, returns a Web Audio API Gain Node
*
* @method masterVolume
* @param {Number|Object} volume Volume (amplitude) between 0.0
* and 1.0 or modulating signal/oscillator
* @param {Number} [rampTime] Fade for t seconds
* @param {Number} [timeFromNow] Schedule this event to happen at
* t seconds in the future
*/
p5.prototype.masterVolume = function (vol, rampTime, tFromNow) {
if (typeof vol === 'number') {
var rampTime = rampTime || 0;
var tFromNow = tFromNow || 0;
var now = p5sound.audiocontext.currentTime;
var currentVol = p5sound.output.gain.value;
p5sound.output.gain.cancelScheduledValues(now + tFromNow);
p5sound.output.gain.linearRampToValueAtTime(currentVol, now + tFromNow);
p5sound.output.gain.linearRampToValueAtTime(vol, now + tFromNow + rampTime);
} else if (vol) {
vol.connect(p5sound.output.gain);
} else {
// return the Gain Node
return p5sound.output.gain;
}
};
/**
* p5.soundOut is the p5.sound master output. It sends output to
* the destination of this window's web audio context. It contains
* Web Audio API nodes including a dyanmicsCompressor (.limiter),
* and Gain Nodes for .input and .output.
*
* @property p5.soundOut
* @type {Object}
*/
p5.soundOut = p5sound;
/**
* a silent connection to the DesinationNode
* which will ensure that anything connected to it
* will not be garbage collected
*
* @private
*/
p5.soundOut._silentNode = p5sound.audiocontext.createGain();
p5.soundOut._silentNode.gain.value = 0;
p5.soundOut._silentNode.connect(p5sound.audiocontext.destination);
return p5sound;
}(sndcore);
var helpers;
helpers = function () {
'use strict';
var p5sound = master;
/**
* Returns a number representing the sample rate, in samples per second,
* of all sound objects in this audio context. It is determined by the
* sampling rate of your operating system's sound card, and it is not
* currently possile to change.
* It is often 44100, or twice the range of human hearing.
*
* @method sampleRate
* @return {Number} samplerate samples per second
*/
p5.prototype.sampleRate = function () {
return p5sound.audiocontext.sampleRate;
};
/**
* Returns the closest MIDI note value for
* a given frequency.
*
* @param {Number} frequency A freqeuncy, for example, the "A"
* above Middle C is 440Hz
* @return {Number} MIDI note value
*/
p5.prototype.freqToMidi = function (f) {
var mathlog2 = Math.log(f / 440) / Math.log(2);
var m = Math.round(12 * mathlog2) + 57;
return m;
};
/**
* Returns the frequency value of a MIDI note value.
* General MIDI treats notes as integers where middle C
* is 60, C# is 61, D is 62 etc. Useful for generating
* musical frequencies with oscillators.
*
* @method midiToFreq
* @param {Number} midiNote The number of a MIDI note
* @return {Number} Frequency value of the given MIDI note
* @example
*
* var notes = [60, 64, 67, 72];
* var i = 0;
*
* function setup() {
* osc = new p5.Oscillator('Triangle');
* osc.start();
* frameRate(1);
* }
*
* function draw() {
* var freq = midiToFreq(notes[i]);
* osc.freq(freq);
* i++;
* if (i >= notes.length){
* i = 0;
* }
* }
*
*/
p5.prototype.midiToFreq = function (m) {
return 440 * Math.pow(2, (m - 69) / 12);
};
/**
* List the SoundFile formats that you will include. LoadSound
* will search your directory for these extensions, and will pick
* a format that is compatable with the client's web browser.
* Here is a free online file
* converter.
*
* @method soundFormats
* @param {String|Strings} formats i.e. 'mp3', 'wav', 'ogg'
* @example
*
* function preload() {
* // set the global sound formats
* soundFormats('mp3', 'ogg');
*
* // load either beatbox.mp3, or .ogg, depending on browser
* mySound = loadSound('../sounds/beatbox.mp3');
* }
*
* function setup() {
* mySound.play();
* }
*
*/
p5.prototype.soundFormats = function () {
// reset extensions array
p5sound.extensions = [];
// add extensions
for (var i = 0; i < arguments.length; i++) {
arguments[i] = arguments[i].toLowerCase();
if ([
'mp3',
'wav',
'ogg',
'm4a',
'aac'
].indexOf(arguments[i]) > -1) {
p5sound.extensions.push(arguments[i]);
} else {
throw arguments[i] + ' is not a valid sound format!';
}
}
};
p5.prototype.disposeSound = function () {
for (var i = 0; i < p5sound.soundArray.length; i++) {
p5sound.soundArray[i].dispose();
}
};
// register removeSound to dispose of p5sound SoundFiles, Convolvers,
// Oscillators etc when sketch ends
p5.prototype.registerMethod('remove', p5.prototype.disposeSound);
p5.prototype._checkFileFormats = function (paths) {
var path;
// if path is a single string, check to see if extension is provided
if (typeof paths === 'string') {
path = paths;
// see if extension is provided
var extTest = path.split('.').pop();
// if an extension is provided...
if ([
'mp3',
'wav',
'ogg',
'm4a',
'aac'
].indexOf(extTest) > -1) {
var supported = p5.prototype.isFileSupported(extTest);
if (supported) {
path = path;
} else {
var pathSplit = path.split('.');
var pathCore = pathSplit[pathSplit.length - 1];
for (var i = 0; i < p5sound.extensions.length; i++) {
var extension = p5sound.extensions[i];
var supported = p5.prototype.isFileSupported(extension);
if (supported) {
pathCore = '';
if (pathSplit.length === 2) {
pathCore += pathSplit[0];
}
for (var i = 1; i <= pathSplit.length - 2; i++) {
var p = pathSplit[i];
pathCore += '.' + p;
}
path = pathCore += '.';
path = path += extension;
break;
}
}
}
} else {
for (var i = 0; i < p5sound.extensions.length; i++) {
var extension = p5sound.extensions[i];
var supported = p5.prototype.isFileSupported(extension);
if (supported) {
path = path + '.' + extension;
break;
}
}
}
} else if (typeof paths === 'object') {
for (var i = 0; i < paths.length; i++) {
var extension = paths[i].split('.').pop();
var supported = p5.prototype.isFileSupported(extension);
if (supported) {
// console.log('.'+extension + ' is ' + supported +
// ' supported by your browser.');
path = paths[i];
break;
}
}
}
return path;
};
/**
* Used by Osc and Env to chain signal math
*/
p5.prototype._mathChain = function (o, math, thisChain, nextChain, type) {
// if this type of math already exists in the chain, replace it
for (var i in o.mathOps) {
if (o.mathOps[i] instanceof type) {
o.mathOps[i].dispose();
thisChain = i;
if (thisChain < o.mathOps.length - 1) {
nextChain = o.mathOps[i + 1];
}
}
}
o.mathOps[thisChain - 1].disconnect();
o.mathOps[thisChain - 1].connect(math);
math.connect(nextChain);
o.mathOps[thisChain] = math;
return o;
};
}(master);
var panner;
panner = function () {
'use strict';
var p5sound = master;
var ac = p5sound.audiocontext;
// Stereo panner
// if there is a stereo panner node use it
if (typeof ac.createStereoPanner !== 'undefined') {
p5.Panner = function (input, output, numInputChannels) {
this.stereoPanner = this.input = ac.createStereoPanner();
input.connect(this.stereoPanner);
this.stereoPanner.connect(output);
};
p5.Panner.prototype.pan = function (val, tFromNow) {
var time = tFromNow || 0;
var t = ac.currentTime + time;
this.stereoPanner.pan.linearRampToValueAtTime(val, t);
};
p5.Panner.prototype.inputChannels = function (numChannels) {
};
p5.Panner.prototype.connect = function (obj) {
this.stereoPanner.connect(obj);
};
p5.Panner.prototype.disconnect = function (obj) {
this.stereoPanner.disconnect();
};
} else {
// if there is no createStereoPanner object
// such as in safari 7.1.7 at the time of writing this
// use this method to create the effect
p5.Panner = function (input, output, numInputChannels) {
this.input = ac.createGain();
input.connect(this.input);
this.left = ac.createGain();
this.right = ac.createGain();
this.left.channelInterpretation = 'discrete';
this.right.channelInterpretation = 'discrete';
// if input is stereo
if (numInputChannels > 1) {
this.splitter = ac.createChannelSplitter(2);
this.input.connect(this.splitter);
this.splitter.connect(this.left, 1);
this.splitter.connect(this.right, 0);
} else {
this.input.connect(this.left);
this.input.connect(this.right);
}
this.output = ac.createChannelMerger(2);
this.left.connect(this.output, 0, 1);
this.right.connect(this.output, 0, 0);
this.output.connect(output);
};
// -1 is left, +1 is right
p5.Panner.prototype.pan = function (val, tFromNow) {
var time = tFromNow || 0;
var t = ac.currentTime + time;
var v = (val + 1) / 2;
var rightVal = Math.cos(v * Math.PI / 2);
var leftVal = Math.sin(v * Math.PI / 2);
this.left.gain.linearRampToValueAtTime(leftVal, t);
this.right.gain.linearRampToValueAtTime(rightVal, t);
};
p5.Panner.prototype.inputChannels = function (numChannels) {
if (numChannels === 1) {
this.input.disconnect();
this.input.connect(this.left);
this.input.connect(this.right);
} else if (numChannels === 2) {
if (typeof (this.splitter === 'undefined')) {
this.splitter = ac.createChannelSplitter(2);
}
this.input.disconnect();
this.input.connect(this.splitter);
this.splitter.connect(this.left, 1);
this.splitter.connect(this.right, 0);
}
};
p5.Panner.prototype.connect = function (obj) {
this.output.connect(obj);
};
p5.Panner.prototype.disconnect = function (obj) {
this.output.disconnect();
};
}
// 3D panner
p5.Panner3D = function (input, output) {
var panner3D = ac.createPanner();
panner3D.panningModel = 'HRTF';
panner3D.distanceModel = 'linear';
panner3D.setPosition(0, 0, 0);
input.connect(panner3D);
panner3D.connect(output);
panner3D.pan = function (xVal, yVal, zVal) {
panner3D.setPosition(xVal, yVal, zVal);
};
return panner3D;
};
}(master);
var soundfile;
soundfile = function () {
'use strict';
var p5sound = master;
var ac = p5sound.audiocontext;
/**
* SoundFile object with a path to a file.
*
* The p5.SoundFile may not be available immediately because
* it loads the file information asynchronously.
*
* To do something with the sound as soon as it loads
* pass the name of a function as the second parameter.
*
* Only one file path is required. However, audio file formats
* (i.e. mp3, ogg, wav and m4a/aac) are not supported by all
* web browsers. If you want to ensure compatability, instead of a single
* file path, you may include an Array of filepaths, and the browser will
* choose a format that works.
*
* @class p5.SoundFile
* @constructor
* @param {String/Array} path path to a sound file (String). Optionally,
* you may include multiple file formats in
* an array. Alternately, accepts an object
* from the HTML5 File API, or a p5.File.
* @param {Function} [callback] Name of a function to call once file loads
* @return {Object} p5.SoundFile Object
* @example
*
*
* function preload() {
* mySound = loadSound('assets/doorbell.mp3');
* }
*
* function setup() {
* mySound.setVolume(0.1);
* mySound.play();
* }
*
*
*/
p5.SoundFile = function (paths, onload, whileLoading) {
if (typeof paths !== 'undefined') {
if (typeof paths == 'string' || typeof paths[0] == 'string') {
var path = p5.prototype._checkFileFormats(paths);
this.url = path;
} else if (typeof paths == 'object') {
if (!(window.File && window.FileReader && window.FileList && window.Blob)) {
// The File API isn't supported in this browser
throw 'Unable to load file because the File API is not supported';
}
}
// if type is a p5.File...get the actual file
if (paths.file) {
paths = paths.file;
}
this.file = paths;
}
this._looping = false;
this._playing = false;
this._paused = false;
this._pauseTime = 0;
// cues for scheduling events with addCue() removeCue()
this._cues = [];
// position of the most recently played sample
this._lastPos = 0;
this._counterNode;
this._scopeNode;
// array of sources so that they can all be stopped!
this.bufferSourceNodes = [];
// current source
this.bufferSourceNode = null;
this.buffer = null;
this.playbackRate = 1;
this.gain = 1;
this.input = p5sound.audiocontext.createGain();
this.output = p5sound.audiocontext.createGain();
this.reversed = false;
// start and end of playback / loop
this.startTime = 0;
this.endTime = null;
this.pauseTime = 0;
// "restart" would stop playback before retriggering
this.mode = 'sustain';
// time that playback was started, in millis
this.startMillis = null;
this.amplitude = new p5.Amplitude();
this.output.connect(this.amplitude.input);
// stereo panning
this.panPosition = 0;
this.panner = new p5.Panner(this.output, p5sound.input, 2);
// it is possible to instantiate a soundfile with no path
if (this.url || this.file) {
this.load(onload);
}
// add this p5.SoundFile to the soundArray
p5sound.soundArray.push(this);
if (typeof whileLoading === 'function') {
this.whileLoading = whileLoading;
} else {
this.whileLoading = function () {
};
}
};
// register preload handling of loadSound
p5.prototype.registerPreloadMethod('loadSound', p5.prototype);
/**
* loadSound() returns a new p5.SoundFile from a specified
* path. If called during preload(), the p5.SoundFile will be ready
* to play in time for setup() and draw(). If called outside of
* preload, the p5.SoundFile will not be ready immediately, so
* loadSound accepts a callback as the second parameter. Using a
*
* local server is recommended when loading external files.
*
* @method loadSound
* @param {String/Array} path Path to the sound file, or an array with
* paths to soundfiles in multiple formats
* i.e. ['sound.ogg', 'sound.mp3'].
* Alternately, accepts an object: either
* from the HTML5 File API, or a p5.File.
* @param {Function} [callback] Name of a function to call once file loads
* @param {Function} [callback] Name of a function to call while file is loading.
* This function will receive a percentage from 0.0
* to 1.0.
* @return {SoundFile} Returns a p5.SoundFile
* @example
*
* function preload() {
* mySound = loadSound('assets/doorbell.mp3');
* }
*
* function setup() {
* mySound.setVolume(0.1);
* mySound.play();
* }
*
*/
p5.prototype.loadSound = function (path, callback, whileLoading) {
// if loading locally without a server
if (window.location.origin.indexOf('file://') > -1 && window.cordova === 'undefined') {
alert('This sketch may require a server to load external files. Please see http://bit.ly/1qcInwS');
}
var s = new p5.SoundFile(path, callback, whileLoading);
return s;
};
/**
* This is a helper function that the p5.SoundFile calls to load
* itself. Accepts a callback (the name of another function)
* as an optional parameter.
*
* @private
* @param {Function} [callback] Name of a function to call once file loads
*/
p5.SoundFile.prototype.load = function (callback) {
if (this.url != undefined && this.url != '') {
var sf = this;
var request = new XMLHttpRequest();
request.addEventListener('progress', function (evt) {
sf._updateProgress(evt);
}, false);
request.open('GET', this.url, true);
request.responseType = 'arraybuffer';
// decode asyncrohonously
var self = this;
request.onload = function () {
ac.decodeAudioData(request.response, function (buff) {
self.buffer = buff;
self.panner.inputChannels(buff.numberOfChannels);
if (callback) {
callback(self);
}
});
};
request.send();
} else if (this.file != undefined) {
var reader = new FileReader();
var self = this;
reader.onload = function () {
ac.decodeAudioData(reader.result, function (buff) {
self.buffer = buff;
self.panner.inputChannels(buff.numberOfChannels);
if (callback) {
callback(self);
}
});
};
reader.readAsArrayBuffer(this.file);
}
};
// TO DO: use this method to create a loading bar that shows progress during file upload/decode.
p5.SoundFile.prototype._updateProgress = function (evt) {
if (evt.lengthComputable) {
var percentComplete = Math.log(evt.loaded / evt.total * 9.9);
this.whileLoading(percentComplete);
} else {
console.log('size unknown');
}
};
/**
* Returns true if the sound file finished loading successfully.
*
* @method isLoaded
* @return {Boolean}
*/
p5.SoundFile.prototype.isLoaded = function () {
if (this.buffer) {
return true;
} else {
return false;
}
};
/**
* Play the p5.SoundFile
*
* @method play
* @param {Number} [startTime] (optional) schedule playback to start (in seconds from now).
* @param {Number} [rate] (optional) playback rate
* @param {Number} [amp] (optional) amplitude (volume)
* of playback
* @param {Number} [cueStart] (optional) cue start time in seconds
* @param {Number} [duration] (optional) duration of playback in seconds
*/
p5.SoundFile.prototype.play = function (time, rate, amp, _cueStart, duration) {
var self = this;
var now = p5sound.audiocontext.currentTime;
var cueStart, cueEnd;
var time = time || 0;
if (time < 0) {
time = 0;
}
time = time + now;
// TO DO: if already playing, create array of buffers for easy stop()
if (this.buffer) {
// reset the pause time (if it was paused)
this._pauseTime = 0;
// handle restart playmode
if (this.mode === 'restart' && this.buffer && this.bufferSourceNode) {
var now = p5sound.audiocontext.currentTime;
this.bufferSourceNode.stop(time);
this._counterNode.stop(time);
}
// make a new source and counter. They are automatically assigned playbackRate and buffer
this.bufferSourceNode = this._initSourceNode();
this._counterNode = this._initCounterNode();
if (_cueStart) {
if (_cueStart >= 0 && _cueStart < this.buffer.duration) {
// this.startTime = cueStart;
cueStart = _cueStart;
} else {
throw 'start time out of range';
}
} else {
cueStart = 0;
}
if (duration) {
// if duration is greater than buffer.duration, just play entire file anyway rather than throw an error
duration = duration <= this.buffer.duration - cueStart ? duration : this.buffer.duration;
} else {
duration = this.buffer.duration - cueStart;
}
// TO DO: Fix this. It broke in Safari
//
// method of controlling gain for individual bufferSourceNodes, without resetting overall soundfile volume
// if (typeof(this.bufferSourceNode.gain === 'undefined' ) ) {
// this.bufferSourceNode.gain = p5sound.audiocontext.createGain();
// }
// this.bufferSourceNode.connect(this.bufferSourceNode.gain);
// set local amp if provided, otherwise 1
var a = amp || 1;
// this.bufferSourceNode.gain.gain.setValueAtTime(a, p5sound.audiocontext.currentTime);
// this.bufferSourceNode.gain.connect(this.output);
this.bufferSourceNode.connect(this.output);
this.output.gain.value = a;
// not necessary with _initBufferSource ?
// this.bufferSourceNode.playbackRate.cancelScheduledValues(now);
rate = rate || Math.abs(this.playbackRate);
this.bufferSourceNode.playbackRate.setValueAtTime(rate, now);
// if it was paused, play at the pause position
if (this._paused) {
this.bufferSourceNode.start(time, this.pauseTime, duration);
this._counterNode.start(time, this.pauseTime, duration);
} else {
this.bufferSourceNode.start(time, cueStart, duration);
this._counterNode.start(time, cueStart, duration);
}
this._playing = true;
this._paused = false;
// add source to sources array, which is used in stopAll()
this.bufferSourceNodes.push(this.bufferSourceNode);
this.bufferSourceNode._arrayIndex = this.bufferSourceNodes.length - 1;
// delete this.bufferSourceNode from the sources array when it is done playing:
this.bufferSourceNode.onended = function (e) {
var theNode = this;
// if (self.bufferSourceNodes.length === 1) {
this._playing = false;
// }
setTimeout(function () {
self.bufferSourceNodes.splice(theNode._arrayIndex, 1);
if (self.bufferSourceNodes.length === 0) {
self._playing = false;
}
}, 1);
};
} else {
throw 'not ready to play file, buffer has yet to load. Try preload()';
}
// if looping, will restart at original time
this.bufferSourceNode.loop = this._looping;
this._counterNode.loop = this._looping;
if (this._looping === true) {
var cueEnd = cueStart + duration;
this.bufferSourceNode.loopStart = cueStart;
this.bufferSourceNode.loopEnd = cueEnd;
this._counterNode.loopStart = cueStart;
this._counterNode.loopEnd = cueEnd;
}
};
/**
* p5.SoundFile has two play modes: restart and
* sustain. Play Mode determines what happens to a
* p5.SoundFile if it is triggered while in the middle of playback.
* In sustain mode, playback will continue simultaneous to the
* new playback. In restart mode, play() will stop playback
* and start over. Sustain is the default mode.
*
* @method playMode
* @param {String} str 'restart' or 'sustain'
* @example
*
* function setup(){
* mySound = loadSound('assets/Damscray_DancingTiger.mp3');
* }
* function mouseClicked() {
* mySound.playMode('sustain');
* mySound.play();
* }
* function keyPressed() {
* mySound.playMode('restart');
* mySound.play();
* }
*
*
*/
p5.SoundFile.prototype.playMode = function (str) {
var s = str.toLowerCase();
// if restart, stop all other sounds from playing
if (s === 'restart' && this.buffer && this.bufferSourceNode) {
for (var i = 0; i < this.bufferSourceNodes.length - 1; i++) {
var now = p5sound.audiocontext.currentTime;
this.bufferSourceNodes[i].stop(now);
}
}
// set play mode to effect future playback
if (s === 'restart' || s === 'sustain') {
this.mode = s;
} else {
throw 'Invalid play mode. Must be either "restart" or "sustain"';
}
};
/**
* Pauses a file that is currently playing. If the file is not
* playing, then nothing will happen.
*
* After pausing, .play() will resume from the paused
* position.
* If p5.SoundFile had been set to loop before it was paused,
* it will continue to loop after it is unpaused with .play().
*
* @method pause
* @param {Number} [startTime] (optional) schedule event to occur
* seconds from now
* @example
*
* var soundFile;
*
* function preload() {
* soundFormats('ogg', 'mp3');
* soundFile = loadSound('assets/Damscray_-_Dancing_Tiger_02.mp3');
* }
* function setup() {
* background(0, 255, 0);
* soundFile.setVolume(0.1);
* soundFile.loop();
* }
* function keyTyped() {
* if (key == 'p') {
* soundFile.pause();
* background(255, 0, 0);
* }
* }
*
* function keyReleased() {
* if (key == 'p') {
* soundFile.play();
* background(0, 255, 0);
* }
*
*
*/
p5.SoundFile.prototype.pause = function (time) {
var now = p5sound.audiocontext.currentTime;
var time = time || 0;
var pTime = time + now;
if (this.isPlaying() && this.buffer && this.bufferSourceNode) {
this.pauseTime = this.currentTime();
this.bufferSourceNode.stop(pTime);
this._counterNode.stop(pTime);
this._paused = true;
this._playing = false;
this._pauseTime = this.currentTime();
} else {
this._pauseTime = 0;
}
};
/**
* Loop the p5.SoundFile. Accepts optional parameters to set the
* playback rate, playback volume, loopStart, loopEnd.
*
* @method loop
* @param {Number} [startTime] (optional) schedule event to occur
* seconds from now
* @param {Number} [rate] (optional) playback rate
* @param {Number} [amp] (optional) playback volume
* @param {Number} [cueLoopStart](optional) startTime in seconds
* @param {Number} [duration] (optional) loop duration in seconds
*/
p5.SoundFile.prototype.loop = function (startTime, rate, amp, loopStart, duration) {
this._looping = true;
this.play(startTime, rate, amp, loopStart, duration);
};
/**
* Set a p5.SoundFile's looping flag to true or false. If the sound
* is currently playing, this change will take effect when it
* reaches the end of the current playback.
*
* @param {Boolean} Boolean set looping to true or false
*/
p5.SoundFile.prototype.setLoop = function (bool) {
if (bool === true) {
this._looping = true;
} else if (bool === false) {
this._looping = false;
} else {
throw 'Error: setLoop accepts either true or false';
}
if (this.bufferSourceNode) {
this.bufferSourceNode.loop = this._looping;
this._counterNode.loop = this._looping;
}
};
/**
* Returns 'true' if a p5.SoundFile is currently looping and playing, 'false' if not.
*
* @return {Boolean}
*/
p5.SoundFile.prototype.isLooping = function () {
if (!this.bufferSourceNode) {
return false;
}
if (this._looping === true && this.isPlaying() === true) {
return true;
}
return false;
};
/**
* Returns true if a p5.SoundFile is playing, false if not (i.e.
* paused or stopped).
*
* @method isPlaying
* @return {Boolean}
*/
p5.SoundFile.prototype.isPlaying = function () {
return this._playing;
};
/**
* Returns true if a p5.SoundFile is paused, false if not (i.e.
* playing or stopped).
*
* @method isPaused
* @return {Boolean}
*/
p5.SoundFile.prototype.isPaused = function () {
return this._paused;
};
/**
* Stop soundfile playback.
*
* @method stop
* @param {Number} [startTime] (optional) schedule event to occur
* in seconds from now
*/
p5.SoundFile.prototype.stop = function (timeFromNow) {
var time = timeFromNow || 0;
if (this.mode == 'sustain') {
this.stopAll(time);
this._playing = false;
this.pauseTime = 0;
this._paused = false;
} else if (this.buffer && this.bufferSourceNode) {
var now = p5sound.audiocontext.currentTime;
var t = time || 0;
this.pauseTime = 0;
this.bufferSourceNode.stop(now + t);
this._counterNode.stop(now + t);
this._playing = false;
this._paused = false;
}
};
/**
* Stop playback on all of this soundfile's sources.
* @private
*/
p5.SoundFile.prototype.stopAll = function (_time) {
var now = p5sound.audiocontext.currentTime;
var time = _time || 0;
if (this.buffer && this.bufferSourceNode) {
for (var i = 0; i < this.bufferSourceNodes.length; i++) {
if (typeof this.bufferSourceNodes[i] != undefined) {
try {
this.bufferSourceNodes[i].stop(now + time);
} catch (e) {
}
}
}
this._counterNode.stop(now + time);
}
};
/**
* Multiply the output volume (amplitude) of a sound file
* between 0.0 (silence) and 1.0 (full volume).
* 1.0 is the maximum amplitude of a digital sound, so multiplying
* by greater than 1.0 may cause digital distortion. To
* fade, provide a rampTime parameter. For more
* complex fades, see the Env class.
*
* Alternately, you can pass in a signal source such as an
* oscillator to modulate the amplitude with an audio signal.
*
* @method setVolume
* @param {Number|Object} volume Volume (amplitude) between 0.0
* and 1.0 or modulating signal/oscillator
* @param {Number} [rampTime] Fade for t seconds
* @param {Number} [timeFromNow] Schedule this event to happen at
* t seconds in the future
*/
p5.SoundFile.prototype.setVolume = function (vol, rampTime, tFromNow) {
if (typeof vol === 'number') {
var rampTime = rampTime || 0;
var tFromNow = tFromNow || 0;
var now = p5sound.audiocontext.currentTime;
var currentVol = this.output.gain.value;
this.output.gain.cancelScheduledValues(now + tFromNow);
this.output.gain.linearRampToValueAtTime(currentVol, now + tFromNow);
this.output.gain.linearRampToValueAtTime(vol, now + tFromNow + rampTime);
} else if (vol) {
vol.connect(this.output.gain);
} else {
// return the Gain Node
return this.output.gain;
}
};
// same as setVolume, to match Processing Sound
p5.SoundFile.prototype.amp = p5.SoundFile.prototype.setVolume;
// these are the same thing
p5.SoundFile.prototype.fade = p5.SoundFile.prototype.setVolume;
p5.SoundFile.prototype.getVolume = function () {
return this.output.gain.value;
};
/**
* Set the stereo panning of a p5.sound object to
* a floating point number between -1.0 (left) and 1.0 (right).
* Default is 0.0 (center).
*
* @method pan
* @param {Number} [panValue] Set the stereo panner
* @param {Number} timeFromNow schedule this event to happen
* seconds from now
* @example
*
*
* var ball = {};
* var soundFile;
*
* function setup() {
* soundFormats('ogg', 'mp3');
* soundFile = loadSound('assets/beatbox.mp3');
* }
*
* function draw() {
* background(0);
* ball.x = constrain(mouseX, 0, width);
* ellipse(ball.x, height/2, 20, 20)
* }
*
* function mousePressed(){
* // map the ball's x location to a panning degree
* // between -1.0 (left) and 1.0 (right)
* var panning = map(ball.x, 0., width,-1.0, 1.0);
* soundFile.pan(panning);
* soundFile.play();
* }
*
*/
p5.SoundFile.prototype.pan = function (pval, tFromNow) {
this.panPosition = pval;
this.panner.pan(pval, tFromNow);
};
/**
* Returns the current stereo pan position (-1.0 to 1.0)
*
* @return {Number} Returns the stereo pan setting of the Oscillator
* as a number between -1.0 (left) and 1.0 (right).
* 0.0 is center and default.
*/
p5.SoundFile.prototype.getPan = function () {
return this.panPosition;
};
/**
* Set the playback rate of a sound file. Will change the speed and the pitch.
* Values less than zero will reverse the audio buffer.
*
* @method rate
* @param {Number} [playbackRate] Set the playback rate. 1.0 is normal,
* .5 is half-speed, 2.0 is twice as fast.
* Must be greater than zero.
* @example
*
* var song;
*
* function preload() {
* song = loadSound('assets/Damscray_DancingTiger.mp3');
* }
*
* function setup() {
* song.loop();
* }
*
* function draw() {
* background(200);
*
* // Set the rate to a range between 0.1 and 4
* // Changing the rate also alters the pitch
* var speed = map(mouseY, 0.1, height, 0, 2);
* speed = constrain(speed, 0.01, 4);
* song.rate(speed);
*
* // Draw a circle to show what is going on
* stroke(0);
* fill(51, 100);
* ellipse(mouseX, 100, 48, 48);
* }
*
*
*
*
*/
p5.SoundFile.prototype.rate = function (playbackRate) {
if (this.playbackRate === playbackRate && this.bufferSourceNode) {
if (this.bufferSourceNode.playbackRate.value === playbackRate) {
return;
}
}
this.playbackRate = playbackRate;
var rate = playbackRate;
if (this.playbackRate === 0 && this._playing) {
this.pause();
}
if (this.playbackRate < 0 && !this.reversed) {
var cPos = this.currentTime();
var cRate = this.bufferSourceNode.playbackRate.value;
// this.pause();
this.reverseBuffer();
rate = Math.abs(playbackRate);
var newPos = (cPos - this.duration()) / rate;
this.pauseTime = newPos;
} else if (this.playbackRate > 0 && this.reversed) {
this.reverseBuffer();
}
if (this.bufferSourceNode) {
var now = p5sound.audiocontext.currentTime;
this.bufferSourceNode.playbackRate.cancelScheduledValues(now);
this.bufferSourceNode.playbackRate.linearRampToValueAtTime(Math.abs(rate), now);
this._counterNode.playbackRate.cancelScheduledValues(now);
this._counterNode.playbackRate.linearRampToValueAtTime(Math.abs(rate), now);
}
};
// TO DO: document this
p5.SoundFile.prototype.setPitch = function (num) {
var newPlaybackRate = midiToFreq(num) / midiToFreq(60);
this.rate(newPlaybackRate);
};
p5.SoundFile.prototype.getPlaybackRate = function () {
return this.playbackRate;
};
/**
* Returns the duration of a sound file in seconds.
*
* @method duration
* @return {Number} The duration of the soundFile in seconds.
*/
p5.SoundFile.prototype.duration = function () {
// Return Duration
if (this.buffer) {
return this.buffer.duration;
} else {
return 0;
}
};
/**
* Return the current position of the p5.SoundFile playhead, in seconds.
* Note that if you change the playbackRate while the p5.SoundFile is
* playing, the results may not be accurate.
*
* @method currentTime
* @return {Number} currentTime of the soundFile in seconds.
*/
p5.SoundFile.prototype.currentTime = function () {
// TO DO --> make reverse() flip these values appropriately
if (this._pauseTime > 0) {
return this._pauseTime;
} else {
return this._lastPos / ac.sampleRate;
}
};
/**
* Move the playhead of the song to a position, in seconds. Start
* and Stop time. If none are given, will reset the file to play
* entire duration from start to finish.
*
* @method jump
* @param {Number} cueTime cueTime of the soundFile in seconds.
* @param {Number} uuration duration in seconds.
*/
p5.SoundFile.prototype.jump = function (cueTime, duration) {
if (cueTime < 0 || cueTime > this.buffer.duration) {
throw 'jump time out of range';
}
if (duration > this.buffer.duration - cueTime) {
throw 'end time out of range';
}
var cTime = cueTime || 0;
var eTime = duration || this.buffer.duration - cueTime;
if (this.isPlaying()) {
this.stop();
}
this.play(0, this.playbackRate, this.output.gain.value, cTime, eTime);
};
/**
* Return the number of channels in a sound file.
* For example, Mono = 1, Stereo = 2.
*
* @method channels
* @return {Number} [channels]
*/
p5.SoundFile.prototype.channels = function () {
return this.buffer.numberOfChannels;
};
/**
* Return the sample rate of the sound file.
*
* @method sampleRate
* @return {Number} [sampleRate]
*/
p5.SoundFile.prototype.sampleRate = function () {
return this.buffer.sampleRate;
};
/**
* Return the number of samples in a sound file.
* Equal to sampleRate * duration.
*
* @method frames
* @return {Number} [sampleCount]
*/
p5.SoundFile.prototype.frames = function () {
return this.buffer.length;
};
/**
* Returns an array of amplitude peaks in a p5.SoundFile that can be
* used to draw a static waveform. Scans through the p5.SoundFile's
* audio buffer to find the greatest amplitudes. Accepts one
* parameter, 'length', which determines size of the array.
* Larger arrays result in more precise waveform visualizations.
*
* Inspired by Wavesurfer.js.
*
* @method getPeaks
* @params {Number} [length] length is the size of the returned array.
* Larger length results in more precision.
* Defaults to 5*width of the browser window.
* @returns {Float32Array} Array of peaks.
*/
p5.SoundFile.prototype.getPeaks = function (length) {
if (this.buffer) {
// set length to window's width if no length is provided
if (!length) {
length = window.width * 5;
}
if (this.buffer) {
var buffer = this.buffer;
var sampleSize = buffer.length / length;
var sampleStep = ~~(sampleSize / 10) || 1;
var channels = buffer.numberOfChannels;
var peaks = new Float32Array(Math.round(length));
for (var c = 0; c < channels; c++) {
var chan = buffer.getChannelData(c);
for (var i = 0; i < length; i++) {
var start = ~~(i * sampleSize);
var end = ~~(start + sampleSize);
var max = 0;
for (var j = start; j < end; j += sampleStep) {
var value = chan[j];
if (value > max) {
max = value;
} else if (-value > max) {
max = value;
}
}
if (c === 0 || Math.abs(max) > peaks[i]) {
peaks[i] = max;
}
}
}
return peaks;
}
} else {
throw 'Cannot load peaks yet, buffer is not loaded';
}
};
/**
* Reverses the p5.SoundFile's buffer source.
* Playback must be handled separately (see example).
*
* @method reverseBuffer
* @example
*
* var drum;
*
* function preload() {
* drum = loadSound('assets/drum.mp3');
* }
*
* function setup() {
* drum.reverseBuffer();
* drum.play();
* }
*
*
*
*/
p5.SoundFile.prototype.reverseBuffer = function () {
var curVol = this.getVolume();
this.setVolume(0, 0.01, 0);
this.pause();
if (this.buffer) {
for (var i = 0; i < this.buffer.numberOfChannels; i++) {
Array.prototype.reverse.call(this.buffer.getChannelData(i));
}
// set reversed flag
this.reversed = !this.reversed;
} else {
throw 'SoundFile is not done loading';
}
this.setVolume(curVol, 0.01, 0.0101);
this.play();
};
// private function for onended behavior
p5.SoundFile.prototype._onEnded = function (s) {
s.onended = function (s) {
var now = p5sound.audiocontext.currentTime;
s.stop(now);
};
};
p5.SoundFile.prototype.add = function () {
};
p5.SoundFile.prototype.dispose = function () {
var now = p5sound.audiocontext.currentTime;
this.stop(now);
if (this.buffer && this.bufferSourceNode) {
for (var i = 0; i < this.bufferSourceNodes.length - 1; i++) {
if (this.bufferSourceNodes[i] !== null) {
this.bufferSourceNodes[i].disconnect();
try {
this.bufferSourceNodes[i].stop(now);
} catch (e) {
}
this.bufferSourceNodes[i] = null;
}
}
if (this.isPlaying()) {
try {
this._counterNode.stop(now);
} catch (e) {
console.log(e);
}
this._counterNode = null;
}
}
if (this.output) {
this.output.disconnect();
this.output = null;
}
if (this.panner) {
this.panner.disconnect();
this.panner = null;
}
};
/**
* Connects the output of a p5sound object to input of another
* p5.sound object. For example, you may connect a p5.SoundFile to an
* FFT or an Effect. If no parameter is given, it will connect to
* the master output. Most p5sound objects connect to the master
* output when they are created.
*
* @method connect
* @param {Object} [object] Audio object that accepts an input
*/
p5.SoundFile.prototype.connect = function (unit) {
if (!unit) {
this.panner.connect(p5sound.input);
} else {
if (unit.hasOwnProperty('input')) {
this.panner.connect(unit.input);
} else {
this.panner.connect(unit);
}
}
};
/**
* Disconnects the output of this p5sound object.
*
* @method disconnect
*/
p5.SoundFile.prototype.disconnect = function (unit) {
this.panner.disconnect(unit);
};
/**
* Read the Amplitude (volume level) of a p5.SoundFile. The
* p5.SoundFile class contains its own instance of the Amplitude
* class to help make it easy to get a SoundFile's volume level.
* Accepts an optional smoothing value (0.0 < 1.0).
*
* @method getLevel
* @param {Number} [smoothing] Smoothing is 0.0 by default.
* Smooths values based on previous values.
* @return {Number} Volume level (between 0.0 and 1.0)
*/
p5.SoundFile.prototype.getLevel = function (smoothing) {
if (smoothing) {
this.amplitude.smoothing = smoothing;
}
return this.amplitude.getLevel();
};
/**
* Reset the source for this SoundFile to a
* new path (URL).
*
* @method setPath
* @param {String} path path to audio file
* @param {Function} callback Callback
*/
p5.SoundFile.prototype.setPath = function (p, callback) {
var path = p5.prototype._checkFileFormats(p);
this.url = path;
this.load(callback);
};
/**
* Replace the current Audio Buffer with a new Buffer.
*
* @param {Array} buf Array of Float32 Array(s). 2 Float32 Arrays
* will create a stereo source. 1 will create
* a mono source.
*/
p5.SoundFile.prototype.setBuffer = function (buf) {
var numChannels = buf.length;
var size = buf[0].length;
var newBuffer = ac.createBuffer(numChannels, size, ac.sampleRate);
if (!buf[0] instanceof Float32Array) {
buf[0] = new Float32Array(buf[0]);
}
for (var channelNum = 0; channelNum < numChannels; channelNum++) {
var channel = newBuffer.getChannelData(channelNum);
channel.set(buf[channelNum]);
}
this.buffer = newBuffer;
// set numbers of channels on input to the panner
this.panner.inputChannels(numChannels);
};
//////////////////////////////////////////////////
// script processor node with an empty buffer to help
// keep a sample-accurate position in playback buffer.
// Inspired by Chinmay Pendharkar's technique for Sonoport --> http://bit.ly/1HwdCsV
// Copyright [2015] [Sonoport (Asia) Pte. Ltd.],
// Licensed under the Apache License http://apache.org/licenses/LICENSE-2.0
////////////////////////////////////////////////////////////////////////////////////
// initialize counterNode, set its initial buffer and playbackRate
p5.SoundFile.prototype._initCounterNode = function () {
var self = this;
var now = ac.currentTime;
var cNode = ac.createBufferSource();
// dispose of scope node if it already exists
if (self._scopeNode) {
self._scopeNode.disconnect();
self._scopeNode = null;
}
self._scopeNode = ac.createScriptProcessor(256, 1, 1);
// create counter buffer of the same length as self.buffer
cNode.buffer = _createCounterBuffer(self.buffer);
cNode.playbackRate.setValueAtTime(self.playbackRate, now);
cNode.connect(self._scopeNode);
self._scopeNode.connect(p5.soundOut._silentNode);
self._scopeNode.onaudioprocess = function (processEvent) {
var inputBuffer = processEvent.inputBuffer.getChannelData(0);
// update the lastPos
self._lastPos = inputBuffer[inputBuffer.length - 1] || 0;
// do any callbacks that have been scheduled
self._onTimeUpdate(self._lastPos);
};
return cNode;
};
// initialize sourceNode, set its initial buffer and playbackRate
p5.SoundFile.prototype._initSourceNode = function () {
var self = this;
var now = ac.currentTime;
var bufferSourceNode = ac.createBufferSource();
bufferSourceNode.buffer = self.buffer;
bufferSourceNode.playbackRate.setValueAtTime(self.playbackRate, now);
return bufferSourceNode;
};
var _createCounterBuffer = function (buffer) {
var array = new Float32Array(buffer.length);
var audioBuf = ac.createBuffer(1, buffer.length, 44100);
for (var index = 0; index < buffer.length; index++) {
array[index] = index;
}
audioBuf.getChannelData(0).set(array);
return audioBuf;
};
/**
* processPeaks returns an array of timestamps where it thinks there is a beat.
*
* This is an asynchronous function that processes the soundfile in an offline audio context,
* and sends the results to your callback function.
*
* The process involves running the soundfile through a lowpass filter, and finding all of the
* peaks above the initial threshold. If the total number of peaks are below the minimum number of peaks,
* it decreases the threshold and re-runs the analysis until either minPeaks or minThreshold are reached.
*
* @method processPeaks
* @param {Function} callback a function to call once this data is returned
* @param {Number} [initThreshold] initial threshold defaults to 0.9
* @param {Number} [minThreshold] minimum threshold defaults to 0.22
* @param {Number} [minPeaks] minimum number of peaks defaults to 200
* @return {Array} Array of timestamped peaks
*/
p5.SoundFile.prototype.processPeaks = function (callback, _initThreshold, _minThreshold, _minPeaks) {
var bufLen = this.buffer.length;
var sampleRate = this.buffer.sampleRate;
var buffer = this.buffer;
var initialThreshold = _initThreshold || 0.9, threshold = initialThreshold, minThreshold = _minThreshold || 0.22, minPeaks = _minPeaks || 200;
// Create offline context
var offlineContext = new OfflineAudioContext(1, bufLen, sampleRate);
// create buffer source
var source = offlineContext.createBufferSource();
source.buffer = buffer;
// Create filter. TO DO: allow custom setting of filter
var filter = offlineContext.createBiquadFilter();
filter.type = 'lowpass';
source.connect(filter);
filter.connect(offlineContext.destination);
// start playing at time:0
source.start(0);
offlineContext.startRendering();
// Render the song
// act on the result
offlineContext.oncomplete = function (e) {
var data = {};
var filteredBuffer = e.renderedBuffer;
var bufferData = filteredBuffer.getChannelData(0);
// step 1:
// create Peak instances, add them to array, with strength and sampleIndex
do {
allPeaks = getPeaksAtThreshold(bufferData, threshold);
threshold -= 0.005;
} while (Object.keys(allPeaks).length < minPeaks && threshold >= minThreshold);
// step 2:
// find intervals for each peak in the sampleIndex, add tempos array
var intervalCounts = countIntervalsBetweenNearbyPeaks(allPeaks);
// step 3: find top tempos
var groups = groupNeighborsByTempo(intervalCounts, filteredBuffer.sampleRate);
// sort top intervals
var topTempos = groups.sort(function (intA, intB) {
return intB.count - intA.count;
}).splice(0, 5);
// set this SoundFile's tempo to the top tempo ??
this.tempo = topTempos[0].tempo;
// step 4:
// new array of peaks at top tempo within a bpmVariance
var bpmVariance = 5;
var tempoPeaks = getPeaksAtTopTempo(allPeaks, topTempos[0].tempo, filteredBuffer.sampleRate, bpmVariance);
callback(tempoPeaks);
};
};
// process peaks
var Peak = function (amp, i) {
this.sampleIndex = i;
this.amplitude = amp;
this.tempos = [];
this.intervals = [];
};
var allPeaks = [];
// 1. for processPeaks() Function to identify peaks above a threshold
// returns an array of peak indexes as frames (samples) of the original soundfile
function getPeaksAtThreshold(data, threshold) {
var peaksObj = {};
var length = data.length;
for (var i = 0; i < length; i++) {
if (data[i] > threshold) {
var amp = data[i];
var peak = new Peak(amp, i);
peaksObj[i] = peak;
// Skip forward ~ 1/8s to get past this peak.
i += 6000;
}
i++;
}
return peaksObj;
}
// 2. for processPeaks()
function countIntervalsBetweenNearbyPeaks(peaksObj) {
var intervalCounts = [];
var peaksArray = Object.keys(peaksObj).sort();
for (var index = 0; index < peaksArray.length; index++) {
// find intervals in comparison to nearby peaks
for (var i = 0; i < 10; i++) {
var startPeak = peaksObj[peaksArray[index]];
var endPeak = peaksObj[peaksArray[index + i]];
if (startPeak && endPeak) {
var startPos = startPeak.sampleIndex;
var endPos = endPeak.sampleIndex;
var interval = endPos - startPos;
// add a sample interval to the startPeek in the allPeaks array
if (interval > 0) {
startPeak.intervals.push(interval);
}
// tally the intervals and return interval counts
var foundInterval = intervalCounts.some(function (intervalCount, p) {
if (intervalCount.interval === interval) {
intervalCount.count++;
return intervalCount;
}
});
// store with JSON like formatting
if (!foundInterval) {
intervalCounts.push({
interval: interval,
count: 1
});
}
}
}
}
return intervalCounts;
}
// 3. for processPeaks --> find tempo
function groupNeighborsByTempo(intervalCounts, sampleRate) {
var tempoCounts = [];
intervalCounts.forEach(function (intervalCount, i) {
try {
// Convert an interval to tempo
var theoreticalTempo = Math.abs(60 / (intervalCount.interval / sampleRate));
theoreticalTempo = mapTempo(theoreticalTempo);
var foundTempo = tempoCounts.some(function (tempoCount) {
if (tempoCount.tempo === theoreticalTempo)
return tempoCount.count += intervalCount.count;
});
if (!foundTempo) {
if (isNaN(theoreticalTempo)) {
return;
}
tempoCounts.push({
tempo: Math.round(theoreticalTempo),
count: intervalCount.count
});
}
} catch (e) {
throw e;
}
});
return tempoCounts;
}
// 4. for processPeaks - get peaks at top tempo
function getPeaksAtTopTempo(peaksObj, tempo, sampleRate, bpmVariance) {
var peaksAtTopTempo = [];
var peaksArray = Object.keys(peaksObj).sort();
// TO DO: filter out peaks that have the tempo and return
for (var i = 0; i < peaksArray.length; i++) {
var key = peaksArray[i];
var peak = peaksObj[key];
for (var j = 0; j < peak.intervals.length; j++) {
var intervalBPM = Math.round(Math.abs(60 / (peak.intervals[j] / sampleRate)));
intervalBPM = mapTempo(intervalBPM);
var dif = intervalBPM - tempo;
if (Math.abs(intervalBPM - tempo) < bpmVariance) {
// convert sampleIndex to seconds
peaksAtTopTempo.push(peak.sampleIndex / 44100);
}
}
}
// filter out peaks that are very close to each other
peaksAtTopTempo = peaksAtTopTempo.filter(function (peakTime, index, arr) {
var dif = arr[index + 1] - peakTime;
if (dif > 0.01) {
return true;
}
});
return peaksAtTopTempo;
}
// helper function for processPeaks
function mapTempo(theoreticalTempo) {
// these scenarios create infinite while loop
if (!isFinite(theoreticalTempo) || theoreticalTempo == 0) {
return;
}
// Adjust the tempo to fit within the 90-180 BPM range
while (theoreticalTempo < 90)
theoreticalTempo *= 2;
while (theoreticalTempo > 180 && theoreticalTempo > 90)
theoreticalTempo /= 2;
return theoreticalTempo;
}
/*** SCHEDULE EVENTS ***/
/**
* Schedule events to trigger every time a MediaElement
* (audio/video) reaches a playback cue point.
*
* Accepts a callback function, a time (in seconds) at which to trigger
* the callback, and an optional parameter for the callback.
*
* Time will be passed as the first parameter to the callback function,
* and param will be the second parameter.
*
*
* @method addCue
* @param {Number} time Time in seconds, relative to this media
* element's playback. For example, to trigger
* an event every time playback reaches two
* seconds, pass in the number 2. This will be
* passed as the first parameter to
* the callback function.
* @param {Function} callback Name of a function that will be
* called at the given time. The callback will
* receive time and (optionally) param as its
* two parameters.
* @param {Object} [value] An object to be passed as the
* second parameter to the
* callback function.
* @return {Number} id ID of this cue,
* useful for removeCue(id)
* @example
*
* function setup() {
* background(0);
* noStroke();
* fill(255);
* textAlign(CENTER);
* text('click to play', width/2, height/2);
*
* mySound = loadSound('assets/beat.mp3');
*
* // schedule calls to changeText
* mySound.addCue(0.50, changeText, "hello" );
* mySound.addCue(1.00, changeText, "p5" );
* mySound.addCue(1.50, changeText, "what" );
* mySound.addCue(2.00, changeText, "do" );
* mySound.addCue(2.50, changeText, "you" );
* mySound.addCue(3.00, changeText, "want" );
* mySound.addCue(4.00, changeText, "to" );
* mySound.addCue(5.00, changeText, "make" );
* mySound.addCue(6.00, changeText, "?" );
* }
*
* function changeText(val) {
* background(0);
* text(val, width/2, height/2);
* }
*
* function mouseClicked() {
* if (mouseX > 0 && mouseX < width && mouseY > 0 && mouseY < height) {
* if (mySound.isPlaying() ) {
* mySound.stop();
* } else {
* mySound.play();
* }
* }
* }
*
*/
p5.SoundFile.prototype.addCue = function (time, callback, val) {
var id = this._cueIDCounter++;
var cue = new Cue(callback, time, id, val);
this._cues.push(cue);
// if (!this.elt.ontimeupdate) {
// this.elt.ontimeupdate = this._onTimeUpdate.bind(this);
// }
return id;
};
/**
* Remove a callback based on its ID. The ID is returned by the
* addCue method.
*
* @method removeCue
* @param {Number} id ID of the cue, as returned by addCue
*/
p5.SoundFile.prototype.removeCue = function (id) {
for (var i = 0; i < this._cues.length; i++) {
var cue = this._cues[i];
if (cue.id === id) {
this.cues.splice(i, 1);
}
}
if (this._cues.length === 0) {
}
};
/**
* Remove all of the callbacks that had originally been scheduled
* via the addCue method.
*
* @method clearCues
*/
p5.SoundFile.prototype.clearCues = function () {
this._cues = [];
};
// private method that checks for cues to be fired if events
// have been scheduled using addCue(callback, time).
p5.SoundFile.prototype._onTimeUpdate = function (position) {
var playbackTime = position / this.buffer.sampleRate;
for (var i = 0; i < this._cues.length; i++) {
var callbackTime = this._cues[i].time;
var val = this._cues[i].val;
if (this._prevTime < callbackTime && callbackTime <= playbackTime) {
// pass the scheduled callbackTime as parameter to the callback
this._cues[i].callback(val);
}
}
this._prevTime = playbackTime;
};
// Cue inspired by JavaScript setTimeout, and the
// Tone.js Transport Timeline Event, MIT License Yotam Mann 2015 tonejs.org
var Cue = function (callback, time, id, val) {
this.callback = callback;
this.time = time;
this.id = id;
this.val = val;
};
}(sndcore, master);
var amplitude;
amplitude = function () {
'use strict';
var p5sound = master;
/**
* Amplitude measures volume between 0.0 and 1.0.
* Listens to all p5sound by default, or use setInput()
* to listen to a specific sound source. Accepts an optional
* smoothing value, which defaults to 0.
*
* @class p5.Amplitude
* @constructor
* @param {Number} [smoothing] between 0.0 and .999 to smooth
* amplitude readings (defaults to 0)
* @return {Object} Amplitude Object
* @example
*
* var sound, amplitude, cnv;
*
* function preload(){
* sound = loadSound('assets/beat.mp3');
* }
* function setup() {
* cnv = createCanvas(100,100);
* amplitude = new p5.Amplitude();
*
* // start / stop the sound when canvas is clicked
* cnv.mouseClicked(function() {
* if (sound.isPlaying() ){
* sound.stop();
* } else {
* sound.play();
* }
* });
* }
* function draw() {
* background(0);
* fill(255);
* var level = amplitude.getLevel();
* var size = map(level, 0, 1, 0, 200);
* ellipse(width/2, height/2, size, size);
* }
*
*
*/
p5.Amplitude = function (smoothing) {
// Set to 2048 for now. In future iterations, this should be inherited or parsed from p5sound's default
this.bufferSize = 2048;
// set audio context
this.audiocontext = p5sound.audiocontext;
this.processor = this.audiocontext.createScriptProcessor(this.bufferSize, 2, 1);
// for connections
this.input = this.processor;
this.output = this.audiocontext.createGain();
// smoothing defaults to 0
this.smoothing = smoothing || 0;
// the variables to return
this.volume = 0;
this.average = 0;
this.stereoVol = [
0,
0
];
this.stereoAvg = [
0,
0
];
this.stereoVolNorm = [
0,
0
];
this.volMax = 0.001;
this.normalize = false;
this.processor.onaudioprocess = this._audioProcess.bind(this);
this.processor.connect(this.output);
this.output.gain.value = 0;
// this may only be necessary because of a Chrome bug
this.output.connect(this.audiocontext.destination);
// connect to p5sound master output by default, unless set by input()
p5sound.meter.connect(this.processor);
};
/**
* Connects to the p5sound instance (master output) by default.
* Optionally, you can pass in a specific source (i.e. a soundfile).
*
* @method setInput
* @param {soundObject|undefined} [snd] set the sound source
* (optional, defaults to
* master output)
* @param {Number|undefined} [smoothing] a range between 0.0 and 1.0
* to smooth amplitude readings
* @example
*
* function preload(){
* sound1 = loadSound('assets/beat.mp3');
* sound2 = loadSound('assets/drum.mp3');
* }
* function setup(){
* amplitude = new p5.Amplitude();
* sound1.play();
* sound2.play();
* amplitude.setInput(sound2);
* }
* function draw() {
* background(0);
* fill(255);
* var level = amplitude.getLevel();
* var size = map(level, 0, 1, 0, 200);
* ellipse(width/2, height/2, size, size);
* }
* function mouseClicked(){
* sound1.stop();
* sound2.stop();
* }
*
*/
p5.Amplitude.prototype.setInput = function (source, smoothing) {
p5sound.meter.disconnect();
if (smoothing) {
this.smoothing = smoothing;
}
// connect to the master out of p5s instance if no snd is provided
if (source == null) {
console.log('Amplitude input source is not ready! Connecting to master output instead');
p5sound.meter.connect(this.processor);
} else if (source instanceof p5.Signal) {
source.output.connect(this.processor);
} else if (source) {
source.connect(this.processor);
this.processor.disconnect();
this.processor.connect(this.output);
} else {
p5sound.meter.connect(this.processor);
}
};
p5.Amplitude.prototype.connect = function (unit) {
if (unit) {
if (unit.hasOwnProperty('input')) {
this.output.connect(unit.input);
} else {
this.output.connect(unit);
}
} else {
this.output.connect(this.panner.connect(p5sound.input));
}
};
p5.Amplitude.prototype.disconnect = function (unit) {
this.output.disconnect();
};
// TO DO make this stereo / dependent on # of audio channels
p5.Amplitude.prototype._audioProcess = function (event) {
for (var channel = 0; channel < event.inputBuffer.numberOfChannels; channel++) {
var inputBuffer = event.inputBuffer.getChannelData(channel);
var bufLength = inputBuffer.length;
var total = 0;
var sum = 0;
var x;
for (var i = 0; i < bufLength; i++) {
x = inputBuffer[i];
if (this.normalize) {
total += Math.max(Math.min(x / this.volMax, 1), -1);
sum += Math.max(Math.min(x / this.volMax, 1), -1) * Math.max(Math.min(x / this.volMax, 1), -1);
} else {
total += x;
sum += x * x;
}
}
var average = total / bufLength;
// ... then take the square root of the sum.
var rms = Math.sqrt(sum / bufLength);
this.stereoVol[channel] = Math.max(rms, this.stereoVol[channel] * this.smoothing);
this.stereoAvg[channel] = Math.max(average, this.stereoVol[channel] * this.smoothing);
this.volMax = Math.max(this.stereoVol[channel], this.volMax);
}
// add volume from all channels together
var self = this;
var volSum = this.stereoVol.reduce(function (previousValue, currentValue, index) {
self.stereoVolNorm[index - 1] = Math.max(Math.min(self.stereoVol[index - 1] / self.volMax, 1), 0);
self.stereoVolNorm[index] = Math.max(Math.min(self.stereoVol[index] / self.volMax, 1), 0);
return previousValue + currentValue;
});
// volume is average of channels
this.volume = volSum / this.stereoVol.length;
// normalized value
this.volNorm = Math.max(Math.min(this.volume / this.volMax, 1), 0);
};
/**
* Returns a single Amplitude reading at the moment it is called.
* For continuous readings, run in the draw loop.
*
* @method getLevel
* @param {Number} [channel] Optionally return only channel 0 (left) or 1 (right)
* @return {Number} Amplitude as a number between 0.0 and 1.0
* @example
*
* function preload(){
* sound = loadSound('assets/beat.mp3');
* }
* function setup() {
* amplitude = new p5.Amplitude();
* sound.play();
* }
* function draw() {
* background(0);
* fill(255);
* var level = amplitude.getLevel();
* var size = map(level, 0, 1, 0, 200);
* ellipse(width/2, height/2, size, size);
* }
* function mouseClicked(){
* sound.stop();
* }
*
*/
p5.Amplitude.prototype.getLevel = function (channel) {
if (typeof channel !== 'undefined') {
if (this.normalize) {
return this.stereoVolNorm[channel];
} else {
return this.stereoVol[channel];
}
} else if (this.normalize) {
return this.volNorm;
} else {
return this.volume;
}
};
/**
* Determines whether the results of Amplitude.process() will be
* Normalized. To normalize, Amplitude finds the difference the
* loudest reading it has processed and the maximum amplitude of
* 1.0. Amplitude adds this difference to all values to produce
* results that will reliably map between 0.0 and 1.0. However,
* if a louder moment occurs, the amount that Normalize adds to
* all the values will change. Accepts an optional boolean parameter
* (true or false). Normalizing is off by default.
*
* @method toggleNormalize
* @param {boolean} [boolean] set normalize to true (1) or false (0)
*/
p5.Amplitude.prototype.toggleNormalize = function (bool) {
if (typeof bool === 'boolean') {
this.normalize = bool;
} else {
this.normalize = !this.normalize;
}
};
/**
* Smooth Amplitude analysis by averaging with the last analysis
* frame. Off by default.
*
* @method smooth
* @param {Number} set smoothing from 0.0 <= 1
*/
p5.Amplitude.prototype.smooth = function (s) {
if (s >= 0 && s < 1) {
this.smoothing = s;
} else {
console.log('Error: smoothing must be between 0 and 1');
}
};
}(master);
var fft;
fft = function () {
'use strict';
var p5sound = master;
/**
* FFT (Fast Fourier Transform) is an analysis algorithm that
* isolates individual
*
* audio frequencies within a waveform.
*
* Once instantiated, a p5.FFT object can return an array based on
* two types of analyses:
• FFT.waveform() computes
* amplitude values along the time domain. The array indices correspond
* to samples across a brief moment in time. Each value represents
* amplitude of the waveform at that sample of time.
* • FFT.analyze() computes amplitude values along the
* frequency domain. The array indices correspond to frequencies (i.e.
* pitches), from the lowest to the highest that humans can hear. Each
* value represents amplitude at that slice of the frequency spectrum.
* Use with getEnergy() to measure amplitude at specific
* frequencies, or within a range of frequencies.
*
* FFT analyzes a very short snapshot of sound called a sample
* buffer. It returns an array of amplitude measurements, referred
* to as bins. The array is 1024 bins long by default.
* You can change the bin array length, but it must be a power of 2
* between 16 and 1024 in order for the FFT algorithm to function
* correctly. The actual size of the FFT buffer is twice the
* number of bins, so given a standard sample rate, the buffer is
* 2048/44100 seconds long.
*
*
* @class p5.FFT
* @constructor
* @param {Number} [smoothing] Smooth results of Freq Spectrum.
* 0.0 < smoothing < 1.0.
* Defaults to 0.8.
* @param {Number} [bins] Length of resulting array.
* Must be a power of two between
* 16 and 1024. Defaults to 1024.
* @return {Object} FFT Object
* @example
*
* function preload(){
* sound = loadSound('assets/Damscray_DancingTiger.mp3');
* }
*
* function setup(){
* cnv = createCanvas(100,100);
* sound.amp(0);
* sound.loop();
* fft = new p5.FFT();
* }
*
* function draw(){
* background(0);
*
* var spectrum = fft.analyze();
* noStroke();
* fill(0,255,0); // spectrum is green
* for (var i = 0; i< spectrum.length; i++){
* var x = map(i, 0, spectrum.length, 0, width);
* var h = -height + map(spectrum[i], 0, 255, height, 0);
* rect(x, height, width / spectrum.length, h )
* }
*
* var waveform = fft.waveform();
* noFill();
* beginShape();
* stroke(255,0,0); // waveform is red
* strokeWeight(1);
* for (var i = 0; i< waveform.length; i++){
* var x = map(i, 0, waveform.length, 0, width);
* var y = map( waveform[i], -1, 1, 0, height);
* vertex(x,y);
* }
* endShape();
*
* isMouseOverCanvas();
* }
*
* // fade sound if mouse is over canvas
* function isMouseOverCanvas() {
* var mX = mouseX, mY = mouseY;
* if (mX > 0 && mX < width && mY < height && mY > 0) {
* sound.amp(0.5, 0.2);
* } else {
* sound.amp(0, 0.2);
* }
* }
*
*/
p5.FFT = function (smoothing, bins) {
this.smoothing = smoothing || 0.8;
this.bins = bins || 1024;
var FFT_SIZE = bins * 2 || 2048;
this.input = this.analyser = p5sound.audiocontext.createAnalyser();
// default connections to p5sound fftMeter
p5sound.fftMeter.connect(this.analyser);
this.analyser.smoothingTimeConstant = this.smoothing;
this.analyser.fftSize = FFT_SIZE;
this.freqDomain = new Uint8Array(this.analyser.frequencyBinCount);
this.timeDomain = new Uint8Array(this.analyser.frequencyBinCount);
// predefined frequency ranages, these will be tweakable
this.bass = [
20,
140
];
this.lowMid = [
140,
400
];
this.mid = [
400,
2600
];
this.highMid = [
2600,
5200
];
this.treble = [
5200,
14000
];
};
/**
* Set the input source for the FFT analysis. If no source is
* provided, FFT will analyze all sound in the sketch.
*
* @method setInput
* @param {Object} [source] p5.sound object (or web audio API source node)
*/
p5.FFT.prototype.setInput = function (source) {
if (!source) {
p5sound.fftMeter.connect(this.analyser);
} else {
if (source.output) {
source.output.connect(this.analyser);
} else if (source.connect) {
source.connect(this.analyser);
}
p5sound.fftMeter.disconnect();
}
};
/**
* Returns an array of amplitude values (between -1.0 and +1.0) that represent
* a snapshot of amplitude readings in a single buffer. Length will be
* equal to bins (defaults to 1024). Can be used to draw the waveform
* of a sound.
*
* @method waveform
* @param {Number} [bins] Must be a power of two between
* 16 and 1024. Defaults to 1024.
* @param {String} [precision] If any value is provided, will return results
* in a Float32 Array which is more precise
* than a regular array.
* @return {Array} Array Array of amplitude values (-1 to 1)
* over time. Array length = bins.
*
*/
p5.FFT.prototype.waveform = function () {
var bins, mode, normalArray;
for (var i = 0; i < arguments.length; i++) {
if (typeof arguments[i] === 'number') {
bins = arguments[i];
this.analyser.fftSize = bins * 2;
}
if (typeof arguments[i] === 'string') {
mode = arguments[i];
}
}
// getFloatFrequencyData doesnt work in Safari as of 5/2015
if (mode && !p5.prototype._isSafari()) {
timeToFloat(this, this.timeDomain);
this.analyser.getFloatTimeDomainData(this.timeDomain);
return this.timeDomain;
} else {
timeToInt(this, this.timeDomain);
this.analyser.getByteTimeDomainData(this.timeDomain);
var normalArray = new Array();
for (var i = 0; i < this.timeDomain.length; i++) {
var scaled = p5.prototype.map(this.timeDomain[i], 0, 255, -1, 1);
normalArray.push(scaled);
}
return normalArray;
}
};
/**
* Returns an array of amplitude values (between 0 and 255)
* across the frequency spectrum. Length is equal to FFT bins
* (1024 by default). The array indices correspond to frequencies
* (i.e. pitches), from the lowest to the highest that humans can
* hear. Each value represents amplitude at that slice of the
* frequency spectrum. Must be called prior to using
* getEnergy().
*
* @method analyze
* @param {Number} [bins] Must be a power of two between
* 16 and 1024. Defaults to 1024.
* @param {Number} [scale] If "dB," returns decibel
* float measurements between
* -140 and 0 (max).
* Otherwise returns integers from 0-255.
* @return {Array} spectrum Array of energy (amplitude/volume)
* values across the frequency spectrum.
* Lowest energy (silence) = 0, highest
* possible is 255.
* @example
*
* var osc;
* var fft;
*
* function setup(){
* createCanvas(100,100);
* osc = new p5.Oscillator();
* osc.amp(0);
* osc.start();
* fft = new p5.FFT();
* }
*
* function draw(){
* background(0);
*
* var freq = map(mouseX, 0, 800, 20, 15000);
* freq = constrain(freq, 1, 20000);
* osc.freq(freq);
*
* var spectrum = fft.analyze();
* noStroke();
* fill(0,255,0); // spectrum is green
* for (var i = 0; i< spectrum.length; i++){
* var x = map(i, 0, spectrum.length, 0, width);
* var h = -height + map(spectrum[i], 0, 255, height, 0);
* rect(x, height, width / spectrum.length, h );
* }
*
* stroke(255);
* text('Freq: ' + round(freq)+'Hz', 10, 10);
*
* isMouseOverCanvas();
* }
*
* // only play sound when mouse is over canvas
* function isMouseOverCanvas() {
* var mX = mouseX, mY = mouseY;
* if (mX > 0 && mX < width && mY < height && mY > 0) {
* osc.amp(0.5, 0.2);
* } else {
* osc.amp(0, 0.2);
* }
* }
*
*
*
*/
p5.FFT.prototype.analyze = function () {
var bins, mode;
for (var i = 0; i < arguments.length; i++) {
if (typeof arguments[i] === 'number') {
bins = this.bins = arguments[i];
this.analyser.fftSize = this.bins * 2;
}
if (typeof arguments[i] === 'string') {
mode = arguments[i];
}
}
if (mode && mode.toLowerCase() === 'db') {
freqToFloat(this);
this.analyser.getFloatFrequencyData(this.freqDomain);
return this.freqDomain;
} else {
freqToInt(this, this.freqDomain);
this.analyser.getByteFrequencyData(this.freqDomain);
var normalArray = Array.apply([], this.freqDomain);
normalArray.length === this.analyser.fftSize;
normalArray.constructor === Array;
return normalArray;
}
};
/**
* Returns the amount of energy (volume) at a specific
*
* frequency, or the average amount of energy between two
* frequencies. Accepts Number(s) corresponding
* to frequency (in Hz), or a String corresponding to predefined
* frequency ranges ("bass", "lowMid", "mid", "highMid", "treble").
* Returns a range between 0 (no energy/volume at that frequency) and
* 255 (maximum energy).
* NOTE: analyze() must be called prior to getEnergy(). Analyze()
* tells the FFT to analyze frequency data, and getEnergy() uses
* the results determine the value at a specific frequency or
* range of frequencies.
*
* @method getEnergy
* @param {Number|String} frequency1 Will return a value representing
* energy at this frequency. Alternately,
* the strings "bass", "lowMid" "mid",
* "highMid", and "treble" will return
* predefined frequency ranges.
* @param {Number} [frequency2] If a second frequency is given,
* will return average amount of
* energy that exists between the
* two frequencies.
* @return {Number} Energy Energy (volume/amplitude) from
* 0 and 255.
*
*/
p5.FFT.prototype.getEnergy = function (frequency1, frequency2) {
var nyquist = p5sound.audiocontext.sampleRate / 2;
if (frequency1 === 'bass') {
frequency1 = this.bass[0];
frequency2 = this.bass[1];
} else if (frequency1 === 'lowMid') {
frequency1 = this.lowMid[0];
frequency2 = this.lowMid[1];
} else if (frequency1 === 'mid') {
frequency1 = this.mid[0];
frequency2 = this.mid[1];
} else if (frequency1 === 'highMid') {
frequency1 = this.highMid[0];
frequency2 = this.highMid[1];
} else if (frequency1 === 'treble') {
frequency1 = this.treble[0];
frequency2 = this.treble[1];
}
if (typeof frequency1 !== 'number') {
throw 'invalid input for getEnergy()';
} else if (!frequency2) {
var index = Math.round(frequency1 / nyquist * this.freqDomain.length);
return this.freqDomain[index];
} else if (frequency1 && frequency2) {
// if second is higher than first
if (frequency1 > frequency2) {
var swap = frequency2;
frequency2 = frequency1;
frequency1 = swap;
}
var lowIndex = Math.round(frequency1 / nyquist * this.freqDomain.length);
var highIndex = Math.round(frequency2 / nyquist * this.freqDomain.length);
var total = 0;
var numFrequencies = 0;
// add up all of the values for the frequencies
for (var i = lowIndex; i <= highIndex; i++) {
total += this.freqDomain[i];
numFrequencies += 1;
}
// divide by total number of frequencies
var toReturn = total / numFrequencies;
return toReturn;
} else {
throw 'invalid input for getEnergy()';
}
};
// compatability with v.012, changed to getEnergy in v.0121. Will be deprecated...
p5.FFT.prototype.getFreq = function (freq1, freq2) {
console.log('getFreq() is deprecated. Please use getEnergy() instead.');
var x = this.getEnergy(freq1, freq2);
return x;
};
/**
* Smooth FFT analysis by averaging with the last analysis frame.
*
* @method smooth
* @param {Number} smoothing 0.0 < smoothing < 1.0.
* Defaults to 0.8.
*/
p5.FFT.prototype.smooth = function (s) {
if (s) {
this.smoothing = s;
}
this.analyser.smoothingTimeConstant = s;
};
// helper methods to convert type from float (dB) to int (0-255)
var freqToFloat = function (fft) {
if (fft.freqDomain instanceof Float32Array === false) {
fft.freqDomain = new Float32Array(fft.analyser.frequencyBinCount);
}
};
var freqToInt = function (fft) {
if (fft.freqDomain instanceof Uint8Array === false) {
fft.freqDomain = new Uint8Array(fft.analyser.frequencyBinCount);
}
};
var timeToFloat = function (fft) {
if (fft.timeDomain instanceof Float32Array === false) {
fft.timeDomain = new Float32Array(fft.analyser.frequencyBinCount);
}
};
var timeToInt = function (fft) {
if (fft.timeDomain instanceof Uint8Array === false) {
fft.timeDomain = new Uint8Array(fft.analyser.frequencyBinCount);
}
};
}(master);
/** Tone.js module by Yotam Mann, MIT License 2014 http://opensource.org/licenses/MIT **/
var Tone_core_Tone;
Tone_core_Tone = function () {
'use strict';
function isUndef(val) {
return val === void 0;
}
var audioContext;
if (isUndef(window.AudioContext)) {
window.AudioContext = window.webkitAudioContext;
}
if (isUndef(window.OfflineAudioContext)) {
window.OfflineAudioContext = window.webkitOfflineAudioContext;
}
if (!isUndef(AudioContext)) {
audioContext = new AudioContext();
} else {
throw new Error('Web Audio is not supported in this browser');
}
if (typeof AudioContext.prototype.createGain !== 'function') {
AudioContext.prototype.createGain = AudioContext.prototype.createGainNode;
}
if (typeof AudioContext.prototype.createDelay !== 'function') {
AudioContext.prototype.createDelay = AudioContext.prototype.createDelayNode;
}
if (typeof AudioContext.prototype.createPeriodicWave !== 'function') {
AudioContext.prototype.createPeriodicWave = AudioContext.prototype.createWaveTable;
}
if (typeof AudioBufferSourceNode.prototype.start !== 'function') {
AudioBufferSourceNode.prototype.start = AudioBufferSourceNode.prototype.noteGrainOn;
}
if (typeof AudioBufferSourceNode.prototype.stop !== 'function') {
AudioBufferSourceNode.prototype.stop = AudioBufferSourceNode.prototype.noteOff;
}
if (typeof OscillatorNode.prototype.start !== 'function') {
OscillatorNode.prototype.start = OscillatorNode.prototype.noteOn;
}
if (typeof OscillatorNode.prototype.stop !== 'function') {
OscillatorNode.prototype.stop = OscillatorNode.prototype.noteOff;
}
if (typeof OscillatorNode.prototype.setPeriodicWave !== 'function') {
OscillatorNode.prototype.setPeriodicWave = OscillatorNode.prototype.setWaveTable;
}
if (!window.Tone) {
AudioNode.prototype._nativeConnect = AudioNode.prototype.connect;
AudioNode.prototype.connect = function (B, outNum, inNum) {
if (B.input) {
if (Array.isArray(B.input)) {
if (isUndef(inNum)) {
inNum = 0;
}
this.connect(B.input[inNum]);
} else {
this.connect(B.input, outNum, inNum);
}
} else {
try {
if (B instanceof AudioNode) {
this._nativeConnect(B, outNum, inNum);
} else {
this._nativeConnect(B, outNum);
}
} catch (e) {
throw new Error('error connecting to node: ' + B);
}
}
};
}
var Tone = function (inputs, outputs) {
if (isUndef(inputs) || inputs === 1) {
this.input = this.context.createGain();
} else if (inputs > 1) {
this.input = new Array(inputs);
}
if (isUndef(outputs) || outputs === 1) {
this.output = this.context.createGain();
} else if (outputs > 1) {
this.output = new Array(inputs);
}
};
Tone.context = audioContext;
Tone.prototype.context = Tone.context;
Tone.prototype.bufferSize = 2048;
Tone.prototype.bufferTime = Tone.prototype.bufferSize / Tone.context.sampleRate;
Tone.prototype.connect = function (unit, outputNum, inputNum) {
if (Array.isArray(this.output)) {
outputNum = this.defaultArg(outputNum, 0);
this.output[outputNum].connect(unit, 0, inputNum);
} else {
this.output.connect(unit, outputNum, inputNum);
}
};
Tone.prototype.disconnect = function (outputNum) {
if (Array.isArray(this.output)) {
outputNum = this.defaultArg(outputNum, 0);
this.output[outputNum].disconnect();
} else {
this.output.disconnect();
}
};
Tone.prototype.connectSeries = function () {
if (arguments.length > 1) {
var currentUnit = arguments[0];
for (var i = 1; i < arguments.length; i++) {
var toUnit = arguments[i];
currentUnit.connect(toUnit);
currentUnit = toUnit;
}
}
};
Tone.prototype.connectParallel = function () {
var connectFrom = arguments[0];
if (arguments.length > 1) {
for (var i = 1; i < arguments.length; i++) {
var connectTo = arguments[i];
connectFrom.connect(connectTo);
}
}
};
Tone.prototype.chain = function () {
if (arguments.length > 0) {
var currentUnit = this;
for (var i = 0; i < arguments.length; i++) {
var toUnit = arguments[i];
currentUnit.connect(toUnit);
currentUnit = toUnit;
}
}
};
Tone.prototype.fan = function () {
if (arguments.length > 0) {
for (var i = 1; i < arguments.length; i++) {
this.connect(arguments[i]);
}
}
};
AudioNode.prototype.chain = Tone.prototype.chain;
AudioNode.prototype.fan = Tone.prototype.fan;
Tone.prototype.defaultArg = function (given, fallback) {
if (typeof given === 'object' && typeof fallback === 'object') {
var ret = {};
for (var givenProp in given) {
ret[givenProp] = this.defaultArg(given[givenProp], given[givenProp]);
}
for (var prop in fallback) {
ret[prop] = this.defaultArg(given[prop], fallback[prop]);
}
return ret;
} else {
return isUndef(given) ? fallback : given;
}
};
Tone.prototype.optionsObject = function (values, keys, defaults) {
var options = {};
if (values.length === 1 && typeof values[0] === 'object') {
options = values[0];
} else {
for (var i = 0; i < keys.length; i++) {
options[keys[i]] = values[i];
}
}
if (!this.isUndef(defaults)) {
return this.defaultArg(options, defaults);
} else {
return options;
}
};
Tone.prototype.isUndef = isUndef;
Tone.prototype.equalPowerScale = function (percent) {
var piFactor = 0.5 * Math.PI;
return Math.sin(percent * piFactor);
};
Tone.prototype.logScale = function (gain) {
return Math.max(this.normalize(this.gainToDb(gain), -100, 0), 0);
};
Tone.prototype.expScale = function (gain) {
return this.dbToGain(this.interpolate(gain, -100, 0));
};
Tone.prototype.dbToGain = function (db) {
return Math.pow(2, db / 6);
};
Tone.prototype.gainToDb = function (gain) {
return 20 * (Math.log(gain) / Math.LN10);
};
Tone.prototype.interpolate = function (input, outputMin, outputMax) {
return input * (outputMax - outputMin) + outputMin;
};
Tone.prototype.normalize = function (input, inputMin, inputMax) {
if (inputMin > inputMax) {
var tmp = inputMax;
inputMax = inputMin;
inputMin = tmp;
} else if (inputMin == inputMax) {
return 0;
}
return (input - inputMin) / (inputMax - inputMin);
};
Tone.prototype.dispose = function () {
if (!this.isUndef(this.input)) {
if (this.input instanceof AudioNode) {
this.input.disconnect();
}
this.input = null;
}
if (!this.isUndef(this.output)) {
if (this.output instanceof AudioNode) {
this.output.disconnect();
}
this.output = null;
}
};
var _silentNode = null;
Tone.prototype.noGC = function () {
this.output.connect(_silentNode);
};
AudioNode.prototype.noGC = function () {
this.connect(_silentNode);
};
Tone.prototype.now = function () {
return this.context.currentTime;
};
Tone.prototype.samplesToSeconds = function (samples) {
return samples / this.context.sampleRate;
};
Tone.prototype.toSamples = function (time) {
var seconds = this.toSeconds(time);
return Math.round(seconds * this.context.sampleRate);
};
Tone.prototype.toSeconds = function (time, now) {
now = this.defaultArg(now, this.now());
if (typeof time === 'number') {
return time;
} else if (typeof time === 'string') {
var plusTime = 0;
if (time.charAt(0) === '+') {
time = time.slice(1);
plusTime = now;
}
return parseFloat(time) + plusTime;
} else {
return now;
}
};
Tone.prototype.frequencyToSeconds = function (freq) {
return 1 / parseFloat(freq);
};
Tone.prototype.secondsToFrequency = function (seconds) {
return 1 / seconds;
};
var newContextCallbacks = [];
Tone._initAudioContext = function (callback) {
callback(Tone.context);
newContextCallbacks.push(callback);
};
Tone.setContext = function (ctx) {
Tone.prototype.context = ctx;
Tone.context = ctx;
for (var i = 0; i < newContextCallbacks.length; i++) {
newContextCallbacks[i](ctx);
}
};
Tone.extend = function (child, parent) {
if (isUndef(parent)) {
parent = Tone;
}
function TempConstructor() {
}
TempConstructor.prototype = parent.prototype;
child.prototype = new TempConstructor();
child.prototype.constructor = child;
};
Tone.startMobile = function () {
var osc = Tone.context.createOscillator();
var silent = Tone.context.createGain();
silent.gain.value = 0;
osc.connect(silent);
silent.connect(Tone.context.destination);
var now = Tone.context.currentTime;
osc.start(now);
osc.stop(now + 1);
};
Tone._initAudioContext(function (audioContext) {
Tone.prototype.bufferTime = Tone.prototype.bufferSize / audioContext.sampleRate;
_silentNode = audioContext.createGain();
_silentNode.gain.value = 0;
_silentNode.connect(audioContext.destination);
});
return Tone;
}();
/** Tone.js module by Yotam Mann, MIT License 2014 http://opensource.org/licenses/MIT **/
var Tone_signal_SignalBase;
Tone_signal_SignalBase = function (Tone) {
'use strict';
Tone.SignalBase = function () {
};
Tone.extend(Tone.SignalBase);
Tone.SignalBase.prototype.connect = function (node, outputNumber, inputNumber) {
if (node instanceof Tone.Signal) {
node.setValue(0);
} else if (node instanceof AudioParam) {
node.value = 0;
}
Tone.prototype.connect.call(this, node, outputNumber, inputNumber);
};
Tone.SignalBase.prototype.dispose = function () {
Tone.prototype.dispose.call(this);
};
return Tone.SignalBase;
}(Tone_core_Tone);
/** Tone.js module by Yotam Mann, MIT License 2014 http://opensource.org/licenses/MIT **/
var Tone_signal_WaveShaper;
Tone_signal_WaveShaper = function (Tone) {
'use strict';
Tone.WaveShaper = function (mapping, bufferLen) {
this._shaper = this.input = this.output = this.context.createWaveShaper();
this._curve = null;
if (Array.isArray(mapping)) {
this.setCurve(mapping);
} else if (isFinite(mapping) || this.isUndef(mapping)) {
this._curve = new Float32Array(this.defaultArg(mapping, 1024));
} else if (typeof mapping === 'function') {
this._curve = new Float32Array(this.defaultArg(bufferLen, 1024));
this.setMap(mapping);
}
};
Tone.extend(Tone.WaveShaper, Tone.SignalBase);
Tone.WaveShaper.prototype.setMap = function (mapping) {
for (var i = 0, len = this._curve.length; i < len; i++) {
var normalized = i / len * 2 - 1;
var normOffOne = i / (len - 1) * 2 - 1;
this._curve[i] = mapping(normalized, i, normOffOne);
}
this._shaper.curve = this._curve;
};
Tone.WaveShaper.prototype.setCurve = function (mapping) {
if (this._isSafari()) {
var first = mapping[0];
mapping.unshift(first);
}
this._curve = new Float32Array(mapping);
this._shaper.curve = this._curve;
};
Tone.WaveShaper.prototype.setOversample = function (oversampling) {
this._shaper.oversample = oversampling;
};
Tone.WaveShaper.prototype._isSafari = function () {
var ua = navigator.userAgent.toLowerCase();
return ua.indexOf('safari') !== -1 && ua.indexOf('chrome') === -1;
};
Tone.WaveShaper.prototype.dispose = function () {
Tone.prototype.dispose.call(this);
this._shaper.disconnect();
this._shaper = null;
this._curve = null;
};
return Tone.WaveShaper;
}(Tone_core_Tone);
/** Tone.js module by Yotam Mann, MIT License 2014 http://opensource.org/licenses/MIT **/
var Tone_signal_Signal;
Tone_signal_Signal = function (Tone) {
'use strict';
Tone.Signal = function (value) {
this._scalar = this.context.createGain();
this.input = this.output = this.context.createGain();
this._syncRatio = 1;
this.value = this.defaultArg(value, 0);
Tone.Signal._constant.chain(this._scalar, this.output);
};
Tone.extend(Tone.Signal, Tone.SignalBase);
Tone.Signal.prototype.getValue = function () {
return this._scalar.gain.value;
};
Tone.Signal.prototype.setValue = function (value) {
if (this._syncRatio === 0) {
value = 0;
} else {
value *= this._syncRatio;
}
this._scalar.gain.value = value;
};
Tone.Signal.prototype.setValueAtTime = function (value, time) {
value *= this._syncRatio;
this._scalar.gain.setValueAtTime(value, this.toSeconds(time));
};
Tone.Signal.prototype.setCurrentValueNow = function (now) {
now = this.defaultArg(now, this.now());
var currentVal = this.getValue();
this.cancelScheduledValues(now);
this._scalar.gain.setValueAtTime(currentVal, now);
return currentVal;
};
Tone.Signal.prototype.linearRampToValueAtTime = function (value, endTime) {
value *= this._syncRatio;
this._scalar.gain.linearRampToValueAtTime(value, this.toSeconds(endTime));
};
Tone.Signal.prototype.exponentialRampToValueAtTime = function (value, endTime) {
value *= this._syncRatio;
try {
this._scalar.gain.exponentialRampToValueAtTime(value, this.toSeconds(endTime));
} catch (e) {
this._scalar.gain.linearRampToValueAtTime(value, this.toSeconds(endTime));
}
};
Tone.Signal.prototype.exponentialRampToValueNow = function (value, endTime) {
var now = this.now();
this.setCurrentValueNow(now);
if (endTime.toString().charAt(0) === '+') {
endTime = endTime.substr(1);
}
this.exponentialRampToValueAtTime(value, now + this.toSeconds(endTime));
};
Tone.Signal.prototype.linearRampToValueNow = function (value, endTime) {
var now = this.now();
this.setCurrentValueNow(now);
value *= this._syncRatio;
if (endTime.toString().charAt(0) === '+') {
endTime = endTime.substr(1);
}
this._scalar.gain.linearRampToValueAtTime(value, now + this.toSeconds(endTime));
};
Tone.Signal.prototype.setTargetAtTime = function (value, startTime, timeConstant) {
value *= this._syncRatio;
this._scalar.gain.setTargetAtTime(value, this.toSeconds(startTime), timeConstant);
};
Tone.Signal.prototype.setValueCurveAtTime = function (values, startTime, duration) {
for (var i = 0; i < values.length; i++) {
values[i] *= this._syncRatio;
}
this._scalar.gain.setValueCurveAtTime(values, this.toSeconds(startTime), this.toSeconds(duration));
};
Tone.Signal.prototype.cancelScheduledValues = function (startTime) {
this._scalar.gain.cancelScheduledValues(this.toSeconds(startTime));
};
Tone.Signal.prototype.sync = function (signal, ratio) {
if (ratio) {
this._syncRatio = ratio;
} else {
if (signal.getValue() !== 0) {
this._syncRatio = this.getValue() / signal.getValue();
} else {
this._syncRatio = 0;
}
}
this._scalar.disconnect();
this._scalar = this.context.createGain();
this.connectSeries(signal, this._scalar, this.output);
this._scalar.gain.value = this._syncRatio;
};
Tone.Signal.prototype.unsync = function () {
var currentGain = this.getValue();
this._scalar.disconnect();
this._scalar = this.context.createGain();
this._scalar.gain.value = currentGain / this._syncRatio;
this._syncRatio = 1;
Tone.Signal._constant.chain(this._scalar, this.output);
};
Tone.Signal.prototype.dispose = function () {
Tone.prototype.dispose.call(this);
this._scalar.disconnect();
this._scalar = null;
};
Object.defineProperty(Tone.Signal.prototype, 'value', {
get: function () {
return this.getValue();
},
set: function (val) {
this.setValue(val);
}
});
Tone.Signal._generator = null;
Tone.Signal._constant = null;
Tone._initAudioContext(function (audioContext) {
Tone.Signal._generator = audioContext.createOscillator();
Tone.Signal._constant = new Tone.WaveShaper([
1,
1
]);
Tone.Signal._generator.connect(Tone.Signal._constant);
Tone.Signal._generator.start(0);
Tone.Signal._generator.noGC();
});
return Tone.Signal;
}(Tone_core_Tone);
/** Tone.js module by Yotam Mann, MIT License 2014 http://opensource.org/licenses/MIT **/
var Tone_signal_Add;
Tone_signal_Add = function (Tone) {
'use strict';
Tone.Add = function (value) {
Tone.call(this, 2, 0);
this._sum = this.input[0] = this.input[1] = this.output = this.context.createGain();
this._value = null;
if (isFinite(value)) {
this._value = new Tone.Signal(value);
this._value.connect(this._sum);
}
};
Tone.extend(Tone.Add, Tone.SignalBase);
Tone.Add.prototype.setValue = function (value) {
if (this._value !== null) {
this._value.setValue(value);
} else {
throw new Error('cannot switch from signal to number');
}
};
Tone.Add.prototype.dispose = function () {
Tone.prototype.dispose.call(this);
this._sum = null;
if (this._value) {
this._value.dispose();
this._value = null;
}
};
return Tone.Add;
}(Tone_core_Tone);
/** Tone.js module by Yotam Mann, MIT License 2014 http://opensource.org/licenses/MIT **/
var Tone_signal_Multiply;
Tone_signal_Multiply = function (Tone) {
'use strict';
Tone.Multiply = function (value) {
Tone.call(this, 2, 0);
this._mult = this.input[0] = this.output = this.context.createGain();
this._factor = this.input[1] = this.output.gain;
this._factor.value = this.defaultArg(value, 0);
};
Tone.extend(Tone.Multiply, Tone.SignalBase);
Tone.Multiply.prototype.setValue = function (value) {
this._factor.value = value;
};
Tone.Multiply.prototype.dispose = function () {
Tone.prototype.dispose.call(this);
this._mult = null;
this._factor = null;
};
return Tone.Multiply;
}(Tone_core_Tone);
/** Tone.js module by Yotam Mann, MIT License 2014 http://opensource.org/licenses/MIT **/
var Tone_signal_Scale;
Tone_signal_Scale = function (Tone) {
'use strict';
Tone.Scale = function (outputMin, outputMax) {
this._outputMin = this.defaultArg(outputMin, 0);
this._outputMax = this.defaultArg(outputMax, 1);
this._scale = this.input = new Tone.Multiply(1);
this._add = this.output = new Tone.Add(0);
this._scale.connect(this._add);
this._setRange();
};
Tone.extend(Tone.Scale, Tone.SignalBase);
Tone.Scale.prototype.setMin = function (min) {
this._outputMin = min;
this._setRange();
};
Tone.Scale.prototype.setMax = function (max) {
this._outputMax = max;
this._setRange();
};
Tone.Scale.prototype._setRange = function () {
this._add.setValue(this._outputMin);
this._scale.setValue(this._outputMax - this._outputMin);
};
Tone.Scale.prototype.dispose = function () {
Tone.prototype.dispose.call(this);
this._add.dispose();
this._add = null;
this._scale.dispose();
this._scale = null;
};
return Tone.Scale;
}(Tone_core_Tone, Tone_signal_Add, Tone_signal_Multiply);
var signal;
signal = function () {
'use strict';
// Signal is built with the Tone.js signal by Yotam Mann
// https://github.com/TONEnoTONE/Tone.js/
var Signal = Tone_signal_Signal;
var Add = Tone_signal_Add;
var Mult = Tone_signal_Multiply;
var Scale = Tone_signal_Scale;
var Tone = Tone_core_Tone;
var p5sound = master;
Tone.setContext(p5sound.audiocontext);
/**
* p5.Signal is a constant audio-rate signal used by p5.Oscillator
* and p5.Envelope for modulation math.
*
* This is necessary because Web Audio is processed on a seprate clock.
* For example, the p5 draw loop runs about 60 times per second. But
* the audio clock must process samples 44100 times per second. If we
* want to add a value to each of those samples, we can't do it in the
* draw loop, but we can do it by adding a constant-rate audio signal.This class mostly functions behind the scenes in p5.sound, and returns
* a Tone.Signal from the Tone.js library by Yotam Mann.
* If you want to work directly with audio signals for modular
* synthesis, check out
* tone.js.
*
* @class p5.Signal
* @constructor
* @return {Tone.Signal} A Signal object from the Tone.js library
* @example
*
* function setup() {
* carrier = new p5.Oscillator('sine');
* carrier.amp(1); // set amplitude
* carrier.freq(220); // set frequency
* carrier.start(); // start oscillating
*
* modulator = new p5.Oscillator('sawtooth');
* modulator.disconnect();
* modulator.amp(1);
* modulator.freq(4);
* modulator.start();
*
* // Modulator's default amplitude range is -1 to 1.
* // Multiply it by -200, so the range is -200 to 200
* // then add 220 so the range is 20 to 420
* carrier.freq( modulator.mult(-200).add(220) );
* }
*
*/
p5.Signal = function (value) {
var s = new Signal(value);
// p5sound.soundArray.push(s);
return s;
};
/**
* Fade to value, for smooth transitions
*
* @method fade
* @param {Number} value Value to set this signal
* @param {[Number]} secondsFromNow Length of fade, in seconds from now
*/
Signal.prototype.fade = Signal.prototype.linearRampToValueAtTime;
Mult.prototype.fade = Signal.prototype.fade;
Add.prototype.fade = Signal.prototype.fade;
Scale.prototype.fade = Signal.prototype.fade;
/**
* Connect a p5.sound object or Web Audio node to this
* p5.Signal so that its amplitude values can be scaled.
*
* @param {Object} input
*/
Signal.prototype.setInput = function (_input) {
_input.connect(this);
};
Mult.prototype.setInput = Signal.prototype.setInput;
Add.prototype.setInput = Signal.prototype.setInput;
Scale.prototype.setInput = Signal.prototype.setInput;
// signals can add / mult / scale themselves
/**
* Add a constant value to this audio signal,
* and return the resulting audio signal. Does
* not change the value of the original signal,
* instead it returns a new p5.SignalAdd.
*
* @method add
* @param {Number} number
* @return {p5.SignalAdd} object
*/
Signal.prototype.add = function (num) {
var add = new Add(num);
// add.setInput(this);
this.connect(add);
return add;
};
Mult.prototype.add = Signal.prototype.add;
Add.prototype.add = Signal.prototype.add;
Scale.prototype.add = Signal.prototype.add;
/**
* Multiply this signal by a constant value,
* and return the resulting audio signal. Does
* not change the value of the original signal,
* instead it returns a new p5.SignalMult.
*
* @method mult
* @param {Number} number to multiply
* @return {Tone.Multiply} object
*/
Signal.prototype.mult = function (num) {
var mult = new Mult(num);
// mult.setInput(this);
this.connect(mult);
return mult;
};
Mult.prototype.mult = Signal.prototype.mult;
Add.prototype.mult = Signal.prototype.mult;
Scale.prototype.mult = Signal.prototype.mult;
/**
* Scale this signal value to a given range,
* and return the result as an audio signal. Does
* not change the value of the original signal,
* instead it returns a new p5.SignalScale.
*
* @method scale
* @param {Number} number to multiply
* @param {Number} inMin input range minumum
* @param {Number} inMax input range maximum
* @param {Number} outMin input range minumum
* @param {Number} outMax input range maximum
* @return {p5.SignalScale} object
*/
Signal.prototype.scale = function (inMin, inMax, outMin, outMax) {
var mapOutMin, mapOutMax;
if (arguments.length === 4) {
mapOutMin = p5.prototype.map(outMin, inMin, inMax, 0, 1) - 0.5;
mapOutMax = p5.prototype.map(outMax, inMin, inMax, 0, 1) - 0.5;
} else {
mapOutMin = arguments[0];
mapOutMax = arguments[1];
}
var scale = new Scale(mapOutMin, mapOutMax);
this.connect(scale);
return scale;
};
Mult.prototype.scale = Signal.prototype.scale;
Add.prototype.scale = Signal.prototype.scale;
Scale.prototype.scale = Signal.prototype.scale;
}(Tone_signal_Signal, Tone_signal_Add, Tone_signal_Multiply, Tone_signal_Scale, Tone_core_Tone, master);
var oscillator;
oscillator = function () {
'use strict';
var p5sound = master;
var Signal = Tone_signal_Signal;
var Add = Tone_signal_Add;
var Mult = Tone_signal_Multiply;
var Scale = Tone_signal_Scale;
/**
* Creates a signal that oscillates between -1.0 and 1.0.
* By default, the oscillation takes the form of a sinusoidal
* shape ('sine'). Additional types include 'triangle',
* 'sawtooth' and 'square'. The frequency defaults to
* 440 oscillations per second (440Hz, equal to the pitch of an
* 'A' note).
*
* Set the type of oscillation with setType(), or by creating a
* specific oscillator.
For example:
* new p5.SinOsc(freq)
* new p5.TriOsc(freq)
* new p5.SqrOsc(freq)
* new p5.SawOsc(freq).
*
*
* @class p5.Oscillator
* @constructor
* @param {Number} [freq] frequency defaults to 440Hz
* @param {String} [type] type of oscillator. Options:
* 'sine' (default), 'triangle',
* 'sawtooth', 'square'
* @return {Object} Oscillator object
* @example
*
* var osc;
* var playing = false;
*
* function setup() {
* backgroundColor = color(255,0,255);
* textAlign(CENTER);
*
* osc = new p5.Oscillator();
* osc.setType('sine');
* osc.freq(240);
* osc.amp(0);
* osc.start();
* }
*
* function draw() {
* background(backgroundColor)
* text('click to play', width/2, height/2);
* }
*
* function mouseClicked() {
* if (mouseX > 0 && mouseX < width && mouseY < height && mouseY > 0) {
* if (!playing) {
* // ramp amplitude to 0.5 over 0.1 seconds
* osc.amp(0.5, 0.05);
* playing = true;
* backgroundColor = color(0,255,255);
* } else {
* // ramp amplitude to 0 over 0.5 seconds
* osc.amp(0, 0.5);
* playing = false;
* backgroundColor = color(255,0,255);
* }
* }
* }
*
*/
p5.Oscillator = function (freq, type) {
if (typeof freq === 'string') {
var f = type;
type = freq;
freq = f;
}
if (typeof type === 'number') {
var f = type;
type = freq;
freq = f;
}
this.started = false;
// components
this.oscillator = p5sound.audiocontext.createOscillator();
this.f = freq || 440;
// frequency
this.oscillator.frequency.setValueAtTime(this.f, p5sound.audiocontext.currentTime);
this.oscillator.type = type || 'sine';
var o = this.oscillator;
// connections
this.input = p5sound.audiocontext.createGain();
this.output = p5sound.audiocontext.createGain();
this._freqMods = [];
// modulators connected to this oscillator's frequency
// set default output gain to 0.5
this.output.gain.value = 0.5;
this.output.gain.setValueAtTime(0.5, p5sound.audiocontext.currentTime);
this.oscillator.connect(this.output);
// stereo panning
this.panPosition = 0;
this.connection = p5sound.input;
// connect to p5sound by default
this.panner = new p5.Panner(this.output, this.connection, 1);
//array of math operation signal chaining
this.mathOps = [this.output];
// add to the soundArray so we can dispose of the osc later
p5sound.soundArray.push(this);
};
/**
* Start an oscillator. Accepts an optional parameter to
* determine how long (in seconds from now) until the
* oscillator starts.
*
* @method start
* @param {Number} [time] startTime in seconds from now.
* @param {Number} [frequency] frequency in Hz.
*/
p5.Oscillator.prototype.start = function (time, f) {
if (this.started) {
var now = p5sound.audiocontext.currentTime;
this.stop(now);
}
if (!this.started) {
var freq = f || this.f;
var type = this.oscillator.type;
// var detune = this.oscillator.frequency.value;
this.oscillator = p5sound.audiocontext.createOscillator();
this.oscillator.frequency.exponentialRampToValueAtTime(Math.abs(freq), p5sound.audiocontext.currentTime);
this.oscillator.type = type;
// this.oscillator.detune.value = detune;
this.oscillator.connect(this.output);
time = time || 0;
this.oscillator.start(time + p5sound.audiocontext.currentTime);
this.freqNode = this.oscillator.frequency;
// if other oscillators are already connected to this osc's freq
for (var i in this._freqMods) {
if (typeof this._freqMods[i].connect !== 'undefined') {
this._freqMods[i].connect(this.oscillator.frequency);
}
}
this.started = true;
}
};
/**
* Stop an oscillator. Accepts an optional parameter
* to determine how long (in seconds from now) until the
* oscillator stops.
*
* @method stop
* @param {Number} secondsFromNow Time, in seconds from now.
*/
p5.Oscillator.prototype.stop = function (time) {
if (this.started) {
var t = time || 0;
var now = p5sound.audiocontext.currentTime;
this.oscillator.stop(t + now);
this.started = false;
}
};
/**
* Set the amplitude between 0 and 1.0. Or, pass in an object
* such as an oscillator to modulate amplitude with an audio signal.
*
* @method amp
* @param {Number|Object} vol between 0 and 1.0
* or a modulating signal/oscillator
* @param {Number} [rampTime] create a fade that lasts rampTime
* @param {Number} [timeFromNow] schedule this event to happen
* seconds from now
* @return {AudioParam} gain If no value is provided,
* returns the Web Audio API
* AudioParam that controls
* this oscillator's
* gain/amplitude/volume)
*/
p5.Oscillator.prototype.amp = function (vol, rampTime, tFromNow) {
var self = this;
if (typeof vol === 'number') {
var rampTime = rampTime || 0;
var tFromNow = tFromNow || 0;
var now = p5sound.audiocontext.currentTime;
var currentVol = this.output.gain.value;
this.output.gain.cancelScheduledValues(now);
this.output.gain.linearRampToValueAtTime(currentVol, now + tFromNow);
this.output.gain.linearRampToValueAtTime(vol, now + tFromNow + rampTime);
} else if (vol) {
console.log(vol);
vol.connect(self.output.gain);
} else {
// return the Gain Node
return this.output.gain;
}
};
// these are now the same thing
p5.Oscillator.prototype.fade = p5.Oscillator.prototype.amp;
p5.Oscillator.prototype.getAmp = function () {
return this.output.gain.value;
};
/**
* Set frequency of an oscillator to a value. Or, pass in an object
* such as an oscillator to modulate the frequency with an audio signal.
*
* @method freq
* @param {Number|Object} Frequency Frequency in Hz
* or modulating signal/oscillator
* @param {Number} [rampTime] Ramp time (in seconds)
* @param {Number} [timeFromNow] Schedule this event to happen
* at x seconds from now
* @return {AudioParam} Frequency If no value is provided,
* returns the Web Audio API
* AudioParam that controls
* this oscillator's frequency
* @example
*
* var osc = new p5.Oscillator(300);
* osc.start();
* osc.freq(40, 10);
*
*/
p5.Oscillator.prototype.freq = function (val, rampTime, tFromNow) {
if (typeof val === 'number' && !isNaN(val)) {
this.f = val;
var now = p5sound.audiocontext.currentTime;
var rampTime = rampTime || 0;
var tFromNow = tFromNow || 0;
var currentFreq = this.oscillator.frequency.value;
this.oscillator.frequency.cancelScheduledValues(now);
this.oscillator.frequency.setValueAtTime(currentFreq, now + tFromNow);
if (val > 0) {
this.oscillator.frequency.exponentialRampToValueAtTime(val, tFromNow + rampTime + now);
} else {
this.oscillator.frequency.linearRampToValueAtTime(val, tFromNow + rampTime + now);
}
} else if (val) {
if (val.output) {
val = val.output;
}
val.connect(this.oscillator.frequency);
// keep track of what is modulating this param
// so it can be re-connected if
this._freqMods.push(val);
} else {
// return the Frequency Node
return this.oscillator.frequency;
}
};
p5.Oscillator.prototype.getFreq = function () {
return this.oscillator.frequency.value;
};
/**
* Set type to 'sine', 'triangle', 'sawtooth' or 'square'.
*
* @method setType
* @param {String} type 'sine', 'triangle', 'sawtooth' or 'square'.
*/
p5.Oscillator.prototype.setType = function (type) {
this.oscillator.type = type;
};
p5.Oscillator.prototype.getType = function () {
return this.oscillator.type;
};
/**
* Connect to a p5.sound / Web Audio object.
*
* @method connect
* @param {Object} unit A p5.sound or Web Audio object
*/
p5.Oscillator.prototype.connect = function (unit) {
if (!unit) {
this.panner.connect(p5sound.input);
} else if (unit.hasOwnProperty('input')) {
this.panner.connect(unit.input);
this.connection = unit.input;
} else {
this.panner.connect(unit);
this.connection = unit;
}
};
/**
* Disconnect all outputs
*
* @method disconnect
*/
p5.Oscillator.prototype.disconnect = function (unit) {
this.output.disconnect();
this.panner.disconnect();
this.output.connect(this.panner);
this.oscMods = [];
};
/**
* Pan between Left (-1) and Right (1)
*
* @method pan
* @param {Number} panning Number between -1 and 1
* @param {Number} timeFromNow schedule this event to happen
* seconds from now
*/
p5.Oscillator.prototype.pan = function (pval, tFromNow) {
this.panPosition = pval;
this.panner.pan(pval, tFromNow);
};
p5.Oscillator.prototype.getPan = function () {
return this.panPosition;
};
// get rid of the oscillator
p5.Oscillator.prototype.dispose = function () {
if (this.oscillator) {
var now = p5sound.audiocontext.currentTime;
this.stop(now);
this.disconnect();
this.oscillator.disconnect();
this.panner = null;
this.oscillator = null;
}
// if it is a Pulse
if (this.osc2) {
this.osc2.dispose();
}
};
/**
* Set the phase of an oscillator between 0.0 and 1.0
*
* @method phase
* @param {Number} phase float between 0.0 and 1.0
*/
p5.Oscillator.prototype.phase = function (p) {
if (!this.dNode) {
// create a delay node
this.dNode = p5sound.audiocontext.createDelay();
// put the delay node in between output and panner
this.output.disconnect();
this.output.connect(this.dNode);
this.dNode.connect(this.panner);
}
// set delay time based on PWM width
var now = p5sound.audiocontext.currentTime;
this.dNode.delayTime.linearRampToValueAtTime(p5.prototype.map(p, 0, 1, 0, 1 / this.oscillator.frequency.value), now);
};
// ========================== //
// SIGNAL MATH FOR MODULATION //
// ========================== //
// return sigChain(this, scale, thisChain, nextChain, Scale);
var sigChain = function (o, mathObj, thisChain, nextChain, type) {
var chainSource = o.oscillator;
// if this type of math already exists in the chain, replace it
for (var i in o.mathOps) {
if (o.mathOps[i] instanceof type) {
chainSource.disconnect();
o.mathOps[i].dispose();
thisChain = i;
// assume nextChain is output gain node unless...
if (thisChain < o.mathOps.length - 2) {
nextChain = o.mathOps[i + 1];
}
}
}
if (thisChain == o.mathOps.length - 1) {
o.mathOps.push(nextChain);
}
// assume source is the oscillator unless i > 0
if (i > 0) {
chainSource = o.mathOps[i - 1];
}
chainSource.disconnect();
chainSource.connect(mathObj);
mathObj.connect(nextChain);
o.mathOps[thisChain] = mathObj;
return o;
};
/**
* Add a value to the p5.Oscillator's output amplitude,
* and return the oscillator. Calling this method again
* will override the initial add() with a new value.
*
* @method add
* @param {Number} number Constant number to add
* @return {p5.Oscillator} Oscillator Returns this oscillator
* with scaled output
*
*/
p5.Oscillator.prototype.add = function (num) {
var add = new Add(num);
var thisChain = this.mathOps.length - 1;
var nextChain = this.output;
return sigChain(this, add, thisChain, nextChain, Add);
};
/**
* Multiply the p5.Oscillator's output amplitude
* by a fixed value (i.e. turn it up!). Calling this method
* again will override the initial mult() with a new value.
*
* @method mult
* @param {Number} number Constant number to multiply
* @return {p5.Oscillator} Oscillator Returns this oscillator
* with multiplied output
*/
p5.Oscillator.prototype.mult = function (num) {
var mult = new Mult(num);
var thisChain = this.mathOps.length - 1;
var nextChain = this.output;
return sigChain(this, mult, thisChain, nextChain, Mult);
};
/**
* Scale this oscillator's amplitude values to a given
* range, and return the oscillator. Calling this method
* again will override the initial scale() with new values.
*
* @method scale
* @param {Number} inMin input range minumum
* @param {Number} inMax input range maximum
* @param {Number} outMin input range minumum
* @param {Number} outMax input range maximum
* @return {p5.Oscillator} Oscillator Returns this oscillator
* with scaled output
*/
p5.Oscillator.prototype.scale = function (inMin, inMax, outMin, outMax) {
var mapOutMin, mapOutMax;
if (arguments.length === 4) {
mapOutMin = p5.prototype.map(outMin, inMin, inMax, 0, 1) - 0.5;
mapOutMax = p5.prototype.map(outMax, inMin, inMax, 0, 1) - 0.5;
} else {
mapOutMin = arguments[0];
mapOutMax = arguments[1];
}
var scale = new Scale(mapOutMin, mapOutMax);
var thisChain = this.mathOps.length - 1;
var nextChain = this.output;
return sigChain(this, scale, thisChain, nextChain, Scale);
};
// ============================== //
// SinOsc, TriOsc, SqrOsc, SawOsc //
// ============================== //
/**
* Constructor: new p5.SinOsc().
* This creates a Sine Wave Oscillator and is
* equivalent to new p5.Oscillator('sine')
* or creating a p5.Oscillator and then calling
* its method setType('sine').
* See p5.Oscillator for methods.
*
* @method p5.SinOsc
* @param {[Number]} freq Set the frequency
*/
p5.SinOsc = function (freq) {
p5.Oscillator.call(this, freq, 'sine');
};
p5.SinOsc.prototype = Object.create(p5.Oscillator.prototype);
/**
* Constructor: new p5.TriOsc().
* This creates a Triangle Wave Oscillator and is
* equivalent to new p5.Oscillator('triangle')
* or creating a p5.Oscillator and then calling
* its method setType('triangle').
* See p5.Oscillator for methods.
*
* @method p5.TriOsc
* @param {[Number]} freq Set the frequency
*/
p5.TriOsc = function (freq) {
p5.Oscillator.call(this, freq, 'triangle');
};
p5.TriOsc.prototype = Object.create(p5.Oscillator.prototype);
/**
* Constructor: new p5.SawOsc().
* This creates a SawTooth Wave Oscillator and is
* equivalent to new p5.Oscillator('sawtooth')
* or creating a p5.Oscillator and then calling
* its method setType('sawtooth').
* See p5.Oscillator for methods.
*
* @method p5.SawOsc
* @param {[Number]} freq Set the frequency
*/
p5.SawOsc = function (freq) {
p5.Oscillator.call(this, freq, 'sawtooth');
};
p5.SawOsc.prototype = Object.create(p5.Oscillator.prototype);
/**
* Constructor: new p5.SqrOsc().
* This creates a Square Wave Oscillator and is
* equivalent to new p5.Oscillator('square')
* or creating a p5.Oscillator and then calling
* its method setType('square').
* See p5.Oscillator for methods.
*
* @method p5.SqrOsc
* @param {[Number]} freq Set the frequency
*/
p5.SqrOsc = function (freq) {
p5.Oscillator.call(this, freq, 'square');
};
p5.SqrOsc.prototype = Object.create(p5.Oscillator.prototype);
}(master, Tone_signal_Signal, Tone_signal_Add, Tone_signal_Multiply, Tone_signal_Scale);
var env;
env = function () {
'use strict';
var p5sound = master;
var Add = Tone_signal_Add;
var Mult = Tone_signal_Multiply;
var Scale = Tone_signal_Scale;
var Tone = Tone_core_Tone;
Tone.setContext(p5sound.audiocontext);
// oscillator or buffer source to clear on env complete
// to save resources if/when it is retriggered
var sourceToClear = null;
// set to true if attack is set, then false on release
var wasTriggered = false;
/**
* Envelopes are pre-defined amplitude distribution over time.
* The p5.Env accepts up to four time/level pairs, where time
* determines how long of a ramp before value reaches level.
* Typically, envelopes are used to control the output volume
* of an object, a series of fades referred to as Attack, Decay,
* Sustain and Release (ADSR). But p5.Env can control any
* Web Audio Param, for example it can be passed to an Oscillator
* frequency like osc.freq(env)
*
* @class p5.Env
* @constructor
* @param {Number} aTime Time (in seconds) before level
* reaches attackLevel
* @param {Number} aLevel Typically an amplitude between
* 0.0 and 1.0
* @param {Number} dTime Time
* @param {Number} [dLevel] Amplitude (In a standard ADSR envelope,
* decayLevel = sustainLevel)
* @param {Number} [sTime] Time (in seconds)
* @param {Number} [sLevel] Amplitude 0.0 to 1.0
* @param {Number} [rTime] Time (in seconds)
* @param {Number} [rLevel] Amplitude 0.0 to 1.0
* @example
*
* var aT = 0.1; // attack time in seconds
* var aL = 0.7; // attack level 0.0 to 1.0
* var dT = 0.3; // decay time in seconds
* var dL = 0.1; // decay level 0.0 to 1.0
* var sT = 0.2; // sustain time in seconds
* var sL = dL; // sustain level 0.0 to 1.0
* var rT = 0.5; // release time in seconds
* // release level defaults to zero
*
* var env;
* var triOsc;
*
* function setup() {
* background(0);
* noStroke();
* fill(255);
* textAlign(CENTER);
* text('click to play', width/2, height/2);
*
* env = new p5.Env(aT, aL, dT, dL, sT, sL, rT);
* triOsc = new p5.Oscillator('triangle');
* triOsc.amp(env); // give the env control of the triOsc's amp
* triOsc.start();
* }
*
* // mouseClick triggers envelope if over canvas
* function mouseClicked() {
* // is mouse over canvas?
* if (mouseX > 0 && mouseX < width && mouseY > 0 && mouseY < height) {
* env.play(noise);
* }
* }
*
*/
p5.Env = function (t1, l1, t2, l2, t3, l3, t4, l4) {
/**
* @property attackTime
*/
this.aTime = t1;
/**
* @property attackLevel
*/
this.aLevel = l1;
/**
* @property decayTime
*/
this.dTime = t2 || 0;
/**
* @property decayLevel
*/
this.dLevel = l2 || 0;
/**
* @property sustainTime
*/
this.sTime = t3 || 0;
/**
* @property sustainLevel
*/
this.sLevel = l3 || 0;
/**
* @property releaseTime
*/
this.rTime = t4 || 0;
/**
* @property releaseLevel
*/
this.rLevel = l4 || 0;
this.output = p5sound.audiocontext.createGain();
this.control = new p5.Signal();
this.control.connect(this.output);
this.connection = null;
// store connection
//array of math operation signal chaining
this.mathOps = [this.control];
// add to the soundArray so we can dispose of the env later
p5sound.soundArray.push(this);
};
/**
* Reset the envelope with a series of time/value pairs.
*
* @method set
* @param {Number} aTime Time (in seconds) before level
* reaches attackLevel
* @param {Number} aLevel Typically an amplitude between
* 0.0 and 1.0
* @param {Number} dTime Time
* @param {Number} [dLevel] Amplitude (In a standard ADSR envelope,
* decayLevel = sustainLevel)
* @param {Number} [sTime] Time (in seconds)
* @param {Number} [sLevel] Amplitude 0.0 to 1.0
* @param {Number} [rTime] Time (in seconds)
* @param {Number} [rLevel] Amplitude 0.0 to 1.0
*/
p5.Env.prototype.set = function (t1, l1, t2, l2, t3, l3, t4, l4) {
this.aTime = t1;
this.aLevel = l1;
this.dTime = t2 || 0;
this.dLevel = l2 || 0;
this.sTime = t3 || 0;
this.sLevel = l3 || 0;
this.rTime = t4 || 0;
this.rLevel = l4 || 0;
};
/**
* Assign a parameter to be controlled by this envelope.
* If a p5.Sound object is given, then the p5.Env will control its
* output gain. If multiple inputs are provided, the env will
* control all of them.
*
* @method setInput
* @param {Object} unit A p5.sound object or
* Web Audio Param.
*/
p5.Env.prototype.setInput = function (unit) {
for (var i = 0; i < arguments.length; i++) {
this.connect(arguments[i]);
}
};
p5.Env.prototype.ctrl = function (unit) {
this.connect(unit);
};
/**
* Play tells the envelope to start acting on a given input.
* If the input is a p5.sound object (i.e. AudioIn, Oscillator,
* SoundFile), then Env will control its output volume.
* Envelopes can also be used to control any
* Web Audio Audio Param.
*
* @method play
* @param {Object} unit A p5.sound object or
* Web Audio Param.
* @param {Number} secondsFromNow time from now (in seconds)
*/
p5.Env.prototype.play = function (unit, secondsFromNow) {
var now = p5sound.audiocontext.currentTime;
var tFromNow = secondsFromNow || 0;
var t = now + tFromNow;
if (unit) {
if (this.connection !== unit) {
this.connect(unit);
}
}
var currentVal = this.control.getValue();
this.control.cancelScheduledValues(t);
this.control.linearRampToValueAtTime(currentVal, t);
// attack
this.control.linearRampToValueAtTime(this.aLevel, t + this.aTime);
// decay to decay level
this.control.linearRampToValueAtTime(this.dLevel, t + this.aTime + this.dTime);
// hold sustain level
this.control.linearRampToValueAtTime(this.sLevel, t + this.aTime + this.dTime + this.sTime);
// release
this.control.linearRampToValueAtTime(this.rLevel, t + this.aTime + this.dTime + this.sTime + this.rTime);
var clearTime = t + this.aTime + this.dTime + this.sTime + this.rTime;
};
/**
* Trigger the Attack, Decay, and Sustain of the Envelope.
* Similar to holding down a key on a piano, but it will
* hold the sustain level until you let go. Input can be
* any p5.sound object, or a
* Web Audio Param.
*
* @method triggerAttack
* @param {Object} unit p5.sound Object or Web Audio Param
* @param {Number} secondsFromNow time from now (in seconds)
*/
p5.Env.prototype.triggerAttack = function (unit, secondsFromNow) {
var now = p5sound.audiocontext.currentTime;
var tFromNow = secondsFromNow || 0;
var t = now + tFromNow;
this.lastAttack = t;
wasTriggered = true;
// we should set current value, but this is not working on Firefox
var currentVal = this.control.getValue();
console.log(currentVal);
this.control.cancelScheduledValues(t);
this.control.linearRampToValueAtTime(currentVal, t);
if (unit) {
if (this.connection !== unit) {
this.connect(unit);
}
}
this.control.linearRampToValueAtTime(this.aLevel, t + this.aTime);
// attack
this.control.linearRampToValueAtTime(this.aLevel, t + this.aTime);
// decay to sustain level
this.control.linearRampToValueAtTime(this.dLevel, t + this.aTime + this.dTime);
this.control.linearRampToValueAtTime(this.sLevel, t + this.aTime + this.dTime + this.sTime);
};
/**
* Trigger the Release of the Envelope. This is similar to releasing
* the key on a piano and letting the sound fade according to the
* release level and release time.
*
* @method triggerRelease
* @param {Object} unit p5.sound Object or Web Audio Param
* @param {Number} secondsFromNow time to trigger the release
*/
p5.Env.prototype.triggerRelease = function (unit, secondsFromNow) {
// only trigger a release if an attack was triggered
if (!wasTriggered) {
return;
}
var now = p5sound.audiocontext.currentTime;
var tFromNow = secondsFromNow || 0;
var t = now + tFromNow;
var relTime;
if (unit) {
if (this.connection !== unit) {
this.connect(unit);
}
}
this.control.cancelScheduledValues(t);
// ideally would get & set currentValue here,
// but this.control._scalar.gain.value not working in firefox
// release based on how much time has passed since this.lastAttack
if (now - this.lastAttack < this.aTime) {
var a = this.aTime - (t - this.lastAttack);
this.control.linearRampToValueAtTime(this.aLevel, t + a);
this.control.linearRampToValueAtTime(this.dLevel, t + a + this.dTime);
this.control.linearRampToValueAtTime(this.sLevel, t + a + this.dTime + this.sTime);
this.control.linearRampToValueAtTime(this.rLevel, t + a + this.dTime + this.sTime + this.rTime);
relTime = t + this.dTime + this.sTime + this.rTime;
} else if (now - this.lastAttack < this.aTime + this.dTime) {
var d = this.aTime + this.dTime - (now - this.lastAttack);
this.control.linearRampToValueAtTime(this.dLevel, t + d);
// this.control.linearRampToValueAtTime(this.sLevel, t + d + this.sTime);
this.control.linearRampToValueAtTime(this.sLevel, t + d + 0.01);
this.control.linearRampToValueAtTime(this.rLevel, t + d + 0.01 + this.rTime);
relTime = t + this.sTime + this.rTime;
} else if (now - this.lastAttack < this.aTime + this.dTime + this.sTime) {
var s = this.aTime + this.dTime + this.sTime - (now - this.lastAttack);
this.control.linearRampToValueAtTime(this.sLevel, t + s);
this.control.linearRampToValueAtTime(this.rLevel, t + s + this.rTime);
relTime = t + this.rTime;
} else {
this.control.linearRampToValueAtTime(this.sLevel, t);
this.control.linearRampToValueAtTime(this.rLevel, t + this.rTime);
relTime = t + this.dTime + this.sTime + this.rTime;
}
// clear osc / sources
var clearTime = t + this.aTime + this.dTime + this.sTime + this.rTime;
// * 1000;
if (this.connection && this.connection.hasOwnProperty('oscillator')) {
sourceToClear = this.connection.oscillator;
sourceToClear.stop(clearTime + 0.01);
} else if (this.connect && this.connection.hasOwnProperty('source')) {
sourceToClear = this.connection.source;
sourceToClear.stop(clearTime + 0.01);
}
wasTriggered = false;
};
p5.Env.prototype.connect = function (unit) {
this.connection = unit;
// assume we're talking about output gain
// unless given a different audio param
if (unit instanceof p5.Oscillator || unit instanceof p5.SoundFile || unit instanceof p5.AudioIn || unit instanceof p5.Reverb || unit instanceof p5.Noise || unit instanceof p5.Filter || unit instanceof p5.Delay) {
unit = unit.output.gain;
}
if (unit instanceof AudioParam) {
//set the initial value
unit.setValueAtTime(0, p5sound.audiocontext.currentTime);
}
if (unit instanceof p5.Signal) {
unit.setValue(0);
}
this.output.connect(unit);
};
p5.Env.prototype.disconnect = function (unit) {
this.output.disconnect();
};
// Signal Math
/**
* Add a value to the p5.Oscillator's output amplitude,
* and return the oscillator. Calling this method
* again will override the initial add() with new values.
*
* @method add
* @param {Number} number Constant number to add
* @return {p5.Env} Envelope Returns this envelope
* with scaled output
*/
p5.Env.prototype.add = function (num) {
var add = new Add(num);
var thisChain = this.mathOps.length;
var nextChain = this.output;
return p5.prototype._mathChain(this, add, thisChain, nextChain, Add);
};
/**
* Multiply the p5.Env's output amplitude
* by a fixed value. Calling this method
* again will override the initial mult() with new values.
*
* @method mult
* @param {Number} number Constant number to multiply
* @return {p5.Env} Envelope Returns this envelope
* with scaled output
*/
p5.Env.prototype.mult = function (num) {
var mult = new Mult(num);
var thisChain = this.mathOps.length;
var nextChain = this.output;
return p5.prototype._mathChain(this, mult, thisChain, nextChain, Mult);
};
/**
* Scale this envelope's amplitude values to a given
* range, and return the envelope. Calling this method
* again will override the initial scale() with new values.
*
* @method scale
* @param {Number} inMin input range minumum
* @param {Number} inMax input range maximum
* @param {Number} outMin input range minumum
* @param {Number} outMax input range maximum
* @return {p5.Env} Envelope Returns this envelope
* with scaled output
*/
p5.Env.prototype.scale = function (inMin, inMax, outMin, outMax) {
var scale = new Scale(inMin, inMax, outMin, outMax);
var thisChain = this.mathOps.length;
var nextChain = this.output;
return p5.prototype._mathChain(this, scale, thisChain, nextChain, Scale);
};
// get rid of the oscillator
p5.Env.prototype.dispose = function () {
var now = p5sound.audiocontext.currentTime;
this.disconnect();
try {
this.control.dispose();
this.control = null;
} catch (e) {
}
for (var i = 1; i < this.mathOps.length; i++) {
mathOps[i].dispose();
}
};
}(master, Tone_signal_Add, Tone_signal_Multiply, Tone_signal_Scale, Tone_core_Tone);
var pulse;
pulse = function () {
'use strict';
var p5sound = master;
/**
* Creates a Pulse object, an oscillator that implements
* Pulse Width Modulation.
* The pulse is created with two oscillators.
* Accepts a parameter for frequency, and to set the
* width between the pulses. See
* p5.Oscillator for a full list of methods.
*
* @class p5.Pulse
* @constructor
* @param {Number} [freq] Frequency in oscillations per second (Hz)
* @param {Number} [w] Width between the pulses (0 to 1.0,
* defaults to 0)
* @example
*
* var pulse;
* function setup() {
* background(0);
*
* // Create and start the pulse wave oscillator
* pulse = new p5.Pulse();
* pulse.amp(0.5);
* pulse.freq(220);
* pulse.start();
* }
*
* function draw() {
* var w = map(mouseX, 0, width, 0, 1);
* w = constrain(w, 0, 1);
* pulse.width(w)
* }
*
*/
p5.Pulse = function (freq, w) {
p5.Oscillator.call(this, freq, 'sawtooth');
// width of PWM, should be betw 0 to 1.0
this.w = w || 0;
// create a second oscillator with inverse frequency
this.osc2 = new p5.SawOsc(freq);
// create a delay node
this.dNode = p5sound.audiocontext.createDelay();
// dc offset
this.dcOffset = createDCOffset();
this.dcGain = p5sound.audiocontext.createGain();
this.dcOffset.connect(this.dcGain);
this.dcGain.connect(this.output);
// set delay time based on PWM width
this.f = freq || 440;
var mW = this.w / this.oscillator.frequency.value;
this.dNode.delayTime.value = mW;
this.dcGain.gain.value = 1.7 * (0.5 - this.w);
// disconnect osc2 and connect it to delay, which is connected to output
this.osc2.disconnect();
this.osc2.panner.disconnect();
this.osc2.amp(-1);
// inverted amplitude
this.osc2.output.connect(this.dNode);
this.dNode.connect(this.output);
this.output.gain.value = 1;
this.output.connect(this.panner);
};
p5.Pulse.prototype = Object.create(p5.Oscillator.prototype);
/**
* Set the width of a Pulse object (an oscillator that implements
* Pulse Width Modulation).
*
* @method width
* @param {Number} [width] Width between the pulses (0 to 1.0,
* defaults to 0)
*/
p5.Pulse.prototype.width = function (w) {
if (typeof w === 'number') {
if (w <= 1 && w >= 0) {
this.w = w;
// set delay time based on PWM width
// var mW = map(this.w, 0, 1.0, 0, 1/this.f);
var mW = this.w / this.oscillator.frequency.value;
this.dNode.delayTime.value = mW;
}
this.dcGain.gain.value = 1.7 * (0.5 - this.w);
} else {
w.connect(this.dNode.delayTime);
var sig = new p5.SignalAdd(-0.5);
sig.setInput(w);
sig = sig.mult(-1);
sig = sig.mult(1.7);
sig.connect(this.dcGain.gain);
}
};
p5.Pulse.prototype.start = function (f, time) {
var now = p5sound.audiocontext.currentTime;
var t = time || 0;
if (!this.started) {
var freq = f || this.f;
var type = this.oscillator.type;
this.oscillator = p5sound.audiocontext.createOscillator();
this.oscillator.frequency.setValueAtTime(freq, now);
this.oscillator.type = type;
this.oscillator.connect(this.output);
this.oscillator.start(t + now);
// set up osc2
this.osc2.oscillator = p5sound.audiocontext.createOscillator();
this.osc2.oscillator.frequency.setValueAtTime(freq, t + now);
this.osc2.oscillator.type = type;
this.osc2.oscillator.connect(this.osc2.output);
this.osc2.start(t + now);
this.freqNode = [
this.oscillator.frequency,
this.osc2.oscillator.frequency
];
// start dcOffset, too
this.dcOffset = createDCOffset();
this.dcOffset.connect(this.dcGain);
this.dcOffset.start(t + now);
// if LFO connections depend on these oscillators
if (this.mods !== undefined && this.mods.frequency !== undefined) {
this.mods.frequency.connect(this.freqNode[0]);
this.mods.frequency.connect(this.freqNode[1]);
}
this.started = true;
this.osc2.started = true;
}
};
p5.Pulse.prototype.stop = function (time) {
if (this.started) {
var t = time || 0;
var now = p5sound.audiocontext.currentTime;
this.oscillator.stop(t + now);
this.osc2.oscillator.stop(t + now);
this.dcOffset.stop(t + now);
this.started = false;
this.osc2.started = false;
}
};
p5.Pulse.prototype.freq = function (val, rampTime, tFromNow) {
if (typeof val === 'number') {
this.f = val;
var now = p5sound.audiocontext.currentTime;
var rampTime = rampTime || 0;
var tFromNow = tFromNow || 0;
var currentFreq = this.oscillator.frequency.value;
this.oscillator.frequency.cancelScheduledValues(now);
this.oscillator.frequency.setValueAtTime(currentFreq, now + tFromNow);
this.oscillator.frequency.exponentialRampToValueAtTime(val, tFromNow + rampTime + now);
this.osc2.oscillator.frequency.cancelScheduledValues(now);
this.osc2.oscillator.frequency.setValueAtTime(currentFreq, now + tFromNow);
this.osc2.oscillator.frequency.exponentialRampToValueAtTime(val, tFromNow + rampTime + now);
if (this.freqMod) {
this.freqMod.output.disconnect();
this.freqMod = null;
}
} else if (val.output) {
val.output.disconnect();
val.output.connect(this.oscillator.frequency);
val.output.connect(this.osc2.oscillator.frequency);
this.freqMod = val;
}
};
// inspiration: http://webaudiodemos.appspot.com/oscilloscope/
function createDCOffset() {
var ac = p5sound.audiocontext;
var buffer = ac.createBuffer(1, 2048, ac.sampleRate);
var data = buffer.getChannelData(0);
for (var i = 0; i < 2048; i++)
data[i] = 1;
var bufferSource = ac.createBufferSource();
bufferSource.buffer = buffer;
bufferSource.loop = true;
return bufferSource;
}
}(master, oscillator);
var noise;
noise = function () {
'use strict';
var p5sound = master;
/**
* Noise is a type of oscillator that generates a buffer with random values.
*
* @class p5.Noise
* @constructor
* @param {String} type Type of noise can be 'white' (default),
* 'brown' or 'pink'.
* @return {Object} Noise Object
*/
p5.Noise = function (type) {
p5.Oscillator.call(this);
delete this.f;
delete this.freq;
delete this.oscillator;
this.buffer = _whiteNoise;
};
p5.Noise.prototype = Object.create(p5.Oscillator.prototype);
// generate noise buffers
var _whiteNoise = function () {
var bufferSize = 2 * p5sound.audiocontext.sampleRate;
var whiteBuffer = p5sound.audiocontext.createBuffer(1, bufferSize, p5sound.audiocontext.sampleRate);
var noiseData = whiteBuffer.getChannelData(0);
for (var i = 0; i < bufferSize; i++) {
noiseData[i] = Math.random() * 2 - 1;
}
whiteBuffer.type = 'white';
return whiteBuffer;
}();
var _pinkNoise = function () {
var bufferSize = 2 * p5sound.audiocontext.sampleRate;
var pinkBuffer = p5sound.audiocontext.createBuffer(1, bufferSize, p5sound.audiocontext.sampleRate);
var noiseData = pinkBuffer.getChannelData(0);
var b0, b1, b2, b3, b4, b5, b6;
b0 = b1 = b2 = b3 = b4 = b5 = b6 = 0;
for (var i = 0; i < bufferSize; i++) {
var white = Math.random() * 2 - 1;
b0 = 0.99886 * b0 + white * 0.0555179;
b1 = 0.99332 * b1 + white * 0.0750759;
b2 = 0.969 * b2 + white * 0.153852;
b3 = 0.8665 * b3 + white * 0.3104856;
b4 = 0.55 * b4 + white * 0.5329522;
b5 = -0.7616 * b5 - white * 0.016898;
noiseData[i] = b0 + b1 + b2 + b3 + b4 + b5 + b6 + white * 0.5362;
noiseData[i] *= 0.11;
// (roughly) compensate for gain
b6 = white * 0.115926;
}
pinkBuffer.type = 'pink';
return pinkBuffer;
}();
var _brownNoise = function () {
var bufferSize = 2 * p5sound.audiocontext.sampleRate;
var brownBuffer = p5sound.audiocontext.createBuffer(1, bufferSize, p5sound.audiocontext.sampleRate);
var noiseData = brownBuffer.getChannelData(0);
var lastOut = 0;
for (var i = 0; i < bufferSize; i++) {
var white = Math.random() * 2 - 1;
noiseData[i] = (lastOut + 0.02 * white) / 1.02;
lastOut = noiseData[i];
noiseData[i] *= 3.5;
}
brownBuffer.type = 'brown';
return brownBuffer;
}();
/**
* Set type of noise to 'white', 'pink' or 'brown'.
* White is the default.
*
* @method setType
* @param {String} [type] 'white', 'pink' or 'brown'
*/
p5.Noise.prototype.setType = function (type) {
switch (type) {
case 'white':
this.buffer = _whiteNoise;
break;
case 'pink':
this.buffer = _pinkNoise;
break;
case 'brown':
this.buffer = _brownNoise;
break;
default:
this.buffer = _whiteNoise;
}
if (this.started) {
var now = p5sound.audiocontext.currentTime;
this.stop(now);
this.start(now + 0.01);
}
};
p5.Noise.prototype.getType = function () {
return this.buffer.type;
};
/**
* Start the noise
*
* @method start
*/
p5.Noise.prototype.start = function () {
if (this.started) {
this.stop();
}
this.noise = p5sound.audiocontext.createBufferSource();
this.noise.buffer = this.buffer;
this.noise.loop = true;
this.noise.connect(this.output);
var now = p5sound.audiocontext.currentTime;
this.noise.start(now);
this.started = true;
};
/**
* Stop the noise.
*
* @method stop
*/
p5.Noise.prototype.stop = function () {
var now = p5sound.audiocontext.currentTime;
if (this.noise) {
this.noise.stop(now);
this.started = false;
}
};
/**
* Pan the noise.
*
* @method pan
* @param {Number} panning Number between -1 (left)
* and 1 (right)
* @param {Number} timeFromNow schedule this event to happen
* seconds from now
*/
/**
* Set the amplitude of the noise between 0 and 1.0. Or,
* modulate amplitude with an audio signal such as an oscillator.
*
* @param {Number|Object} volume amplitude between 0 and 1.0
* or modulating signal/oscillator
* @param {Number} [rampTime] create a fade that lasts rampTime
* @param {Number} [timeFromNow] schedule this event to happen
* seconds from now
*/
/**
* Send output to a p5.sound or web audio object
*
* @method connect
* @param {Object} unit
*/
/**
* Disconnect all output.
*
* @method disconnect
*/
p5.Noise.prototype.dispose = function () {
var now = p5sound.audiocontext.currentTime;
if (this.noise) {
this.noise.disconnect();
this.stop(now);
}
if (this.output) {
this.output.disconnect();
}
if (this.panner) {
this.panner.disconnect();
}
this.output = null;
this.panner = null;
this.buffer = null;
this.noise = null;
};
}(master);
var audioin;
audioin = function () {
'use strict';
var p5sound = master;
/**
* Get audio from an input, i.e. your computer's microphone.
*
* Turn the mic on/off with the start() and stop() methods. When the mic
* is on, its volume can be measured with getLevel or by connecting an
* FFT object.
*
* If you want to hear the AudioIn, use the .connect() method.
* AudioIn does not connect to p5.sound output by default to prevent
* feedback.
*
* Note: This uses the getUserMedia/
* Stream API, which is not supported by certain browsers.
*
* @class p5.AudioIn
* @constructor
* @return {Object} AudioIn
* @example
*
* var mic;
* function setup(){
* mic = new p5.AudioIn()
* mic.start();
* }
* function draw(){
* background(0);
* micLevel = mic.getLevel();
* ellipse(width/2, constrain(height-micLevel*height*5, 0, height), 10, 10);
* }
*
*/
p5.AudioIn = function () {
// set up audio input
this.input = p5sound.audiocontext.createGain();
this.output = p5sound.audiocontext.createGain();
this.stream = null;
this.mediaStream = null;
this.currentSource = 0;
/**
* Client must allow browser to access their microphone / audioin source.
* Default: false. Will become true when the client enables acces.
*
* @property {Boolean} enabled
*/
this.enabled = false;
// create an amplitude, connect to it by default but not to master out
this.amplitude = new p5.Amplitude();
this.output.connect(this.amplitude.input);
// Some browsers let developer determine their input sources
if (typeof window.MediaStreamTrack === 'undefined') {
window.alert('This browser does not support MediaStreamTrack');
} else if (typeof window.MediaStreamTrack.getSources === 'function') {
// Chrome supports getSources to list inputs. Dev picks default
window.MediaStreamTrack.getSources(this._gotSources);
} else {
}
// add to soundArray so we can dispose on close
p5sound.soundArray.push(this);
};
/**
* Start processing audio input. This enables the use of other
* AudioIn methods like getLevel(). Note that by default, AudioIn
* is not connected to p5.sound's output. So you won't hear
* anything unless you use the connect() method.
*
* @method start
*/
p5.AudioIn.prototype.start = function () {
var self = this;
// if _gotSources() i.e. developers determine which source to use
if (p5sound.inputSources[self.currentSource]) {
// set the audio source
var audioSource = p5sound.inputSources[self.currentSource].id;
var constraints = { audio: { optional: [{ sourceId: audioSource }] } };
navigator.getUserMedia(constraints, this._onStream = function (stream) {
self.stream = stream;
self.enabled = true;
// Wrap a MediaStreamSourceNode around the live input
self.mediaStream = p5sound.audiocontext.createMediaStreamSource(stream);
self.mediaStream.connect(self.output);
// only send to the Amplitude reader, so we can see it but not hear it.
self.amplitude.setInput(self.output);
}, this._onStreamError = function (stream) {
console.error(stream);
});
} else {
// if Firefox where users select their source via browser
// if (typeof MediaStreamTrack.getSources === 'undefined') {
// Only get the audio stream.
window.navigator.getUserMedia({ 'audio': true }, this._onStream = function (stream) {
self.stream = stream;
self.enabled = true;
// Wrap a MediaStreamSourceNode around the live input
self.mediaStream = p5sound.audiocontext.createMediaStreamSource(stream);
self.mediaStream.connect(self.output);
// only send to the Amplitude reader, so we can see it but not hear it.
self.amplitude.setInput(self.output);
}, this._onStreamError = function (stream) {
console.error(stream);
});
}
};
/**
* Turn the AudioIn off. If the AudioIn is stopped, it cannot getLevel().
*
* @method stop
*/
p5.AudioIn.prototype.stop = function () {
if (this.stream) {
this.stream.stop();
}
};
/**
* Connect to an audio unit. If no parameter is provided, will
* connect to the master output (i.e. your speakers).
*
* @method connect
* @param {Object} [unit] An object that accepts audio input,
* such as an FFT
*/
p5.AudioIn.prototype.connect = function (unit) {
if (unit) {
if (unit.hasOwnProperty('input')) {
this.output.connect(unit.input);
} else if (unit.hasOwnProperty('analyser')) {
this.output.connect(unit.analyser);
} else {
this.output.connect(unit);
}
} else {
this.output.connect(p5sound.input);
}
};
/**
* Disconnect the AudioIn from all audio units. For example, if
* connect() had been called, disconnect() will stop sending
* signal to your speakers.
*
* @method disconnect
*/
p5.AudioIn.prototype.disconnect = function (unit) {
this.output.disconnect(unit);
// stay connected to amplitude even if not outputting to p5
this.output.connect(this.amplitude.input);
};
/**
* Read the Amplitude (volume level) of an AudioIn. The AudioIn
* class contains its own instance of the Amplitude class to help
* make it easy to get a microphone's volume level. Accepts an
* optional smoothing value (0.0 < 1.0). NOTE: AudioIn must
* .start() before using .getLevel().
*
* @method getLevel
* @param {Number} [smoothing] Smoothing is 0.0 by default.
* Smooths values based on previous values.
* @return {Number} Volume level (between 0.0 and 1.0)
*/
p5.AudioIn.prototype.getLevel = function (smoothing) {
if (smoothing) {
this.amplitude.smoothing = smoothing;
}
return this.amplitude.getLevel();
};
/**
* Add input sources to the list of available sources.
*
* @private
*/
p5.AudioIn.prototype._gotSources = function (sourceInfos) {
for (var i = 0; i < sourceInfos.length; i++) {
var sourceInfo = sourceInfos[i];
if (sourceInfo.kind === 'audio') {
// add the inputs to inputSources
//p5sound.inputSources.push(sourceInfo);
return sourceInfo;
}
}
};
/**
* Set amplitude (volume) of a mic input between 0 and 1.0.
*
* @method amp
* @param {Number} vol between 0 and 1.0
* @param {Number} [time] ramp time (optional)
*/
p5.AudioIn.prototype.amp = function (vol, t) {
if (t) {
var rampTime = t || 0;
var currentVol = this.output.gain.value;
this.output.gain.cancelScheduledValues(p5sound.audiocontext.currentTime);
this.output.gain.setValueAtTime(currentVol, p5sound.audiocontext.currentTime);
this.output.gain.linearRampToValueAtTime(vol, rampTime + p5sound.audiocontext.currentTime);
} else {
this.output.gain.cancelScheduledValues(p5sound.audiocontext.currentTime);
this.output.gain.setValueAtTime(vol, p5sound.audiocontext.currentTime);
}
};
p5.AudioIn.prototype.listSources = function () {
console.log('listSources is deprecated - please use AudioIn.getSources');
console.log('input sources: ');
if (p5sound.inputSources.length > 0) {
return p5sound.inputSources;
} else {
return 'This browser does not support MediaStreamTrack.getSources()';
}
};
/**
* Chrome only. Returns a list of available input sources
* and allows the user to set the media source. Firefox allows
* the user to choose from input sources in the permissions dialogue
* instead of enumerating available sources and selecting one.
* Note: in order to have descriptive media names your page must be
* served over a secure (HTTPS) connection and the page should
* request user media before enumerating devices. Otherwise device
* ID will be a long device ID number and does not specify device
* type. For example see
* https://simpl.info/getusermedia/sources/index.html vs.
* http://simpl.info/getusermedia/sources/index.html
*
* @method getSources
* @param {Function} callback a callback to handle the sources
* when they have been enumerated
* @example
*
* var audiograb;
*
* function setup(){
* //new audioIn
* audioGrab = new p5.AudioIn();
*
* audioGrab.getSources(function(sourceList) {
* //print out the array of available sources
* console.log(sourceList);
* //set the source to the first item in the inputSources array
* audioGrab.setSource(0);
* });
* }
* function draw(){
* }
*
*/
p5.AudioIn.prototype.getSources = function (callback) {
if (typeof window.MediaStreamTrack.getSources === 'function') {
window.MediaStreamTrack.getSources(function (data) {
for (var i = 0, max = data.length; i < max; i++) {
var sourceInfo = data[i];
if (sourceInfo.kind === 'audio') {
// add the inputs to inputSources
p5sound.inputSources.push(sourceInfo);
}
}
callback(p5sound.inputSources);
});
} else {
console.log('This browser does not support MediaStreamTrack.getSources()');
}
};
/**
* Set the input source. Accepts a number representing a
* position in the array returned by listSources().
* This is only available in browsers that support
* MediaStreamTrack.getSources(). Instead, some browsers
* give users the option to set their own media source.
*
* @method setSource
* @param {number} num position of input source in the array
*/
p5.AudioIn.prototype.setSource = function (num) {
// TO DO - set input by string or # (array position)
var self = this;
if (p5sound.inputSources.length > 0 && num < p5sound.inputSources.length) {
// set the current source
self.currentSource = num;
console.log('set source to ' + p5sound.inputSources[self.currentSource].id);
} else {
console.log('unable to set input source');
}
};
// private method
p5.AudioIn.prototype.dispose = function () {
this.stop();
if (this.output) {
this.output.disconnect();
}
if (this.amplitude) {
this.amplitude.disconnect();
}
this.amplitude = null;
this.output = null;
};
}(master);
var filter;
filter = function () {
'use strict';
var p5sound = master;
/**
* A p5.Filter uses a Web Audio Biquad Filter to filter
* the frequency response of an input source. Inheriting
* classes include:
* * p5.LowPass - allows frequencies below
* the cutoff frequency to pass through, and attenuates
* frequencies above the cutoff.
* * p5.HighPass - the opposite of a lowpass
* filter.
* * p5.BandPass - allows a range of
* frequencies to pass through and attenuates the frequencies
* below and above this frequency range.
*
* The .res() method controls either width of the
* bandpass, or resonance of the low/highpass cutoff frequency.
*
* @class p5.Filter
* @constructor
* @param {[String]} type 'lowpass' (default), 'highpass', 'bandpass'
* @return {Object} p5.Filter
* @example
*
* var fft, noise, filter;
*
* function setup() {
* fill(255, 40, 255);
*
* filter = new p5.BandPass();
*
* noise = new p5.Noise();
* // disconnect unfiltered noise,
* // and connect to filter
* noise.disconnect();
* noise.connect(filter);
* noise.start();
*
* fft = new p5.FFT();
* }
*
* function draw() {
* background(30);
*
* // set the BandPass frequency based on mouseX
* var freq = map(mouseX, 0, width, 20, 10000);
* filter.freq(freq);
* // give the filter a narrow band (lower res = wider bandpass)
* filter.res(50);
*
* // draw filtered spectrum
* var spectrum = fft.analyze();
* noStroke();
* for (var i = 0; i < spectrum.length; i++) {
* var x = map(i, 0, spectrum.length, 0, width);
* var h = -height + map(spectrum[i], 0, 255, height, 0);
* rect(x, height, width/spectrum.length, h);
* }
*
* isMouseOverCanvas();
* }
*
* function isMouseOverCanvas() {
* var mX = mouseX, mY = mouseY;
* if (mX > 0 && mX < width && mY < height && mY > 0) {
* noise.amp(0.5, 0.2);
* } else {
* noise.amp(0, 0.2);
* }
* }
*
*/
p5.Filter = function (type) {
this.ac = p5sound.audiocontext;
this.input = this.ac.createGain();
this.output = this.ac.createGain();
/**
* The p5.Filter is built with a
*
* Web Audio BiquadFilter Node.
*
* @property biquadFilter
* @type {Object} Web Audio Delay Node
*/
this.biquad = this.ac.createBiquadFilter();
this.input.connect(this.biquad);
this.biquad.connect(this.output);
this.connect();
if (type) {
this.setType(type);
}
};
/**
* Filter an audio signal according to a set
* of filter parameters.
*
* @method process
* @param {Object} Signal An object that outputs audio
* @param {[Number]} freq Frequency in Hz, from 10 to 22050
* @param {[Number]} res Resonance/Width of the filter frequency
* from 0.001 to 1000
*/
p5.Filter.prototype.process = function (src, freq, res) {
src.connect(this.input);
this.set(freq, res);
};
/**
* Set the frequency and the resonance of the filter.
*
* @method set
* @param {Number} freq Frequency in Hz, from 10 to 22050
* @param {Number} res Resonance (Q) from 0.001 to 1000
* @param {Number} [timeFromNow] schedule this event to happen
* seconds from now
*/
p5.Filter.prototype.set = function (freq, res, time) {
if (freq) {
this.freq(freq, time);
}
if (res) {
this.res(res, time);
}
};
/**
* Set the filter frequency, in Hz, from 10 to 22050 (the range of
* human hearing, although in reality most people hear in a narrower
* range).
*
* @method freq
* @param {Number} freq Filter Frequency
* @param {Number} [timeFromNow] schedule this event to happen
* seconds from now
* @return {Number} value Returns the current frequency value
*/
p5.Filter.prototype.freq = function (freq, time) {
var self = this;
var t = time || 0;
if (freq <= 0) {
freq = 1;
}
if (typeof freq === 'number') {
self.biquad.frequency.value = freq;
self.biquad.frequency.cancelScheduledValues(this.ac.currentTime + 0.01 + t);
self.biquad.frequency.exponentialRampToValueAtTime(freq, this.ac.currentTime + 0.02 + t);
} else if (freq) {
freq.connect(this.biquad.frequency);
}
return self.biquad.frequency.value;
};
/**
* Controls either width of a bandpass frequency,
* or the resonance of a low/highpass cutoff frequency.
*
* @method res
* @param {Number} res Resonance/Width of filter freq
* from 0.001 to 1000
* @param {Number} [timeFromNow] schedule this event to happen
* seconds from now
* @return {Number} value Returns the current res value
*/
p5.Filter.prototype.res = function (res, time) {
var self = this;
var t = time || 0;
if (typeof res == 'number') {
self.biquad.Q.value = res;
self.biquad.Q.cancelScheduledValues(self.ac.currentTime + 0.01 + t);
self.biquad.Q.linearRampToValueAtTime(res, self.ac.currentTime + 0.02 + t);
} else if (res) {
freq.connect(this.biquad.Q);
}
return self.biquad.Q.value;
};
/**
* Set the type of a p5.Filter. Possible types include:
* "lowpass" (default), "highpass", "bandpass",
* "lowshelf", "highshelf", "peaking", "notch",
* "allpass".
*
* @method setType
* @param {String}
*/
p5.Filter.prototype.setType = function (t) {
this.biquad.type = t;
};
/**
* Set the output level of the filter.
*
* @method amp
* @param {Number} volume amplitude between 0 and 1.0
* @param {Number} [rampTime] create a fade that lasts rampTime
* @param {Number} [timeFromNow] schedule this event to happen
* seconds from now
*/
p5.Filter.prototype.amp = function (vol, rampTime, tFromNow) {
var rampTime = rampTime || 0;
var tFromNow = tFromNow || 0;
var now = p5sound.audiocontext.currentTime;
var currentVol = this.output.gain.value;
this.output.gain.cancelScheduledValues(now);
this.output.gain.linearRampToValueAtTime(currentVol, now + tFromNow + 0.001);
this.output.gain.linearRampToValueAtTime(vol, now + tFromNow + rampTime + 0.001);
};
/**
* Send output to a p5.sound or web audio object
*
* @method connect
* @param {Object} unit
*/
p5.Filter.prototype.connect = function (unit) {
var u = unit || p5.soundOut.input;
this.output.connect(u);
};
/**
* Disconnect all output.
*
* @method disconnect
*/
p5.Filter.prototype.disconnect = function () {
this.output.disconnect();
};
/**
* Constructor: new p5.LowPass() Filter.
* This is the same as creating a p5.Filter and then calling
* its method setType('lowpass').
* See p5.Filter for methods.
*
* @method p5.LowPass
*/
p5.LowPass = function () {
p5.Filter.call(this, 'lowpass');
};
p5.LowPass.prototype = Object.create(p5.Filter.prototype);
/**
* Constructor: new p5.HighPass() Filter.
* This is the same as creating a p5.Filter and then calling
* its method setType('highpass').
* See p5.Filter for methods.
*
* @method p5.HighPass
*/
p5.HighPass = function () {
p5.Filter.call(this, 'highpass');
};
p5.HighPass.prototype = Object.create(p5.Filter.prototype);
/**
* Constructor: new p5.BandPass() Filter.
* This is the same as creating a p5.Filter and then calling
* its method setType('bandpass').
* See p5.Filter for methods.
*
* @method p5.BandPass
*/
p5.BandPass = function () {
p5.Filter.call(this, 'bandpass');
};
p5.BandPass.prototype = Object.create(p5.Filter.prototype);
}(master);
var delay;
delay = function () {
'use strict';
var p5sound = master;
var Filter = filter;
/**
* Delay is an echo effect. It processes an existing sound source,
* and outputs a delayed version of that sound. The p5.Delay can
* produce different effects depending on the delayTime, feedback,
* filter, and type. In the example below, a feedback of 0.5 will
* produce a looping delay that decreases in volume by
* 50% each repeat. A filter will cut out the high frequencies so
* that the delay does not sound as piercing as the original source.
*
* @class p5.Delay
* @constructor
* @return {Object} Returns a p5.Delay object
* @example
*
* var noise, env, delay;
*
* function setup() {
* background(0);
* noStroke();
* fill(255);
* textAlign(CENTER);
* text('click to play', width/2, height/2);
*
* noise = new p5.Noise('brown');
* noise.amp(0);
* noise.start();
*
* delay = new p5.Delay();
*
* // delay.process() accepts 4 parameters:
* // source, delayTime, feedback, filter frequency
* // play with these numbers!!
* delay.process(noise, .12, .7, 2300);
*
* // play the noise with an envelope,
* // a series of fades ( time / value pairs )
* env = new p5.Env(.01, 0.2, .2, .1);
* }
*
* // mouseClick triggers envelope
* function mouseClicked() {
* // is mouse over canvas?
* if (mouseX > 0 && mouseX < width && mouseY > 0 && mouseY < height) {
* env.play(noise);
* }
* }
*
*/
p5.Delay = function () {
this.ac = p5sound.audiocontext;
this.input = this.ac.createGain();
this.output = this.ac.createGain();
this._split = this.ac.createChannelSplitter(2);
this._merge = this.ac.createChannelMerger(2);
this._leftGain = this.ac.createGain();
this._rightGain = this.ac.createGain();
/**
* The p5.Delay is built with two
*
* Web Audio Delay Nodes, one for each stereo channel.
*
* @property leftDelay
* @type {Object} Web Audio Delay Node
*/
this.leftDelay = this.ac.createDelay();
/**
* The p5.Delay is built with two
*
* Web Audio Delay Nodes, one for each stereo channel.
*
* @property rightDelay
* @type {Object} Web Audio Delay Node
*/
this.rightDelay = this.ac.createDelay();
this._leftFilter = new p5.Filter();
this._rightFilter = new p5.Filter();
this._leftFilter.disconnect();
this._rightFilter.disconnect();
/**
* Internal filter. Set to lowPass by default, but can be accessed directly.
* See p5.Filter for methods. Or use the p5.Delay.filter() method to change
* frequency and q.
*
* @property lowPass
* @type {p5.Filter}
*/
this.lowPass = this._leftFilter;
this._leftFilter.biquad.frequency.setValueAtTime(1200, this.ac.currentTime);
this._rightFilter.biquad.frequency.setValueAtTime(1200, this.ac.currentTime);
this._leftFilter.biquad.Q.setValueAtTime(0.3, this.ac.currentTime);
this._rightFilter.biquad.Q.setValueAtTime(0.3, this.ac.currentTime);
// graph routing
this.input.connect(this._split);
this.leftDelay.connect(this._leftGain);
this.rightDelay.connect(this._rightGain);
this._leftGain.connect(this._leftFilter.input);
this._rightGain.connect(this._rightFilter.input);
this._merge.connect(this.output);
this.output.connect(p5.soundOut.input);
this._leftFilter.biquad.gain.setValueAtTime(1, this.ac.currentTime);
this._rightFilter.biquad.gain.setValueAtTime(1, this.ac.currentTime);
// default routing
this.setType(0);
this._maxDelay = this.leftDelay.delayTime.maxValue;
};
/**
* Add delay to an audio signal according to a set
* of delay parameters.
*
* @method process
* @param {Object} Signal An object that outputs audio
* @param {Number} [delayTime] Time (in seconds) of the delay/echo.
* Some browsers limit delayTime to
* 1 second.
* @param {Number} [feedback] sends the delay back through itself
* in a loop that decreases in volume
* each time.
* @param {Number} [lowPass] Cutoff frequency. Only frequencies
* below the lowPass will be part of the
* delay.
*/
p5.Delay.prototype.process = function (src, _delayTime, _feedback, _filter) {
var feedback = _feedback || 0;
var delayTime = _delayTime || 0;
if (feedback >= 1) {
throw new Error('Feedback value will force a positive feedback loop.');
}
if (delayTime >= this._maxDelay) {
throw new Error('Delay Time exceeds maximum delay time of ' + this._maxDelay + ' second.');
}
src.connect(this.input);
this.leftDelay.delayTime.setValueAtTime(delayTime, this.ac.currentTime);
this.rightDelay.delayTime.setValueAtTime(delayTime, this.ac.currentTime);
this._leftGain.gain.setValueAtTime(feedback, this.ac.currentTime);
this._rightGain.gain.setValueAtTime(feedback, this.ac.currentTime);
if (_filter) {
this._leftFilter.freq(_filter);
this._rightFilter.freq(_filter);
}
};
/**
* Set the delay (echo) time, in seconds. Usually this value will be
* a floating point number between 0.0 and 1.0.
*
* @method delayTime
* @param {Number} delayTime Time (in seconds) of the delay
*/
p5.Delay.prototype.delayTime = function (t) {
// if t is an audio node...
if (typeof t !== 'number') {
t.connect(this.leftDelay.delayTime);
t.connect(this.rightDelay.delayTime);
} else {
this.leftDelay.delayTime.cancelScheduledValues(this.ac.currentTime);
this.rightDelay.delayTime.cancelScheduledValues(this.ac.currentTime);
this.leftDelay.delayTime.linearRampToValueAtTime(t, this.ac.currentTime);
this.rightDelay.delayTime.linearRampToValueAtTime(t, this.ac.currentTime);
}
};
/**
* Feedback occurs when Delay sends its signal back through its input
* in a loop. The feedback amount determines how much signal to send each
* time through the loop. A feedback greater than 1.0 is not desirable because
* it will increase the overall output each time through the loop,
* creating an infinite feedback loop.
*
* @method feedback
* @param {Number|Object} feedback 0.0 to 1.0, or an object such as an
* Oscillator that can be used to
* modulate this param
*/
p5.Delay.prototype.feedback = function (f) {
// if f is an audio node...
if (typeof f !== 'number') {
f.connect(this._leftGain.gain);
f.connect(this._rightGain.gain);
} else if (f >= 1) {
throw new Error('Feedback value will force a positive feedback loop.');
} else {
this._leftGain.gain.exponentialRampToValueAtTime(f, this.ac.currentTime);
this._rightGain.gain.exponentialRampToValueAtTime(f, this.ac.currentTime);
}
};
/**
* Set a lowpass filter frequency for the delay. A lowpass filter
* will cut off any frequencies higher than the filter frequency.
*
* @method filter
* @param {Number|Object} cutoffFreq A lowpass filter will cut off any
* frequencies higher than the filter frequency.
* @param {Number|Object} res Resonance of the filter frequency
* cutoff, or an object (i.e. a p5.Oscillator)
* that can be used to modulate this parameter.
* High numbers (i.e. 15) will produce a resonance,
* low numbers (i.e. .2) will produce a slope.
*/
p5.Delay.prototype.filter = function (freq, q) {
this._leftFilter.set(freq, q);
this._rightFilter.set(freq, q);
};
/**
* Choose a preset type of delay. 'pingPong' bounces the signal
* from the left to the right channel to produce a stereo effect.
* Any other parameter will revert to the default delay setting.
*
* @method setType
* @param {String|Number} type 'pingPong' (1) or 'default' (0)
*/
p5.Delay.prototype.setType = function (t) {
if (t === 1) {
t = 'pingPong';
}
this._split.disconnect();
this._leftFilter.disconnect();
this._rightFilter.disconnect();
this._split.connect(this.leftDelay, 0);
this._split.connect(this.rightDelay, 1);
switch (t) {
case 'pingPong':
this._rightFilter.setType(this._leftFilter.biquad.type);
this._leftFilter.output.connect(this._merge, 0, 0);
this._rightFilter.output.connect(this._merge, 0, 1);
this._leftFilter.output.connect(this.rightDelay);
this._rightFilter.output.connect(this.leftDelay);
break;
default:
this._leftFilter.output.connect(this._merge, 0, 0);
this._leftFilter.output.connect(this._merge, 0, 1);
this._leftFilter.output.connect(this.leftDelay);
this._leftFilter.output.connect(this.rightDelay);
}
};
/**
* Set the output level of the delay effect.
*
* @method amp
* @param {Number} volume amplitude between 0 and 1.0
* @param {Number} [rampTime] create a fade that lasts rampTime
* @param {Number} [timeFromNow] schedule this event to happen
* seconds from now
*/
p5.Delay.prototype.amp = function (vol, rampTime, tFromNow) {
var rampTime = rampTime || 0;
var tFromNow = tFromNow || 0;
var now = p5sound.audiocontext.currentTime;
var currentVol = this.output.gain.value;
this.output.gain.cancelScheduledValues(now);
this.output.gain.linearRampToValueAtTime(currentVol, now + tFromNow + 0.001);
this.output.gain.linearRampToValueAtTime(vol, now + tFromNow + rampTime + 0.001);
};
/**
* Send output to a p5.sound or web audio object
*
* @method connect
* @param {Object} unit
*/
p5.Delay.prototype.connect = function (unit) {
var u = unit || p5.soundOut.input;
this.output.connect(u);
};
/**
* Disconnect all output.
*
* @method disconnect
*/
p5.Delay.prototype.disconnect = function () {
this.output.disconnect();
};
}(master, filter);
var reverb;
reverb = function () {
'use strict';
var p5sound = master;
/**
* Reverb adds depth to a sound through a large number of decaying
* echoes. It creates the perception that sound is occurring in a
* physical space. The p5.Reverb has paramters for Time (how long does the
* reverb last) and decayRate (how much the sound decays with each echo)
* that can be set with the .set() or .process() methods. The p5.Convolver
* extends p5.Reverb allowing you to recreate the sound of actual physical
* spaces through convolution.
*
* @class p5.Reverb
* @constructor
* @example
*
* var soundFile, reverb;
* function preload() {
* soundFile = loadSound('assets/Damscray_DancingTiger.mp3');
* }
*
* function setup() {
* reverb = new p5.Reverb();
* soundFile.disconnect(); // so we'll only hear reverb...
*
* // connect soundFile to reverb, process w/
* // 3 second reverbTime, decayRate of 2%
* reverb.process(soundFile, 3, 2);
* soundFile.play();
* }
*
*/
p5.Reverb = function () {
this.ac = p5sound.audiocontext;
this.convolverNode = this.ac.createConvolver();
this.input = this.ac.createGain();
this.output = this.ac.createGain();
// otherwise, Safari distorts
this.input.gain.value = 0.5;
this.input.connect(this.convolverNode);
this.convolverNode.connect(this.output);
// default params
this._seconds = 3;
this._decay = 2;
this._reverse = false;
this._buildImpulse();
this.connect();
p5sound.soundArray.push(this);
};
/**
* Connect a source to the reverb, and assign reverb parameters.
*
* @method process
* @param {Object} src p5.sound / Web Audio object with a sound
* output.
* @param {Number} [seconds] Duration of the reverb, in seconds.
* Min: 0, Max: 10. Defaults to 3.
* @param {Number} [decayRate] Percentage of decay with each echo.
* Min: 0, Max: 100. Defaults to 2.
* @param {Boolean} [reverse] Play the reverb backwards or forwards.
*/
p5.Reverb.prototype.process = function (src, seconds, decayRate, reverse) {
src.connect(this.input);
var rebuild = false;
if (seconds) {
this._seconds = seconds;
rebuild = true;
}
if (decayRate) {
this._decay = decayRate;
}
if (reverse) {
this._reverse = reverse;
}
if (rebuild) {
this._buildImpulse();
}
};
/**
* Set the reverb settings. Similar to .process(), but without
* assigning a new input.
*
* @method set
* @param {Number} [seconds] Duration of the reverb, in seconds.
* Min: 0, Max: 10. Defaults to 3.
* @param {Number} [decayRate] Percentage of decay with each echo.
* Min: 0, Max: 100. Defaults to 2.
* @param {Boolean} [reverse] Play the reverb backwards or forwards.
*/
p5.Reverb.prototype.set = function (seconds, decayRate, reverse) {
var rebuild = false;
if (seconds) {
this._seconds = seconds;
rebuild = true;
}
if (decayRate) {
this._decay = decayRate;
}
if (reverse) {
this._reverse = reverse;
}
if (rebuild) {
this._buildImpulse();
}
};
/**
* Set the output level of the delay effect.
*
* @method amp
* @param {Number} volume amplitude between 0 and 1.0
* @param {Number} [rampTime] create a fade that lasts rampTime
* @param {Number} [timeFromNow] schedule this event to happen
* seconds from now
*/
p5.Reverb.prototype.amp = function (vol, rampTime, tFromNow) {
var rampTime = rampTime || 0;
var tFromNow = tFromNow || 0;
var now = p5sound.audiocontext.currentTime;
var currentVol = this.output.gain.value;
this.output.gain.cancelScheduledValues(now);
this.output.gain.linearRampToValueAtTime(currentVol, now + tFromNow + 0.001);
this.output.gain.linearRampToValueAtTime(vol, now + tFromNow + rampTime + 0.001);
};
/**
* Send output to a p5.sound or web audio object
*
* @method connect
* @param {Object} unit
*/
p5.Reverb.prototype.connect = function (unit) {
var u = unit || p5.soundOut.input;
this.output.connect(u.input ? u.input : u);
};
/**
* Disconnect all output.
*
* @method disconnect
*/
p5.Reverb.prototype.disconnect = function () {
this.output.disconnect();
};
/**
* Inspired by Simple Reverb by Jordan Santell
* https://github.com/web-audio-components/simple-reverb/blob/master/index.js
*
* Utility function for building an impulse response
* based on the module parameters.
*
* @private
*/
p5.Reverb.prototype._buildImpulse = function () {
var rate = this.ac.sampleRate;
var length = rate * this._seconds;
var decay = this._decay;
var impulse = this.ac.createBuffer(2, length, rate);
var impulseL = impulse.getChannelData(0);
var impulseR = impulse.getChannelData(1);
var n, i;
for (i = 0; i < length; i++) {
n = this.reverse ? length - i : i;
impulseL[i] = (Math.random() * 2 - 1) * Math.pow(1 - n / length, decay);
impulseR[i] = (Math.random() * 2 - 1) * Math.pow(1 - n / length, decay);
}
this.convolverNode.buffer = impulse;
};
p5.Reverb.prototype.dispose = function () {
if (this.convolverNode) {
this.convolverNode.buffer = null;
this.convolverNode = null;
}
if (typeof this.output !== 'undefined') {
this.output.disconnect();
this.output = null;
}
if (typeof this.panner !== 'undefined') {
this.panner.disconnect();
this.panner = null;
}
};
// =======================================================================
// *** p5.Convolver ***
// =======================================================================
/**
* p5.Convolver extends p5.Reverb. It can emulate the sound of real
* physical spaces through a process called
* convolution.
*
* Convolution multiplies any audio input by an "impulse response"
* to simulate the dispersion of sound over time. The impulse response is
* generated from an audio file that you provide. One way to
* generate an impulse response is to pop a balloon in a reverberant space
* and record the echo. Convolution can also be used to experiment with
* sound.
*
* Use the method createConvolution(path) to instantiate a
* p5.Convolver with a path to your impulse response audio file.
*
* @class p5.Convolver
* @constructor
* @param {String} path path to a sound file
* @param {[Function]} callback function (optional)
* @example
*
* var cVerb, sound;
* function preload() {
* // We have both MP3 and OGG versions of all sound assets
* soundFormats('ogg', 'mp3');
*
* // Try replacing 'bx-spring' with other soundfiles like
* // 'concrete-tunnel' 'small-plate' 'drum' 'beatbox'
* cVerb = createConvolver('assets/bx-spring.mp3');
*
* // Try replacing 'Damscray_DancingTiger' with
* // 'beat', 'doorbell', lucky_dragons_-_power_melody'
* sound = loadSound('assets/Damscray_DancingTiger.mp3');
* }
*
* function setup() {
* // disconnect from master output...
* sound.disconnect();
*
* // ...and process with cVerb
* // so that we only hear the convolution
* cVerb.process(sound);
*
* sound.play();
* }
*
*/
p5.Convolver = function (path, callback) {
this.ac = p5sound.audiocontext;
/**
* Internally, the p5.Convolver uses the a
*
* Web Audio Convolver Node.
*
* @property convolverNode
* @type {Object} Web Audio Convolver Node
*/
this.convolverNode = this.ac.createConvolver();
this.input = this.ac.createGain();
this.output = this.ac.createGain();
// otherwise, Safari distorts
this.input.gain.value = 0.5;
this.input.connect(this.convolverNode);
this.convolverNode.connect(this.output);
if (path) {
this.impulses = [];
this._loadBuffer(path, callback);
} else {
// parameters
this._seconds = 3;
this._decay = 2;
this._reverse = false;
this._buildImpulse();
}
this.connect();
p5sound.soundArray.push(this);
};
p5.Convolver.prototype = Object.create(p5.Reverb.prototype);
p5.prototype.registerPreloadMethod('createConvolver', p5.prototype);
/**
* Create a p5.Convolver. Accepts a path to a soundfile
* that will be used to generate an impulse response.
*
* @method createConvolver
* @param {String} path path to a sound file
* @param {[Function]} callback function (optional)
* @return {p5.Convolver}
* @example
*
* var cVerb, sound;
* function preload() {
* // We have both MP3 and OGG versions of all sound assets
* soundFormats('ogg', 'mp3');
*
* // Try replacing 'bx-spring' with other soundfiles like
* // 'concrete-tunnel' 'small-plate' 'drum' 'beatbox'
* cVerb = createConvolver('assets/bx-spring.mp3');
*
* // Try replacing 'Damscray_DancingTiger' with
* // 'beat', 'doorbell', lucky_dragons_-_power_melody'
* sound = loadSound('assets/Damscray_DancingTiger.mp3');
* }
*
* function setup() {
* // disconnect from master output...
* sound.disconnect();
*
* // ...and process with cVerb
* // so that we only hear the convolution
* cVerb.process(sound);
*
* sound.play();
* }
*
*/
p5.prototype.createConvolver = function (path, callback) {
// if loading locally without a server
if (window.location.origin.indexOf('file://') > -1 && window.cordova === 'undefined') {
alert('This sketch may require a server to load external files. Please see http://bit.ly/1qcInwS');
}
var cReverb = new p5.Convolver(path, callback);
cReverb.impulses = [];
return cReverb;
};
/**
* Private method to load a buffer as an Impulse Response,
* assign it to the convolverNode, and add to the Array of .impulses.
*
* @param {String} path
* @param {Function} callback
* @private
*/
p5.Convolver.prototype._loadBuffer = function (path, callback) {
path = p5.prototype._checkFileFormats(path);
var request = new XMLHttpRequest();
request.open('GET', path, true);
request.responseType = 'arraybuffer';
// decode asyncrohonously
var self = this;
request.onload = function () {
var ac = p5.prototype.getAudioContext();
ac.decodeAudioData(request.response, function (buff) {
var buffer = {};
var chunks = path.split('/');
buffer.name = chunks[chunks.length - 1];
buffer.audioBuffer = buff;
self.impulses.push(buffer);
self.convolverNode.buffer = buffer.audioBuffer;
if (callback) {
callback(buffer);
}
});
};
request.send();
};
p5.Convolver.prototype.set = null;
/**
* Connect a source to the reverb, and assign reverb parameters.
*
* @method process
* @param {Object} src p5.sound / Web Audio object with a sound
* output.
* @example
*
* var cVerb, sound;
* function preload() {
* soundFormats('ogg', 'mp3');
*
* cVerb = createConvolver('assets/concrete-tunnel.mp3');
*
* sound = loadSound('assets/beat.mp3');
* }
*
* function setup() {
* // disconnect from master output...
* sound.disconnect();
*
* // ...and process with (i.e. connect to) cVerb
* // so that we only hear the convolution
* cVerb.process(sound);
*
* sound.play();
* }
*
*/
p5.Convolver.prototype.process = function (src) {
src.connect(this.input);
};
/**
* If you load multiple impulse files using the .addImpulse method,
* they will be stored as Objects in this Array. Toggle between them
* with the toggleImpulse(id) method.
*
* @property impulses
* @type {Array} Array of Web Audio Buffers
*/
p5.Convolver.prototype.impulses = [];
/**
* Load and assign a new Impulse Response to the p5.Convolver.
* The impulse is added to the .impulses array. Previous
* impulses can be accessed with the .toggleImpulse(id)
* method.
*
* @method addImpulse
* @param {String} path path to a sound file
* @param {[Function]} callback function (optional)
*/
p5.Convolver.prototype.addImpulse = function (path, callback) {
// if loading locally without a server
if (window.location.origin.indexOf('file://') > -1 && window.cordova === 'undefined') {
alert('This sketch may require a server to load external files. Please see http://bit.ly/1qcInwS');
}
this._loadBuffer(path, callback);
};
/**
* Similar to .addImpulse, except that the .impulses
* Array is reset to save memory. A new .impulses
* array is created with this impulse as the only item.
*
* @method resetImpulse
* @param {String} path path to a sound file
* @param {[Function]} callback function (optional)
*/
p5.Convolver.prototype.resetImpulse = function (path, callback) {
// if loading locally without a server
if (window.location.origin.indexOf('file://') > -1 && window.cordova === 'undefined') {
alert('This sketch may require a server to load external files. Please see http://bit.ly/1qcInwS');
}
this.impulses = [];
this._loadBuffer(path, callback);
};
/**
* If you have used .addImpulse() to add multiple impulses
* to a p5.Convolver, then you can use this method to toggle between
* the items in the .impulses Array. Accepts a parameter
* to identify which impulse you wish to use, identified either by its
* original filename (String) or by its position in the .impulses
* Array (Number).
* You can access the objects in the .impulses Array directly. Each
* Object has two attributes: an .audioBuffer (type:
* Web Audio
* AudioBuffer) and a .name, a String that corresponds
* with the original filename.
*
* @method toggleImpulse
* @param {String|Number} id Identify the impulse by its original filename
* (String), or by its position in the
* .impulses Array (Number).
*/
p5.Convolver.prototype.toggleImpulse = function (id) {
if (typeof id === 'number' && id < this.impulses.length) {
this.convolverNode.buffer = this.impulses[id].audioBuffer;
}
if (typeof id === 'string') {
for (var i = 0; i < this.impulses.length; i++) {
if (this.impulses[i].name === id) {
this.convolverNode.buffer = this.impulses[i].audioBuffer;
break;
}
}
}
};
p5.Convolver.prototype.dispose = function () {
// remove all the Impulse Response buffers
for (var i in this.impulses) {
this.impulses[i] = null;
}
this.convolverNode.disconnect();
this.concolverNode = null;
if (typeof this.output !== 'undefined') {
this.output.disconnect();
this.output = null;
}
if (typeof this.panner !== 'undefined') {
this.panner.disconnect();
this.panner = null;
}
};
}(master, sndcore);
/** Tone.js module by Yotam Mann, MIT License 2014 http://opensource.org/licenses/MIT **/
var Tone_core_Clock;
Tone_core_Clock = function (Tone) {
'use strict';
Tone.Clock = function (rate, callback) {
this._oscillator = null;
this._jsNode = this.context.createScriptProcessor(this.bufferSize, 1, 1);
this._jsNode.onaudioprocess = this._processBuffer.bind(this);
this._controlSignal = new Tone.Signal(1);
this._upTick = false;
this.tick = this.defaultArg(callback, function () {
});
this._jsNode.noGC();
this.setRate(rate);
};
Tone.extend(Tone.Clock);
Tone.Clock.prototype.setRate = function (rate, rampTime) {
var freqVal = this.secondsToFrequency(this.toSeconds(rate));
if (!rampTime) {
this._controlSignal.cancelScheduledValues(0);
this._controlSignal.setValue(freqVal);
} else {
this._controlSignal.exponentialRampToValueNow(freqVal, rampTime);
}
};
Tone.Clock.prototype.getRate = function () {
return this._controlSignal.getValue();
};
Tone.Clock.prototype.start = function (time) {
this._oscillator = this.context.createOscillator();
this._oscillator.type = 'square';
this._oscillator.connect(this._jsNode);
this._controlSignal.connect(this._oscillator.frequency);
this._upTick = false;
var startTime = this.toSeconds(time);
this._oscillator.start(startTime);
this._oscillator.onended = function () {
};
};
Tone.Clock.prototype.stop = function (time, onend) {
var stopTime = this.toSeconds(time);
this._oscillator.onended = onend;
this._oscillator.stop(stopTime);
};
Tone.Clock.prototype._processBuffer = function (event) {
var now = this.defaultArg(event.playbackTime, this.now());
var bufferSize = this._jsNode.bufferSize;
var incomingBuffer = event.inputBuffer.getChannelData(0);
var upTick = this._upTick;
var self = this;
for (var i = 0; i < bufferSize; i++) {
var sample = incomingBuffer[i];
if (sample > 0 && !upTick) {
upTick = true;
setTimeout(function () {
var tickTime = now + self.samplesToSeconds(i + bufferSize * 2);
return function () {
self.tick(tickTime);
};
}(), 0);
} else if (sample < 0 && upTick) {
upTick = false;
}
}
this._upTick = upTick;
};
Tone.Clock.prototype.dispose = function () {
this._jsNode.disconnect();
this._controlSignal.dispose();
if (this._oscillator) {
this._oscillator.onended();
this._oscillator.disconnect();
}
this._jsNode.onaudioprocess = function () {
};
this._jsNode = null;
this._controlSignal = null;
this._oscillator = null;
};
return Tone.Clock;
}(Tone_core_Tone);
var metro;
metro = function () {
'use strict';
var p5sound = master;
// requires the Tone.js library's Clock (MIT license, Yotam Mann)
// https://github.com/TONEnoTONE/Tone.js/
var Clock = Tone_core_Clock;
var ac = p5sound.audiocontext;
// var upTick = false;
p5.Metro = function () {
this.clock = new Clock(ac.sampleRate, this.ontick.bind(this));
this.syncedParts = [];
this.bpm = 120;
// gets overridden by p5.Part
this._init();
this.tickCallback = function () {
};
};
var prevTick = 0;
var tatumTime = 0;
p5.Metro.prototype.ontick = function (tickTime) {
var elapsedTime = tickTime - prevTick;
var secondsFromNow = tickTime - p5sound.audiocontext.currentTime;
if (elapsedTime - tatumTime <= -0.02) {
return;
} else {
prevTick = tickTime;
// for all of the active things on the metro:
for (var i in this.syncedParts) {
var thisPart = this.syncedParts[i];
thisPart.incrementStep(secondsFromNow);
// each synced source keeps track of its own beat number
for (var j in thisPart.phrases) {
var thisPhrase = thisPart.phrases[j];
var phraseArray = thisPhrase.sequence;
var bNum = this.metroTicks % phraseArray.length;
if (phraseArray[bNum] !== 0 && (this.metroTicks < phraseArray.length || !thisPhrase.looping)) {
thisPhrase.callback(secondsFromNow, phraseArray[bNum]);
}
}
}
this.metroTicks += 1;
this.tickCallback(secondsFromNow);
}
};
p5.Metro.prototype.setBPM = function (bpm, rampTime) {
var beatTime = 60 / (bpm * this.tatums);
tatumTime = beatTime;
var ramp = rampTime || 0;
this.clock.setRate(beatTime, rampTime + p5sound.audiocontext.currentTime);
this.bpm = bpm;
};
p5.Metro.prototype.getBPM = function (tempo) {
return this.clock.getRate() / this.tatums * 60;
};
p5.Metro.prototype._init = function () {
this.metroTicks = 0;
};
// clear existing synced parts, add only this one
p5.Metro.prototype.resetSync = function (part) {
this.syncedParts = [part];
};
// push a new synced part to the array
p5.Metro.prototype.pushSync = function (part) {
this.syncedParts.push(part);
};
p5.Metro.prototype.start = function (time) {
var t = time || 0;
this.clock.start(t);
this.setBPM(this.bpm);
};
p5.Metro.prototype.stop = function (time) {
var t = time || 0;
if (this.clock._oscillator) {
this.clock.stop(t);
}
};
p5.Metro.prototype.beatLength = function (tatums) {
this.tatums = 1 / tatums / 4;
};
}(master, Tone_core_Clock);
var looper;
looper = function () {
'use strict';
var p5sound = master;
var bpm = 120;
/**
* Set the global tempo, in beats per minute, for all
* p5.Parts. This method will impact all active p5.Parts.
*
* @param {Number} BPM Beats Per Minute
* @param {Number} rampTime Seconds from now
*/
p5.prototype.setBPM = function (BPM, rampTime) {
bpm = BPM;
for (var i in p5sound.parts) {
p5sound.parts[i].setBPM(bpm, rampTime);
}
};
/**
* A phrase is a pattern of musical events over time, i.e.
* a series of notes and rests.
*
* Phrases must be added to a p5.Part for playback, and
* each part can play multiple phrases at the same time.
* For example, one Phrase might be a kick drum, another
* could be a snare, and another could be the bassline.
*
* The first parameter is a name so that the phrase can be
* modified or deleted later. The callback is a a function that
* this phrase will call at every step—for example it might be
* called playNote(value){}. The array determines
* which value is passed into the callback at each step of the
* phrase. It can be numbers, an object with multiple numbers,
* or a zero (0) indicates a rest so the callback won't be called).
*
* @class p5.Phrase
* @constructor
* @param {String} name Name so that you can access the Phrase.
* @param {Function} callback The name of a function that this phrase
* will call. Typically it will play a sound,
* and accept two parameters: a time at which
* to play the sound (in seconds from now),
* and a value from the sequence array. The
* time should be passed into the play() or
* start() method to ensure precision.
* @param {Array} sequence Array of values to pass into the callback
* at each step of the phrase.
* @example
*
* var mySound, myPhrase, myPart;
* var pattern = [1,0,0,2,0,2,0,0];
* var msg = 'click to play';
*
* function preload() {
* mySound = loadSound('assets/beatbox.mp3');
* }
*
* function setup() {
* noStroke();
* fill(255);
* textAlign(CENTER);
* masterVolume(0.1);
*
* myPhrase = new p5.Phrase('bbox', makeSound, pattern);
* myPart = new p5.Part();
* myPart.addPhrase(myPhrase);
* myPart.setBPM(60);
* }
*
* function draw() {
* background(0);
* text(msg, width/2, height/2);
* }
*
* function makeSound(time, playbackRate) {
* mySound.rate(playbackRate);
* mySound.play(time);
* }
*
* function mouseClicked() {
* if (mouseX > 0 && mouseX < width && mouseY > 0 && mouseY < height) {
* myPart.start();
* msg = 'playing pattern';
* }
* }
*
*
*/
p5.Phrase = function (name, callback, sequence) {
this.phraseStep = 0;
this.name = name;
this.callback = callback;
/**
* Array of values to pass into the callback
* at each step of the phrase. Depending on the callback
* function's requirements, these values may be numbers,
* strings, or an object with multiple parameters.
* Zero (0) indicates a rest.
*
* @property sequence
* @type {Array}
*/
this.sequence = sequence;
};
/**
* A p5.Part plays back one or more p5.Phrases. Instantiate a part
* with steps and tatums. By default, each step represents 1/16th note.
*
* See p5.Phrase for more about musical timing.
*
* @class p5.Part
* @constructor
* @param {Number} [steps] Steps in the part
* @param {Number} [tatums] Divisions of a beat (default is 1/16, a quarter note)
* @example
*
* var box, drum, myPart;
* var boxPat = [1,0,0,2,0,2,0,0];
* var drumPat = [0,1,1,0,2,0,1,0];
* var msg = 'click to play';
*
* function preload() {
* box = loadSound('assets/beatbox.mp3');
* drum = loadSound('assets/drum.mp3');
* }
*
* function setup() {
* noStroke();
* fill(255);
* textAlign(CENTER);
* masterVolume(0.1);
*
* var boxPhrase = new p5.Phrase('box', playBox, boxPat);
* var drumPhrase = new p5.Phrase('drum', playDrum, drumPat);
* myPart = new p5.Part();
* myPart.addPhrase(boxPhrase);
* myPart.addPhrase(drumPhrase);
* myPart.setBPM(60);
* masterVolume(0.1);
* }
*
* function draw() {
* background(0);
* text(msg, width/2, height/2);
* }
*
* function playBox(time, playbackRate) {
* box.rate(playbackRate);
* box.play(time);
* }
*
* function playDrum(time, playbackRate) {
* drum.rate(playbackRate);
* drum.play(time);
* }
*
* function mouseClicked() {
* if (mouseX > 0 && mouseX < width && mouseY > 0 && mouseY < height) {
* myPart.start();
* msg = 'playing part';
* }
* }
*
*/
p5.Part = function (steps, bLength) {
this.length = steps || 0;
// how many beats
this.partStep = 0;
this.phrases = [];
this.looping = false;
this.isPlaying = false;
// what does this looper do when it gets to the last step?
this.onended = function () {
this.stop();
};
this.tatums = bLength || 0.0625;
// defaults to quarter note
this.metro = new p5.Metro();
this.metro._init();
this.metro.beatLength(this.tatums);
this.metro.setBPM(bpm);
p5sound.parts.push(this);
this.callback = function () {
};
};
/**
* Set the tempo of this part, in Beats Per Minute.
*
* @method setBPM
* @param {Number} BPM Beats Per Minute
* @param {Number} [rampTime] Seconds from now
*/
p5.Part.prototype.setBPM = function (tempo, rampTime) {
this.metro.setBPM(tempo, rampTime);
};
/**
* Returns the Beats Per Minute of this currently part.
*
* @method getBPM
* @return {Number}
*/
p5.Part.prototype.getBPM = function () {
return this.metro.getBPM();
};
/**
* Start playback of this part. It will play
* through all of its phrases at a speed
* determined by setBPM.
*
* @method start
* @param {Number} [time] seconds from now
*/
p5.Part.prototype.start = function (time) {
if (!this.isPlaying) {
this.isPlaying = true;
this.metro.resetSync(this);
var t = time || 0;
this.metro.start(t);
}
};
/**
* Loop playback of this part. It will begin
* looping through all of its phrases at a speed
* determined by setBPM.
*
* @method loop
* @param {Number} [time] seconds from now
*/
p5.Part.prototype.loop = function (time) {
this.looping = true;
// rest onended function
this.onended = function () {
this.partStep = 0;
};
var t = time || 0;
this.start(t);
};
/**
* Tell the part to stop looping.
*
* @method noLoop
*/
p5.Part.prototype.noLoop = function () {
this.looping = false;
// rest onended function
this.onended = function () {
this.stop();
};
};
/**
* Stop the part and cue it to step 0.
*
* @method stop
* @param {Number} [time] seconds from now
*/
p5.Part.prototype.stop = function (time) {
this.partStep = 0;
this.pause(time);
};
/**
* Pause the part. Playback will resume
* from the current step.
*
* @method pause
* @param {Number} time seconds from now
*/
p5.Part.prototype.pause = function (time) {
this.isPlaying = false;
var t = time || 0;
this.metro.stop(t);
};
/**
* Add a p5.Phrase to this Part.
*
* @method addPhrase
* @param {p5.Phrase} phrase reference to a p5.Phrase
*/
p5.Part.prototype.addPhrase = function (name, callback, array) {
var p;
if (arguments.length === 3) {
p = new p5.Phrase(name, callback, array);
} else if (arguments[0] instanceof p5.Phrase) {
p = arguments[0];
} else {
throw 'invalid input. addPhrase accepts name, callback, array or a p5.Phrase';
}
this.phrases.push(p);
// reset the length if phrase is longer than part's existing length
if (p.sequence.length > this.length) {
this.length = p.sequence.length;
}
};
/**
* Remove a phrase from this part, based on the name it was
* given when it was created.
*
* @method removePhrase
* @param {String} phraseName
*/
p5.Part.prototype.removePhrase = function (name) {
for (var i in this.phrases) {
if (this.phrases[i].name === name) {
this.phrases.split(i, 1);
}
}
};
/**
* Get a phrase from this part, based on the name it was
* given when it was created. Now you can modify its array.
*
* @method getPhrase
* @param {String} phraseName
*/
p5.Part.prototype.getPhrase = function (name) {
for (var i in this.phrases) {
if (this.phrases[i].name === name) {
return this.phrases[i];
}
}
};
/**
* Get a phrase from this part, based on the name it was
* given when it was created. Now you can modify its array.
*
* @method replaceSequence
* @param {String} phraseName
* @param {Array} sequence Array of values to pass into the callback
* at each step of the phrase.
*/
p5.Part.prototype.replaceSequence = function (name, array) {
for (var i in this.phrases) {
if (this.phrases[i].name === name) {
this.phrases[i].sequence = array;
}
}
};
p5.Part.prototype.incrementStep = function (time) {
if (this.partStep < this.length - 1) {
this.callback(time);
this.partStep += 1;
} else {
if (this.looping) {
this.callback(time);
}
this.onended();
this.partStep = 0;
}
};
/**
* Fire a callback function at every step.
*
* @method onStep
* @param {Function} callback The name of the callback
* you want to fire
* on every beat/tatum.
*/
p5.Part.prototype.onStep = function (callback) {
this.callback = callback;
};
// ===============
// p5.Score
// ===============
/**
* A Score consists of a series of Parts. The parts will
* be played back in order. For example, you could have an
* A part, a B part, and a C part, and play them back in this order
* new p5.Score(a, a, b, a, c)
*
* @class p5.Score
* @constructor
* @param {p5.Part} part(s) One or multiple parts, to be played in sequence.
* @return {p5.Score}
*/
p5.Score = function () {
// for all of the arguments
this.parts = [];
this.currentPart = 0;
var thisScore = this;
for (var i in arguments) {
this.parts[i] = arguments[i];
this.parts[i].nextPart = this.parts[i + 1];
this.parts[i].onended = function () {
thisScore.resetPart(i);
playNextPart(thisScore);
};
}
this.looping = false;
};
p5.Score.prototype.onended = function () {
if (this.looping) {
// this.resetParts();
this.parts[0].start();
} else {
this.parts[this.parts.length - 1].onended = function () {
this.stop();
this.resetParts();
};
}
this.currentPart = 0;
};
/**
* Start playback of the score.
*
* @method start
*/
p5.Score.prototype.start = function () {
this.parts[this.currentPart].start();
this.scoreStep = 0;
};
/**
* Stop playback of the score.
*
* @method stop
*/
p5.Score.prototype.stop = function () {
this.parts[this.currentPart].stop();
this.currentPart = 0;
this.scoreStep = 0;
};
/**
* Pause playback of the score.
*
* @method pause
*/
p5.Score.prototype.pause = function () {
this.parts[this.currentPart].stop();
};
/**
* Loop playback of the score.
*
* @method loop
*/
p5.Score.prototype.loop = function () {
this.looping = true;
this.start();
};
/**
* Stop looping playback of the score. If it
* is currently playing, this will go into effect
* after the current round of playback completes.
*
* @method noLoop
*/
p5.Score.prototype.noLoop = function () {
this.looping = false;
};
p5.Score.prototype.resetParts = function () {
for (var i in this.parts) {
this.resetPart(i);
}
};
p5.Score.prototype.resetPart = function (i) {
this.parts[i].stop();
this.parts[i].partStep = 0;
for (var p in this.parts[i].phrases) {
this.parts[i].phrases[p].phraseStep = 0;
}
};
/**
* Set the tempo for all parts in the score
*
* @param {Number} BPM Beats Per Minute
* @param {Number} rampTime Seconds from now
*/
p5.Score.prototype.setBPM = function (bpm, rampTime) {
for (var i in this.parts) {
this.parts[i].setBPM(bpm, rampTime);
}
};
function playNextPart(aScore) {
aScore.currentPart++;
if (aScore.currentPart >= aScore.parts.length) {
aScore.scoreStep = 0;
aScore.onended();
} else {
aScore.scoreStep = 0;
aScore.parts[aScore.currentPart - 1].stop();
aScore.parts[aScore.currentPart].start();
}
}
}(master);
var soundRecorder;
soundRecorder = function () {
'use strict';
var p5sound = master;
var ac = p5sound.audiocontext;
/**
* Record sounds for playback and/or to save as a .wav file.
* The p5.SoundRecorder records all sound output from your sketch,
* or can be assigned a specific source with setInput().
* The record() method accepts a p5.SoundFile as a parameter.
* When playback is stopped (either after the given amount of time,
* or with the stop() method), the p5.SoundRecorder will send its
* recording to that p5.SoundFile for playback.
*
* @class p5.SoundRecorder
* @constructor
* @example
*
* var mic, recorder, soundFile;
* var state = 0;
*
* function setup() {
* background(200);
* // create an audio in
* mic = new p5.AudioIn();
*
* // prompts user to enable their browser mic
* mic.start();
*
* // create a sound recorder
* recorder = new p5.SoundRecorder();
*
* // connect the mic to the recorder
* recorder.setInput(mic);
*
* // this sound file will be used to
* // playback & save the recording
* soundFile = new p5.SoundFile();
*
* text('keyPress to record', 20, 20);
* }
*
* function keyPressed() {
* // make sure user enabled the mic
* if (state === 0 && mic.enabled) {
*
* // record to our p5.SoundFile
* recorder.record(soundFile);
*
* background(255,0,0);
* text('Recording!', 20, 20);
* state++;
* }
* else if (state === 1) {
* background(0,255,0);
*
* // stop recorder and
* // send result to soundFile
* recorder.stop();
*
* text('Stopped', 20, 20);
* state++;
* }
*
* else if (state === 2) {
* soundFile.play(); // play the result!
* save(soundFile, 'mySound.wav');
* state++;
* }
* }
*
*/
p5.SoundRecorder = function () {
this.input = ac.createGain();
this.output = ac.createGain();
this.recording = false;
this.bufferSize = 1024;
this._channels = 2;
// stereo (default)
this._clear();
// initialize variables
this._jsNode = ac.createScriptProcessor(this.bufferSize, this._channels, 2);
this._jsNode.onaudioprocess = this._audioprocess.bind(this);
/**
* callback invoked when the recording is over
* @private
* @type {function(Float32Array)}
*/
this._callback = function () {
};
// connections
this._jsNode.connect(p5.soundOut._silentNode);
this.setInput();
// add this p5.SoundFile to the soundArray
p5sound.soundArray.push(this);
};
/**
* Connect a specific device to the p5.SoundRecorder.
* If no parameter is given, p5.SoundRecorer will record
* all audible p5.sound from your sketch.
*
* @method setInput
* @param {Object} [unit] p5.sound object or a web audio unit
* that outputs sound
*/
p5.SoundRecorder.prototype.setInput = function (unit) {
this.input.disconnect();
this.input = null;
this.input = ac.createGain();
this.input.connect(this._jsNode);
this.input.connect(this.output);
if (unit) {
unit.connect(this.input);
} else {
p5.soundOut.output.connect(this.input);
}
};
/**
* Start recording. To access the recording, provide
* a p5.SoundFile as the first parameter. The p5.SoundRecorder
* will send its recording to that p5.SoundFile for playback once
* recording is complete. Optional parameters include duration
* (in seconds) of the recording, and a callback function that
* will be called once the complete recording has been
* transfered to the p5.SoundFile.
*
* @method record
* @param {p5.SoundFile} soundFile p5.SoundFile
* @param {Number} [duration] Time (in seconds)
* @param {Function} [callback] The name of a function that will be
* called once the recording completes
*/
p5.SoundRecorder.prototype.record = function (sFile, duration, callback) {
this.recording = true;
if (duration) {
this.sampleLimit = Math.round(duration * ac.sampleRate);
}
if (sFile && callback) {
this._callback = function () {
this.buffer = this._getBuffer();
sFile.setBuffer(this.buffer);
callback();
};
} else if (sFile) {
this._callback = function () {
this.buffer = this._getBuffer();
sFile.setBuffer(this.buffer);
};
}
};
/**
* Stop the recording. Once the recording is stopped,
* the results will be sent to the p5.SoundFile that
* was given on .record(), and if a callback function
* was provided on record, that function will be called.
*
* @method stop
*/
p5.SoundRecorder.prototype.stop = function () {
this.recording = false;
this._callback();
this._clear();
};
p5.SoundRecorder.prototype._clear = function () {
this._leftBuffers = [];
this._rightBuffers = [];
this.recordedSamples = 0;
this.sampleLimit = null;
};
/**
* internal method called on audio process
*
* @private
* @param {AudioProcessorEvent} event
*/
p5.SoundRecorder.prototype._audioprocess = function (event) {
if (this.recording === false) {
return;
} else if (this.recording === true) {
// if we are past the duration, then stop... else:
if (this.sampleLimit && this.recordedSamples >= this.sampleLimit) {
this.stop();
} else {
// get channel data
var left = event.inputBuffer.getChannelData(0);
var right = event.inputBuffer.getChannelData(1);
// clone the samples
this._leftBuffers.push(new Float32Array(left));
this._rightBuffers.push(new Float32Array(right));
this.recordedSamples += this.bufferSize;
}
}
};
p5.SoundRecorder.prototype._getBuffer = function () {
var buffers = [];
buffers.push(this._mergeBuffers(this._leftBuffers));
buffers.push(this._mergeBuffers(this._rightBuffers));
return buffers;
};
p5.SoundRecorder.prototype._mergeBuffers = function (channelBuffer) {
var result = new Float32Array(this.recordedSamples);
var offset = 0;
var lng = channelBuffer.length;
for (var i = 0; i < lng; i++) {
var buffer = channelBuffer[i];
result.set(buffer, offset);
offset += buffer.length;
}
return result;
};
p5.SoundRecorder.prototype.dispose = function () {
this._clear();
this._callback = function () {
};
if (this.input) {
this.input.disconnect();
}
this.input = null;
this._jsNode = null;
};
/**
* Save a p5.SoundFile as a .wav audio file.
*
* @method saveSound
* @param {p5.SoundFile} soundFile p5.SoundFile that you wish to save
* @param {String} name name of the resulting .wav file.
*/
p5.prototype.saveSound = function (soundFile, name) {
var leftChannel = soundFile.buffer.getChannelData(0);
var rightChannel = soundFile.buffer.getChannelData(1);
var interleaved = interleave(leftChannel, rightChannel);
// create the buffer and view to create the .WAV file
var buffer = new ArrayBuffer(44 + interleaved.length * 2);
var view = new DataView(buffer);
// write the WAV container,
// check spec at: https://ccrma.stanford.edu/courses/422/projects/WaveFormat/
// RIFF chunk descriptor
writeUTFBytes(view, 0, 'RIFF');
view.setUint32(4, 44 + interleaved.length * 2, true);
writeUTFBytes(view, 8, 'WAVE');
// FMT sub-chunk
writeUTFBytes(view, 12, 'fmt ');
view.setUint32(16, 16, true);
view.setUint16(20, 1, true);
// stereo (2 channels)
view.setUint16(22, 2, true);
view.setUint32(24, 44100, true);
view.setUint32(28, 44100 * 4, true);
view.setUint16(32, 4, true);
view.setUint16(34, 16, true);
// data sub-chunk
writeUTFBytes(view, 36, 'data');
view.setUint32(40, interleaved.length * 2, true);
// write the PCM samples
var lng = interleaved.length;
var index = 44;
var volume = 1;
for (var i = 0; i < lng; i++) {
view.setInt16(index, interleaved[i] * (32767 * volume), true);
index += 2;
}
p5.prototype.writeFile([view], name, 'wav');
};
// helper methods to save waves
function interleave(leftChannel, rightChannel) {
var length = leftChannel.length + rightChannel.length;
var result = new Float32Array(length);
var inputIndex = 0;
for (var index = 0; index < length;) {
result[index++] = leftChannel[inputIndex];
result[index++] = rightChannel[inputIndex];
inputIndex++;
}
return result;
}
function writeUTFBytes(view, offset, string) {
var lng = string.length;
for (var i = 0; i < lng; i++) {
view.setUint8(offset + i, string.charCodeAt(i));
}
}
}(sndcore, master);
var peakdetect;
peakdetect = function () {
'use strict';
var p5sound = master;
/**
* PeakDetect works in conjunction with p5.FFT to
* look for onsets in some or all of the frequency spectrum.
*
*
* To use p5.PeakDetect, call update in the draw loop
* and pass in a p5.FFT object.
*
*
* You can listen for a specific part of the frequency spectrum by
* setting the range between freq1 and freq2.
*
*
* threshold is the threshold for detecting a peak,
* scaled between 0 and 1. It is logarithmic, so 0.1 is half as loud
* as 1.0.
*
*
* The update method is meant to be run in the draw loop, and
* frames determines how many loops must pass before
* another peak can be detected.
* For example, if the frameRate() = 60, you could detect the beat of a
* 120 beat-per-minute song with this equation:
* framesPerPeak = 60 / (estimatedBPM / 60 );
*
*
*
* Based on example contribtued by @b2renger, and a simple beat detection
* explanation by Felix Turner.
*
*
* @class p5.PeakDetect
* @constructor
* @param {Number} [freq1] lowFrequency - defaults to 20Hz
* @param {Number} [freq2] highFrequency - defaults to 20000 Hz
* @param {Number} [threshold] Threshold for detecting a beat between 0 and 1
* scaled logarithmically where 0.1 is 1/2 the loudness
* of 1.0. Defaults to 0.35.
* @param {Number} [framesPerPeak] Defaults to 20.
* @example
*
*
* var cnv, soundFile, fft, peakDetect;
* var ellipseWidth = 10;
*
* function setup() {
* background(0);
* noStroke();
* fill(255);
* textAlign(CENTER);
*
* soundFile = loadSound('assets/beat.mp3');
*
* // p5.PeakDetect requires a p5.FFT
* fft = new p5.FFT();
* peakDetect = new p5.PeakDetect();
*
* }
*
* function draw() {
* background(0);
* text('click to play/pause', width/2, height/2);
*
* // peakDetect accepts an fft post-analysis
* fft.analyze();
* peakDetect.update(fft);
*
* if ( peakDetect.isDetected ) {
* ellipseWidth = 50;
* } else {
* ellipseWidth *= 0.95;
* }
*
* ellipse(width/2, height/2, ellipseWidth, ellipseWidth);
* }
*
* // toggle play/stop when canvas is clicked
* function mouseClicked() {
* if (mouseX > 0 && mouseX < width && mouseY > 0 && mouseY < height) {
* if (soundFile.isPlaying() ) {
* soundFile.stop();
* } else {
* soundFile.play();
* }
* }
* }
*
*/
p5.PeakDetect = function (freq1, freq2, threshold, _framesPerPeak) {
var framesPerPeak;
// framesPerPeak determines how often to look for a beat.
// If a beat is provided, try to look for a beat based on bpm
this.framesPerPeak = _framesPerPeak || 20;
this.framesSinceLastPeak = 0;
this.decayRate = 0.95;
this.threshold = threshold || 0.35;
this.cutoff = 0;
// how much to increase the cutoff
// TO DO: document this / figure out how to make it accessible
this.cutoffMult = 1.5;
this.energy = 0;
this.penergy = 0;
// TO DO: document this property / figure out how to make it accessible
this.currentValue = 0;
/**
* isDetected is set to true when a peak is detected.
*
* @attribute isDetected
* @type {Boolean}
* @default false
*/
this.isDetected = false;
this.f1 = freq1 || 40;
this.f2 = freq2 || 20000;
// function to call when a peak is detected
this._onPeak = function () {
};
};
/**
* The update method is run in the draw loop.
*
* Accepts an FFT object. You must call .analyze()
* on the FFT object prior to updating the peakDetect
* because it relies on a completed FFT analysis.
*
* @method update
* @param {p5.FFT} fftObject A p5.FFT object
*/
p5.PeakDetect.prototype.update = function (fftObject) {
var nrg = this.energy = fftObject.getEnergy(this.f1, this.f2) / 255;
if (nrg > this.cutoff && nrg > this.threshold && nrg - this.penergy > 0) {
// trigger callback
this._onPeak();
this.isDetected = true;
// debounce
this.cutoff = nrg * this.cutoffMult;
this.framesSinceLastPeak = 0;
} else {
this.isDetected = false;
if (this.framesSinceLastPeak <= this.framesPerPeak) {
this.framesSinceLastPeak++;
} else {
this.cutoff *= this.decayRate;
this.cutoff = Math.max(this.cutoff, this.threshold);
}
}
this.currentValue = nrg;
this.penergy = nrg;
};
/**
* onPeak accepts two arguments: a function to call when
* a peak is detected. The value of the peak,
* between 0.0 and 1.0, is passed to the callback.
*
* @method onPeak
* @param {Function} callback Name of a function that will
* be called when a peak is
* detected.
* @param {Object} [val] Optional value to pass
* into the function when
* a peak is detected.
* @example
*
* var cnv, soundFile, fft, peakDetect;
* var ellipseWidth = 0;
*
* function setup() {
* cnv = createCanvas(100,100);
* textAlign(CENTER);
*
* soundFile = loadSound('assets/beat.mp3');
* fft = new p5.FFT();
* peakDetect = new p5.PeakDetect();
*
* setupSound();
*
* // when a beat is detected, call triggerBeat()
* peakDetect.onPeak(triggerBeat);
* }
*
* function draw() {
* background(0);
* fill(255);
* text('click to play', width/2, height/2);
*
* fft.analyze();
* peakDetect.update(fft);
*
* ellipseWidth *= 0.95;
* ellipse(width/2, height/2, ellipseWidth, ellipseWidth);
* }
*
* // this function is called by peakDetect.onPeak
* function triggerBeat() {
* ellipseWidth = 50;
* }
*
* // mouseclick starts/stops sound
* function setupSound() {
* cnv.mouseClicked( function() {
* if (soundFile.isPlaying() ) {
* soundFile.stop();
* } else {
* soundFile.play();
* }
* });
* }
*
*/
p5.PeakDetect.prototype.onPeak = function (callback, val) {
var self = this;
self._onPeak = function () {
callback(self.energy, val);
};
};
}(master);
var gain;
gain = function () {
'use strict';
var p5sound = master;
/**
* A gain node is usefull to set the relative volume of sound.
* It's typically used to build mixers.
*
* @class p5.Gain
* @constructor
* @example
*
*
* // load two soundfile and crossfade beetween them
* var sound1,sound2;
* var gain1, gain2, gain3;
*
* function preload(){
* soundFormats('ogg', 'mp3');
* sound1 = loadSound('../_files/Damscray_-_Dancing_Tiger_01');
* sound2 = loadSound('../_files/beat.mp3');
* }
*
* function setup() {
* createCanvas(400,200);
*
* // create a 'master' gain to which we will connect both soundfiles
* gain3 = new p5.Gain();
* gain3.connect();
*
* // setup first sound for playing
* sound1.rate(1);
* sound1.loop();
* sound1.disconnect(); // diconnect from p5 output
*
* gain1 = new p5.Gain(); // setup a gain node
* gain1.setInput(sound1); // connect the first sound to its input
* gain1.connect(gain3); // connect its output to the 'master'
*
* sound2.rate(1);
* sound2.disconnect();
* sound2.loop();
*
* gain2 = new p5.Gain();
* gain2.setInput(sound2);
* gain2.connect(gain3);
*
* }
*
* function draw(){
* background(180);
*
* // calculate the horizontal distance beetween the mouse and the right of the screen
* var d = dist(mouseX,0,width,0);
*
* // map the horizontal position of the mouse to values useable for volume control of sound1
* var vol1 = map(mouseX,0,width,0,1);
* var vol2 = 1-vol1; // when sound1 is loud, sound2 is quiet and vice versa
*
* gain1.amp(vol1,0.5,0);
* gain2.amp(vol2,0.5,0);
*
* // map the vertical position of the mouse to values useable for 'master volume control'
* var vol3 = map(mouseY,0,height,0,1);
* gain3.amp(vol3,0.5,0);
* }
*
*
*/
p5.Gain = function () {
this.ac = p5sound.audiocontext;
this.input = this.ac.createGain();
this.output = this.ac.createGain();
// otherwise, Safari distorts
this.input.gain.value = 0.5;
this.input.connect(this.output);
};
/**
* Connect a source to the gain node.
*
* @method setInput
* @param {Object} src p5.sound / Web Audio object with a sound
* output.
*/
p5.Gain.prototype.setInput = function (src) {
src.connect(this.input);
};
/**
* Send output to a p5.sound or web audio object
*
* @method connect
* @param {Object} unit
*/
p5.Gain.prototype.connect = function (unit) {
var u = unit || p5.soundOut.input;
this.output.connect(u.input ? u.input : u);
};
/**
* Disconnect all output.
*
* @method disconnect
*/
p5.Gain.prototype.disconnect = function () {
this.output.disconnect();
};
/**
* Set the output level of the gain node.
*
* @method amp
* @param {Number} volume amplitude between 0 and 1.0
* @param {Number} [rampTime] create a fade that lasts rampTime
* @param {Number} [timeFromNow] schedule this event to happen
* seconds from now
*/
p5.Gain.prototype.amp = function (vol, rampTime, tFromNow) {
var rampTime = rampTime || 0;
var tFromNow = tFromNow || 0;
var now = p5sound.audiocontext.currentTime;
var currentVol = this.output.gain.value;
this.output.gain.cancelScheduledValues(now);
this.output.gain.linearRampToValueAtTime(currentVol, now + tFromNow);
this.output.gain.linearRampToValueAtTime(vol, now + tFromNow + rampTime);
};
}(master, sndcore);
var src_app;
src_app = function () {
'use strict';
var p5SOUND = sndcore;
return p5SOUND;
}(sndcore, master, helpers, panner, soundfile, amplitude, fft, signal, oscillator, env, pulse, noise, audioin, filter, delay, reverb, metro, looper, soundRecorder, peakdetect, gain);
}));
================================================
FILE: p5-ableton/libraries/socket.io.js
================================================
/*!
* socket.io-node
* Copyright(c) 2011 LearnBoost
* MIT Licensed
*/
/**
* Module dependencies.
*/
var client = require('socket.io-client');
/**
* Version.
*/
exports.version = '0.9.16';
/**
* Supported protocol version.
*/
exports.protocol = 1;
/**
* Client that we serve.
*/
exports.clientVersion = client.version;
/**
* Attaches a manager
*
* @param {HTTPServer/Number} a HTTP/S server or a port number to listen on.
* @param {Object} opts to be passed to Manager and/or http server
* @param {Function} callback if a port is supplied
* @api public
*/
exports.listen = function (server, options, fn) {
if ('function' == typeof server) {
console.warn('Socket.IO\'s `listen()` method expects an `http.Server` instance\n'
+ 'as its first parameter. Are you migrating from Express 2.x to 3.x?\n'
+ 'If so, check out the "Socket.IO compatibility" section at:\n'
+ 'https://github.com/visionmedia/express/wiki/Migrating-from-2.x-to-3.x');
}
if ('function' == typeof options) {
fn = options;
options = {};
}
if ('undefined' == typeof server) {
// create a server that listens on port 80
server = 80;
}
if ('number' == typeof server) {
// if a port number is passed
var port = server;
if (options && options.key)
server = require('https').createServer(options);
else
server = require('http').createServer();
// default response
server.on('request', function (req, res) {
res.writeHead(200);
res.end('Welcome to socket.io.');
});
server.listen(port, fn);
}
// otherwise assume a http/s server
return new exports.Manager(server, options);
};
/**
* Manager constructor.
*
* @api public
*/
exports.Manager = require('./manager');
/**
* Transport constructor.
*
* @api public
*/
exports.Transport = require('./transport');
/**
* Socket constructor.
*
* @api public
*/
exports.Socket = require('./socket');
/**
* Static constructor.
*
* @api public
*/
exports.Static = require('./static');
/**
* Store constructor.
*
* @api public
*/
exports.Store = require('./store');
/**
* Memory Store constructor.
*
* @api public
*/
exports.MemoryStore = require('./stores/memory');
/**
* Redis Store constructor.
*
* @api public
*/
exports.RedisStore = require('./stores/redis');
/**
* Parser.
*
* @api public
*/
exports.parser = require('./parser');
================================================
FILE: p5-ableton/sketch.js
================================================
/*
This sketch will move a ball on our p5 canvas and
send an OSC message each time the ball hits a wall.
- Billy Bennett, ITP NYU, May 2021
*/
// we need a handle to the socket to send our osc values
var socket;
var isConnected;
// set variables for ball location and speed
var x, y, xSpeed, ySpeed;
function setup() {
createCanvas(800, 600);
setupOsc(8000, 12000);
x = random(width);
y = random(height);
xSpeed = 5;
ySpeed = 6;
}
function draw() {
background(0);
// ball is an ellipse
ellipse(x, y, 100, 100);
// move the ball location
x+=xSpeed;
y+=ySpeed;
// check if the ball hits a wall and send a message if so
if (x > width || x < 0) {
// reverse the direction
xSpeed *= -1
sendOsc('message', 1);
} else if (y > height || y < 0) {
// reverse the direction
ySpeed *= -1
sendOsc('message', 1);
}
}
function receiveOsc(address, value) {
console.log("received OSC: " + address + ", " + value);
}
function sendOsc(address, value) {
if (isConnected) {
socket.emit('message', [address, value]);
}
}
function setupOsc(oscPortIn, oscPortOut) {
socket = io.connect('http://127.0.0.1:8081', { port: 8081, rememberTransport: false });
socket.on('connect', function() {
socket.emit('config', {
server: { port: oscPortIn, host: '127.0.0.1'},
client: { port: oscPortOut, host: '127.0.0.1'}
});
});
socket.on('connect', function() {
isConnected = true;
});
socket.on('message', function(msg) {
if (msg[0] == '#bundle') {
for (var i=2; i