forked from thread-pond/signature-pad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.signaturepad.min.js
11 lines (11 loc) · 8.06 KB
/
jquery.signaturepad.min.js
1
2
3
4
5
6
7
8
9
10
11
/**!
* SignaturePad: A jQuery plugin for assisting in the creation of an HTML5 canvas based signature pad. Records the drawn signature in JSON for later regeneration.
* @project signature-pad
* @author Thomas J Bradley <hey@thomasjbradley.ca>
* @link http://thomasjbradley.ca/lab/signature-pad
* @link https://github.com/thomasjbradley/signature-pad
* @copyright 2014 Thomas J Bradley
* @license BSD-3-CLAUSE
* @version 2.5.2
*/
!function(e){function t(t,n){function a(){clearTimeout(T),T=!1,S=!1}function i(t,n){var a,i,r;return t.preventDefault(),a=e(t.target).offset(),clearTimeout(T),T=!1,"undefined"!=typeof t.targetTouches?(i=Math.floor(t.targetTouches[0].pageX-a.left),r=Math.floor(t.targetTouches[0].pageY-a.top)):(i=Math.floor(t.pageX-a.left),r=Math.floor(t.pageY-a.top)),O.x===i&&O.y===r?!0:(null===O.x&&(O.x=i),null===O.y&&(O.y=r),n&&(r+=n),k.beginPath(),k.moveTo(O.x,O.y),k.lineTo(i,r),k.lineCap=I.penCap,k.stroke(),k.closePath(),P.push({lx:i,ly:r,mx:O.x,my:O.y}),O.x=i,O.y=r,void(I.onDraw&&"function"==typeof I.onDraw&&I.onDraw.apply(b)))}function r(){o()}function o(t){t?i(t,1):(W?D.each(function(){this.removeEventListener("touchmove",i)}):D.unbind("mousemove.signaturepad"),P.length>0&&I.onDrawEnd&&"function"==typeof I.onDrawEnd&&I.onDrawEnd.apply(b)),O.x=null,O.y=null,I.output&&P.length>0&&e(I.output,x).val(JSON.stringify(P))}function u(){return I.lineWidth?(k.beginPath(),k.lineWidth=I.lineWidth,k.strokeStyle=I.lineColour,k.moveTo(I.lineMargin,I.lineTop),k.lineTo(E.width-I.lineMargin,I.lineTop),k.stroke(),void k.closePath()):!1}function s(){k.clearRect(0,0,E.width,E.height),k.fillStyle=I.bgColour,k.fillRect(0,0,E.width,E.height),I.displayOnly||u(),k.lineWidth=I.penWidth,k.strokeStyle=I.penColour,e(I.output,x).val(""),P=[],o()}function l(e,t){null==O.x?i(e,1):i(e,t)}function d(e,t){W?t.addEventListener("touchmove",l,!1):D.bind("mousemove.signaturepad",l),i(e,1)}function c(){F=!1,D.each(function(){this.removeEventListener&&(this.removeEventListener("touchend",r),this.removeEventListener("touchcancel",r),this.removeEventListener("touchmove",i)),this.ontouchstart&&(this.ontouchstart=null)}),e(document).unbind("mouseup.signaturepad"),D.unbind("mousedown.signaturepad"),D.unbind("mousemove.signaturepad"),D.unbind("mouseleave.signaturepad"),e(I.clear,x).unbind("click.signaturepad")}function p(t){return F?!1:(F=!0,"undefined"!=typeof t.targetTouches&&(W=!0),void(W?(D.each(function(){this.addEventListener("touchend",r,!1),this.addEventListener("touchcancel",r,!1)}),D.unbind("mousedown.signaturepad")):(e(document).bind("mouseup.signaturepad",function(){S&&(o(),a())}),D.bind("mouseleave.signaturepad",function(e){S&&o(e),S&&!T&&(T=setTimeout(function(){o(),a()},500))}),D.each(function(){this.ontouchstart=null}))))}function f(){e(I.typed,x).hide(),s(),D.each(function(){this.ontouchstart=function(e){e.preventDefault(),S=!0,p(e),d(e,this)}}),D.bind("mousedown.signaturepad",function(e){return e.preventDefault(),e.which>1?!1:(S=!0,p(e),void d(e))}),e(I.clear,x).bind("click.signaturepad",function(e){e.preventDefault(),s()}),e(I.typeIt,x).bind("click.signaturepad",function(e){e.preventDefault(),h()}),e(I.drawIt,x).unbind("click.signaturepad"),e(I.drawIt,x).bind("click.signaturepad",function(e){e.preventDefault()}),e(I.typeIt,x).removeClass(I.currentClass),e(I.drawIt,x).addClass(I.currentClass),e(I.sig,x).addClass(I.currentClass),e(I.typeItDesc,x).hide(),e(I.drawItDesc,x).show(),e(I.clear,x).show()}function h(){s(),c(),e(I.typed,x).show(),e(I.drawIt,x).bind("click.signaturepad",function(e){e.preventDefault(),f()}),e(I.typeIt,x).unbind("click.signaturepad"),e(I.typeIt,x).bind("click.signaturepad",function(e){e.preventDefault()}),e(I.output,x).val(""),e(I.drawIt,x).removeClass(I.currentClass),e(I.typeIt,x).addClass(I.currentClass),e(I.sig,x).removeClass(I.currentClass),e(I.drawItDesc,x).hide(),e(I.clear,x).hide(),e(I.typeItDesc,x).show(),L=M=e(I.typed,x).css("font-size").replace(/px/,"")}function g(t){var n=e(I.typed,x),a=e.trim(t.replace(/>/g,">").replace(/</g,"<")),i=N,r=.5*L;if(N=a.length,n.html(a),!a)return void n.css("font-size",M+"px");if(N>i&&n.outerWidth()>E.width)for(;n.outerWidth()>E.width;)L--,n.css("font-size",L+"px");if(i>N&&n.outerWidth()+r<E.width&&M>L)for(;n.outerWidth()+r<E.width&&M>L;)L++,n.css("font-size",L+"px")}function v(t,n){e("p."+n.errorClass,t).remove(),t.removeClass(n.errorClass),e("input, label",t).removeClass(n.errorClass)}function m(t,n,a){t.nameInvalid&&(n.prepend(['<p class="',a.errorClass,'">',a.errorMessage,"</p>"].join("")),e(a.name,n).focus(),e(a.name,n).addClass(a.errorClass),e("label[for="+e(a.name).attr("id")+"]",n).addClass(a.errorClass)),t.drawInvalid&&n.prepend(['<p class="',a.errorClass,'">',a.errorMessageDraw,"</p>"].join(""))}function y(){var t=!0,n={drawInvalid:!1,nameInvalid:!1},a=[x,I],i=[n,x,I];return I.onBeforeValidate&&"function"==typeof I.onBeforeValidate?I.onBeforeValidate.apply(b,a):v.apply(b,a),I.drawOnly&&P.length<1&&(n.drawInvalid=!0,t=!1),""===e(I.name,x).val()&&(n.nameInvalid=!0,t=!1),I.onFormError&&"function"==typeof I.onFormError?I.onFormError.apply(b,i):m.apply(b,i),t}function w(e,t,n){for(var a in e)"object"==typeof e[a]&&(t.beginPath(),t.moveTo(e[a].mx,e[a].my),t.lineTo(e[a].lx,e[a].ly),t.lineCap=I.penCap,t.stroke(),t.closePath(),n&&P.push({lx:e[a].lx,ly:e[a].ly,mx:e[a].mx,my:e[a].my}))}function C(){parseFloat((/CPU.+OS ([0-9_]{3}).*AppleWebkit.*Mobile/i.exec(navigator.userAgent)||[0,"4_2"])[1].replace("_","."))<4.1&&(e.fn.Oldoffset=e.fn.offset,e.fn.offset=function(){var t=e(this).Oldoffset();return t.top-=window.scrollY,t.left-=window.scrollX,t}),e(I.typed,x).bind("selectstart.signaturepad",function(t){return e(t.target).is(":input")}),D.bind("selectstart.signaturepad",function(t){return e(t.target).is(":input")}),!E.getContext&&FlashCanvas&&FlashCanvas.initElement(E),E.getContext&&(k=E.getContext("2d"),e(I.sig,x).show(),I.displayOnly||(I.drawOnly||(e(I.name,x).bind("keyup.signaturepad",function(){g(e(this).val())}),e(I.name,x).bind("blur.signaturepad",function(){g(e(this).val())}),e(I.drawIt,x).bind("click.signaturepad",function(e){e.preventDefault(),f()})),I.drawOnly||"drawIt"===I.defaultAction?f():h(),I.validateFields&&(e(t).is("form")?e(t).bind("submit.signaturepad",function(){return y()}):e(t).parents("form").bind("submit.signaturepad",function(){return y()})),e(I.sigNav,x).show()))}var b=this,I=e.extend({},e.fn.signaturePad.defaults,n),x=e(t),D=e(I.canvas,x),E=D.get(0),k=null,O={x:null,y:null},P=[],T=!1,S=!1,W=!1,F=!1,M=30,L=M,N=0;e.extend(b,{signaturePad:"{{version}}",init:function(){C()},updateOptions:function(t){e.extend(I,t)},regenerate:function(t){b.clearCanvas(),e(I.typed,x).hide(),"string"==typeof t&&(t=JSON.parse(t)),w(t,k,!0),I.output&&e(I.output,x).length>0&&e(I.output,x).val(JSON.stringify(P))},clearCanvas:function(){s()},getSignature:function(){return P},getSignatureString:function(){return JSON.stringify(P)},getSignatureImage:function(){var e=document.createElement("canvas"),t=null,n=null;return e.style.position="absolute",e.style.top="-999em",e.width=E.width,e.height=E.height,document.body.appendChild(e),!e.getContext&&FlashCanvas&&FlashCanvas.initElement(e),t=e.getContext("2d"),t.fillStyle=I.bgColour,t.fillRect(0,0,E.width,E.height),t.lineWidth=I.penWidth,t.strokeStyle=I.penColour,w(P,t),n=e.toDataURL.apply(e,arguments),document.body.removeChild(e),e=null,n},validateForm:function(){return y()}})}e.fn.signaturePad=function(n){var a=null;return this.each(function(){e.data(this,"plugin-signaturePad")?(a=e.data(this,"plugin-signaturePad"),a.updateOptions(n)):(a=new t(this,n),a.init(),e.data(this,"plugin-signaturePad",a))}),a},e.fn.signaturePad.defaults={defaultAction:"typeIt",displayOnly:!1,drawOnly:!1,canvas:"canvas",sig:".sig",sigNav:".sigNav",bgColour:"#ffffff",penColour:"#145394",penWidth:2,penCap:"round",lineColour:"#ccc",lineWidth:2,lineMargin:5,lineTop:35,name:".name",typed:".typed",clear:".clearButton",typeIt:".typeIt a",drawIt:".drawIt a",typeItDesc:".typeItDesc",drawItDesc:".drawItDesc",output:".output",currentClass:"current",validateFields:!0,errorClass:"error",errorMessage:"Please enter your name",errorMessageDraw:"Please sign the document",onBeforeValidate:null,onFormError:null,onDraw:null,onDrawEnd:null}}(jQuery);