8000 pt/dist/pt-ns.js at master · jakedahm/pt · GitHub
[go: up one dir, main page]

Skip to content
< 7FFF div data-target="react-partial.reactRoot">

Latest commit

 

History

History
6137 lines (5540 loc) · 157 KB

pt-ns.js

File metadata and controls

6137 lines (5540 loc) · 157 KB
window.Pt = {};(function() {var CanvasSpace, Circle, Color, Const, Curve, DOMSpace, Delaunay, Easing, Form, Grid, GridCascade, Line, Matrix, Noise, Pair, Particle, ParticleEmitter, ParticleField, ParticleSystem, Point, PointSet, QuadTree, Rectangle, SVGForm, SVGSpace, SamplePoints, Space, StripeBound, Timer, Triangle, UI, Util, Vector,
bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
hasProp = {}.hasOwnProperty,
slice = [].slice;
Const = (function() {
function Const() {}
Const.xy = 'xy';
Const.yz = 'yz';
Const.xz = 'xz';
Const.xyz = 'xyz';
Const.identical = -1;
Const.right = 3;
Const.bottom_right = 4;
Const.bottom = 5;
Const.bottom_left = 6;
Const.left = 7;
Const.top_left = 0;
Const.top = 1;
Const.top_right = 2;
Const.sideLabels = ["identical", "right", "bottom right", "bottom", "bottom left", "left", "top left", "top", "top right"];
Const.epsilon = 0.0001;
Const.pi = Math.PI;
Const.two_pi = 6.283185307179586;
Const.half_pi = 1.5707963267948966;
Const.quarter_pi = 0.7853981633974483;
Const.one_degree = 0.017453292519943295;
Const.rad_to_deg = 57.29577951308232;
Const.deg_to_rad = 0.017453292519943295;
Const.gravity = 9.81;
Const.newton = 0.10197;
Const.gaussian = 0.3989422804014327;
return Const;
})();
this.Const = Const;
Matrix = (function() {
function Matrix() {}
Matrix.rotateAnchor2D = function(radian, anchor, axis) {
var a, cosA, sinA;
if (axis == null) {
axis = Const.xy;
}
a = anchor.get2D(axis);
cosA = Math.cos(radian);
sinA = Math.sin(radian);
return [cosA, sinA, 0, -sinA, cosA, 0, a.x * (1 - cosA) + a.y * sinA, a.y * (1 - cosA) - a.x * sinA, 1];
};
Matrix.reflectAnchor2D = function(line, axis) {
var ang2, cosA, inc, sinA;
if (axis == null) {
axis = Const.xy;
}
inc = line.intercept(axis);
ang2 = Math.atan(inc.slope) * 2;
cosA = Math.cos(ang2);
sinA = Math.sin(ang2);
return [cosA, sinA, 0, sinA, -cosA, 0, -inc.yi * sinA, inc.yi + inc.yi * cosA, 1];
};
Matrix.shearAnchor2D = function(sx, sy, anchor, axis) {
var a, tx, ty;
if (axis == null) {
axis = Const.xy;
}
a = anchor.get2D(axis);
tx = Math.tan(sx);
ty = Math.tan(sy);
return [1, tx, 0, ty, 1, 0, -a.y * ty, -a.x * tx, 1];
};
Matrix.scaleAnchor2D = function(sx, sy, anchor, axis) {
var a;
if (axis == null) {
axis = Const.xy;
}
a = anchor.get2D(axis);
return [sx, 0, 0, 0, sy, 0, -a.x * sx + a.x, -a.y * sy + a.y, 1];
};
Matrix.scale2D = function(x, y) {
return [x, 0, 0, 0, y, 0, 0, 0, 1];
};
Matrix.shear2D = function(x, y) {
return [1, Math.tan(x), 0, Math.tan(y), 1, 0, 0, 0, 1];
};
Matrix.rotate2D = function(cosA, sinA) {
return [cosA, sinA, 0, -sinA, cosA, 0, 0, 0, 1];
};
Matrix.translate2D = function(x, y) {
return [1, 0, 0, 0, 1, 0, x, y, 1];
};
Matrix.transform2D = function(pt, m, axis, byValue) {
var v, x, y;
if (axis == null) {
axis = Const.xy;
}
if (byValue == null) {
byValue = false;
}
v = pt.get2D(axis);
x = v.x * m[0] + v.y * m[3] + m[6];
y = v.x * m[1] + v.y * m[4] + m[7];
v.x = x;
v.y = y;
v = v.get2D(axis, true);
if (!byValue) {
pt.set(v);
return pt;
}
return v;
};
return Matrix;
})();
this.Matrix = Matrix;
Util = (function() {
function Util() {}
Util.toRadian = function(angle) {
return angle * Const.deg_to_rad;
};
Util.toDegree = function(radian) {
return radian * Const.rad_to_deg;
};
Util.toHexColor = function(number) {
var h;
h = Math.floor(number).toString(16);
if (h.length === 1) {
return "0" + h;
} else {
return h;
}
};
Util.toRGBColor = function(hexString, asRGBA, opacity) {
var b, g, r;
if (asRGBA == null) {
asRGBA = false;
}
if (opacity == null) {
opacity = 1;
}
if (hexString[0] === "#") {
hexString = hexString.substr(1);
}
if (hexString.length === 3) {
r = parseInt(hexString[0] + hexString[0], 16);
g = parseInt(hexString[1] + hexString[1], 16);
b = parseInt(hexString[2] + hexString[2], 16);
} else if (hexString.length >= 6) {
r = parseInt(hexString[0] + hexString[1], 16);
g = parseInt(hexString[2] + hexString[3], 16);
b = parseInt(hexString[4] + hexString[5], 16);
} else {
r = 0;
g = 0;
b = 0;
}
if (asRGBA) {
return "rgba(" + r + "," + g + "," + b + "," + opacity + ")";
} else {
return [r, g, b, opacity];
}
};
Util.bound = function(val, max, positive) {
var a, half;
if (positive == null) {
positive = false;
}
a = val % max;
half = max / 2;
if (a > half) {
a -= max;
} else if (a < -half) {
a += max;
}
if (positive) {
if (a < 0) {
return a + max;
} else {
return a;
}
} else {
return a;
}
};
Util.boundAngle = function(ang, positive) {
return Util.bound(ang, 360, positive);
};
Util.boundRadian = function(radian, positive) {
return Util.bound(radian, Const.two_pi, positive);
};
Util.boundingBox = function(points, is3D) {
var aa, len1, maxPt, minPt, p;
if (is3D == null) {
is3D = false;
}
minPt = new Point(Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY);
maxPt = new Point(Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY);
for (aa = 0, len1 = points.length; aa < len1; aa++) {
p = points[aa];
if (p.x < minPt.x) {
minPt.x = p.x;
}
if (p.y < minPt.y) {
minPt.y = p.y;
}
if (p.x > maxPt.x) {
maxPt.x = p.x;
}
if (p.y > maxPt.y) {
maxPt.y = p.y;
}
if (is3D) {
if (p.z < minPt.z) {
minPt.z = p.z;
}
if (p.z > maxPt.z) {
maxPt.z = p.z;
}
}
}
return new Rectangle(minPt).to(maxPt);
};
Util.lerp = function(a, b, t) {
return (1 - t) * a + t * b;
};
Util.centroid = function(points) {
var aa, c, len1, p;
c = new Vector();
for (aa = 0, len1 = points.length; aa < len1; aa++) {
p = points[aa];
c.add(p);
}
return c.divide(points.length);
};
Util.same = function(a, b, threshold) {
if (threshold == null) {
threshold = Const.epsilon;
}
return Math.abs(a - b) < threshold;
};
Util.within = function(p, a, b) {
return p >= Math.min(a, b) && p <= Math.max(a, b);
};
Util.randomRange = function(a, b) {
var r;
if (b == null) {
b = 0;
}
r = a > b ? a - b : b - a;
return a + Math.random() * r;
};
Util.mixin = function(klass, mix) {
var k, v;
for (k in mix) {
v = mix[k];
if (mix.hasOwnProperty(k)) {
klass.prototype[k] = mix[k];
}
}
return klass;
};
Util.extend = function(klass, parent) {
klass.prototype = Object.create(parent.prototype);
klass.prototype.constructor = klass;
return klass;
};
Util.clonePoints = function(array) {
var aa, len1, p, results;
results = [];
for (aa = 0, len1 = array.length; aa < len1; aa++) {
p = array[aa];
results.push(p.clone());
}
return results;
};
Util.contextRotateOrigin = function(ctx, bound, radian, origin, mask) {
var msz, size;
if (origin == null) {
origin = false;
}
size = bound.size();
if (!origin) {
origin = size.$multiply(0.5);
origin.add(bound);
}
if (mask) {
msz = mask.size();
Form.rect(ctx, mask);
ctx.clip();
}
ctx.translate(origin.x, origin.y);
ctx.rotate(radian);
return ctx.translate(-origin.x, -origin.y);
};
Util.sinCosTable = function() {
var aa, cos, i, sin;
cos = [];
sin = [];
for (i = aa = 0; aa <= 360; i = aa += 1) {
cos[i] = Math.cos(i * Math.PI / 180);
sin[i] = Math.sin(i * Math.PI / 180);
}
return {
sin: sin,
cos: cos
};
};
Util.chance = function(p) {
return Math.random() < p;
};
Util.gaussian = function(x, mean, sigma) {
if (mean == null) {
mean = 0;
}
if (sigma == null) {
sigma = 1;
}
x = (x - mean) / sigma;
return Const.gaussian * Math.exp(-0.5 * x * x) / sigma;
};
return Util;
})();
this.Util = Util;
Timer = (function() {
function Timer(d) {
if (d == null) {
d = 1000;
}
this.duration = d;
this._time = 0;
this._ease = function(t, b, c, d) {
return t / d;
};
this._intervalID = -1;
}
Timer.prototype.start = function(reset) {
var diff;
diff = Math.min(Date.now() - this._time, this.duration);
if (reset || diff >= this.duration) {
return this._time = Date.now();
}
};
Timer.prototype.setEasing = function(ease) {
return this._ease = ease;
};
Timer.prototype.check = function() {
var diff;
diff = Math.min(Date.now() - this._time, this.duration);
return this._ease(diff, 0, 1, this.duration);
};
Timer.prototype.track = function(callback) {
var me;
clearInterval(this._intervalID);
this.start(true);
me = this;
this._intervalID = setInterval((function() {
var t;
t = me.check();
callback(t);
if (t >= 1) {
return clearInterval(me._intervalID);
}
}), 25);
return this._intervalID;
};
return Timer;
})();
this.Timer = Timer;
Space = (function() {
function Space(id) {
if (id == null) {
id = 'space';
}
this.id = id;
this.size = new Vector();
this.center = new Vector();
this._timePrev = 0;
this._timeDiff = 0;
this._timeEnd = -1;
this.items = {};
this._animID = -1;
this._animCount = 0;
this._animPause = false;
this._refresh = true;
}
Space.prototype.refresh = function(b) {
this._refresh = b;
return this;
};
Space.prototype.render = function(context) {
return this;
};
Space.prototype.resize = function(w, h) {};
Space.prototype.clear = function() {};
Space.prototype.add = function(item) {
var k;
if ((item.animate != null) && typeof item.animate === 'function') {
k = this._animCount++;
this.items[k] = item;
item.animateID = k;
if (item.onSpaceResize != null) {
item.onSpaceResize(this.size.x, this.size.y);
}
} else {
throw "a player object for Space.add must define animate()";
}
return this;
};
Space.prototype.remove = function(item) {
delete this.items[item.animateID];
return this;
};
Space.prototype.removeAll = function() {
this.items = {};
return this;
};
Space.prototype.play = function(time) {
var err;
if (time == null) {
time = 0;
}
this._animID = requestAnimationFrame((function(_this) {
return function(t) {
return _this.play(t);
};
})(this));
if (this._animPause) {
return;
}
this._timeDiff = time - this._timePrev;
try {
this._playItems(time);
} catch (_error) {
err = _error;
cancelAnimationFrame(this._animID);
console.error(err.stack);
throw err;
}
this._timePrev = time;
return this;
};
Space.prototype._playItems = function(time) {
var k, ref, v;
if (this._refresh) {
this.clear();
}
ref = this.items;
for (k in ref) {
v = ref[k];
v.animate(time, this._timeDiff, this.ctx);
}
if (this._timeEnd >= 0 && time > this._timeEnd) {
cancelAnimationFrame(this._animID);
}
return this;
};
Space.prototype.pause = function(toggle) {
if (toggle == null) {
toggle = false;
}
this._animPause = toggle ? !this._animPause : true;
return this;
};
Space.prototype.resume = function() {
this._animPause = false;
return this;
};
Space.prototype.stop = function(t) {
if (t == null) {
t = 0;
}
this._timeEnd = t;
return this;
};
Space.prototype.playTime = function(duration) {
if (duration == null) {
duration = 5000;
}
this.play();
return this.stop(duration);
};
Space.prototype.bindCanvas = function(evt, callback) {
if (this.space.addEventListener) {
return this.space.addEventListener(evt, callback);
}
};
Space.prototype.bindMouse = function(_bind) {
if (_bind == null) {
_bind = true;
}
if (this.space.addEventListener && this.space.removeEventListener) {
if (_bind) {
this.space.addEventListener("mousedown", this._mouseDown.bind(this));
this.space.addEventListener("mouseup", this._mouseUp.bind(this));
this.space.addEventListener("mouseover", this._mouseOver.bind(this));
this.space.addEventListener("mouseout", this._mouseOut.bind(this));
return this.space.addEventListener("mousemove", this._mouseMove.bind(this));
} else {
this.space.removeEventListener("mousedown", this._mouseDown.bind(this));
this.space.removeEventListener("mouseup", this._mouseUp.bind(this));
this.space.removeEventListener("mouseover", this._mouseOver.bind(this));
this.space.removeEventListener("mouseout", this._mouseOut.bind(this));
return this.space.removeEventListener("mousemove", this._mouseMove.bind(this));
}
}
};
Space.prototype.bindTouch = function(_bind) {
if (_bind == null) {
_bind = true;
}
if (this.space.addEventListener && this.space.removeEventListener) {
if (_bind) {
this.space.addEventListener("touchstart", this._mouseDown.bind(this));
this.space.addEventListener("touchend", this._mouseUp.bind(this));
this.space.addEventListener("touchmove", ((function(_this) {
return function(evt) {
evt.preventDefault();
return _this._mouseMove(evt);
};
})(this)));
return this.space.addEventListener("touchcancel", this._mouseOut.bind(this));
} else {
this.space.removeEventListener("touchstart", this._mouseDown.bind(this));
this.space.removeEventListener("touchend", this._mouseUp.bind(this));
this.space.removeEventListener("touchmove", this._mouseMove.bind(this));
return this.space.removeEventListener("touchcancel", this._mouseOut.bind(this));
}
}
};
Space.prototype.touchesToPoints = function(evt, which) {
var t;
if (which == null) {
which = "touches";
}
if (!evt || !evt[which]) {
return [];
}
return (function() {
var aa, len1, ref, results;
ref = evt[which];
results = [];
for (aa = 0, len1 = ref.length; aa < len1; aa++) {
t = ref[aa];
results.push(new Vector(t.pageX - this.boundRect.left, t.pageY - this.boundRect.top));
}
return results;
}).call(this);
};
Space.prototype._mouseAction = function(type, evt) {
var _c, k, px, py, ref, ref1, results, results1, v;
if (evt.touches || evt.changedTouches) {
ref = this.items;
results = [];
for (k in ref) {
v = ref[k];
if (v.onTouchAction != null) {
_c = evt.changedTouches && evt.changedTouches.length > 0;
px = _c ? evt.changedTouches.item(0).pageX : 0;
py = _c ? evt.changedTouches.item(0).pageY : 0;
results.push(v.onTouchAction(type, px, py, evt));
} else {
results.push(void 0);
}
}
return results;
} else {
ref1 = this.items;
results1 = [];
for (k in ref1) {
v = ref1[k];
if (v.onMouseAction != null) {
px = evt.offsetX || evt.layerX;
py = evt.offsetY || evt.layerY;
results1.push(v.onMouseAction(type, px, py, evt));
} else {
results1.push(void 0);
}
}
return results1;
}
};
Space.prototype._mouseDown = function(evt) {
this._mouseAction("down", evt);
return this._mdown = true;
};
Space.prototype._mouseUp = function(evt) {
this._mouseAction("up", evt);
if (this._mdrag) {
this._mouseAction("drop", evt);
}
this._mdown = false;
return this._mdrag = false;
};
Space.prototype._mouseMove = function(evt) {
this._mouseAction("move", evt);
if (this._mdown) {
this._mdrag = true;
return this._mouseAction("drag", evt);
}
};
Space.prototype._mouseOver = function(evt) {
return this._mouseAction("over", evt);
};
Space.prototype._mouseOut = function(evt) {
this._mouseAction("out", evt);
if (this._mdrag) {
this._mouseAction("drop", evt);
}
return this._mdrag = false;
};
return Space;
})();
this.Space = Space;
CanvasSpace = (function(superClass) {
extend(CanvasSpace, superClass);
function CanvasSpace(id, bgcolor, context) {
if (id == null) {
id = 'pt_space';
}
if (bgcolor == null) {
bgcolor = false;
}
if (context == null) {
context = '2d';
}
this._resizeHandler = bind(this._resizeHandler, this);
CanvasSpace.__super__.constructor.apply(this, arguments);
this.space = document.querySelector("#" + this.id);
this.bound = null;
this.boundRect = {
top: 0,
left: 0,
width: 0,
height: 0
};
this.pixelScale = 1;
this.appended = true;
if (!this.space) {
this.space = document.createElement("canvas");
this.space.setAttribute("id", this.id);
this.appended = false;
}
this._mdown = false;
this._mdrag = false;
this.bgcolor = bgcolor;
this.ctx = this.space.getContext(context);
}
CanvasSpace.prototype.display = function(parent_id, readyCallback, devicePixelSupport) {
var r1, r2;
if (parent_id == null) {
parent_id = "#pt";
}
if (devicePixelSupport == null) {
devicePixelSupport = true;
}
if (!this.appended) {
this.bound = document.querySelector(parent_id);
this.boundRect = this.bound.getBoundingClientRect();
this.pixelScale = 1;
if (devicePixelSupport) {
r1 = window.devicePixelRatio || 1;
r2 = this.ctx.webkitBackingStorePixelRatio || this.ctx.mozBackingStorePixelRatio || this.ctx.msBackingStorePixelRatio || this.ctx.oBackingStorePixelRatio || this.ctx.backingStorePixelRatio || 1;
this.pixelScale = r1 / r2;
}
if (this.bound) {
this.resize(this.boundRect.width, this.boundRect.height);
this.autoResize(true);
if (this.space.parentNode !== this.bound) {
this.bound.appendChild(this.space);
}
this.appended = true;
setTimeout((function() {
this.space.dispatchEvent(new Event('ready'));
if (readyCallback) {
return readyCallback(this.boundRect.width, this.boundRect.height, this.space);
}
}).bind(this));
} else {
throw 'Cannot add canvas to element ' + parent_id;
}
}
return this;
};
CanvasSpace.prototype._resizeHandler = function(evt) {
this.boundRect = this.bound.getBoundingClientRect();
return this.resize(this.boundRect.width, this.boundRect.height, evt);
};
CanvasSpace.prototype.autoResize = function(auto) {
if (auto == null) {
auto = true;
}
if (auto) {
window.addEventListener('resize', this._resizeHandler);
} else {
window.removeEventListener('resize', this._resizeHandler);
}
return this;
};
CanvasSpace.prototype.resize = function(w, h, evt) {
var k, p, ref;
w = Math.floor(w);
h = Math.floor(h);
this.size.set(w, h);
this.center = new Vector(w / 2, h / 2);
this.boundRect.width = w;
this.boundRect.height = h;
this.space.width = w * this.pixelScale;
this.space.height = h * this.pixelScale;
this.space.style.width = w + "px";
this.space.style.height = h + "px";
if (this.pixelScale !== 1) {
this.ctx.scale(this.pixelScale, this.pixelScale);
}
ref = this.items;
for (k in ref) {
p = ref[k];
if (p.onSpaceResize != null) {
p.onSpaceResize(w, h, evt);
}
}
this.render(this.ctx);
return this;
};
CanvasSpace.prototype.clear = function(bg) {
var lastColor;
if (bg) {
this.bgcolor = bg;
}
lastColor = this.ctx.fillStyle;
if (this.bgcolor) {
this.ctx.fillStyle = this.bgcolor;
this.ctx.fillRect(0, 0, this.size.x, this.size.y);
} else {
this.ctx.clearRect(0, 0, this.size.x, this.size.y);
}
this.ctx.fillStyle = lastColor;
return this;
};
CanvasSpace.prototype.animate = function(time) {
var k, ref, v;
this.ctx.save();
if (this._refresh) {
this.clear();
}
ref = this.items;
for (k in ref) {
v = ref[k];
v.animate(time, this._timeDiff, this.ctx);
}
if (this._timeEnd >= 0 && time > this._timeEnd) {
cancelAnimationFrame(this._animID);
}
this.ctx.restore();
return this;
};
return CanvasSpace;
})(Space);
this.CanvasSpace = CanvasSpace;
DOMSpace = (function(superClass) {
extend(DOMSpace, superClass);
function DOMSpace(id, bgcolor, context) {
if (id == null) {
id = 'pt_space';
}
if (bgcolor == null) {
bgcolor = false;
}
if (context == null) {
context = 'html';
}
this._resizeHandler = bind(this._resizeHandler, this);
DOMSpace.__super__.constructor.apply(this, arguments);
this.space = document.querySelector("#" + this.id);
this.css = {
width: "100%",
height: "100%"
};
this.bound = null;
this.boundRect = {
top: 0,
left: 0,
width: 0,
height: 0
};
this.appended = true;
if (!this.space) {
this._createSpaceElement();
}
this._mdown = false;
this._mdrag = false;
this.bgcolor = bgcolor;
this.ctx = {};
}
DOMSpace.prototype._createSpaceElement = function() {
this.space = document.createElement("div");
this.space.setAttribute("id", this.id);
return this.appended = false;
};
DOMSpace.prototype.setCSS = function(key, val, isPx) {
if (isPx == null) {
isPx = false;
}
this.css[key] = (isPx ? val + "px" : val);
return this;
};
DOMSpace.prototype.updateCSS = function() {
var k, ref, results, v;
ref = this.css;
results = [];
for (k in ref) {
v = ref[k];
results.push(this.space.style[k] = v);
}
return results;
};
DOMSpace.prototype.display = function(parent_id, readyCallback) {
if (parent_id == null) {
parent_id = "#pt";
}
if (!this.appended) {
this.bound = document.querySelector(parent_id);
this.boundRect = this.bound.getBoundingClientRect();
if (this.bound) {
this.resize(this.boundRect.width, this.boundRect.height);
this.autoResize(true);
if (this.space.parentNode !== this.bound) {
this.bound.appendChild(this.space);
}
this.appended = true;
setTimeout((function() {
this.space.dispatchEvent(new Event('ready'));
if (readyCallback) {
return readyCallback(this.boundRect.width, this.boundRect.height, this.space);
}
}).bind(this));
} else {
throw 'Cannot add canvas to element ' + parent_id;
}
}
return this;
};
DOMSpace.prototype._resizeHandler = function(evt) {
this.boundRect = this.bound.getBoundingClientRect();
return this.resize(this.boundRect.width, this.boundRect.height, evt);
};
DOMSpace.prototype.autoResize = function(auto) {
if (auto == null) {
auto = true;
}
if (auto) {
window.addEventListener('resize', this._resizeHandler);
} else {
window.removeEventListener('resize', this._resizeHandler);
}
return this;
};
DOMSpace.prototype.resize = function(w, h, evt) {
var k, p, ref;
this.size.set(w, h);
this.center = new Vector(w / 2, h / 2);
ref = this.items;
for (k in ref) {
p = ref[k];
if (p.onSpaceResize != null) {
p.onSpaceResize(w, h, evt);
}
}
return this;
};
DOMSpace.prototype.clear = function() {
return this.space.innerHML = "";
};
DOMSpace.prototype.animate = function(time) {
var k, ref, v;
ref = this.items;
for (k in ref) {
v = ref[k];
v.animate(time, this._timeDiff, this.ctx);
}
if (this._timeEnd >= 0 && time > this._timeEnd) {
cancelAnimationFrame(this._animID);
0