(function(n,t){typeof define=="function"&&define.amd?define(["exports"],t):typeof exports=="object"&&typeof exports.nodeName!="string"?t(exports):t(n.commonJsStrict={})})(this,function(n){function w(n){if(n in y)return n;for(var i=n[0].toUpperCase()+n.slice(1),t=v.length;t--;)if(n=v[t]+i,n in y)return n}function c(n,t){n=n||{};for(var i in t)t[i]&&t[i].constructor&&t[i].constructor===Object?(n[i]=n[i]||{},arguments.callee(n[i],t[i])):n[i]=t[i];return n}function et(n,t,i){var r;return function(){var u=this,f=arguments,e=function(){r=null;i||n.apply(u,f)},o=i&&!r;clearTimeout(r);r=setTimeout(e,t);o&&n.apply(u,f)}}function b(n){if("createEvent"in document){var t=document.createEvent("HTMLEvents");t.initEvent("change",!1,!0);n.dispatchEvent(t)}else n.fireEvent("onchange")}function i(n,t,i){var u,r;typeof t=="string"&&(u=t,t={},t[u]=i);for(r in t)n.style[r]=t[r]}function u(n,t){n.classList?n.classList.add(t):n.className+=" "+t}function ot(n,t){n.classList?n.classList.remove(t):n.className=n.className.replace(t,"")}function st(n,t){var i=t||new Image,r;return r=new Promise(function(t){n.substring(0,4).toLowerCase()==="http"&&i.setAttribute("crossOrigin","anonymous");i.onload=function(){setTimeout(function(){t(i)},1)}}),i.style.opacity=0,i.src=n,r}function ct(n,t){window.EXIF||t(0);EXIF.getData(n,function(){var n=EXIF.getTag(this,"Orientation");t(n)})}function lt(n,t,i,r){var u=i.width,f=i.height;n.width=i.width;n.height=i.height;t.save();switch(r){case 2:t.translate(u,0);t.scale(-1,1);break;case 3:t.translate(u,f);t.rotate(Math.PI/1);break;case 4:t.translate(0,f);t.scale(1,-1);break;case 5:n.width=f;n.height=u;t.rotate(Math.PI/2);t.scale(1,-1);break;case 6:n.width=f;n.height=u;t.rotate(Math.PI/2);t.translate(0,-f);break;case 7:n.width=f;n.height=u;t.rotate(Math.PI/-2);t.translate(-u,f);t.scale(1,-1);break;case 8:n.width=f;n.height=u;t.translate(0,u);t.rotate(Math.PI/-2)}t.drawImage(i,0,0,u,f);t.restore()}function at(){var n=this,e=n.options.viewport.type?"cr-vp-"+n.options.viewport.type:null,t,o,r,f;n.options.useCanvas=n.options.exif&&window.EXIF;n.data={};n.elements={};t=n.elements.boundary=document.createElement("div");r=n.elements.viewport=document.createElement("div");o=n.elements.img=document.createElement("img");f=n.elements.overlay=document.createElement("div");n.options.useCanvas?(n.elements.canvas=document.createElement("canvas"),n.elements.preview=n.elements.canvas):n.elements.preview=n.elements.img;u(t,"cr-boundary");i(t,{width:n.options.boundary.width+"px",height:n.options.boundary.height+"px"});u(r,"cr-viewport");e&&u(r,e);i(r,{width:n.options.viewport.width+"px",height:n.options.viewport.height+"px"});u(n.elements.preview,"cr-image");u(f,"cr-overlay");n.element.appendChild(t);t.appendChild(n.elements.preview);t.appendChild(r);t.appendChild(f);u(n.element,"croppie-container");n.options.customClass&&u(n.element,n.options.customClass);wt.call(this);n.options.enableZoom&&vt.call(n)}function l(n){this.options.enableZoom&&(this.elements.zoomer.value=e(n,4))}function vt(){function f(){nt.call(n);o=new s(n.elements.preview);h=n.elements.viewport.getBoundingClientRect();c=t.parse(n.elements.preview)}function e(){yt.call(n,{value:parseFloat(i.value),origin:o||new s(n.elements.preview),viewportRect:h||n.elements.viewport.getBoundingClientRect(),transform:c||t.parse(n.elements.preview)})}function a(t){var i,r;i=t.wheelDelta?t.wheelDelta/1200:t.deltaY?t.deltaY/1060:t.detail?t.detail/60:0;r=n._currentZoom+i;t.preventDefault();f();l.call(n,r);e()}var n=this,r=n.elements.zoomerWrap=document.createElement("div"),i=n.elements.zoomer=document.createElement("input"),o,h,c;u(r,"cr-slider-wrap");u(i,"cr-slider");i.type="range";i.step="0.01";i.value=1;i.style.display=n.options.showZoomer?"":"none";n.element.appendChild(r);r.appendChild(i);n._currentZoom=1;n.elements.zoomer.addEventListener("mousedown",f);n.elements.zoomer.addEventListener("touchstart",f);n.elements.zoomer.addEventListener("input",e);n.elements.zoomer.addEventListener("change",e);n.options.mouseWheelZoom&&(n.elements.boundary.addEventListener("mousewheel",a),n.elements.boundary.addEventListener("DOMMouseScroll",a))}function yt(n){var e=this,t=n.transform,l=n.viewportRect,o=n.origin,h;e._currentZoom=n.value;t.scale=e._currentZoom;var c=pt.call(e,l),u=c.translate,s=c.origin;t.x>=u.maxX&&(o.x=s.minX,t.x=u.maxX);t.x<=u.minX&&(o.x=s.maxX,t.x=u.minX);t.y>=u.maxY&&(o.y=s.minY,t.y=u.maxY);t.y<=u.minY&&(o.y=s.maxY,t.y=u.minY);h={};h[r]=t.toString();h[f]=o.toString();i(e.elements.preview,h);tt.call(e);a.call(e)}function pt(n){var i=this,t=i._currentZoom,r=n.width,u=n.height,v=i.options.boundary.width/2,y=i.options.boundary.height/2,p=i._originalImageWidth,w=i._originalImageHeight,f=p*t,e=w*t,o=r/2,s=u/2,h=(o/t-v)*-1,b=h-(f*(1/t)-r*(1/t)),c=(s/t-y)*-1,k=c-(e*(1/t)-u*(1/t)),l=1/t*o,d=f*(1/t)-l,a=1/t*s,g=e*(1/t)-a;return{translate:{maxX:h,minX:b,maxY:c,minY:k},origin:{maxX:d,minX:l,maxY:g,minY:a}}}function nt(){var n=this,e=n._currentZoom,a=n.elements.preview.getBoundingClientRect(),o=n.elements.viewport.getBoundingClientRect(),l=t.parse(n.elements.preview.style[r]),v=new s(n.elements.preview),y=o.top-a.top+o.height/2,p=o.left-a.left+o.width/2,u={},h={},c;u.y=y/e;u.x=p/e;h.y=(u.y-v.y)*(1-e);h.x=(u.x-v.x)*(1-e);l.x-=h.x;l.y-=h.y;c={};c[f]=u.x+"px "+u.y+"px";c[r]=l.toString();i(n.elements.preview,c)}function wt(){function v(i){if(i.preventDefault(),!c){if(c=!0,f=i.pageX,e=i.pageY,i.touches){var r=i.touches[0];f=r.pageX;e=r.pageY}transform=t.parse(n.elements.preview);window.addEventListener("mousemove",s);window.addEventListener("touchmove",s);window.addEventListener("mouseup",h);window.addEventListener("touchend",h);document.body.style[p]="none";u=n.elements.viewport.getBoundingClientRect()}}function s(t){var s,h,y,nt;t.preventDefault();s=t.pageX;h=t.pageY;t.touches&&(y=t.touches[0],s=y.pageX,h=y.pageY);var p=s-f,w=h-e,c=n.elements.preview.getBoundingClientRect(),tt=transform.y+w,it=transform.x+p,d={};if(t.type=="touchmove"&&t.touches.length>1){var a=t.touches[0],v=t.touches[1],g=Math.sqrt((a.pageX-v.pageX)*(a.pageX-v.pageX)+(a.pageY-v.pageY)*(a.pageY-v.pageY));o||(o=g/n._currentZoom);nt=g/o;l.call(n,nt);b(n.elements.zoomer);return}u.top>c.top+w&&u.bottom<c.bottom+w&&(transform.y=tt);u.left>c.left+p&&u.right<c.right+p&&(transform.x=it);d[r]=transform.toString();i(n.elements.preview,d);k.call(n);e=h;f=s}function h(){c=!1;window.removeEventListener("mousemove",s);window.removeEventListener("touchmove",s);window.removeEventListener("mouseup",h);window.removeEventListener("touchend",h);document.body.style[p]="";nt.call(n);a.call(n);o=0}var n=this,c=!1,f,e,o,u;n.elements.overlay.addEventListener("mousedown",v);n.elements.overlay.addEventListener("touchstart",v)}function k(){var t=this,r=t.elements.boundary.getBoundingClientRect(),n=t.elements.preview.getBoundingClientRect();i(t.elements.overlay,{width:n.width+"px",height:n.height+"px",top:n.top-r.top+"px",left:n.left-r.left+"px"})}function a(){var n=this;it.call(n)&&n.options.update.call(n,n.get())}function it(){return this.elements.preview.offsetHeight>0&&this.elements.preview.offsetWidth>0}function d(){var n=this,h=0,a=1.5,c=1,o={},v=n.elements.preview,y=n.elements.zoomer,p=new t(0,0,c),tt=new s,rt=it.call(n),u,w,d,g,nt;rt&&!n.data.bound&&(n.data.bound=!0,o[r]=p.toString(),o[f]=tt.toString(),o.opacity=1,i(v,o),u=v.getBoundingClientRect(),w=n.elements.viewport.getBoundingClientRect(),d=n.elements.boundary.getBoundingClientRect(),n._originalImageWidth=u.width,n._originalImageHeight=u.height,n.options.enableZoom&&(g=w.width/u.width,nt=w.height/u.height,h=Math.max(g,nt),h>=a&&(a=h+1),y.min=e(h,4),y.max=e(a,4),c=Math.max(d.width/u.width,d.height/u.height),l.call(n,c),b(y)),n._currentZoom=p.scale=c,o[r]=p.toString(),i(v,o),n.data.points.length?bt.call(n,n.data.points):kt.call(n),k.call(n))}function bt(n){if(n.length!=4)throw"Croppie - Invalid number of points supplied: "+n;var u=this,a=n[2]-n[0],e=u.elements.viewport.getBoundingClientRect(),h=u.elements.boundary.getBoundingClientRect(),c={left:e.left-h.left,top:e.top-h.top},o=e.width/a,v=n[1],y=n[0],p=-1*n[1]+c.top,w=-1*n[0]+c.left,s={};s[f]=y+"px "+v+"px";s[r]=new t(w,p,o).toString();i(u.elements.preview,s);l.call(u,o);u._currentZoom=o}function kt(){var n=this,f=n.elements.preview.getBoundingClientRect(),u=n.elements.viewport.getBoundingClientRect(),e=n.elements.boundary.getBoundingClientRect(),o=u.left-e.left,s=u.top-e.top,h=o-(f.width-u.width)/2,c=s-(f.height-u.height)/2,l=new t(h,c,n._currentZoom);i(n.elements.preview,r,l.toString())}function dt(){var i=this,n=i.elements.canvas,t=i.elements.img,r=n.getContext("2d");r.clearRect(0,0,n.width,n.height);n.width=t.width;n.height=t.height;ct(t,function(i){lt(n,r,t,parseInt(i))})}function gt(n){var t=n.points,r=document.createElement("div"),f=document.createElement("img"),e=t[2]-t[0],o=t[3]-t[1];return u(r,"croppie-result"),r.appendChild(f),i(f,{left:-1*t[0]+"px",top:-1*t[1]+"px"}),f.src=n.url,i(r,{width:e+"px",height:o+"px"}),r}function ni(n,t){var r=t.points,h=r[0],c=r[1],o=r[2]-r[0],s=r[3]-r[1],l=t.circle,f=document.createElement("canvas"),i=f.getContext("2d"),u=o,e=s;return t.outputWidth&&t.outputHeight&&(u=t.outputWidth,e=t.outputHeight),f.width=u,f.height=e,i.drawImage(n,h,c,o,s,0,0,u,e),l&&(i.fillStyle="#fff",i.globalCompositeOperation="destination-in",i.beginPath(),i.arc(u/2,e/2,u/2,0,Math.PI*2,!0),i.closePath(),i.fill()),f.toDataURL(t.format,t.quality)}function ti(n,t){var i=this,r,u=[],f;if(typeof n=="string")r=n,n={};else if(Array.isArray(n))u=n.slice();else{if(typeof n=="undefined"&&i.data.url)return d.call(i),a.call(i),null;r=n.url;u=n.points||[]}return i.data.bound=!1,i.data.url=r||i.data.url,i.data.points=(u||i.data.points).map(function(n){return parseFloat(n)}),f=st(r,i.elements.img),f.then(function(){i.options.useCanvas&&(i.elements.img.exifdata=null,dt.call(i));d.call(i);a.call(i);t&&t()}),f}function e(n,t){return parseFloat(n).toFixed(t||0)}function rt(){var u=this,s=u.elements.preview.getBoundingClientRect(),t=u.elements.viewport.getBoundingClientRect(),i=t.left-s.left,r=t.top-s.top,f=i+t.width,o=r+t.height,n=u._currentZoom;return(n===Infinity||isNaN(n))&&(n=1),i=Math.max(0,i/n),r=Math.max(0,r/n),f=Math.max(0,f/n),o=Math.max(0,o/n),{points:[e(i),e(r),e(f),e(o)],zoom:n}}function ii(n){var i=this,t=rt.call(i),r=c(ut,c({},n)),e=typeof n=="string"?n:r.type,o=r.size,f=r.format,s=r.quality,u;return o==="viewport"&&(u=i.elements.viewport.getBoundingClientRect(),t.outputWidth=u.width,t.outputHeight=u.height),ft.indexOf(f)>-1&&(t.format="image/"+f,t.quality=s),t.circle=i.options.viewport.type==="circle",t.url=i.data.url,new Promise(function(n){e==="canvas"?n(ni.call(i,i.elements.preview,t)):n(gt.call(i,t))})}function ri(){d.call(this)}function ui(){var n=this;n.element.removeChild(n.elements.boundary);ot(n.element,"croppie-container");n.options.enableZoom&&n.element.removeChild(n.elements.zoomerWrap);delete n.elements}function h(n,t){this.element=n;this.options=c(c({},h.defaults),t);at.call(this)}var v,y,f,r,p,s,tt,ut,ft,o;typeof Promise!="function"&&!function(n){function i(n,t){return function(){n.apply(t,arguments)}}function t(n){if("object"!=typeof this)throw new TypeError("Promises must be constructed via new");if("function"!=typeof n)throw new TypeError("not a function");this._state=null;this._value=null;this._deferreds=[];o(n,i(f,this),i(r,this))}function u(n){var t=this;return null===this._state?void this._deferreds.push(n):void s(function(){var i=t._state?n.onFulfilled:n.onRejected,r;if(null===i)return void(t._state?n.resolve:n.reject)(t._value);try{r=i(t._value)}catch(u){return void n.reject(u)}n.resolve(r)})}function f(n){try{if(n===this)throw new TypeError("A promise cannot be resolved with itself.");if(n&&("object"==typeof n||"function"==typeof n)){var t=n.then;if("function"==typeof t)return void o(i(t,n),i(f,this),i(r,this))}this._state=!0;this._value=n;e.call(this)}catch(u){r.call(this,u)}}function r(n){this._state=!1;this._value=n;e.call(this)}function e(){for(var n=0,t=this._deferreds.length;t>n;n++)u.call(this,this._deferreds[n]);this._deferreds=null}function h(n,t,i,r){this.onFulfilled="function"==typeof n?n:null;this.onRejected="function"==typeof t?t:null;this.resolve=i;this.reject=r}function o(n,t,i){var r=!1;try{n(function(n){r||(r=!0,t(n))},function(n){r||(r=!0,i(n))})}catch(u){if(r)return;r=!0;i(u)}}var c=setTimeout,s="function"==typeof setImmediate&&setImmediate||function(n){c(n,1)},l=Array.isArray||function(n){return"[object Array]"===Object.prototype.toString.call(n)};t.prototype["catch"]=function(n){return this.then(null,n)};t.prototype.then=function(n,i){var r=this;return new t(function(t,f){u.call(r,new h(n,i,t,f))})};t.all=function(){var n=Array.prototype.slice.call(1===arguments.length&&l(arguments[0])?arguments[0]:arguments);return new t(function(t,i){function u(r,e){try{if(e&&("object"==typeof e||"function"==typeof e)){var o=e.then;if("function"==typeof o)return void o.call(e,function(n){u(r,n)},i)}n[r]=e;0==--f&&t(n)}catch(s){i(s)}}if(0===n.length)return t([]);for(var f=n.length,r=0;r<n.length;r++)u(r,n[r])})};t.resolve=function(n){return n&&"object"==typeof n&&n.constructor===t?n:new t(function(t){t(n)})};t.reject=function(n){return new t(function(t,i){i(n)})};t.race=function(n){return new t(function(t,i){for(var r=0,u=n.length;u>r;r++)n[r].then(t,i)})};t._setImmediateFn=function(n){s=n};"undefined"!=typeof module&&module.exports?module.exports=t:n.Promise||(n.Promise=t)}(this);v=["Webkit","Moz","ms"];y=document.createElement("div").style;r=w("transform");f=w("transformOrigin");p=w("userSelect");var g="translate3d",ht=", 0px",t=function(n,t,i){this.x=parseFloat(n);this.y=parseFloat(t);this.scale=parseFloat(i)};t.parse=function(n){return n.style?t.parse(n.style[r]):n.indexOf("matrix")>-1||n.indexOf("none")>-1?t.fromMatrix(n):t.fromString(n)};t.fromMatrix=function(n){var i=n.substring(7).split(",");return i.length&&n!=="none"||(i=[1,0,0,1,0,0]),new t(parseInt(i[4],10),parseInt(i[5],10),parseFloat(i[0]))};t.fromString=function(n){var r=n.split(") "),i=r[0].substring(g.length+1).split(","),u=r.length>1?r[1].substring(6):1,f=i.length>1?i[0]:0,e=i.length>1?i[1]:0;return new t(f,e,u)};t.prototype.toString=function(){return g+"("+this.x+"px, "+this.y+"px"+ht+") scale("+this.scale+")"};s=function(n){if(!n||!n.style[f]){this.x=0;this.y=0;return}var t=n.style[f].split(" ");this.x=parseFloat(t[0]);this.y=parseFloat(t[1])};s.prototype.toString=function(){return this.x+"px "+this.y+"px"};tt=et(k,500);ut={type:"canvas",size:"viewport",format:"png",quality:1};ft=["jpeg","webp","png"];this.jQuery&&(o=this.jQuery,o.fn.croppie=function(n){var r=typeof n,i,t;return r==="string"?(i=Array.prototype.slice.call(arguments,1),t=o(this).data("croppie"),n==="get")?t.get():n==="result"?t.result.apply(t,i):this.each(function(){var t=o(this).data("croppie"),r;if(t)if(r=t[n],o.isFunction(r))r.apply(t,i),n==="destroy"&&o(this).removeData("croppie");else throw"Croppie "+n+" method not found";}):this.each(function(){var t=new h(this,n);o(this).data("croppie",t)})});h.defaults={viewport:{width:100,height:100,type:"square"},boundary:{width:300,height:300},customClass:"",showZoomer:!0,enableZoom:!0,mouseWheelZoom:!0,exif:!1,update:function(){}};c(h.prototype,{bind:function(n,t){return ti.call(this,n,t)},get:function(){return rt.call(this)},result:function(n){return ii.call(this,n)},refresh:function(){return ri.call(this)},setZoom:function(n){l.call(this,n);b(this.elements.zoomer)},destroy:function(){return ui.call(this)}});n.Croppie=window.Croppie=h;typeof module!="object"||!module.exports||(module.exports=h)})