kopia lustrzana https://github.com/snarfed/bridgy-fed
8 wiersze
41 KiB
JavaScript
8 wiersze
41 KiB
JavaScript
![]() |
/**
|
||
|
* q5.js
|
||
|
* @version 1.8
|
||
|
* @author quinton-ashley and LingDong-
|
||
|
* @license GPL-3.0-only
|
||
|
*/
|
||
|
function Q5(e,t){let a=0;if(void 0===e&&(e="global",a++,setTimeout((()=>a--),32)),"auto"==e){if(void 0===window.setup)return;e="global"}1==arguments.length&&"string"!=typeof e&&"function"!=typeof e&&(t=arguments[0],e=null),"global"==e&&(Q5._hasGlobal=!0);let n=this;n.canvas=document.createElement("canvas");let o=n._ctx=n.canvas.getContext("2d");if(n.canvas.classList.add("p5Canvas","q5Canvas"),n.canvas.id="defaultCanvas"+Q5._instanceCount++,n.width=100,n.height=100,n.canvas.width=n.width,n.canvas.height=n.height,n._windowResizedFn=()=>{},"graphics"!=e&&"image"!=e){function i(){t??=document.getElementsByTagName("main")[0],t||(t=document.createElement("main"),document.body.append(t)),n.canvas.parent(t)}n._setupDone=!1,n._resize=()=>{n.frameCount>1&&(n._shouldResize=!0)},n.canvas.parent=e=>{"string"==typeof e&&(e=document.getElementById(e)),e.append(n.canvas),"undefined"!=typeof ResizeObserver?(n._ro&&n._ro.disconnect(),n._ro=new ResizeObserver(n._resize),n._ro.observe(t)):0==n.frameCount&&addEventListener("resize",n._resize)},document.body?i():window.addEventListener("load",i)}M(),n.MAGIC=161533525,n.pixels=[];let r=null;n.createCanvas=function(t,a){if(n.width=t,n.height=a,n.canvas.width=t,n.canvas.height=a,M(),"image"!=e){let t=n.displayDensity();"graphics"==e&&(t=this._pixelDensity),n.pixelDensity(Math.ceil(t))}else this._pixelDensity=1;return n.canvas},n._createCanvas=n.createCanvas,n.loadPixels=()=>{r=o.getImageData(0,0,n.canvas.width,n.canvas.height),n.pixels=r.data},n.updatePixels=()=>{null!=r&&o.putImageData(r,0,0)};let s={};function l(e,t){null==p&&(p=document.createElement("canvas").getContext("2d")),t??=e||o.canvas.height,e??=o.canvas.width,p.canvas.width==e&&p.canvas.height==t||(p.canvas.width=e,p.canvas.height=t)}function h(){let e=o.canvas.width*o.canvas.height*4;v&&e==v.length||(v=new Uint8ClampedArray(e))}function c(e){p.clearRect(0,0,p.canvas.width,p.canvas.height),p.filter=e,p.drawImage(o.canvas,0,0),o.save(),o.resetTransform(),o.clearRect(0,0,o.canvas.width,o.canvas.height),o.drawImage(p.canvas,0,0),o.restore()}s[n.THRESHOLD]=(e,t)=>{void 0===t?t=127.5:t*=255;for(let a=0;a<e.length;a+=4){const n=.2126*e[a]+.7152*e[a+1]+.0722*e[a+2];e[a]=e[a+1]=e[a+2]=n>=t?255:0}},s[n.GRAY]=e=>{for(let t=0;t<e.length;t+=4){const a=.2126*e[t]+.7152*e[t+1]+.0722*e[t+2];e[t]=e[t+1]=e[t+2]=a}},s[n.OPAQUE]=e=>{for(let t=0;t<e.length;t+=4)e[t+3]=255},s[n.INVERT]=e=>{for(let t=0;t<e.length;t+=4)e[t]=255-e[t],e[t+1]=255-e[t+1],e[t+2]=255-e[t+2]},s[n.POSTERIZE]=(e,t)=>{let a=t-1;for(let n=0;n<e.length;n+=4)e[n]=255*(e[n]*t>>8)/a,e[n+1]=255*(e[n+1]*t>>8)/a,e[n+2]=255*(e[n+2]*t>>8)/a},s[n.DILATE]=e=>{h(),v.set(e);let[t,a]=[o.canvas.width,o.canvas.height];for(let n=0;n<a;n++)for(let o=0;o<t;o++){let i=4*Math.max(o-1,0),r=4*Math.min(o+1,t-1),s=4*Math.max(n-1,0)*t,l=4*Math.min(n+1,a-1)*t,h=4*n*t,c=4*o;for(let t=0;t<4;t++){let a=t+s,n=t+l,o=t+h;e[h+c+t]=Math.max(v[a+c],v[o+i],v[o+c],v[o+r],v[n+c])}}},s[n.ERODE]=e=>{h(),v.set(e);let[t,a]=[o.canvas.width,o.canvas.height];for(let n=0;n<a;n++)for(let o=0;o<t;o++){let i=4*Math.max(o-1,0),r=4*Math.min(o+1,t-1),s=4*Math.max(n-1,0)*t,l=4*Math.min(n+1,a-1)*t,h=4*n*t,c=4*o;for(let t=0;t<4;t++){let a=t+s,n=t+l,o=t+h;e[h+c+t]=Math.min(v[a+c],v[o+i],v[o+c],v[o+r],v[n+c])}}},s[n.BLUR]=(e,t)=>{t=t||1,t=Math.floor(t*n._pixelDensity),h(),v.set(e);let a=2*t+1;let i=function(e){let a=new Float32Array(e),n=.3*t+.8,o=n*n*2;for(let t=0;t<e;t++){let i=t-e/2,r=Math.exp(-i*i/o)/(2.5066282746*n);a[t]=r}return a}(a),[r,s]=[o.canvas.width,o.canvas.height];for(let n=0;n<s;n++)for(let o=0;o<r;o++){let s=0,l=0,h=0,c=0;for(let e=0;e<a;e++){let a=4*(n*r+Math.min(Math.max(o-t+e,0),r-1));s+=v[a]*i[e],l+=v[a+1]*i[e],h+=v[a+2]*i[e],c+=v[a+3]*i[e]}let d=4*(n*r+o);e[d]=s,e[d+1]=l,e[d+2]=h,e[d+3]=c}v.set(e);for(let n=0;n<s;n++)for(let o=0;o<r;o++){let l=0,h=0,c=0,d=0;for(let e=0;e<a;e++){let a=4*(Math.min(Math.max(n-t+e,0),s-1)*r+o);l+=v[a]*i[e],h+=v[a+1]*i[e],c+=v[a+2]*i[e],d+=v[a+3]*i[e]}let u=4*(n*r+o);e[u]=l,e[u+1]=h,e[u+2]=c,e[u+3]=d}},n.filter=(e,t)=>{if(n.HARDWARE_FILTERS&&null!=o.filter)if(l(),e==n.THRESHOLD){t??=.5,t=
|