http://p5jsbrush.yannest.com/ --> source code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<script language="javascript" type="text/javascript" src="./js/p5.js"></script> | |
<!-- uncomment lines below to include extra p5 libraries --> | |
<!--<script language="javascript" src="../addons/p5.dom.js"></script>--> | |
<!--<script language="javascript" src="../addons/p5.sound.js"></script>--> | |
<script language="javascript" type="text/javascript" src="./js/sketch.js"></script> | |
<!-- this line removes any default padding and style. you might only need one of these values set. --> | |
<style> body {padding: 0; margin: 0; background-color: black;} </style> | |
</head> | |
<body> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! p5.js v0.4.3 March 30, 2015 */ | |
(function (root, factory) { | |
if (typeof define === 'function' && define.amd) | |
define('p5', [], function () { return (root.returnExportsGlobal = factory());}); | |
else if (typeof exports === 'object') | |
module.exports = factory(); | |
else | |
root['p5'] = factory(); | |
}(this, function () { | |
var amdclean = {}; | |
amdclean['shim'] = function (require) { | |
window.requestDraw = function () { | |
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function (callback, element) { | |
window.setTimeout(callback, 1000 / 60); | |
}; | |
}(); | |
}({}); | |
amdclean['constants'] = function (require) { | |
var PI = Math.PI; | |
return { | |
ARROW: 'default', | |
CROSS: 'crosshair', | |
HAND: 'pointer', | |
MOVE: 'move', | |
TEXT: 'text', | |
WAIT: 'wait', | |
HALF_PI: PI / 2, | |
PI: PI, | |
QUARTER_PI: PI / 4, | |
TAU: PI * 2, | |
TWO_PI: PI * 2, | |
DEGREES: 'degrees', | |
RADIANS: 'radians', | |
CORNER: 'corner', | |
CORNERS: 'corners', | |
RADIUS: 'radius', | |
RIGHT: 'right', | |
LEFT: 'left', | |
CENTER: 'center', | |
TOP: 'top', | |
BOTTOM: 'bottom', | |
BASELINE: 'alphabetic', | |
POINTS: 'points', | |
LINES: 'lines', | |
TRIANGLES: 'triangles', | |
TRIANGLE_FAN: 'triangles_fan', | |
TRIANGLE_STRIP: 'triangles_strip', | |
QUADS: 'quads', | |
QUAD_STRIP: 'quad_strip', | |
CLOSE: 'close', | |
OPEN: 'open', | |
CHORD: 'chord', | |
PIE: 'pie', | |
PROJECT: 'square', | |
SQUARE: 'butt', | |
ROUND: 'round', | |
BEVEL: 'bevel', | |
MITER: 'miter', | |
RGB: 'rgb', | |
HSB: 'hsb', | |
AUTO: 'auto', | |
ALT: 18, | |
BACKSPACE: 8, | |
CONTROL: 17, | |
DELETE: 46, | |
DOWN_ARROW: 40, | |
ENTER: 13, | |
ESCAPE: 27, | |
LEFT_ARROW: 37, | |
OPTION: 18, | |
RETURN: 13, | |
RIGHT_ARROW: 39, | |
SHIFT: 16, | |
TAB: 9, | |
UP_ARROW: 38, | |
BLEND: 'normal', | |
ADD: 'lighter', | |
DARKEST: 'darken', | |
LIGHTEST: 'lighten', | |
DIFFERENCE: 'difference', | |
EXCLUSION: 'exclusion', | |
MULTIPLY: 'multiply', | |
SCREEN: 'screen', | |
REPLACE: 'source-over', | |
OVERLAY: 'overlay', | |
HARD_LIGHT: 'hard-light', | |
SOFT_LIGHT: 'soft-light', | |
DODGE: 'color-dodge', | |
BURN: 'color-burn', | |
THRESHOLD: 'threshold', | |
GRAY: 'gray', | |
OPAQUE: 'opaque', | |
INVERT: 'invert', | |
POSTERIZE: 'posterize', | |
DILATE: 'dilate', | |
ERODE: 'erode', | |
BLUR: 'blur', | |
NORMAL: 'normal', | |
ITALIC: 'italic', | |
BOLD: 'bold', | |
LINEAR: 'linear', | |
QUADRATIC: 'quadratic', | |
BEZIER: 'bezier', | |
CURVE: 'curve' | |
}; | |
}({}); | |
amdclean['core'] = function (require, shim, constants) { | |
'use strict'; | |
var constants = constants; | |
var p5 = function (sketch, node, sync) { | |
if (arguments.length === 2 && typeof node === 'boolean') { | |
sync = node; | |
node = undefined; | |
} | |
this._setupDone = false; | |
this._pixelDensity = window.devicePixelRatio || 1; | |
this._startTime = new Date().getTime(); | |
this._userNode = node; | |
this._curElement = null; | |
this._elements = []; | |
this._preloadCount = 0; | |
this._updateInterval = 0; | |
this._isGlobal = false; | |
this._loop = true; | |
this._styles = []; | |
this._defaultCanvasSize = { | |
width: 100, | |
height: 100 | |
}; | |
this._events = { | |
'mousemove': null, | |
'mousedown': null, | |
'mouseup': null, | |
'click': null, | |
'mousewheel': null, | |
'mouseover': null, | |
'mouseout': null, | |
'keydown': null, | |
'keyup': null, | |
'keypress': null, | |
'touchstart': null, | |
'touchmove': null, | |
'touchend': null, | |
'resize': null, | |
'blur': null | |
}; | |
if (window.DeviceOrientationEvent) { | |
this._events.deviceorientation = null; | |
} else if (window.DeviceMotionEvent) { | |
this._events.devicemotion = null; | |
} else { | |
this._events.MozOrientation = null; | |
} | |
this._loadingScreenId = 'p5_loading'; | |
this._start = function () { | |
if (this._userNode) { | |
if (typeof this._userNode === 'string') { | |
this._userNode = document.getElementById(this._userNode); | |
} | |
} | |
this._loadingScreen = document.getElementById(this._loadingScreenId); | |
if (!this._loadingScreen) { | |
this._loadingScreen = document.createElement('loadingDiv'); | |
this._loadingScreen.innerHTML = 'loading...'; | |
this._loadingScreen.style.position = 'absolute'; | |
var node = this._userNode || document.body; | |
node.appendChild(this._loadingScreen); | |
} | |
this.createCanvas(this._defaultCanvasSize.width, this._defaultCanvasSize.height, true); | |
var userPreload = this.preload || window.preload; | |
var context = this._isGlobal ? window : this; | |
if (userPreload) { | |
this._preloadMethods.forEach(function (f) { | |
context[f] = function () { | |
var argsArray = Array.prototype.slice.call(arguments); | |
return context._preload(f, argsArray); | |
}; | |
}); | |
userPreload(); | |
if (this._preloadCount === 0) { | |
this._setup(); | |
this._runFrames(); | |
this._draw(); | |
} | |
} else { | |
this._setup(); | |
this._runFrames(); | |
this._draw(); | |
} | |
}.bind(this); | |
this._preload = function (func, args) { | |
var context = this._isGlobal ? window : this; | |
context._setProperty('_preloadCount', context._preloadCount + 1); | |
var preloadCallback = function (resp) { | |
context._setProperty('_preloadCount', context._preloadCount - 1); | |
if (context._preloadCount === 0) { | |
context._setup(); | |
context._runFrames(); | |
context._draw(); | |
} | |
}; | |
args.push(preloadCallback); | |
return p5.prototype[func].apply(context, args); | |
}.bind(this); | |
this._setup = function () { | |
var context = this._isGlobal ? window : this; | |
if (typeof context.preload === 'function') { | |
this._preloadMethods.forEach(function (f) { | |
context[f] = p5.prototype[f]; | |
}); | |
} | |
if (typeof context.setup === 'function') { | |
context.setup(); | |
} | |
this.canvas.style.visibility = ''; | |
this.canvas.className = this.canvas.className.replace('p5_hidden', ''); | |
this._setupDone = true; | |
this._loadingScreen.parentNode.removeChild(this._loadingScreen); | |
}.bind(this); | |
this._draw = function () { | |
var now = new Date().getTime(); | |
this._frameRate = 1000 / (now - this._lastFrameTime); | |
this._lastFrameTime = now; | |
this._setProperty('frameCount', this.frameCount + 1); | |
if (this._loop) { | |
if (this._drawInterval) { | |
clearInterval(this._drawInterval); | |
} | |
this._drawInterval = setTimeout(function () { | |
window.requestDraw(this._draw.bind(this)); | |
}.bind(this), 1000 / this._targetFrameRate); | |
} | |
this.redraw(); | |
this._updatePAccelerations(); | |
this._updatePMouseCoords(); | |
this._updatePTouchCoords(); | |
}.bind(this); | |
this._runFrames = function () { | |
if (this._updateInterval) { | |
clearInterval(this._updateInterval); | |
} | |
}.bind(this); | |
this._setProperty = function (prop, value) { | |
this[prop] = value; | |
if (this._isGlobal) { | |
window[prop] = value; | |
} | |
}.bind(this); | |
this.remove = function () { | |
if (this._curElement) { | |
this._loop = false; | |
if (this._drawInterval) { | |
clearTimeout(this._drawInterval); | |
} | |
if (this._updateInterval) { | |
clearTimeout(this._updateInterval); | |
} | |
for (var ev in this._events) { | |
window.removeEventListener(ev, this._events[ev]); | |
} | |
for (var i = 0; i < this._elements.length; i++) { | |
var e = this._elements[i]; | |
if (e.elt.parentNode) { | |
e.elt.parentNode.removeChild(e.elt); | |
} | |
for (var elt_ev in e._events) { | |
e.elt.removeEventListener(elt_ev, e._events[elt_ev]); | |
} | |
} | |
var self = this; | |
this._registeredMethods.remove.forEach(function (f) { | |
if (typeof f !== 'undefined') { | |
f.call(self); | |
} | |
}); | |
if (this._isGlobal) { | |
for (var p in p5.prototype) { | |
try { | |
delete window[p]; | |
} catch (x) { | |
window[p] = undefined; | |
} | |
} | |
for (var p2 in this) { | |
if (this.hasOwnProperty(p2)) { | |
try { | |
delete window[p2]; | |
} catch (x) { | |
window[p2] = undefined; | |
} | |
} | |
} | |
} | |
} | |
}.bind(this); | |
for (var k in constants) { | |
p5.prototype[k] = constants[k]; | |
} | |
if (!sketch) { | |
this._isGlobal = true; | |
for (var p in p5.prototype) { | |
if (typeof p5.prototype[p] === 'function') { | |
var ev = p.substring(2); | |
if (!this._events.hasOwnProperty(ev)) { | |
window[p] = p5.prototype[p].bind(this); | |
} | |
} else { | |
window[p] = p5.prototype[p]; | |
} | |
} | |
for (var p2 in this) { | |
if (this.hasOwnProperty(p2)) { | |
window[p2] = this[p2]; | |
} | |
} | |
} else { | |
sketch(this); | |
} | |
for (var e in this._events) { | |
var f = this['_on' + e]; | |
if (f) { | |
var m = f.bind(this); | |
window.addEventListener(e, m); | |
this._events[e] = m; | |
} | |
} | |
var self = this; | |
window.addEventListener('focus', function () { | |
self._setProperty('focused', true); | |
}); | |
window.addEventListener('blur', function () { | |
self._setProperty('focused', false); | |
}); | |
if (sync) { | |
this._start(); | |
} else { | |
if (document.readyState === 'complete') { | |
this._start(); | |
} else { | |
window.addEventListener('load', this._start.bind(this), false); | |
} | |
} | |
}; | |
p5.prototype._preloadMethods = [ | |
'loadJSON', | |
'loadImage', | |
'loadStrings', | |
'loadXML', | |
'loadShape', | |
'loadTable' | |
]; | |
p5.prototype._registeredMethods = { | |
pre: [], | |
post: [], | |
remove: [] | |
}; | |
p5.prototype.registerPreloadMethod = function (m) { | |
p5.prototype._preloadMethods.push(m); | |
}.bind(this); | |
p5.prototype.registerMethod = function (name, m) { | |
if (!p5.prototype._registeredMethods.hasOwnProperty(name)) { | |
p5.prototype._registeredMethods[name] = []; | |
} | |
p5.prototype._registeredMethods[name].push(m); | |
}.bind(this); | |
return p5; | |
}({}, amdclean['shim'], amdclean['constants']); | |
amdclean['utilscolor_utils'] = function (require, core) { | |
var p5 = core; | |
p5.ColorUtils = {}; | |
p5.ColorUtils.hsbaToRGBA = function (hsba) { | |
var h = hsba[0]; | |
var s = hsba[1]; | |
var v = hsba[2]; | |
h /= 255; | |
s /= 255; | |
v /= 255; | |
var RGBA = []; | |
if (s === 0) { | |
RGBA = [ | |
Math.round(v * 255), | |
Math.round(v * 255), | |
Math.round(v * 255), | |
hsba[3] | |
]; | |
} else { | |
var var_h = h * 6; | |
if (var_h === 6) { | |
var_h = 0; | |
} | |
var var_i = Math.floor(var_h); | |
var var_1 = v * (1 - s); | |
var var_2 = v * (1 - s * (var_h - var_i)); | |
var var_3 = v * (1 - s * (1 - (var_h - var_i))); | |
var var_r; | |
var var_g; | |
var var_b; | |
if (var_i === 0) { | |
var_r = v; | |
var_g = var_3; | |
var_b = var_1; | |
} else if (var_i === 1) { | |
var_r = var_2; | |
var_g = v; | |
var_b = var_1; | |
} else if (var_i === 2) { | |
var_r = var_1; | |
var_g = v; | |
var_b = var_3; | |
} else if (var_i === 3) { | |
var_r = var_1; | |
var_g = var_2; | |
var_b = v; | |
} else if (var_i === 4) { | |
var_r = var_3; | |
var_g = var_1; | |
var_b = v; | |
} else { | |
var_r = v; | |
var_g = var_1; | |
var_b = var_2; | |
} | |
RGBA = [ | |
Math.round(var_r * 255), | |
Math.round(var_g * 255), | |
Math.round(var_b * 255), | |
hsba[3] | |
]; | |
} | |
return RGBA; | |
}; | |
p5.ColorUtils.rgbaToHSBA = function (rgba) { | |
var var_R = rgba[0] / 255; | |
var var_G = rgba[1] / 255; | |
var var_B = rgba[2] / 255; | |
var var_Min = Math.min(var_R, var_G, var_B); | |
var var_Max = Math.max(var_R, var_G, var_B); | |
var del_Max = var_Max - var_Min; | |
var H; | |
var S; | |
var V = var_Max; | |
if (del_Max === 0) { | |
H = 0; | |
S = 0; | |
} else { | |
S = del_Max / var_Max; | |
var del_R = ((var_Max - var_R) / 6 + del_Max / 2) / del_Max; | |
var del_G = ((var_Max - var_G) / 6 + del_Max / 2) / del_Max; | |
var del_B = ((var_Max - var_B) / 6 + del_Max / 2) / del_Max; | |
if (var_R === var_Max) { | |
H = del_B - del_G; | |
} else if (var_G === var_Max) { | |
H = 1 / 3 + del_R - del_B; | |
} else if (var_B === var_Max) { | |
H = 2 / 3 + del_G - del_R; | |
} | |
if (H < 0) { | |
H += 1; | |
} | |
if (H > 1) { | |
H -= 1; | |
} | |
} | |
return [ | |
Math.round(H * 255), | |
Math.round(S * 255), | |
Math.round(V * 255), | |
rgba[3] | |
]; | |
}; | |
return p5.ColorUtils; | |
}({}, amdclean['core']); | |
amdclean['p5Color'] = function (require, core, utilscolor_utils, constants) { | |
var p5 = core; | |
var color_utils = utilscolor_utils; | |
var constants = constants; | |
p5.Color = function (pInst, vals) { | |
this.color_array = p5.Color._getFormattedColor.apply(pInst, vals); | |
this._normalizeColorArray(pInst); | |
if (pInst._colorMode === constants.HSB) { | |
this.hsba = this.color_array; | |
this.rgba = color_utils.hsbaToRGBA(this.hsba); | |
} else { | |
this.rgba = this.color_array; | |
this.hsba = color_utils.rgbaToHSBA(this.rgba); | |
} | |
return this; | |
}; | |
p5.Color.prototype._normalizeColorArray = function (pInst) { | |
var isRGB = pInst._colorMode === constants.RGB; | |
var maxArr = isRGB ? pInst._maxRGB : pInst._maxHSB; | |
var arr = this.color_array; | |
arr[0] *= 255 / maxArr[0]; | |
arr[1] *= 255 / maxArr[1]; | |
arr[2] *= 255 / maxArr[2]; | |
arr[3] *= 255 / maxArr[3]; | |
return arr; | |
}; | |
p5.Color.prototype.getHue = function () { | |
return this.hsba[0]; | |
}; | |
p5.Color.prototype.getSaturation = function () { | |
return this.hsba[1]; | |
}; | |
p5.Color.prototype.getBrightness = function () { | |
return this.hsba[2]; | |
}; | |
p5.Color.prototype.getRed = function () { | |
return this.rgba[0]; | |
}; | |
p5.Color.prototype.getGreen = function () { | |
return this.rgba[1]; | |
}; | |
p5.Color.prototype.getBlue = function () { | |
return this.rgba[2]; | |
}; | |
p5.Color.prototype.getAlpha = function () { | |
return this.rgba[3]; | |
}; | |
p5.Color.prototype.toString = function () { | |
var a = this.rgba; | |
for (var i = 0; i < 3; i++) { | |
a[i] = Math.floor(a[i]); | |
} | |
var alpha = typeof a[3] !== 'undefined' ? a[3] / 255 : 1; | |
return 'rgba(' + a[0] + ',' + a[1] + ',' + a[2] + ',' + alpha + ')'; | |
}; | |
p5.Color._getFormattedColor = function () { | |
var r, g, b, a; | |
if (arguments.length >= 3) { | |
r = arguments[0]; | |
g = arguments[1]; | |
b = arguments[2]; | |
a = typeof arguments[3] === 'number' ? arguments[3] : 255; | |
} else { | |
if (this._colorMode === constants.RGB) { | |
r = g = b = arguments[0]; | |
} else { | |
r = b = arguments[0]; | |
g = 0; | |
} | |
a = typeof arguments[1] === 'number' ? arguments[1] : 255; | |
} | |
return [ | |
r, | |
g, | |
b, | |
a | |
]; | |
}; | |
return p5.Color; | |
}({}, amdclean['core'], amdclean['utilscolor_utils'], amdclean['constants']); | |
amdclean['p5Element'] = function (require, core) { | |
var p5 = core; | |
p5.Element = function (elt, pInst) { | |
this.elt = elt; | |
this._pInst = pInst; | |
this._events = {}; | |
this.width = this.elt.offsetWidth; | |
this.height = this.elt.offsetHeight; | |
}; | |
p5.Element.prototype.parent = function (p) { | |
if (typeof p === 'string') { | |
p = document.getElementById(p); | |
} else if (p instanceof p5.Element) { | |
p = p.elt; | |
} | |
p.appendChild(this.elt); | |
return this; | |
}; | |
p5.Element.prototype.id = function (id) { | |
this.elt.id = id; | |
return this; | |
}; | |
p5.Element.prototype.class = function (c) { | |
this.elt.className += ' ' + c; | |
return this; | |
}; | |
p5.Element.prototype.mousePressed = function (fxn) { | |
attachListener('mousedown', fxn, this); | |
attachListener('touchstart', fxn, this); | |
return this; | |
}; | |
p5.Element.prototype.mouseWheel = function (fxn) { | |
attachListener('mousewheel', fxn, this); | |
return this; | |
}; | |
p5.Element.prototype.mouseReleased = function (fxn) { | |
attachListener('mouseup', fxn, this); | |
attachListener('touchend', fxn, this); | |
return this; | |
}; | |
p5.Element.prototype.mouseClicked = function (fxn) { | |
attachListener('click', fxn, this); | |
return this; | |
}; | |
p5.Element.prototype.mouseMoved = function (fxn) { | |
attachListener('mousemove', fxn, this); | |
attachListener('touchmove', fxn, this); | |
return this; | |
}; | |
p5.Element.prototype.mouseOver = function (fxn) { | |
attachListener('mouseover', fxn, this); | |
return this; | |
}; | |
p5.Element.prototype.mouseOut = function (fxn) { | |
attachListener('mouseout', fxn, this); | |
return this; | |
}; | |
p5.Element.prototype.touchStarted = function (fxn) { | |
attachListener('touchstart', fxn, this); | |
attachListener('mousedown', fxn, this); | |
return this; | |
}; | |
p5.Element.prototype.touchMoved = function (fxn) { | |
attachListener('touchmove', fxn, this); | |
attachListener('mousemove', fxn, this); | |
return this; | |
}; | |
p5.Element.prototype.touchEnded = function (fxn) { | |
attachListener('touchend', fxn, this); | |
attachListener('mouseup', fxn, this); | |
return this; | |
}; | |
p5.Element.prototype.dragOver = function (fxn) { | |
attachListener('dragover', fxn, this); | |
return this; | |
}; | |
p5.Element.prototype.dragLeave = function (fxn) { | |
attachListener('dragleave', fxn, this); | |
return this; | |
}; | |
p5.Element.prototype.drop = function (callback, fxn) { | |
function makeLoader(theFile) { | |
var p5file = new p5.File(theFile); | |
return function (e) { | |
p5file.data = e.target.result; | |
callback(p5file); | |
}; | |
} | |
if (window.File && window.FileReader && window.FileList && window.Blob) { | |
attachListener('dragover', function (evt) { | |
evt.stopPropagation(); | |
evt.preventDefault(); | |
}, this); | |
attachListener('dragleave', function (evt) { | |
evt.stopPropagation(); | |
evt.preventDefault(); | |
}, this); | |
if (arguments.length > 1) { | |
attachListener('drop', fxn, this); | |
} | |
attachListener('drop', function (evt) { | |
evt.stopPropagation(); | |
evt.preventDefault(); | |
var files = evt.dataTransfer.files; | |
for (var i = 0; i < files.length; i++) { | |
var f = files[i]; | |
var reader = new FileReader(); | |
reader.onload = makeLoader(f); | |
if (f.type === 'text') { | |
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) { | |
var f = fxn.bind(ctx); | |
ctx.elt.addEventListener(ev, f, false); | |
ctx._events[ev] = f; | |
} | |
p5.Element.prototype._setProperty = function (prop, value) { | |
this[prop] = value; | |
}; | |
return p5.Element; | |
}({}, amdclean['core']); | |
amdclean['p5Graphics'] = function (require, core, constants) { | |
var p5 = core; | |
var constants = constants; | |
p5.Graphics = function (elt, pInst, isMainCanvas) { | |
p5.Element.call(this, elt, pInst); | |
this.canvas = elt; | |
this.drawingContext = this.canvas.getContext('2d'); | |
this._pInst = pInst; | |
if (isMainCanvas) { | |
this._isMainCanvas = true; | |
this._pInst._setProperty('_curElement', this); | |
this._pInst._setProperty('canvas', this.canvas); | |
this._pInst._setProperty('drawingContext', this.drawingContext); | |
this._pInst._setProperty('width', this.width); | |
this._pInst._setProperty('height', this.height); | |
} else { | |
this.canvas.style.display = 'none'; | |
this._styles = []; | |
} | |
}; | |
p5.Graphics.prototype = Object.create(p5.Element.prototype); | |
p5.Graphics.prototype._applyDefaults = function () { | |
this.drawingContext.fillStyle = '#FFFFFF'; | |
this.drawingContext.strokeStyle = '#000000'; | |
this.drawingContext.lineCap = constants.ROUND; | |
this.drawingContext.font = 'normal 12px sans-serif'; | |
}; | |
p5.Graphics.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); | |
} | |
this.drawingContext.scale(this._pInst._pixelDensity, this._pInst._pixelDensity); | |
}; | |
return p5.Graphics; | |
}({}, amdclean['core'], amdclean['constants']); | |
amdclean['filters'] = function (require) { | |
'use strict'; | |
var Filters = {}; | |
Filters._toPixels = function (canvas) { | |
if (canvas instanceof ImageData) { | |
return canvas.data; | |
} else { | |
return canvas.getContext('2d').getImageData(0, 0, canvas.width, canvas.height).data; | |
} | |
}; | |
Filters._getARGB = function (data, i) { | |
var offset = i * 4; | |
return data[offset + 3] << 24 & 4278190080 | data[offset] << 16 & 16711680 | data[offset + 1] << 8 & 65280 | data[offset + 2] & 255; | |
}; | |
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] & 16711680) >>> 16; | |
pixels[offset + 1] = (data[i] & 65280) >>> 8; | |
pixels[offset + 2] = data[i] & 255; | |
pixels[offset + 3] = (data[i] & 4278190080) >>> 24; | |
} | |
}; | |
Filters._toImageData = function (canvas) { | |
if (canvas instanceof ImageData) { | |
return canvas; | |
} else { | |
return canvas.getContext('2d').getImageData(0, 0, canvas.width, canvas.height); | |
} | |
}; | |
Filters._createImageData = function (width, height) { | |
Filters._tmpCanvas = document.createElement('canvas'); | |
Filters._tmpCtx = Filters._tmpCanvas.getContext('2d'); | |
return this._tmpCtx.createImageData(width, height); | |
}; | |
Filters.apply = function (canvas, func, filterParam) { | |
var ctx = canvas.getContext('2d'); | |
var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height); | |
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.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 grey = 0.2126 * r + 0.7152 * g + 0.0722 * b; | |
var val; | |
if (grey >= thresh) { | |
val = 255; | |
} else { | |
val = 0; | |
} | |
pixels[i] = pixels[i + 1] = pixels[i + 2] = val; | |
} | |
}; | |
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]; | |
var gray = 0.2126 * r + 0.7152 * g + 0.0722 * b; | |
pixels[i] = pixels[i + 1] = pixels[i + 2] = gray; | |
} | |
}; | |
Filters.opaque = function (canvas) { | |
var pixels = Filters._toPixels(canvas); | |
for (var i = 0; i < pixels.length; i += 4) { | |
pixels[i + 3] = 255; | |
} | |
return pixels; | |
}; | |
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]; | |
} | |
}; | |
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; | |
} | |
}; | |
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); | |
currLum = 77 * (colOrig >> 16 & 255) + 151 * (colOrig >> 8 & 255) + 28 * (colOrig & 255); | |
lumLeft = 77 * (colLeft >> 16 & 255) + 151 * (colLeft >> 8 & 255) + 28 * (colLeft & 255); | |
lumRight = 77 * (colRight >> 16 & 255) + 151 * (colRight >> 8 & 255) + 28 * (colRight & 255); | |
lumUp = 77 * (colUp >> 16 & 255) + 151 * (colUp >> 8 & 255) + 28 * (colUp & 255); | |
lumDown = 77 * (colDown >> 16 & 255) + 151 * (colDown >> 8 & 255) + 28 * (colDown & 255); | |
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); | |
}; | |
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); | |
currLum = 77 * (colOrig >> 16 & 255) + 151 * (colOrig >> 8 & 255) + 28 * (colOrig & 255); | |
lumLeft = 77 * (colLeft >> 16 & 255) + 151 * (colLeft >> 8 & 255) + 28 * (colLeft & 255); | |
lumRight = 77 * (colRight >> 16 & 255) + 151 * (colRight >> 8 & 255) + 28 * (colRight & 255); | |
lumUp = 77 * (colUp >> 16 & 255) + 151 * (colUp >> 8 & 255) + 28 * (colUp & 255); | |
lumDown = 77 * (colDown >> 16 & 255) + 151 * (colDown >> 8 & 255) + 28 * (colDown & 255); | |
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); | |
}; | |
var blurRadius; | |
var blurKernelSize; | |
var blurKernel; | |
var blurMult; | |
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; | |
} | |
} | |
} | |
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); | |
}; | |
return Filters; | |
}({}); | |
amdclean['p5Image'] = function (require, core, filters) { | |
'use strict'; | |
var p5 = core; | |
var Filters = filters; | |
p5.Image = function (width, height) { | |
this.width = width; | |
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.pixels = []; | |
}; | |
p5.Image.prototype._setProperty = function (prop, value) { | |
this[prop] = value; | |
}; | |
p5.Image.prototype.loadPixels = function () { | |
p5.prototype.loadPixels.call(this); | |
}; | |
p5.Image.prototype.updatePixels = function (x, y, w, h) { | |
p5.prototype.updatePixels.call(this, x, y, w, h); | |
}; | |
p5.Image.prototype.get = function (x, y, w, h) { | |
return p5.prototype.get.call(this, x, y, w, h); | |
}; | |
p5.Image.prototype.set = function (x, y, imgOrCol) { | |
p5.prototype.set.call(this, x, y, imgOrCol); | |
}; | |
p5.Image.prototype.resize = function (width, height) { | |
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); | |
this.canvas.width = this.width = width; | |
this.canvas.height = this.height = height; | |
this.drawingContext.drawImage(tempCanvas, 0, 0, width, height, 0, 0, width, height); | |
if (this.pixels.length > 0) { | |
this.loadPixels(); | |
} | |
}; | |
p5.Image.prototype.copy = function () { | |
p5.prototype.copy.apply(this, arguments); | |
}; | |
p5.Image.prototype.mask = function (p5Image) { | |
if (p5Image === undefined) { | |
p5Image = this; | |
} | |
var currBlend = this.drawingContext.globalCompositeOperation; | |
var scaleFactor = 1; | |
if (p5Image instanceof p5.Graphics) { | |
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; | |
}; | |
p5.Image.prototype.filter = function (operation, value) { | |
Filters.apply(this.canvas, Filters[operation.toLowerCase()], value); | |
}; | |
p5.Image.prototype.blend = function () { | |
p5.prototype.blend.apply(this, arguments); | |
}; | |
p5.Image.prototype.save = function (filename, extension) { | |
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 = this.canvas.toDataURL(mimeType); | |
imageData = imageData.replace(mimeType, downloadMime); | |
p5.prototype.downloadFile(imageData, filename, extension); | |
}; | |
return p5.Image; | |
}({}, amdclean['core'], amdclean['filters']); | |
amdclean['p5File'] = function (require, core) { | |
var p5 = core; | |
p5.File = function (file, pInst) { | |
this.file = file; | |
this._pInst = pInst; | |
var typeList = file.type.split('/'); | |
this.type = typeList[0]; | |
this.subtype = typeList[1]; | |
this.name = file.name; | |
this.size = file.size; | |
this.data = undefined; | |
}; | |
return p5.File; | |
}({}, amdclean['core']); | |
amdclean['polargeometry'] = function (require) { | |
return { | |
degreesToRadians: function (x) { | |
return 2 * Math.PI * x / 360; | |
}, | |
radiansToDegrees: function (x) { | |
return 360 * x / (2 * Math.PI); | |
} | |
}; | |
}({}); | |
amdclean['p5Vector'] = function (require, core, polargeometry, constants) { | |
'use strict'; | |
var p5 = core; | |
var polarGeometry = polargeometry; | |
var constants = constants; | |
p5.Vector = function () { | |
var x, y, z; | |
if (arguments[0] instanceof p5) { | |
this.p5 = arguments[0]; | |
x = arguments[1][0] || 0; | |
y = arguments[1][1] || 0; | |
z = arguments[1][2] || 0; | |
} else { | |
x = arguments[0] || 0; | |
y = arguments[1] || 0; | |
z = arguments[2] || 0; | |
} | |
this.x = x; | |
this.y = y; | |
this.z = z; | |
}; | |
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; | |
}; | |
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); | |
} | |
}; | |
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; | |
}; | |
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; | |
}; | |
p5.Vector.prototype.mult = function (n) { | |
this.x *= n || 0; | |
this.y *= n || 0; | |
this.z *= n || 0; | |
return this; | |
}; | |
p5.Vector.prototype.div = function (n) { | |
this.x /= n; | |
this.y /= n; | |
this.z /= n; | |
return this; | |
}; | |
p5.Vector.prototype.mag = function () { | |
return Math.sqrt(this.magSq()); | |
}; | |
p5.Vector.prototype.magSq = function () { | |
var x = this.x, y = this.y, z = this.z; | |
return x * x + y * y + z * z; | |
}; | |
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); | |
}; | |
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); | |
} | |
}; | |
p5.Vector.prototype.dist = function (v) { | |
var d = v.copy().sub(this); | |
return d.mag(); | |
}; | |
p5.Vector.prototype.normalize = function () { | |
return this.div(this.mag()); | |
}; | |
p5.Vector.prototype.limit = function (l) { | |
var mSq = this.magSq(); | |
if (mSq > l * l) { | |
this.div(Math.sqrt(mSq)); | |
this.mult(l); | |
} | |
return this; | |
}; | |
p5.Vector.prototype.setMag = function (n) { | |
return this.normalize().mult(n); | |
}; | |
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; | |
} | |
}; | |
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; | |
}; | |
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; | |
}; | |
p5.Vector.prototype.array = function () { | |
return [ | |
this.x || 0, | |
this.y || 0, | |
this.z || 0 | |
]; | |
}; | |
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); | |
} | |
}; | |
p5.Vector.random2D = function () { | |
var angle; | |
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); | |
}; | |
p5.Vector.random3D = function () { | |
var angle, vz; | |
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); | |
} | |
}; | |
p5.Vector.add = function (v1, v2, target) { | |
if (!target) { | |
target = v1.copy(); | |
} else { | |
target.set(v1); | |
} | |
target.add(v2); | |
return target; | |
}; | |
p5.Vector.sub = function (v1, v2, target) { | |
if (!target) { | |
target = v1.copy(); | |
} else { | |
target.set(v1); | |
} | |
target.sub(v2); | |
return target; | |
}; | |
p5.Vector.mult = function (v, n, target) { | |
if (!target) { | |
target = v.copy(); | |
} else { | |
target.set(v); | |
} | |
target.mult(n); | |
return target; | |
}; | |
p5.Vector.div = function (v, n, target) { | |
if (!target) { | |
target = v.copy(); | |
} else { | |
target.set(v); | |
} | |
target.div(n); | |
return target; | |
}; | |
p5.Vector.dot = function (v1, v2) { | |
return v1.dot(v2); | |
}; | |
p5.Vector.cross = function (v1, v2) { | |
return v1.cross(v2); | |
}; | |
p5.Vector.dist = function (v1, v2) { | |
return v1.dist(v2); | |
}; | |
p5.Vector.lerp = function (v1, v2, amt, target) { | |
if (!target) { | |
target = v1.copy(); | |
} else { | |
target.set(v1); | |
} | |
target.lerp(v2, amt); | |
return target; | |
}; | |
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; | |
}; | |
return p5.Vector; | |
}({}, amdclean['core'], amdclean['polargeometry'], amdclean['constants']); | |
amdclean['p5TableRow'] = function (require, core) { | |
'use strict'; | |
var p5 = core; | |
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; | |
}; | |
p5.TableRow.prototype.set = function (column, value) { | |
if (typeof column === 'string') { | |
var cPos = this.table.columns.indexOf(column); | |
if (cPos >= 0) { | |
this.obj[column] = value; | |
this.arr[cPos] = value; | |
} else { | |
throw 'This table has no column named "' + column + '"'; | |
} | |
} 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'; | |
} | |
} | |
}; | |
p5.TableRow.prototype.setNum = function (column, value) { | |
var floatVal = parseFloat(value, 10); | |
this.set(column, floatVal); | |
}; | |
p5.TableRow.prototype.setString = function (column, value) { | |
var stringVal = value.toString(); | |
this.set(column, stringVal); | |
}; | |
p5.TableRow.prototype.get = function (column) { | |
if (typeof column === 'string') { | |
return this.obj[column]; | |
} else { | |
return this.arr[column]; | |
} | |
}; | |
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; | |
}; | |
p5.TableRow.prototype.getString = function (column) { | |
if (typeof column === 'string') { | |
return this.obj[column].toString(); | |
} else { | |
return this.arr[column].toString(); | |
} | |
}; | |
return p5.TableRow; | |
}({}, amdclean['core']); | |
amdclean['p5Table'] = function (require, core) { | |
'use strict'; | |
var p5 = core; | |
p5.Table = function (rows) { | |
this.columns = []; | |
this.rows = []; | |
}; | |
p5.Table.prototype.addRow = function (row) { | |
var r = row || new p5.TableRow(); | |
if (typeof r.arr === 'undefined' || typeof r.obj === 'undefined') { | |
throw 'invalid TableRow: ' + r; | |
} | |
r.table = this; | |
this.rows.push(r); | |
return r; | |
}; | |
p5.Table.prototype.removeRow = function (id) { | |
this.rows[id].table = null; | |
var chunk = this.rows.splice(id + 1, this.rows.length); | |
this.rows.pop(); | |
this.rows = this.rows.concat(chunk); | |
}; | |
p5.Table.prototype.getRow = function (r) { | |
return this.rows[r]; | |
}; | |
p5.Table.prototype.getRows = function () { | |
return this.rows; | |
}; | |
p5.Table.prototype.findRow = function (value, column) { | |
if (typeof column === 'string') { | |
for (var i = 0; i < this.rows.length; i++) { | |
if (this.rows[i].obj[column] === value) { | |
return this.rows[i]; | |
} | |
} | |
} else { | |
for (var j = 0; j < this.rows.length; j++) { | |
if (this.rows[j].arr[column] === value) { | |
return this.rows[j]; | |
} | |
} | |
} | |
return null; | |
}; | |
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]); | |
} | |
} | |
} else { | |
for (var j = 0; j < this.rows.length; j++) { | |
if (this.rows[j].arr[column] === value) { | |
ret.push(this.rows[j]); | |
} | |
} | |
} | |
return ret; | |
}; | |
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; | |
}; | |
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; | |
}; | |
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; | |
}; | |
p5.Table.prototype.clearRows = function () { | |
delete this.rows; | |
this.rows = []; | |
}; | |
p5.Table.prototype.addColumn = function (title) { | |
var t = title || null; | |
this.columns.push(t); | |
}; | |
p5.Table.prototype.getColumnCount = function () { | |
return this.columns.length; | |
}; | |
p5.Table.prototype.getRowCount = function () { | |
return this.rows.length; | |
}; | |
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; | |
} | |
} | |
}; | |
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; | |
} | |
} | |
}; | |
p5.Table.prototype.removeColumn = function (c) { | |
var cString; | |
var cNumber; | |
if (typeof c === 'string') { | |
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]; | |
} | |
}; | |
p5.Table.prototype.set = function (row, column, value) { | |
this.rows[row].set(column, value); | |
}; | |
p5.Table.prototype.setNum = function (row, column, value) { | |
this.rows[row].set(column, value); | |
}; | |
p5.Table.prototype.setString = function (row, column, value) { | |
this.rows[row].set(column, value); | |
}; | |
p5.Table.prototype.get = function (row, column) { | |
return this.rows[row].get(column); | |
}; | |
p5.Table.prototype.getNum = function (row, column) { | |
return this.rows[row].getNum(column); | |
}; | |
p5.Table.prototype.getString = function (row, column) { | |
return this.rows[row].getString(column); | |
}; | |
return p5.Table; | |
}({}, amdclean['core']); | |
amdclean['colorcreating_reading'] = function (require, core, p5Color) { | |
'use strict'; | |
var p5 = core; | |
p5.prototype.alpha = function (c) { | |
if (c instanceof p5.Color || c instanceof Array) { | |
return this.color(c).getAlpha(); | |
} else { | |
throw new Error('Needs p5.Color or pixel array as argument.'); | |
} | |
}; | |
p5.prototype.blue = function (c) { | |
if (c instanceof p5.Color || c instanceof Array) { | |
return this.color(c).getBlue(); | |
} else { | |
throw new Error('Needs p5.Color or pixel array as argument.'); | |
} | |
}; | |
p5.prototype.brightness = function (c) { | |
if (!c instanceof p5.Color) { | |
throw new Error('Needs p5.Color as argument.'); | |
} | |
return c.getBrightness(); | |
}; | |
p5.prototype.color = function () { | |
if (arguments[0] instanceof p5.Color) { | |
return arguments[0]; | |
} else if (arguments[0] instanceof Array) { | |
return new p5.Color(this, arguments[0]); | |
} else { | |
var args = Array.prototype.slice.call(arguments); | |
return new p5.Color(this, args); | |
} | |
}; | |
p5.prototype.green = function (c) { | |
if (c instanceof p5.Color || c instanceof Array) { | |
return this.color(c).getGreen(); | |
} else { | |
throw new Error('Needs p5.Color or pixel array as argument.'); | |
} | |
}; | |
p5.prototype.hue = function (c) { | |
if (!c instanceof p5.Color) { | |
throw new Error('Needs p5.Color as argument.'); | |
} | |
return c.getHue(); | |
}; | |
p5.prototype.lerpColor = function (c1, c2, amt) { | |
if (c1 instanceof Array) { | |
var c = []; | |
for (var i = 0; i < c1.length; i++) { | |
c.push(p5.prototype.lerp(c1[i], c2[i], amt)); | |
} | |
return c; | |
} else if (c1 instanceof p5.Color) { | |
var pc = []; | |
for (var j = 0; j < 4; j++) { | |
pc.push(p5.prototype.lerp(c1.rgba[j], c2.rgba[j], amt)); | |
} | |
return new p5.Color(this, pc); | |
} else { | |
return p5.prototype.lerp(c1, c2, amt); | |
} | |
}; | |
p5.prototype.red = function (c) { | |
if (c instanceof p5.Color || c instanceof Array) { | |
return this.color(c).getRed(); | |
} else { | |
throw new Error('Needs p5.Color or pixel array as argument.'); | |
} | |
}; | |
p5.prototype.saturation = function (c) { | |
if (!c instanceof p5.Color) { | |
throw new Error('Needs p5.Color as argument.'); | |
} | |
return c.getSaturation(); | |
}; | |
return p5; | |
}({}, amdclean['core'], amdclean['p5Color']); | |
amdclean['colorsetting'] = function (require, core, constants, p5Color) { | |
'use strict'; | |
var p5 = core; | |
var constants = constants; | |
p5.prototype._doStroke = true; | |
p5.prototype._doFill = true; | |
p5.prototype._colorMode = constants.RGB; | |
p5.prototype._maxRGB = [ | |
255, | |
255, | |
255, | |
255 | |
]; | |
p5.prototype._maxHSB = [ | |
255, | |
255, | |
255, | |
255 | |
]; | |
p5.prototype.background = function () { | |
this.drawingContext.save(); | |
this.drawingContext.setTransform(1, 0, 0, 1, 0, 0); | |
this.drawingContext.scale(this._pixelDensity, this._pixelDensity); | |
if (arguments[0] instanceof p5.Image) { | |
this.image(arguments[0], 0, 0, this.width, this.height); | |
} else { | |
var curFill = this.drawingContext.fillStyle; | |
var color = this.color.apply(this, arguments); | |
var newFill = color.toString(); | |
this.drawingContext.fillStyle = newFill; | |
this.drawingContext.fillRect(0, 0, this.width, this.height); | |
this.drawingContext.fillStyle = curFill; | |
} | |
this.drawingContext.restore(); | |
}; | |
p5.prototype.clear = function () { | |
this.drawingContext.clearRect(0, 0, this.width, this.height); | |
}; | |
p5.prototype.colorMode = function () { | |
if (arguments[0] === constants.RGB || arguments[0] === constants.HSB) { | |
this._colorMode = arguments[0]; | |
var isRGB = this._colorMode === constants.RGB; | |
var maxArr = isRGB ? this._maxRGB : this._maxHSB; | |
if (arguments.length === 2) { | |
maxArr[0] = arguments[1]; | |
maxArr[1] = arguments[1]; | |
maxArr[2] = arguments[1]; | |
maxArr[3] = arguments[1]; | |
} else if (arguments.length > 2) { | |
maxArr[0] = arguments[1]; | |
maxArr[1] = arguments[2]; | |
maxArr[2] = arguments[3]; | |
} | |
if (arguments.length === 5) { | |
maxArr[3] = arguments[4]; | |
} | |
} | |
}; | |
p5.prototype.fill = function () { | |
this._setProperty('_doFill', true); | |
var ctx = this.drawingContext; | |
var color = this.color.apply(this, arguments); | |
ctx.fillStyle = color.toString(); | |
}; | |
p5.prototype.noFill = function () { | |
this._setProperty('_doFill', false); | |
}; | |
p5.prototype.noStroke = function () { | |
this._setProperty('_doStroke', false); | |
}; | |
p5.prototype.stroke = function () { | |
this._setProperty('_doStroke', true); | |
var ctx = this.drawingContext; | |
var color = this.color.apply(this, arguments); | |
ctx.strokeStyle = color.toString(); | |
}; | |
return p5; | |
}({}, amdclean['core'], amdclean['constants'], amdclean['p5Color']); | |
amdclean['dataconversion'] = function (require, core) { | |
'use strict'; | |
var p5 = core; | |
p5.prototype.float = function (str) { | |
return parseFloat(str); | |
}; | |
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(p5.prototype.int); | |
} | |
}; | |
return p5; | |
}({}, amdclean['core']); | |
amdclean['dataarray_functions'] = function (require, core) { | |
'use strict'; | |
var p5 = core; | |
p5.prototype.append = function (array, value) { | |
array.push(value); | |
return array; | |
}; | |
p5.prototype.arrayCopy = function (src, srcPosition, dst, dstPosition, length) { | |
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') { | |
end = dst; | |
end = Math.min(end, src.length); | |
} else { | |
end = src.length; | |
} | |
start = 0; | |
dst = srcPosition; | |
src = src.slice(0, end); | |
} | |
Array.prototype.splice.apply(dst, [ | |
start, | |
end | |
].concat(src)); | |
}; | |
p5.prototype.concat = function (list0, list1) { | |
return list0.concat(list1); | |
}; | |
p5.prototype.reverse = function (list) { | |
return list.reverse(); | |
}; | |
p5.prototype.shorten = function (list) { | |
list.pop(); | |
return list; | |
}; | |
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; | |
}; | |
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); | |
}; | |
p5.prototype.splice = function (list, value, index) { | |
Array.prototype.splice.apply(list, [ | |
index, | |
0 | |
].concat(value)); | |
return list; | |
}; | |
p5.prototype.subset = function (list, start, count) { | |
if (typeof count !== 'undefined') { | |
return list.slice(start, start + count); | |
} else { | |
return list.slice(start, list.length); | |
} | |
}; | |
return p5; | |
}({}, amdclean['core']); | |
amdclean['datastring_functions'] = function (require, core) { | |
'use strict'; | |
var p5 = core; | |
p5.prototype.join = function (list, separator) { | |
return list.join(separator); | |
}; | |
p5.prototype.match = function (str, reg) { | |
return str.match(reg); | |
}; | |
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); | |
match = re.exec(str); | |
} | |
return matches; | |
}; | |
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 { | |
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) { | |
for (var i = 0; i < arguments[1] - intPart.length; i++) { | |
str += '0'; | |
} | |
str += intPart; | |
str += '.'; | |
str += decPart; | |
for (var j = 0; j < arguments[2] - decPart.length; j++) { | |