stackblur-canvas
Version:
Fast and almost Gaussian blur by Mario Klingemann
41 lines (40 loc) • 8.37 kB
JavaScript
function t(t,r,e){return Object.defineProperty(t,"prototype",{writable:!1}),t}function r(t){return r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},r(t)
/**
* StackBlur - a fast almost Gaussian Blur For Canvas
*
* In case you find this class useful - especially in commercial projects -
* I am not totally unhappy for a small donation to my PayPal account
* mario@quasimondo.de
*
* Or support me on flattr:
* {@link https://flattr.com/thing/72791/StackBlur-a-fast-almost-Gaussian-Blur-Effect-for-CanvasJavascript}.
*
* @module StackBlur
* @author Mario Klingemann
* Contact: mario@quasimondo.com
* Website: {@link http://www.quasimondo.com/StackBlurForCanvas/StackBlurDemo.html}
* Twitter: `@quasimondo`
* @copyright (c) 2010 Mario Klingemann
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/}var e=[512,512,456,512,328,456,335,512,405,328,271,456,388,335,292,512,454,405,364,328,298,271,496,456,420,388,360,335,312,292,273,512,482,454,428,405,383,364,345,328,312,298,284,271,259,496,475,456,437,420,404,388,374,360,347,335,323,312,302,292,282,273,265,512,497,482,468,454,441,428,417,405,394,383,373,364,354,345,337,328,320,312,305,298,291,284,278,271,265,259,507,496,485,475,465,456,446,437,428,420,412,404,396,388,381,374,367,360,354,347,341,335,329,323,318,312,307,302,297,292,287,282,278,273,269,265,261,512,505,497,489,482,475,468,461,454,447,441,435,428,422,417,411,405,399,394,389,383,378,373,368,364,359,354,350,345,341,337,332,328,324,320,316,312,309,305,301,298,294,291,287,284,281,278,274,271,268,265,262,259,257,507,501,496,491,485,480,475,470,465,460,456,451,446,442,437,433,428,424,420,416,412,408,404,400,396,392,388,385,381,377,374,370,367,363,360,357,354,350,347,344,341,338,335,332,329,326,323,320,318,315,312,310,307,304,302,299,297,294,292,289,287,285,282,280,278,275,273,271,269,267,265,263,261,259],a=[9,11,12,13,13,14,14,15,15,15,15,16,16,16,16,17,17,17,17,17,17,17,18,18,18,18,18,18,18,18,18,19,19,19,19,19,19,19,19,19,19,19,19,19,19,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24];function n(t,r,e,a,n,o){if("string"==typeof t&&(t=document.getElementById(t)),t&&("HTMLImageElement"!==Object.prototype.toString.call(t).slice(8,-1)||"naturalWidth"in t)){var g=n?"offset":"natural",c=t[g+"Width"],l=t[g+"Height"];if("ImageBitmap"===Object.prototype.toString.call(t).slice(8,-1)&&(c=t.width,l=t.height),"string"==typeof r&&(r=document.getElementById(r)),r&&"getContext"in r){o||(r.style.width=c+"px",r.style.height=l+"px"),r.width=c,r.height=l;var s=r.getContext("2d");s.clearRect(0,0,c,l),s.drawImage(t,0,0,t.naturalWidth,t.naturalHeight,0,0,c,l),isNaN(e)||e<1||(a?i(r,0,0,c,l,e):f(r,0,0,c,l,e))}}}function o(t,e,a,n,o){if("string"==typeof t&&(t=document.getElementById(t)),!t||"object"!==r(t)||!("getContext"in t))throw new TypeError("Expecting canvas with `getContext` method in processCanvasRGB(A) calls!");var i=t.getContext("2d");try{return i.getImageData(e,a,n,o)}catch(t){throw new Error("unable to access image data: "+t)}}function i(t,r,e,a,n,i){if(!(isNaN(i)||i<1)){i|=0;var f=o(t,r,e,a,n);f=g(f,r,e,a,n,i),t.getContext("2d").putImageData(f,r,e)}}function g(t,r,n,o,i,g){for(var f,c=t.data,s=2*g+1,v=o-1,u=i-1,b=g+1,x=b*(b+1)/2,h=new l,m=h,y=1;y<s;y++)m=m.next=new l,y===b&&(f=m);m.next=h;for(var p=null,d=null,w=0,B=0,C=e[g],I=a[g],E=0;E<i;E++){m=h;for(var S=c[B],N=c[B+1],R=c[B+2],D=c[B+3],G=0;G<b;G++)m.r=S,m.g=N,m.b=R,m.a=D,m=m.next;for(var j=0,M=0,A=0,H=0,O=b*S,T=b*N,W=b*R,L=b*D,P=x*S,k=x*N,q=x*R,z=x*D,F=1;F<b;F++){var J=B+(Math.min(v,F)<<2),K=c[J],Q=c[J+1],U=c[J+2],V=c[J+3],X=b-F;P+=(m.r=K)*X,k+=(m.g=Q)*X,q+=(m.b=U)*X,z+=(m.a=V)*X,j+=K,M+=Q,A+=U,H+=V,m=m.next}p=h,d=f;for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Nt+1],ht+=wt+=p.b=c[Nt+2],mt+=Bt+=p.a=c[Nt+3],p=p.next,lt+=it=d.r,st+=gt=d.g,vt+=ft=d.b,ut+=ct=d.a,Ct-=it,dt-=gt,wt-=ft,Bt-=ct,d=d.next,B+=o}}return t}function f(t,r,e,a,n,i){if(!(isNaN(i)||i<1)){i|=0;var g=o(t,r,e,a,n);g=c(g,r,e,a,n,i),t.getContext("2d").putImageData(g,r,e)}}function c(t,r,n,o,i,g){for(var f,c=t.data,s=2*g+1,v=o-1,u=i-1,b=g+1,x=b*(b+1)/2,h=new l,m=h,y=1;y<s;y++)m=m.next=new l,y===b&&(f=m);m.next=h;for(var p,d,w=null,B=null,C=e[g],I=a[g],E=0,S=0,N=0;N<i;N++){var R=c[S],D=c[S+1],G=c[S+2],j=b*R,M=b*D,A=b*G,H=x*R,O=x*D,T=x*G;m=h;for(var W=0;W<b;W++)m.r=R,m.g=D,m.b=G,m=m.next;for(var L=0,P=0,k=0,q=1;q<b;q++)p=S+(Math.min(v,q)<<2),H+=(m.r=R=c[p])*(d=b-q),O+=(m.g=D=c[p+1])*d,T+=(m.b=G=c[p+2])*d,L+=R,P+=D,k+=G,m=m.next;w=h,B=f;for(var z=0;z<o;z++)c[S]=H*C>>>I,c[S+1]=O*C>>>I,c[S+2]=T*C>>>I,H-=j,O-=M,T-=A,j-=w.r,M-=w.g,A-=w.b,p=E+((p=z+g+1)<v?p:v)<<2,H+=L+=w.r=c[p],O+=P+=w.g=c[p+1],T+=k+=w.b=c[p+2],w=w.next,j+=R=B.r,M+=D=B.g,A+=G=B.b,L-=R,P-=D,k-=G,B=B.next,S+=4;E+=o}for(var F=0;F<o;F++){var J=c[S=F<<2],K=c[S+1],Q=c[S+2],U=b*J,V=b*K,X=b*Q,Y=x*J,Z=x*K,$=x*Q;m=h;for(var _=0;_<b;_++)m.r=J,m.g=K,m.b=Q,m=m.next;for(var tt=0,rt=0,et=0,at=1,nt=o;at<=g;at++)S=nt+F<<2,Y+=(m.r=J=c[S])*(d=b-at),Z+=(m.g=K=c[S+1])*d,$+=(m.b=Q=c[S+2])*d,tt+=J,rt+=K,et+=Q,m=m.next,at<u&&(nt+=o);S=F,w=h,B=f;for(var ot=0;ot<i;ot++)c[p=S<<2]=Y*C>>>I,c[p+1]=Z*C>>>I,c[p+2]=$*C>>>I,Y-=U,Z-=V,$-=X,U-=w.r,V-=w.g,X-=w.b,p=F+((p=ot+b)<u?p:u)*o<<2,Y+=tt+=w.r=c[p],Z+=rt+=w.g=c[p+1],$+=et+=w.b=c[p+2],w=w.next,U+=J=B.r,V+=K=B.g,X+=Q=B.b,tt-=J,rt-=K,et-=Q,B=B.next,S+=o}return t}var l=t(function t(){!function(t,r){if(!(t instanceof r))throw new TypeError("Cannot call a class as a function")}(this,t),this.r=0,this.g=0,this.b=0,this.a=0,this.next=null});export{l as BlurStack,f as canvasRGB,i as canvasRGBA,n as image,c as imageDataRGB,g as imageDataRGBA};
//# sourceMappingURL=stackblur.min.mjs.map