!function(n,t){"function"==typeof define&&define.amd?define([],function(){return n.SignaturePad=t()}):"object"==typeof exports?module.exports=t():n.SignaturePad=t()}(this,function(){return function(n){"use strict";var t=function(n,t){var i=this,r=t||{};this.velocityFilterWeight=r.velocityFilterWeight||.7;this.minWidth=r.minWidth||.5;this.maxWidth=r.maxWidth||2.5;this.dotSize=r.dotSize||function(){return(this.minWidth+this.maxWidth)/2};this.penColor=r.penColor||"black";this.backgroundColor=r.backgroundColor||"rgba(0,0,0,0)";this.onEnd=r.onEnd;this.onBegin=r.onBegin;this._canvas=n;this._ctx=n.getContext("2d");this.clear();this._handleMouseDown=function(n){1===n.which&&(i._mouseButtonDown=!0,i._strokeBegin(n))};this._handleMouseMove=function(n){i._mouseButtonDown&&i._strokeUpdate(n)};this._handleMouseUp=function(n){1===n.which&&i._mouseButtonDown&&(i._mouseButtonDown=!1,i._strokeEnd(n))};this._handleTouchStart=function(n){if(1==n.targetTouches.length){var t=n.changedTouches[0];i._strokeBegin(t)}};this._handleTouchMove=function(n){n.preventDefault();var t=n.targetTouches[0];i._strokeUpdate(t)};this._handleTouchEnd=function(n){var t=n.target===i._canvas;t&&(n.preventDefault(),i._strokeEnd(n))};this._handleMouseEvents();this._handleTouchEvents()},i,r;return t.prototype.clear=function(){var t=this._ctx,n=this._canvas;t.fillStyle=this.backgroundColor;t.clearRect(0,0,n.width,n.height);t.fillRect(0,0,n.width,n.height);this._reset()},t.prototype.toDataURL=function(){var n=this._canvas;return n.toDataURL.apply(n,arguments)},t.prototype.fromDataURL=function(n){var r=this,t=new Image,i=window.devicePixelRatio||1,u=this._canvas.width/i,f=this._canvas.height/i;this._reset();t.src=n;t.onload=function(){r._ctx.drawImage(t,0,0,u,f)};this._isEmpty=!1},t.prototype._strokeUpdate=function(n){var t=this._createPoint(n);this._addPoint(t)},t.prototype._strokeBegin=function(n){this._reset();this._strokeUpdate(n);"function"==typeof this.onBegin&&this.onBegin(n)},t.prototype._strokeDraw=function(n){var t=this._ctx,i="function"==typeof this.dotSize?this.dotSize():this.dotSize;t.beginPath();this._drawPoint(n.x,n.y,i);t.closePath();t.fill()},t.prototype._strokeEnd=function(n){var i=this.points.length>2,t=this.points[0];!i&&t&&this._strokeDraw(t);"function"==typeof this.onEnd&&this.onEnd(n)},t.prototype._handleMouseEvents=function(){this._mouseButtonDown=!1;this._canvas.addEventListener("mousedown",this._handleMouseDown);this._canvas.addEventListener("mousemove",this._handleMouseMove);n.addEventListener("mouseup",this._handleMouseUp)},t.prototype._handleTouchEvents=function(){this._canvas.style.msTouchAction="none";this._canvas.style.touchAction="none";this._canvas.addEventListener("touchstart",this._handleTouchStart);this._canvas.addEventListener("touchmove",this._handleTouchMove);this._canvas.addEventListener("touchend",this._handleTouchEnd)},t.prototype.on=function(){this._handleMouseEvents();this._handleTouchEvents()},t.prototype.off=function(){this._canvas.removeEventListener("mousedown",this._handleMouseDown);this._canvas.removeEventListener("mousemove",this._handleMouseMove);n.removeEventListener("mouseup",this._handleMouseUp);this._canvas.removeEventListener("touchstart",this._handleTouchStart);this._canvas.removeEventListener("touchmove",this._handleTouchMove);this._canvas.removeEventListener("touchend",this._handleTouchEnd)},t.prototype.isEmpty=function(){return this._isEmpty},t.prototype._reset=function(){this.points=[];this._lastVelocity=0;this._lastWidth=(this.minWidth+this.maxWidth)/2;this._isEmpty=!0;this._ctx.fillStyle=this.penColor},t.prototype._createPoint=function(n){var t=this._canvas.getBoundingClientRect();return new i(n.clientX-t.left,n.clientY-t.top)},t.prototype._addPoint=function(n){var u,f,e,i,t=this.points;t.push(n);t.length>2&&(3===t.length&&t.unshift(t[0]),i=this._calculateCurveControlPoints(t[0],t[1],t[2]),u=i.c2,i=this._calculateCurveControlPoints(t[1],t[2],t[3]),f=i.c1,e=new r(t[1],u,f,t[2]),this._addCurve(e),t.shift())},t.prototype._calculateCurveControlPoints=function(n,t,r){var e=n.x-t.x,o=n.y-t.y,s=t.x-r.x,h=t.y-r.y,f={x:(n.x+t.x)/2,y:(n.y+t.y)/2},u={x:(t.x+r.x)/2,y:(t.y+r.y)/2},p=Math.sqrt(e*e+o*o),c=Math.sqrt(s*s+h*h),w=f.x-u.x,b=f.y-u.y,l=c/(p+c),a={x:u.x+w*l,y:u.y+b*l},v=t.x-a.x,y=t.y-a.y;return{c1:new i(f.x+v,f.y+y),c2:new i(u.x+v,u.y+y)}},t.prototype._addCurve=function(n){var t,i,r=n.startPoint,u=n.endPoint;t=u.velocityFrom(r);t=this.velocityFilterWeight*t+(1-this.velocityFilterWeight)*this._lastVelocity;i=this._strokeWidth(t);this._drawCurve(n,this._lastWidth,i);this._lastVelocity=t;this._lastWidth=i},t.prototype._drawPoint=function(n,t,i){var r=this._ctx;r.moveTo(n,t);r.arc(n,t,i,0,2*Math.PI,!1);this._isEmpty=!1},t.prototype._drawCurve=function(n,t,i){var l,y,o,r,s,h,u,c,a,f,e,v=this._ctx,p=i-t;for(l=Math.floor(n.length()),v.beginPath(),o=0;l>o;o++)r=o/l,s=r*r,h=s*r,u=1-r,c=u*u,a=c*u,f=a*n.startPoint.x,f+=3*c*r*n.control1.x,f+=3*u*s*n.control2.x,f+=h*n.endPoint.x,e=a*n.startPoint.y,e+=3*c*r*n.control1.y,e+=3*u*s*n.control2.y,e+=h*n.endPoint.y,y=t+h*p,this._drawPoint(f,e,y);v.closePath();v.fill()},t.prototype._strokeWidth=function(n){return Math.max(this.maxWidth/(n+1),this.minWidth)},i=function(n,t,i){this.x=n;this.y=t;this.time=i||(new Date).getTime()},i.prototype.velocityFrom=function(n){return this.time!==n.time?this.distanceTo(n)/(this.time-n.time):1},i.prototype.distanceTo=function(n){return Math.sqrt(Math.pow(this.x-n.x,2)+Math.pow(this.y-n.y,2))},r=function(n,t,i,r){this.startPoint=n;this.control1=t;this.control2=i;this.endPoint=r},r.prototype.length=function(){for(var t,i,r,e,o,u,f,s=10,h=0,n=0;s>=n;n++)t=n/s,i=this._point(t,this.startPoint.x,this.control1.x,this.control2.x,this.endPoint.x),r=this._point(t,this.startPoint.y,this.control1.y,this.control2.y,this.endPoint.y),n>0&&(u=i-e,f=r-o,h+=Math.sqrt(u*u+f*f)),e=i,o=r;return h},r.prototype._point=function(n,t,i,r,u){return t*(1-n)*(1-n)*(1-n)+3*i*(1-n)*(1-n)*n+3*r*(1-n)*n*n+u*n*n*n},t}(document)})