src/pyams_content/zmi/resources/js/jquery-canvasAreaDraw.min.js
changeset 1072 82860da48dbb
parent 1070 ea0c7ac589c4
equal deleted inserted replaced
1071:e202798d3732 1072:82860da48dbb
     1 (function(b){b.fn.canvasAreaDraw=function(d){this.each(function(e,f){c.apply(f,[e,f,d])})};var c=function(i,o,h){var u,g,x;var p,j,s,q;var l,t,m,r,d,z,y,w,f;var e;var k=false;x=b.extend({imageUrl:b(this).attr("data-image-url")},h);var n=b(o).val().replace(/[^0-9\,]/ig,"");if(n.length){u=n.split(",").map(function(v){return parseInt(v,10)})}else{u=[]}p=b('<i class="fa fa-fw fa-trash"></i>');j=b("<canvas>");s=j[0].getContext("2d");q=new Image();z=function(){j.attr("height",q.height).attr("width",q.width);l()};b(q).load(z);q.src=x.imageUrl;if(q.loaded){z()}j.css({background:"url("+q.src+")"});if(o.type!=="hidden"){b(o).after("<br />")}b(o).after(j,"<br />",p);y=function(){u=[];l()};r=function(v){if(!v.offsetX){v.offsetX=(v.pageX-b(v.target).offset().left);v.offsetY=(v.pageY-b(v.target).offset().top)}u[g]=Math.round(v.offsetX);u[g+1]=Math.round(v.offsetY);l()};d=function(B){if(!B.offsetX){B.offsetX=(B.pageX-b(B.target).offset().left);B.offsetY=(B.pageY-b(B.target).offset().top)}if(!k){k={x:Math.round(B.offsetX),y:Math.round(B.offsetY)}}var v={x:Math.round(B.offsetX),y:Math.round(B.offsetY)};for(var A=0;A<u.length;A++){u[A]=(v.x-k.x)+u[A];u[++A]=(v.y-k.y)+u[A]}k=v;l()};m=function(){b(this).off("mousemove");f();g=null};w=function(B){B.preventDefault();if(!B.offsetX){B.offsetX=(B.pageX-b(B.target).offset().left);B.offsetY=(B.pageY-b(B.target).offset().top)}var v=B.offsetX,C=B.offsetY;for(var A=0;A<u.length;A+=2){dis=Math.sqrt(Math.pow(v-u[A],2)+Math.pow(C-u[A+1],2));if(dis<6){u.splice(A,2);l();f();return false}}return false};t=function(E){var v,G,A,D,C=u.length;if(E.which===3){return false}E.preventDefault();if(!E.offsetX){E.offsetX=(E.pageX-b(E.target).offset().left);E.offsetY=(E.pageY-b(E.target).offset().top)}v=E.offsetX;G=E.offsetY;if(u.length>=6){var F=getCenter();s.fillRect(F.x-4,F.y-4,8,8);A=Math.sqrt(Math.pow(v-F.x,2)+Math.pow(G-F.y,2));if(A<6){k=false;b(this).on("mousemove",d);return false}}for(var B=0;B<u.length;B+=2){A=Math.sqrt(Math.pow(v-u[B],2)+Math.pow(G-u[B+1],2));if(A<6){g=B;b(this).on("mousemove",r);return false}}for(var B=0;B<u.length;B+=2){if(B>1){D=a(v,G,u[B],u[B+1],u[B-2],u[B-1],true);if(D<6){C=B}}}u.splice(C,0,Math.round(v),Math.round(G));g=C;b(this).on("mousemove",r);l();f();return false};l=function(){s.canvas.width=s.canvas.width;f();if(u.length<2){return}s.globalCompositeOperation="destination-over";s.fillStyle="rgb(255,255,255)";s.strokeStyle="rgb(255,20,20)";s.lineWidth=1;if(u.length>=6){var A=getCenter();s.fillRect(A.x-4,A.y-4,8,8)}s.beginPath();s.moveTo(u[0],u[1]);for(var v=0;v<u.length;v+=2){s.fillRect(u[v]-2,u[v+1]-2,4,4);s.strokeRect(u[v]-2,u[v+1]-2,4,4);if(u.length>2&&v>1){s.lineTo(u[v],u[v+1])}}s.closePath();s.fillStyle="rgba(255,0,0,0.3)";s.fill();s.stroke()};f=function(){b(o).val(u.join(","))};getCenter=function(){var E=[];for(A=0;A<u.length;A++){E.push({x:u[A],y:u[++A]})}var C=E[0],I=E[E.length-1];if(C.x!=I.x||C.y!=I.y){E.push(C)}var B=0,G=0,F=0,K=E.length,J,H,D;for(var A=0,v=K-1;A<K;v=A++){J=E[A];H=E[v];D=J.x*H.y-H.x*J.y;B+=D;G+=(J.x+H.x)*D;F+=(J.y+H.y)*D}D=B*3;return{x:G/D,y:F/D}};b(o).on("change",function(){var A=b(o).val().replace(/[^0-9\,]/ig,"");if(A.length){u=A.split(",").map(function(v){return parseInt(v,10)})}else{u=[]}l()});b(document).find(p).click(y);b(document).find(j).on("mousedown",t);b(document).find(j).on("contextmenu",w);b(document).find(j).on("mouseup",m)};b(document).ready(function(){b(".canvas-area[data-image-url]").canvasAreaDraw()});var a=function(p,l,g,q,e,n,f){function d(o,t,r,s){return Math.sqrt((o-=r)*o+(t-=s)*t)}if(f&&!(f=function(o,z,s,u,r,t){if(!(r-s)){return{x:s,y:z}}else{if(!(t-u)){return{x:o,y:u}}}var w,v=-1/((t-u)/(r-s));return{x:w=(r*(o*v-z+u)+s*(o*-v+z-t))/(v*(r-s)+u-t),y:v*w-v*o+z}}(p,l,g,q,e,n),f.x>=Math.min(g,e)&&f.x<=Math.max(g,e)&&f.y>=Math.min(q,n)&&f.y<=Math.max(q,n))){var i=d(p,l,g,q),h=d(p,l,e,n);return i>h?h:i}else{var m=q-n,k=e-g,j=g*n-q*e;return Math.abs(m*p+k*l+j)/Math.sqrt(m*m+k*k)}}})(jQuery);
     1 !function(t){t.fn.canvasAreaDraw=function(t){this.each(function(n,f){e.apply(f,[n,f,t])})};var e=function(e,f,a){var o,r,i,s,u,h,l,c,g,p,d,v,m,y,x,M,X=!1;i=t.extend({imageUrl:t(this).attr("data-image-url")},a);var w=t(f).val().replace(/[^0-9\,]/gi,"");o=w.length?w.split(",").map(function(t){return parseInt(t,10)}):[],s=t('<i class="fa fa-fw fa-trash"></i>'),u=t("<canvas>"),h=u[0].getContext("2d"),l=new Image,m=function(){u.attr("height",l.height).attr("width",l.width),c()},t(l).load(m),l.src=i.imageUrl,l.loaded&&m(),u.css({background:"url("+l.src+")"}),"hidden"!==f.type&&t(f).after("<br />"),t(f).after(u,"<br />",s),y=function(){o=[],c()},d=function(e){e.offsetX||(e.offsetX=e.pageX-t(e.target).offset().left,e.offsetY=e.pageY-t(e.target).offset().top),o[r]=Math.round(e.offsetX),o[r+1]=Math.round(e.offsetY),c()},v=function(e){e.offsetX||(e.offsetX=e.pageX-t(e.target).offset().left,e.offsetY=e.pageY-t(e.target).offset().top),X||(X={x:Math.round(e.offsetX),y:Math.round(e.offsetY)});for(var n={x:Math.round(e.offsetX),y:Math.round(e.offsetY)},f=0;f<o.length;f++)o[f]=n.x-X.x+o[f],o[++f]=n.y-X.y+o[f];X=n,c()},p=function(){t(this).off("mousemove"),M(),r=null},x=function(e){e.preventDefault(),e.offsetX||(e.offsetX=e.pageX-t(e.target).offset().left,e.offsetY=e.pageY-t(e.target).offset().top);for(var n=e.offsetX,f=e.offsetY,a=0;a<o.length;a+=2)if(dis=Math.sqrt(Math.pow(n-o[a],2)+Math.pow(f-o[a+1],2)),dis<6)return o.splice(a,2),c(),M(),!1;return!1},g=function(e){var f,a,i=o.length;if(3===e.which)return!1;if(e.preventDefault(),e.offsetX||(e.offsetX=e.pageX-t(e.target).offset().left,e.offsetY=e.pageY-t(e.target).offset().top),f=e.offsetX,a=e.offsetY,o.length>=6){var s=getCenter();if(h.fillRect(s.x-4,s.y-4,8,8),Math.sqrt(Math.pow(f-s.x,2)+Math.pow(a-s.y,2))<6)return X=!1,t(this).on("mousemove",v),!1}for(u=0;u<o.length;u+=2)if(Math.sqrt(Math.pow(f-o[u],2)+Math.pow(a-o[u+1],2))<6)return r=u,t(this).on("mousemove",d),!1;for(var u=0;u<o.length;u+=2)u>1&&n(f,a,o[u],o[u+1],o[u-2],o[u-1],!0)<6&&(i=u);return o.splice(i,0,Math.round(f),Math.round(a)),r=i,t(this).on("mousemove",d),c(),M(),!1},c=function(){if(h.canvas.width=h.canvas.width,M(),!(o.length<2)){if(h.globalCompositeOperation="destination-over",h.fillStyle="rgb(255,255,255)",h.strokeStyle="rgb(255,20,20)",h.lineWidth=1,o.length>=6){var t=getCenter();h.fillRect(t.x-4,t.y-4,8,8)}h.beginPath(),h.moveTo(o[0],o[1]);for(var e=0;e<o.length;e+=2)h.fillRect(o[e]-2,o[e+1]-2,4,4),h.strokeRect(o[e]-2,o[e+1]-2,4,4),o.length>2&&e>1&&h.lineTo(o[e],o[e+1]);h.closePath(),h.fillStyle="rgba(255,0,0,0.3)",h.fill(),h.stroke()}},M=function(){t(f).val(o.join(","))},getCenter=function(){var t=[];for(l=0;l<o.length;l++)t.push({x:o[l],y:o[++l]});var e=t[0],n=t[t.length-1];e.x==n.x&&e.y==n.y||t.push(e);for(var f,a,r,i=0,s=0,u=0,h=t.length,l=0,c=h-1;l<h;c=l++)f=t[l],a=t[c],i+=r=f.x*a.y-a.x*f.y,s+=(f.x+a.x)*r,u+=(f.y+a.y)*r;return r=3*i,{x:s/r,y:u/r}},t(f).on("change",function(){var e=t(f).val().replace(/[^0-9\,]/gi,"");o=e.length?e.split(",").map(function(t){return parseInt(t,10)}):[],c()}),t(document).find(s).click(y),t(document).find(u).on("mousedown",g),t(document).find(u).on("contextmenu",x),t(document).find(u).on("mouseup",p)};t(document).ready(function(){t(".canvas-area[data-image-url]").canvasAreaDraw()});var n=function(t,e,n,f,a,o,r){function i(t,e,n,f){return Math.sqrt((t-=n)*t+(e-=f)*e)}if(!r||(r=function(t,e,n,f,a,o){if(!(a-n))return{x:n,y:e};if(!(o-f))return{x:t,y:f};var r,i=-1/((o-f)/(a-n));return{x:r=(a*(t*i-e+f)+n*(t*-i+e-o))/(i*(a-n)+f-o),y:i*r-i*t+e}}(t,e,n,f,a,o)).x>=Math.min(n,a)&&r.x<=Math.max(n,a)&&r.y>=Math.min(f,o)&&r.y<=Math.max(f,o)){var s=f-o,u=a-n,h=n*o-f*a;return Math.abs(s*t+u*e+h)/Math.sqrt(s*s+u*u)}var l=i(t,e,n,f),c=i(t,e,a,o);return l>c?c:l}}(jQuery);