cdf
Version:
A library for creating oldschool demo-like animations with JavaScript
47 lines (39 loc) • 1.16 kB
JavaScript
var utils = require('utils');
var Points = function() {
this.push.apply(this, arguments);
};
Points.prototype = Object.create(Array.prototype);
Points.prototype.push = function(){
var that = this;
[].forEach.call(arguments, function(xyPair){
if(!xyPair.forEach || xyPair[1] === undefined || !xyPair[0] === undefined )return;
Array.prototype.push.call(that, [xyPair[0],xyPair[1]]);
});
return this;
}
Points.prototype.map = function(){
var newP = new Points();
[].push.apply(newP, Array.prototype.map.apply(this, arguments));
return newP;
};
Points.prototype.rotate = function(rotInDeg, pivot){
if(rotInDeg === undefined) return this;
return this.map(function (pair) {
return utils.coordRotate(pair, rotInDeg, pivot);
});
};
Points.prototype.scale = function (xs, ys) {
if(xs.length){ys = xs[1];xs=xs[0]}
xs = xs || 1;
ys = ys || xs;
return this.map(function (pair) {
return [pair[0]*(xs), pair[1]*(ys)];
});
};
Points.prototype.translate = function(x, y){
if(x.length){ y = x[1]; x = x[0];}
return this.map(function (pair) {
return [pair[0]+(x||0), pair[1]+(y||1)];
});
};
module.exports = Points;