UNPKG

stackblur-canvas

Version:

Fast and almost Gaussian blur by Mario Klingemann

41 lines (40 loc) 8.67 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).StackBlur={})}(this,function(t){"use strict";function e(t,e,r){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 n=[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 o(t,e,n,a,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,n,a,o)}catch(t){throw new Error("unable to access image data: "+t)}}function i(t,e,r,n,a,i){if(!(isNaN(i)||i<1)){i|=0;var g=o(t,e,r,n,a);g=f(g,e,r,n,a,i),t.getContext("2d").putImageData(g,e,r)}}function f(t,e,r,o,i,f){for(var g,c=t.data,u=2*f+1,s=o-1,v=i-1,b=f+1,x=b*(b+1)/2,p=new l,h=p,y=1;y<u;y++)h=h.next=new l,y===b&&(g=h);h.next=p;for(var d=null,m=null,w=0,B=0,C=n[f],I=a[f],S=0;S<i;S++){h=p;for(var E=c[B],N=c[B+1],R=c[B+2],j=c[B+3],D=0;D<b;D++)h.r=E,h.g=N,h.b=R,h.a=j,h=h.next;for(var G=0,T=0,M=0,A=0,H=b*E,O=b*N,W=b*R,k=b*j,L=x*E,P=x*N,q=x*R,z=x*j,F=1;F<b;F++){var J=B+(Math.min(s,F)<<2),K=c[J],Q=c[J+1],U=c[J+2],V=c[J+3],X=b-F;L+=(h.r=K)*X,P+=(h.g=Q)*X,q+=(h.b=U)*X,z+=(h.a=V)*X,G+=K,T+=Q,M+=U,A+=V,h=h.next}d=p,m=g;for(var Y=0;Y<o;Y++){var Z=z*C>>>I;if(c[B+3]=Z,0!==Z){var $=255/Z;c[B]=(L*C>>>I)*$,c[B+1]=(P*C>>>I)*$,c[B+2]=(q*C>>>I)*$}else c[B]=c[B+1]=c[B+2]=0;L-=H,P-=O,q-=W,z-=k,H-=d.r,O-=d.g,W-=d.b,k-=d.a;var _=Y+f+1;_=w+Math.min(_,s)<<2,L+=G+=d.r=c[_],P+=T+=d.g=c[_+1],q+=M+=d.b=c[_+2],z+=A+=d.a=c[_+3],d=d.next;var tt=m,et=tt.r,rt=tt.g,nt=tt.b,at=tt.a;H+=et,O+=rt,W+=nt,k+=at,G-=et,T-=rt,M-=nt,A-=at,m=m.next,B+=4}w+=o}for(var ot=0;ot<o;ot++){var it=c[B=ot<<2],ft=c[B+1],gt=c[B+2],ct=c[B+3],lt=b*it,ut=b*ft,st=b*gt,vt=b*ct,bt=x*it,xt=x*ft,pt=x*gt,ht=x*ct;h=p;for(var yt=0;yt<b;yt++)h.r=it,h.g=ft,h.b=gt,h.a=ct,h=h.next;for(var dt=o,mt=0,wt=0,Bt=0,Ct=0,It=1;It<=f;It++){B=dt+ot<<2;var St=b-It;bt+=(h.r=it=c[B])*St,xt+=(h.g=ft=c[B+1])*St,pt+=(h.b=gt=c[B+2])*St,ht+=(h.a=ct=c[B+3])*St,Ct+=it,mt+=ft,wt+=gt,Bt+=ct,h=h.next,It<v&&(dt+=o)}B=ot,d=p,m=g;for(var Et=0;Et<i;Et++){var Nt=B<<2;c[Nt+3]=ct=ht*C>>>I,ct>0?(ct=255/ct,c[Nt]=(bt*C>>>I)*ct,c[Nt+1]=(xt*C>>>I)*ct,c[Nt+2]=(pt*C>>>I)*ct):c[Nt]=c[Nt+1]=c[Nt+2]=0,bt-=lt,xt-=ut,pt-=st,ht-=vt,lt-=d.r,ut-=d.g,st-=d.b,vt-=d.a,Nt=ot+((Nt=Et+b)<v?Nt:v)*o<<2,bt+=Ct+=d.r=c[Nt],xt+=mt+=d.g=c[Nt+1],pt+=wt+=d.b=c[Nt+2],ht+=Bt+=d.a=c[Nt+3],d=d.next,lt+=it=m.r,ut+=ft=m.g,st+=gt=m.b,vt+=ct=m.a,Ct-=it,mt-=ft,wt-=gt,Bt-=ct,m=m.next,B+=o}}return t}function g(t,e,r,n,a,i){if(!(isNaN(i)||i<1)){i|=0;var f=o(t,e,r,n,a);f=c(f,e,r,n,a,i),t.getContext("2d").putImageData(f,e,r)}}function c(t,e,r,o,i,f){for(var g,c=t.data,u=2*f+1,s=o-1,v=i-1,b=f+1,x=b*(b+1)/2,p=new l,h=p,y=1;y<u;y++)h=h.next=new l,y===b&&(g=h);h.next=p;for(var d,m,w=null,B=null,C=n[f],I=a[f],S=0,E=0,N=0;N<i;N++){var R=c[E],j=c[E+1],D=c[E+2],G=b*R,T=b*j,M=b*D,A=x*R,H=x*j,O=x*D;h=p;for(var W=0;W<b;W++)h.r=R,h.g=j,h.b=D,h=h.next;for(var k=0,L=0,P=0,q=1;q<b;q++)d=E+(Math.min(s,q)<<2),A+=(h.r=R=c[d])*(m=b-q),H+=(h.g=j=c[d+1])*m,O+=(h.b=D=c[d+2])*m,k+=R,L+=j,P+=D,h=h.next;w=p,B=g;for(var z=0;z<o;z++)c[E]=A*C>>>I,c[E+1]=H*C>>>I,c[E+2]=O*C>>>I,A-=G,H-=T,O-=M,G-=w.r,T-=w.g,M-=w.b,d=S+((d=z+f+1)<s?d:s)<<2,A+=k+=w.r=c[d],H+=L+=w.g=c[d+1],O+=P+=w.b=c[d+2],w=w.next,G+=R=B.r,T+=j=B.g,M+=D=B.b,k-=R,L-=j,P-=D,B=B.next,E+=4;S+=o}for(var F=0;F<o;F++){var J=c[E=F<<2],K=c[E+1],Q=c[E+2],U=b*J,V=b*K,X=b*Q,Y=x*J,Z=x*K,$=x*Q;h=p;for(var _=0;_<b;_++)h.r=J,h.g=K,h.b=Q,h=h.next;for(var tt=0,et=0,rt=0,nt=1,at=o;nt<=f;nt++)E=at+F<<2,Y+=(h.r=J=c[E])*(m=b-nt),Z+=(h.g=K=c[E+1])*m,$+=(h.b=Q=c[E+2])*m,tt+=J,et+=K,rt+=Q,h=h.next,nt<v&&(at+=o);E=F,w=p,B=g;for(var ot=0;ot<i;ot++)c[d=E<<2]=Y*C>>>I,c[d+1]=Z*C>>>I,c[d+2]=$*C>>>I,Y-=U,Z-=V,$-=X,U-=w.r,V-=w.g,X-=w.b,d=F+((d=ot+b)<v?d:v)*o<<2,Y+=tt+=w.r=c[d],Z+=et+=w.g=c[d+1],$+=rt+=w.b=c[d+2],w=w.next,U+=J=B.r,V+=K=B.g,X+=Q=B.b,tt-=J,et-=K,rt-=Q,B=B.next,E+=o}return t}var l=e(function t(){!function(t,e){if(!(t instanceof e))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});t.BlurStack=l,t.canvasRGB=g,t.canvasRGBA=i,t.image=function(t,e,r,n,a,o){if("string"==typeof t&&(t=document.getElementById(t)),t&&("HTMLImageElement"!==Object.prototype.toString.call(t).slice(8,-1)||"naturalWidth"in t)){var f=a?"offset":"natural",c=t[f+"Width"],l=t[f+"Height"];if("ImageBitmap"===Object.prototype.toString.call(t).slice(8,-1)&&(c=t.width,l=t.height),"string"==typeof e&&(e=document.getElementById(e)),e&&"getContext"in e){o||(e.style.width=c+"px",e.style.height=l+"px"),e.width=c,e.height=l;var u=e.getContext("2d");u.clearRect(0,0,c,l),u.drawImage(t,0,0,t.naturalWidth,t.naturalHeight,0,0,c,l),isNaN(r)||r<1||(n?i(e,0,0,c,l,r):g(e,0,0,c,l,r))}}},t.imageDataRGB=c,t.imageDataRGBA=f}); //# sourceMappingURL=stackblur.min.js.map