!function(s,e){"use strict";s.requestAnimFrame=s.requestAnimationFrame||s.webkitRequestAnimationFrame||s.mozRequestAnimationFrame||s.oRequestAnimationFrame||s.msRequestAnimaitonFrame||function(t){s.setTimeout(t,100/60)};var a="jqSignature",n={lineColor:"#222222",lineWidth:1,border:"#AAAAAA",background:"#FFFFFF",width:300,height:100,autoFit:!1},t=0;function h(t,i){this.element=t,this.$element=e(this.element),this.canvas=!1,this.$canvas=!1,this.ctx=!1,this.drawing=!1,this.currentPos={x:0,y:0},this.lastPos=this.currentPos,this._data=this.$element.data(),this.settings=e.extend({},n,i,this._data),this.init()}h.prototype={init:function(){this.id="jq-signature-canvas-"+ ++t,this.$canvas=e("<canvas></canvas>").appendTo(this.$element),this.$canvas.attr({width:this.settings.width,height:this.settings.height}),this.$canvas.css({boxSizing:"border-box",width:this.settings.width+"px",height:this.settings.height+"px",border:"1px dashed "+this.settings.border,background:this.settings.background,cursor:"crosshair"}),this.$canvas.attr("id",this.id),!0===this.settings.autoFit&&this._resizeCanvas(),this.canvas=this.$canvas[0],this._resetCanvas(),this.$canvas.on("mousedown touchstart",e.proxy(this._downHandler,this)),this.$canvas.on("mousemove touchmove",e.proxy(this._moveHandler,this)),this.$canvas.on("mouseup touchend",e.proxy(this._upHandler,this)),this.$canvas.on("mouseout touchend",e.proxy(this._upHandler,this));var i=this;!function t(){s.requestAnimFrame(t),i._renderCanvas()}()},clearCanvas:function(){this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height),this._resetCanvas()},getDataURL:function(){return this.canvas.toDataURL()},_downHandler:function(t){this.drawing=!0,this.lastPos=this.currentPos=this._getPosition(t),t.preventDefault()},_moveHandler:function(t){this.currentPos=this._getPosition(t),t.preventDefault()},_upHandler:function(t){this.drawing=!1;var i=e.Event("jq.signature.changed");this.$element.trigger(i),t.preventDefault()},_getPosition:function(t){var i,s=this.canvas.getBoundingClientRect();return t.originalEvent&&(t=t.originalEvent),s=-1!==t.type.indexOf("touch")?(i=t.touches[0].clientX-s.left,t.touches[0].clientY-s.top):(i=t.clientX-s.left,t.clientY-s.top),{x:i,y:s}},_renderCanvas:function(){this.drawing&&(this.ctx.beginPath(),this.ctx.moveTo(this.lastPos.x,this.lastPos.y),this.ctx.lineTo(this.currentPos.x,this.currentPos.y),this.ctx.stroke(),this.lastPos=this.currentPos)},_resetCanvas:function(){this.ctx=this.canvas.getContext("2d"),this.ctx.strokeStyle=this.settings.lineColor,this.ctx.lineWidth=this.settings.lineWidth},_resizeCanvas:function(){var t=this,i=t.$element.outerWidth();0==i?setTimeout(function(){i=t.$element.outerWidth(),t.$canvas.attr("width",i),t.$canvas.css("width",i+"px"),t._resetCanvas()},10):(t.$canvas.attr("width",i),t.$canvas.css("width",i+"px"))},_fitCanvas:function(){var t=this.$element.css("width");this.$canvas.attr("width",t),this.$canvas.css("width",t)}},e.fn[a]=function(i){var s,n=arguments;return void 0===i||"object"==typeof i?this.each(function(){e.data(this,"plugin_"+a)||e.data(this,"plugin_"+a,new h(this,i))}):"string"==typeof i&&"_"!==i[0]&&"init"!==i?(this.each(function(){var t=e.data(this,"plugin_"+a);t instanceof h&&"function"==typeof t[i]&&(s=t[i].apply(t,Array.prototype.slice.call(n,1))),"destroy"===i&&e.data(this,"plugin_"+a,null)}),void 0!==s?s:this):void 0}}(window,(document,jQuery));