text: "use strict";function _classCallCheck(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function _defineProperties(t,e){for(var i=0;i<e.length;i++){var a=e[i];a.enumerable=a.enumerable||!1,a.configurable=!0,"value"in a&&(a.writable=!0),Object.defineProperty(t,a.key,a)}}function _createClass(t,e,i){return e&&_defineProperties(t.prototype,e),i&&_defineProperties(t,i),Object.defineProperty(t,"prototype",{writable:!1}),t}var pixelit=function(){function t(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};_classCallCheck(this,t),this.drawto=e.to||document.getElementById("pixelitcanvas"),this.drawfrom=e.from||document.getElementById("pixelitimg"),this.hideFromImg(),this.scale=e.scale&&e.scale>0&&e.scale<=50?.01*e.scale:.08,this.palette=e.palette||[[140,143,174],[88,69,99],[62,33,55],[154,99,72],[215,155,125],[245,237,186],[192,199,65],[100,125,52],[228,148,58],[157,48,59],[210,100,113],[112,55,127],[126,196,193],[52,133,157],[23,67,75],[31,14,28]],this.maxHeight=e.maxHeight,this.maxWidth=e.maxWidth,this.ctx=this.drawto.getContext("2d"),this.endColorStats={}}return _createClass(t,[{key:"hideFromImg",value:function(){return this.drawfrom.style.visibility="hidden",this.drawfrom.style.position="fixed",this.drawfrom.style.top=0,this.drawfrom.style.left=0,this}},{key:"setFromImgSource",value:function(t){return this.drawfrom.src=t,this}},{key:"setDrawFrom",value:function(t){return this.drawfrom=t,this}},{key:"setDrawTo",value:function(t){return this.drawto=t,this}},{key:"setPalette",value:function(t){return this.palette=t,this}},{key:"setMaxWidth",value:function(t){return this.maxWidth=t,this}},{key:"setMaxHeight",value:function(t){return this.maxHeight=t,this}},{key:"setScale",value:function(t){return this.scale=t>0&&t<=50?.01*t:.08,this}},{key:"getPalette",value:function(){return this.palette}},{key:"colorSim",value:function(t,e){var i,a,h=0;for(i=0,a=t.length;i<a;i++)h+=(t[i]-e[i])*(t[i]-e[i]);return Math.sqrt(h)}},{key:"similarColor",value:function(t){var e,i=this,a=[],h=this.colorSim(t,this.palette[0]);return this.palette.forEach((function(r){(e=i.colorSim(t,r))<=h&&(a=r,h=e)})),a}},{key:"pixelate",value:function(){this.drawto.width=this.drawfrom.naturalWidth,this.drawto.height=this.drawfrom.naturalHeight;var t=this.drawto.width*this.scale,e=this.drawto.height*this.scale,i=document.createElement("canvas");return i.width=this.drawto.width,i.height=this.drawto.height,i.style.visibility="hidden",i.style.position="fixed",i.style.top="0",i.style.left="0",(this.drawto.width>800||this.drawto.height>800)&&(this.scale*=.25,t=this.drawto.width*this.scale,e=this.drawto.height*this.scale,i.width=Math.max(t,e)+50,i.height=Math.max(t,e)+50),i.getContext("2d").drawImage(this.drawfrom,0,0,t,e),document.body.appendChild(i),this.ctx.mozImageSmoothingEnabled=!1,this.ctx.webkitImageSmoothingEnabled=!1,this.ctx.imageSmoothingEnabled=!1,this.ctx.drawImage(i,0,0,t,e,0,0,this.drawfrom.naturalWidth+Math.max(24,25*this.scale),this.drawfrom.naturalHeight+Math.max(24,25*this.scale)),i.remove(),this}},{key:"convertGrayscale",value:function(){for(var t=this.drawto.width,e=this.drawto.height,i=this.ctx.getImageData(0,0,t,e),a=0;a<i.height;a++)for(var h=0;h<i.width;h++){var r=4*a*i.width+4*h,s=(i.data[r]+i.data[r+1]+i.data[r+2])/3;i.data[r]=s,i.data[r+1]=s,i.data[r+2]=s}return this.ctx.putImageData(i,0,0,0,0,i.width,i.height),this}},{key:"convertPalette",value:function(){for(var t=this.drawto.width,e=this.drawto.height,i=this.ctx.getImageData(0,0,t,e),a=0;a<i.height;a++)for(var h=0;h<i.width;h++){var r=4*a*i.width+4*h,s=this.similarColor([i.data[r],i.data[r+1],i.data[r+2]]);i.data[r]=s[0],i.data[r+1]=s[1],i.data[r+2]=s[2]}return this.ctx.putImageData(i,0,0,0,0,i.width,i.height),this}},{key:"resizeImage",value:function(){var t=document.createElement("canvas"),e=t.getContext("2d"),i=1;return this.maxWidth||this.maxHeight?(this.maxWidth&&this.drawto.width>this.maxWidth&&(i=this.maxWidth/this.drawto.width),this.maxHeight&&this.drawto.height>this.maxHeight&&(i=this.maxHeight/this.drawto.height),t.width=this.drawto.width,t.height=this.drawto.height,e.drawImage(this.drawto,0,0),this.drawto.width=this.drawto.width*i,this.drawto.height=this.drawto.height*i,this.ctx.drawImage(t,0,0,t.width,t.height,0,0,this.drawto.width,this.drawto.height),this):0}},{key:"draw",value:function(){return this.drawto.width=this.drawfrom.width,this.drawto.height=this.drawfrom.height,this.ctx.drawImage(this.drawfrom,0,0),this.resizeImage(),this}},{key:"saveImage",value:function(){var t=document.createElement("a");t.download="pxArt.png",t.href=this.drawto.toDataURL("image/png").replace("image/png","image/octet-stream"),document.querySelector("body").appendChild(t),t.click(),document.querySelector("body").removeChild(t)}}]),t}();