!function(t,i,s){"use strict";t.requestAnimFrame=t.requestAnimationFrame||t.webkitRequestAnimationFrame||t.mozRequestAnimationFrame||t.oRequestAnimationFrame||t.msRequestAnimaitonFrame||function(i){t.setTimeout(i,100/60)};var n="jqSignature",e={lineColor:"#222222",lineWidth:1,border:"#AAAAAA",background:"#FFFFFF",width:300,height:100,autoFit:!1},a=0;function h(t,i){this.element=t,this.$element=s(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=s.extend({},e,i,this._data),this.init()}h.prototype={init:function(){this.id="jq-signature-canvas-"+ ++a,this.$canvas=s("<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",s.proxy(this._downHandler,this)),this.$canvas.on("mousemove touchmove",s.proxy(this._moveHandler,this)),this.$canvas.on("mouseup touchend",s.proxy(this._upHandler,this)),this.$canvas.on("mouseout touchend",s.proxy(this._upHandler,this));var i=this;!function s(){t.requestAnimFrame(s),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=s.Event("jq.signature.changed");this.$element.trigger(i),t.preventDefault()},_getPosition:function(t){var i,s,n;return n=this.canvas.getBoundingClientRect(),t.originalEvent&&(t=t.originalEvent),-1!==t.type.indexOf("touch")?(i=t.touches[0].clientX-n.left,s=t.touches[0].clientY-n.top):(i=t.clientX-n.left,s=t.clientY-n.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)}},s.fn[n]=function(t){var i,e=arguments;return void 0===t||"object"==typeof t?this.each(function(){s.data(this,"plugin_"+n)||s.data(this,"plugin_"+n,new h(this,t))}):"string"==typeof t&&"_"!==t[0]&&"init"!==t?(this.each(function(){var a=s.data(this,"plugin_"+n);a instanceof h&&"function"==typeof a[t]&&(i=a[t].apply(a,Array.prototype.slice.call(e,1))),"destroy"===t&&s.data(this,"plugin_"+n,null)}),void 0!==i?i:this):void 0}}(window,document,jQuery);