-
Notifications
You must be signed in to change notification settings - Fork 0
/
cabbage.min.js
1 lines (1 loc) · 4.82 KB
/
cabbage.min.js
1
!function(a){function b(a,b,e){var f=this;this.x=a,this.y=b,this.neighbors={},e&&d.forEach(function(a){f[a]=e.shift()}),c.forEach(function(a){f.neighbors[a]=f[a]()})}var c=["n","e","s","w","ne","nw","se","sw"],d=["r","g","b","a"];b.prototype.n=function(){return{x:this.x,y:this.y-1}},b.prototype.e=function(){return{x:this.x+1,y:this.y}},b.prototype.s=function(){return{x:this.x,y:this.y+1}},b.prototype.w=function(){return{x:this.x-1,y:this.y}},b.prototype.ne=function(){return{x:this.x+1,y:this.y-1}},b.prototype.nw=function(){return{x:this.x-1,y:this.y-1}},b.prototype.se=function(){return{x:this.x+1,y:this.y-1}},b.prototype.sw=function(){return{x:this.x+1,y:this.y-1}},a.Pixel=b}(this),function(a){function b(a,b,d,e){c=c||e,this.width=b||600,this.height=d||400,this.elem=c.getElementById(a)||this._createCanvas(a),this.ctx=this.elem.getContext("2d"),this.origImg={},this.currImg={}}var c=a.document,d="coordinate",e="image data index";b.prototype.loadImg=function(a,b,c){var d=this;return"string"==typeof a?this._createImage(a,function(a){d._img=a,(a.width!==d.width||a.height!==d.height)&&(d.width=a.width,d.height=a.height,d.elem.width=d.width,d.elem.height=d.height),d.drawImage(b,c)}):/HTMLImageElement/.test(a.constructor)&&(this._img=a,this.drawImage(b,c)),this},b.prototype._createImage=function(a,b){usrImg=new Image,usrImg.onload=function(){b(usrImg)},usrImg.src=a},b.prototype._createCanvas=function(a){var b;return b=c.createElement("canvas"),b.id=a,b.width=this.width,b.height=this.height,c.body.insertBefore(b,c.body.firstChild),b},b.prototype.drawImage=function(a,b){this.ctx.drawImage(this._img,a||0,b||0),this.refreshCurrImageData(),this.origImg=this.getCurrentImg()},b.prototype.setImg=function(){this.putImageData(this.currImg,0,0)},b.prototype.resetImageData=function(){this.putImageData(this.origImg)},b.prototype.getCurrentImg=function(){return this.ctx.getImageData(0,0,this.width,this.height)},b.prototype.originalImg=function(){return this.ctx.createImageData(this.origImg)},b.prototype.map=function(a){var b,c,d,e;for(c=0;c<this.height;c++)for(b=0;b<this.width;b++)e=c*this.width+b,d=4*b+c*this.width*4,a(b,c,e,d)},b.prototype.convolve=function(a,b){var c,d,e,f,g;for(b=b||3,d=0;d<this.height;d++)for(c=0;c<this.width;c++)f=d*this.width+c,e=4*c+d*this.width*4,g=this._buildMatrix(c,d,b),a(g,c,d,f,e)},b.prototype.getPixel=function(a){var b,c,f;return"number"==typeof a?(this._checkValidIDIndex(a)||this._throwValidationError(e,d),b=a,c=this._convertIDIndexToCoords(a)):(this._checkValidCoords(a)||this._throwValidationError(d,e),b=this._convertCoordsToIDIndex(a),c=a),f=Array.prototype.slice.call(this.currImg.data,b,b+4),new Pixel(c.x,c.y,f)},b.prototype.setPixel=function(a,b){var c=this._convertCoordsToIDIndex(a);"number"==typeof b?(this.currImg.data[c]=b,this.currImg.data[c+1]=b,this.currImg.data[c+2]=b):(this.currImg.data[c]=b.r,this.currImg.data[c+1]=b.g,this.currImg.data[c+2]=b.b,this.currImg.data[c+3]=b.a||255)},b.prototype._drawPixel=function(a,b,c,d,e){this.ctx.fillStyle="rgba("+[b,c,d,e].join(", ")+")",this.ctx.fillRect(a.x,a.y,1,1)},b.prototype.isBorder=function(a){return 0===a.x&&a.y<this.height&&a.y>=0||0===a.y&&a.x<this.width&&a.x>=0||a.x===this.width-1&&a.y<this.height&&a.y>=0||a.y===this.height-1&&a.x<this.width&&a.x>=0},b.prototype.isOutOfBounds=function(a){return a.x<0||a.x>this.width-1||a.y<0||a.y>this.height-1},b.prototype.putImageData=function(a,b,c){this.ctx.putImageData(a,b||0,c||0),this.refreshCurrImageData()},b.prototype.refreshCurrImageData=function(){this.currImg=this.getCurrentImg()},b.prototype._buildMatrix=function(a,b,c){var d,e,f=[],g=3,h=Math.max(this.width,this.height);c=c||3,c=c%2===0?c+1:c,(g>c||c>h)&&(c=g);for(var i=0,j=-(c-1)/2;c>i;i++,j++){f[i]=[];for(var k=0,l=-(c-1)/2;c>k;k++,l++)d=a+l,e=b+j,0>d||d>=this.width||0>e||e>=this.height?f[i][k]=void 0:f[i][k]=this._convertCoordsToIDIndex({x:d,y:e})}return f},b.prototype._convertCoordsToIDIndex=function(a){var b=4;return(a.y*this.width+a.x)*b},b.prototype._convertCoordsToPixIndex=function(a){return a.y*this.width+a.x},b.prototype._checkValidCoords=function(a,b){return!!a&&a.x===parseInt(a.x,10)&&a.y===parseInt(a.y,10)&&a.x>=0&&a.x<this.width&&a.y>=0&&a.y<this.height},b.prototype._checkValidPIndex=function(a,b){return a===parseInt(a,10)&&a>=0&&a<this.width*this.height},b.prototype._checkValidIDIndex=function(a,b){return a===parseInt(a,10)&&a>=0&&a<this.width*this.height*4},b.prototype._convertIDIndexToCoords=function(a){var b=4;return a%4>0&&(a-=a%4),{x:a%(this.width*b)/b,y:Math.floor(a/(this.width*b))}},b.prototype._convertIDIndexToPixIndex=function(a){var b=4;return Math.floor(a/b)},b.prototype._convertPixIndexToCoords=function(a){return{x:a%this.width,y:Math.floor(a/this.width)}},b.prototype._convertPixIndexToIDIndex=function(a){return 4*a},b.prototype._throwValidationError=function(a,b){var c="Invalid "+a+". Unable to convert to "+b;throw new Error(c)},a.Cabbage=b}(this);