-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmsqr.min.js
7 lines (7 loc) · 3.5 KB
/
msqr.min.js
1
2
3
4
5
6
7
/*
MSQR v0.2.1 alpha
(c) 2016 K3N / Epistemex
www.epistemex.com
MIT License
*/
function MSQR(C,u){u=u||{};var g;if(C instanceof CanvasRenderingContext2D){g=C}else{if(C instanceof HTMLCanvasElement){g=C.getContext("2d")}else{if(C instanceof HTMLImageElement||C instanceof HTMLVideoElement){g=q(C)}else{throw"Invalid source."}}}var G=g.canvas.width,o=g.canvas.height,l=(u.x||0)|0,m=(u.y||0)|0,k=(u.width||G)|0,f=(u.height||o)|0,e,y=[],x,s=3,p,A,d=Math.max(1,u.bleed||5),t=Math.max(1,u.maxShapes||1),b=Math.max(0,Math.min(254,u.alpha||0)),v=u.padding||0,E=Math.max(0,u.tolerance||0),n=!!u.align,a=u.alignWeight||0.95,B=!!u.path2D,j,r;if(l<0||m<0||l>=G||m>=o||k<1||f<1||l+k>G||m+f>o){return[]}if(t>1||v){e=q(g.canvas);g.save();g.setTransform(1,0,0,1,0,0);g.fillStyle=g.strokeStyle="#000";g.globalAlpha=1;g.shadowColor="rgba(0,0,0,0)";if(v){j=q(g.canvas);r=v<0?4:(v>5?16:8);g.globalCompositeOperation=v<0?"destination-in":"source-over";v=Math.min(10,Math.abs(v));for(var c=0,D=Math.PI*2/r;c<6.28;c+=D){g.drawImage(j.canvas,v*Math.cos(c),v*Math.sin(c))}}g.globalCompositeOperation="destination-out";g.lineWidth=d;g.miterLimit=1;do{x=F();if(x.length){y.push(B?z(x):x);g.beginPath();p=x.length-1;while(A=x[p--]){g.lineTo(A.x,A.y)}g.closePath();g.fill();g.stroke()}}while(x.length&&--t);g.globalCompositeOperation="source-over";g.clearRect(0,0,g.canvas.width,g.canvas.height);g.drawImage(e.canvas,0,0);g.restore();return y}else{x=F();y.push(B?z(x):x)}return y;function F(){var N=[],w,M,L,V,W,T,U,Q=-1,R,O=9,S=[9,0,3,3,2,0,9,3,1,9,1,1,2,0,2,9];w=new Uint32Array(g.getImageData(l,m,k,f).data.buffer);M=w.length;for(L=s;L<M;L++){if((w[L]>>>24)>b){Q=s=L;break}}if(Q>=0){V=T=(Q%k)|0;W=U=(Q/k)|0;do{R=J(V,W);if(R===0){W--}else{if(R===1){W++}else{if(R===2){V--}else{if(R===3){V++}}}}if(R!==O){N.push({x:V+l,y:W+m});O=R}}while(V!==T||W!==U);if(E){N=P(N,E)}if(n&&!v){N=h(N,a)}}function K(i,X){return(i>=0&&X>=0&&i<k&&X<f)?(w[X*k+i]>>>24)>b:false}function J(X,Y){var i=0;if(K(X-1,Y-1)){i|=1}if(K(X,Y-1)){i|=2}if(K(X-1,Y)){i|=4}if(K(X,Y)){i|=8}if(i===6){return O===0?2:3}else{if(i===9){return O===3?0:1}else{return S[i]}}}function P(ai,Z){var ag=ai.length-1;if(ag<2){return ai}var ab=ai[0],ah=ai[ag],aa=Z*Z,ac,ad=-1,X,Y=0,ae,af,aj,ak;for(ac=1;ac<ag;ac++){X=I(ai[ac],ab,ah);if(X>Y){Y=X;ad=ac}}if(Y>aa){ae=ai.slice(0,ad+1);af=ai.slice(ad);aj=P(ae,Z);ak=P(af,Z);return aj.slice(0,aj.length-1).concat(ak)}else{return[ab,ah]}}function I(Z,i,X){var Y=H(i,X),aa;if(!Y){return 0}aa=((Z.x-i.x)*(X.x-i.x)+(Z.y-i.y)*(X.y-i.y))/Y;if(aa<0){return H(Z,i)}else{if(aa>1){return H(Z,X)}else{return H(Z,{x:i.x+aa*(X.x-i.x),y:i.y+aa*(X.y-i.y)})}}}function H(Y,Z){var i=Y.x-Z.x,X=Y.y-Z.y;return i*i+X*X}function h(ad,ah){var aa=[1,-1,-1,1],ab=[1,1,-1,-1],ac,ae=0;while(ac=ad[ae++]){ac.x=Math.round(ac.x);ac.y=Math.round(ac.y);for(var Z=0,af,ag,X,Y;Z<4;Z++){X=aa[Z];Y=ab[Z];af=ac.x+(X<<1);ag=ac.y+(Y<<1);if(af>l&&ag>m&&af<k-1&&ag<f-1){if(!K(af,ag)){af-=X;ag-=Y;if(K(af,ag)){ac.x+=X*ah;ac.y+=Y*ah}}}}}return ad}return N}function q(w){var h=document.createElement("canvas"),i;h.width=w.naturalWidth||w.videoWidth||w.width;h.height=w.naturalHeight||w.videoHeight||w.height;i=h.getContext("2d");i.drawImage(w,0,0);return i}function z(I){var w=new Path2D(),h=0,H;while(H=I[h++]){w.lineTo(H.x,H.y)}w.closePath();return w}}MSQR.getBounds=function(g){var e=9999999,f=9999999,c=-9999999,d=-9999999,a,b=g.length;for(a=0;a<b;a++){if(g[a].x>c){c=g[a].x}if(g[a].x<e){e=g[a].x}if(g[a].y>d){d=g[a].y}if(g[a].y<f){f=g[a].y}}return{x:e|0,y:f|0,width:Math.ceil(c-e),height:Math.ceil(d-f)}};if(typeof exports!=="undefined"){exports.MSQR=MSQR};