Skip to content

Unlock custom brushes, natural fill effects and intuitive hatching in p5.js

70.2 KB
Raw Blame
const t={a:1,b:0,c:0,d:1,x:0,y:0};let e=()=>!1,r=t=>t,n=()=>{throw new Error("No runtime color adapter registered.")},i=()=>t,o=()=>{};const s=Math.sqrt(3),a=.5*(s-1),c=(3-s)/6,l=t=>0|Math.floor(t),h=new Float64Array([1,1,-1,1,1,-1,-1,-1,1,0,-1,0,1,0,-1,0,0,1,0,-1,0,1,0,-1]);function u(t=Math.random){const e=function(t){const e=new Uint8Array(512);for(let t=0;t<256;t++)e[t]=t;for(let r=0;r<255;r++){const n=r+~~(t()*(256-r)),i=e[r];e[r]=e[n],e[n]=i}for(let t=256;t<512;t++)e[t]=e[t-256];return e}(t),r=new Float64Array(e).map(t=>h[t%12*2]),n=new Float64Array(e).map(t=>h[t%12*2+1]);return function(t,i){let o=0,s=0,h=0;const u=(t+i)*a,f=l(t+u),m=l(i+u),d=(f+m)*c,g=t-(f-d),y=i-(m-d);let x,p;g>y?(x=1,p=0):(x=0,p=1);const v=g-x+c,b=y-p+c,w=g-1+2*c,_=y-1+2*c,A=255&f,R=255&m;let E=.5-g*g-y*y;if(E>=0){const t=A+e[R];E*=E,o=E*E*(r[t]*g+n[t]*y)}let M=.5-v*v-b*b;if(M>=0){const t=A+x+e[R+p];M*=M,s=M*M*(r[t]*v+n[t]*b)}let S=.5-w*w-_*_;if(S>=0){const t=A+1+e[R+1];S*=S,h=S*S*(r[t]*w+n[t]*_)}return 70*(o+s+h)}}function f(t){let e=function(t){let e=0;const r=String(t);for(let t=0;t<r.length;t++)e=0|Math.imul(e^r.charCodeAt(t),2654435769),e^=e>>>15;return e=0|Math.imul(e^e>>>16,2246822507),e=0|Math.imul(e^e>>>13,3266489909),(e^e>>>16)>>>0||1}(t);return()=>{e=e+1831565813|0;let t=Math.imul(e^e>>>15,1|e);return t^=t+Math.imul(t^t>>>7,61|t),2.3283064365386963e-10*((t^t>>>14)>>>0)}}let m=f(Math.random()),d=f(Math.random()+":2");const g=[],y=t=>g.push(t),x=t=>{m=f(t),d=f(`${t}:2`),E=!1;for(const t of g)t()};u(f(Math.random()));let p=u(f(Math.random()+":2"));const v=t=>{u(f(t)),p=u(f(`${t}:2`))},b=(t=0,e=1)=>t+m()*(e-t),w=(t=0,e=1)=>t+d()*(e-t),_=t=>t[~~(m()*t.length)],A=(t,e)=>~~b(t,e),R=(t,e)=>~~w(t,e);let E=!1,M=0;const S=(t=0,e=1)=>{if(E)return E=!1,M*e+t;const r=1-m(),n=m(),i=Math.sqrt(-2*Math.log(r)),o=360*n;return M=i*B(o),E=!0,i*N(o)*e+t},P=t=>{let e=0;const r=[];for(const n in t)e+=t[n],r.push({key:n,cumulative:e});const n=m()*e;for(const{key:t,cumulative:e}of r)if(n<e)return isNaN(t)?t:parseInt(t)},T=(t,e,r,n,i,o=!1)=>{let s=n+(t-e)/(r-e)*(i-n);return o?n<i?C(s,n,i):C(s,i,n):s},C=(t,e,r)=>Math.max(Math.min(t,r),e),k=1440,I=2*Math.PI/k,F=new Float32Array(k),L=new Float32Array(k);for(let t=0;t<k;t++)F[t]=Math.cos(t*I),L[t]=Math.sin(t*I);const D=t=>t<0?t>=-360?~~(4*(t+360)):~~(4*((t%=360)<0?t+360:t)):t<360?~~(4*t):t<720?~~(4*(t-360)):t<1080?~~(4*(t-720)):~~(4*((t%=360)<0?t+360:t)),N=t=>F[D(t)],B=t=>L[D(t)],Y=new Float32Array(2),z=t=>{const e=D(t);return Y[0]=F[e],Y[1]=L[e],Y},O=(t,r=!1)=>{if(r||e()){let e=180*t/Math.PI%360;return e<0?e+360:e}return t},X=(t,r=!1)=>r||e()?180*t/Math.PI:t,U=(t,e,r,n,i)=>{const o=z(i),s=o[0],a=o[1];return{x:s*(r-t)+a*(n-e)+t,y:s*(n-e)-a*(r-t)+e}},G=(t,e,r,n)=>Math.hypot(r-t,n-e),V=(t,e,r,n)=>O(Math.atan2(-(n-e),r-t),!0),H=(t,e,r,n,i=!1)=>{const o=t.x,s=t.y,a=e.x,c=e.y,l=r.x,h=r.y,u=a-o,f=c-s,m=n.x-l,d=n.y-h,g=d*u-m*f;if(0===g)return!1;const y=s-h,x=o-l,p=(m*y-d*x)/g,v=(u*y-f*x)/g;return!(!i&&(v<0||v>1))&&{x:o+p*u,y:s+p*f}};let W,j,$,q,K,Z={load:()=>{throw new Error("No target runtime adapter registered.")},syncDensity:()=>K,isCanvasReady:()=>{throw new Error("No target runtime adapter registered.")},instance:t=>{$=t},activateInstance:t=>{$=t},deactivateInstance:()=>{},getActiveFramebuffer:()=>null,isFramebufferTarget:()=>!1};function J(t){"Cwidth"in t&&(W=t.Cwidth),"Cheight"in t&&(j=t.Cheight),"Instance"in t&&($=t.Instance),"Renderer"in t&&(q=t.Renderer),"Density"in t&&(K=t.Density)}const Q=()=>Z.isCanvasReady(),tt=t=>Z.instance(t),et=t=>Z.activateInstance(t),rt=()=>Z.getActiveFramebuffer(),nt=t=>Z.isFramebufferTarget(t),it=(t,e=0)=>t?{minX:t.minX-e,minY:t.minY-e,maxX:t.maxX+e,maxY:t.maxY+e}:null,ot=(t,e,r=!1)=>{const n="undefined"!=typeof OffscreenCanvas?new OffscreenCanvas(t,e):(()=>{const r=document.createElement("canvas");return r.width=t,r.height=e,r})();return n.drawingContext=n.getContext("2d",r?{willReadFrequently:!0}:void 0),n};let st={clearTarget:()=>{throw new Error("No compositor runtime adapter registered.")},ensureBlendShaderProgram:()=>{throw new Error("No compositor runtime adapter registered.")},ensureBlendSourceFramebuffer:()=>{throw new Error("No compositor runtime adapter registered.")},createFramebuffer:()=>{throw new Error("No compositor runtime adapter registered.")},runBlendShaderPass:()=>{throw new Error("No compositor runtime adapter registered.")},blitSourceToFramebuffer:()=>{throw new Error("No compositor runtime adapter registered.")}};const at=(...t)=>st.createFramebuffer(...t);let ct=null,lt=null;const ht=t=>{((...t)=>{st.clearTarget(...t)})(q,t,nt)},ut=()=>({width:Math.max(1,Math.round(W*K)),height:Math.max(1,Math.round(j*K))}),ft=t=>{if(!t)return null;const{width:e,height:r}=ut();return((t,e,r)=>{if(!t)return null;const n=Math.max(0,Math.floor(Math.min(t.minX,t.maxX))),i=Math.max(0,Math.floor(Math.min(t.minY,t.maxY))),o=Math.min(e,Math.ceil(Math.max(t.minX,t.maxX))),s=Math.min(r,Math.ceil(Math.max(t.minY,t.maxY)));return o<=n||s<=i?null:{minX:n,minY:i,maxX:o,maxY:s}})(t,e,r)},mt=()=>{const{width:t,height:e}=ut();return((t,e)=>({minX:0,minY:0,maxX:t,maxY:e}))(t,e)},dt=(t,e=!0)=>{const r=ft(t);if(!r)return null;const{height:n}=ut();return{x:r.minX,y:e?n-r.maxY:r.minY,width:r.maxX-r.minX,height:r.maxY-r.minY}},gt=(t,e,r,n=!0)=>{const i=dt(e,n);if(i){t.enable(t.SCISSOR_TEST),t.scissor(i.x,i.y,i.width,i.height);try{r()}finally{t.disable(t.SCISSOR_TEST)}}else r()},yt={},xt=()=>{q?.loaded||(Q(),pt.load())},pt={isBlending:!1,cachedColor:null,markDirtyRect(t,e){const r=ft(e);var n,i;t&&r&&(t.dirtyRect=(i=r,(n=t.dirtyRect)?i?{minX:Math.min(n.minX,i.minX),minY:Math.min(n.minY,i.minY),maxX:Math.max(n.maxX,i.maxX),maxY:Math.max(n.maxY,i.maxY)}:n:i),t.isDrawn=!0)},clearMask(t){if(!t)return;const e=t===this.glMask?ct:lt;e?.clearMask?.(t,ht)},getCompositeRect(t,e){const r=e?ct:lt;return r?.getCompositeRect?.(t,rt,mt,it,ft)},load(){Z.syncDensity();const t=!q.blendSourceFramebuffer||q.blendSourceFramebuffer.width!==W||q.blendSourceFramebuffer.height!==j||"function"==typeof q.blendSourceFramebuffer.pixelDensity&&q.blendSourceFramebuffer.pixelDensity()!==K;((...t)=>{st.ensureBlendShaderProgram(...t)})(q,"#version 300 es\n#define GLSLIFY 1\nout vec2 p;void main(){vec3 v=vec3(-1);v[gl_VertexID]=3.;p=v.xy;gl_Position=vec4(v.x,-v.y,0,1);}","#version 300 es\nprecision highp float;\n#define GLSLIFY 1\nuniform bool u_isBrush;uniform bool u_targetIsFramebuffer;uniform sampler2D u_source;uniform sampler2D u_mask;uniform vec3 u_color;in vec2 p;out vec4 outColor;const int SPECTRAL_SIZE=38;const float SPECTRAL_GAMMA=2.4;const float SPECTRAL_EPSILON=0.0001;float spectral_uncompand(float x){return(x<0.04045)? x/12.92 : pow((x+0.055)/1.055,SPECTRAL_GAMMA);}float spectral_compand(float x){return(x<0.0031308)? x*12.92 : 1.055*pow(x,1.0/SPECTRAL_GAMMA)-0.055;}vec3 spectral_srgb_to_linear(vec3 srgb){return vec3(spectral_uncompand(srgb[0]),spectral_uncompand(srgb[1]),spectral_uncompand(srgb[2]));}vec3 spectral_linear_to_srgb(vec3 lrgb){return clamp(vec3(spectral_compand(lrgb[0]),spectral_compand(lrgb[1]),spectral_compand(lrgb[2])),0.,1.);}void spectral_linear_to_reflectance(vec3 lrgb,inout float R[SPECTRAL_SIZE]){float w=min(lrgb.r,min(lrgb.g,lrgb.b));lrgb-=w;float c=min(lrgb.g,lrgb.b);float m=min(lrgb.r,lrgb.b);float y=min(lrgb.r,lrgb.g);float r=min(max(0.0,lrgb.r-lrgb.b),max(0.0,lrgb.r-lrgb.g));float g=min(max(0.0,lrgb.g-lrgb.b),max(0.0,lrgb.g-lrgb.r));float b=min(max(0.0,lrgb.b-lrgb.g),max(0.0,lrgb.b-lrgb.r));R[0]=max(SPECTRAL_EPSILON,w*1.0011607271876400+c*0.9705850013229620+m*0.9906735573199880+y*0.0210523371789306+r*0.0315605737777207+g*0.0095560747554212+b*0.9794047525020140);R[1]=max(SPECTRAL_EPSILON,w*1.0011606515972800+c*0.9705924981434250+m*0.9906715249619790+y*0.0210564627517414+r*0.0315520718330149+g*0.0095581580120851+b*0.9794007068431300);R[2]=max(SPECTRAL_EPSILON,w*1.0011603192274700+c*0.9706253487298910+m*0.9906625823534210+y*0.0210746178695038+r*0.0315148215513658+g*0.0095673245444588+b*0.9793829034702610);R[3]=max(SPECTRAL_EPSILON,w*1.0011586727078900+c*0.9707868061190170+m*0.9906181076447950+y*0.0211649058448753+r*0.0313318044982702+g*0.0096129126297349+b*0.9792943649455940);R[4]=max(SPECTRAL_EPSILON,w*1.0011525984455200+c*0.9713686732282480+m*0.9904514808787100+y*0.0215027957272504+r*0.0306729857725527+g*0.0097837090401843+b*0.9789630146085700);R[5]=max(SPECTRAL_EPSILON,w*1.0011325252899800+c*0.9731632306212520+m*0.9898710814002040+y*0.0226738799041561+r*0.0286480476989607+g*0.0103786227058710+b*0.9778144666940430);R[6]=max(SPECTRAL_EPSILON,w*1.0010850066332700+c*0.9767402231587650+m*0.9882866087596400+y*0.0258235649693629+r*0.0246450407045709+g*0.0120026452378567+b*0.9747243211338360);R[7]=max(SPECTRAL_EPSILON,w*1.0009968788945300+c*0.9815876054913770+m*0.9842906927975040+y*0.0334879385639851+r*0.0192960753663651+g*0.0160977721473922+b*0.9671984823439730);R[8]=max(SPECTRAL_EPSILON,w*1.0008652515227400+c*0.9862802656529490+m*0.9739349056253060+y*0.0519069663740307+r*0.0142066612220556+g*0.0267061902231680+b*0.9490796575305750);R[9]=max(SPECTRAL_EPSILON,w*1.0006962900094000+c*0.9899491476891340+m*0.9418178384601450+y*0.1007490148334730+r*0.0102942608878609+g*0.0595555440185881+b*0.9008501289409770);R[10]=max(SPECTRAL_EPSILON,w*1.0005049611488800+c*0.9924927015384200+m*0.8173903261951560+y*0.2391298997068470+r*0.0076191460521811+g*0.1860398265328260+b*0.7631504454622400);R[11]=max(SPECTRAL_EPSILON,w*1.0003080818799200+c*0.9941456804052560+m*0.4324728050657290+y*0.5348043122727480+r*0.0058980410835420+g*0.5705798201161590+b*0.4659221716493190);R[12]=max(SPECTRAL_EPSILON,w*1.0001196660201300+c*0.9951839750332120+m*0.1384539782588700+y*0.7978075786430300+r*0.0048233247781713+g*0.8614677684002920+b*0.2012632804510050);R[13]=max(SPECTRAL_EPSILON,w*0.9999527659684070+c*0.9957567501108180+m*0.0537347216940033+y*0.9114498940673840+r*0.0042298748350633+g*0.9458790897676580+b*0.0877524413419623);R[14]=max(SPECTRAL_EPSILON,w*0.9998218368992970+c*0.9959128182867100+m*0.0292174996673231+y*0.9537979630045070+r*0.0040599171299341+g*0.9704654864743050+b*0.0457176793291679);R[15]=max(SPECTRAL_EPSILON,w*0.9997386095575930+c*0.9956061578345280+m*0.0213136517508590+y*0.9712416154654290+r*0.0043533695594676+g*0.9784136302844500+b*0.0284706050521843);R[16]=max(SPECTRAL_EPSILON,w*0.9997095516396120+c*0.9945976009618540+m*0.0201349530181136+y*0.9793031238075880+r*0.0053434425970201+g*0.9795890314112240+b*0.0205271767569850);R[17]=max(SPECTRAL_EPSILON,w*0.9997319302106270+c*0.9922157154923700+m*0.0241323096280662+y*0.9833801195075750+r*0.0076917201010463+g*0.9755335369086320+b*0.0165302792310211);R[18]=max(SPECTRAL_EPSILON,w*0.9997994363461950+c*0.9862364527832490+m*0.0372236145223627+y*0.9854612465677550+r*0.0135969795736536+g*0.9622887553978130+b*0.0145135107212858);R[19]=max(SPECTRAL_EPSILON,w*0.9999003303166710+c*0.9679433372645410+m*0.0760506552706601+y*0.9864350469766050+r*0.0316975442661115+g*0.9231215745131200+b*0.0136003508637687);R[20]=max(SPECTRAL_EPSILON,w*1.0000204065261100+c*0.8912850042449430+m*0.2053754719423990+y*0.9867382506701410+r*0.1078611963552490+g*0.7934340189431110+b*0.0133604258769571);R[21]=max(SPECTRAL_EPSILON,w*1.0001447879365800+c*0.5362024778620530+m*0.5412689034604390+y*0.9866178824450320+r*0.4638126031687040+g*0.4592701359024290+b*0.0135488943145680);R[22]=max(SPECTRAL_EPSILON,w*1.0002599790341200+c*0.1541081190018780+m*0.8158416850864860+y*0.9862777767586430+r*0.8470554052720110+g*0.1855741036663030+b*0.0139594356366992);R[23]=max(SPECTRAL_EPSILON,w*1.0003557969708900+c*0.0574575093228929+m*0.9128177041239760+y*0.9858605924440560+r*0.9431854093939180+g*0.0881774959955372+b*0.0144434255753570);R[24]=max(SPECTRAL_EPSILON,w*1.0004275378026900+c*0.0315349873107007+m*0.9463398301669620+y*0.9854749276762100+r*0.9688621506965580+g*0.0543630228766700+b*0.0148854440621406);R[25]=max(SPECTRAL_EPSILON,w*1.0004762334488800+c*0.0222633920086335+m*0.9599276963319910+y*0.9851769347655580+r*0.9780306674736030+g*0.0406288447060719+b*0.0152254296999746);R[26]=max(SPECTRAL_EPSILON,w*1.0005072096750800+c*0.0182022841492439+m*0.9662605952303120+y*0.9849715740141810+r*0.9820436438543060+g*0.0342215204316970+b*0.0154592848180209);R[27]=max(SPECTRAL_EPSILON,w*1.0005251915637300+c*0.0162990559732640+m*0.9693259700584240+y*0.9848463034157120+r*0.9839236237187070+g*0.0311185790956966+b*0.0156018026485961);R[28]=max(SPECTRAL_EPSILON,w*1.0005350960689600+c*0.0153656239334613+m*0.9708545367213990+y*0.9847753518111990+r*0.9848454841543820+g*0.0295708898336134+b*0.0156824871281936);R[29]=max(SPECTRAL_EPSILON,w*1.0005402209748200+c*0.0149111568733976+m*0.9716050665281280+y*0.9847380666252650+r*0.9852942758145960+g*0.0288108739348928+b*0.0157248764360615);R[30]=max(SPECTRAL_EPSILON,w*1.0005427281678400+c*0.0146954339898235+m*0.9719627697573920+y*0.9847196483117650+r*0.9855072952198250+g*0.0284486271324597+b*0.0157458108784121);R[31]=max(SPECTRAL_EPSILON,w*1.0005438956908700+c*0.0145964146717719+m*0.9721272722745090+y*0.9847110233919390+r*0.9856050715398370+g*0.0282820301724731+b*0.0157556123350225);R[32]=max(SPECTRAL_EPSILON,w*1.0005444821215100+c*0.0145470156699655+m*0.9722094177458120+y*0.9847066833006760+r*0.9856538499335780+g*0.0281988376490237+b*0.0157605443964911);R[33]=max(SPECTRAL_EPSILON,w*1.0005447695999200+c*0.0145228771899495+m*0.9722495776784240+y*0.9847045543930910+r*0.9856776850338830+g*0.0281581655342037+b*0.0157629637515278);R[34]=max(SPECTRAL_EPSILON,w*1.0005448988776200+c*0.0145120341118965+m*0.9722676219987420+y*0.9847035963093700+r*0.9856883918061220+g*0.0281398910216386+b*0.0157640525629106);R[35]=max(SPECTRAL_EPSILON,w*1.0005449625468900+c*0.0145066940939832+m*0.9722765094621500+y*0.9847031240775520+r*0.9856936646900310+g*0.0281308901665811+b*0.0157645892329510);R[36]=max(SPECTRAL_EPSILON,w*1.0005449892705800+c*0.0145044507314479+m*0.9722802433068740+y*0.9847029256150900+r*0.9856958798482050+g*0.0281271086805816+b*0.0157648147772649);R[37]=max(SPECTRAL_EPSILON,w*1.0005449969930000+c*0.0145038009464639+m*0.9722813248265600+y*0.9847028681227950+r*0.9856965214637620+g*0.0281260133612096+b*0.0157648801149616);}vec3 spectral_xyz_to_srgb(vec3 xyz){mat3 XYZ_RGB;XYZ_RGB[0]=vec3(3.2409699419045200,-1.537383177570090,-0.4986107602930030);XYZ_RGB[1]=vec3(-0.9692436362808790,1.875967501507720,0.0415550574071756);XYZ_RGB[2]=vec3(0.0556300796969936,-0.203976958888976,1.0569715142428700);float r=dot(XYZ_RGB[0],xyz);float g=dot(XYZ_RGB[1],xyz);float b=dot(XYZ_RGB[2],xyz);return spectral_linear_to_srgb(vec3(r,g,b));}vec3 spectral_reflectance_to_xyz(float R[SPECTRAL_SIZE]){vec3 xyz=vec3(0.);xyz+=R[0]*vec3(0.0000646919989576,0.0000018442894440,0.0003050171476380);xyz+=R[1]*vec3(0.0002194098998132,0.0000062053235865,0.0010368066663574);xyz+=R[2]*vec3(0.0011205743509343,0.0000310096046799,0.0053131363323992);xyz+=R[3]*vec3(0.0037666134117111,0.0001047483849269,0.0179543925899536);xyz+=R[4]*vec3(0.0118805536037990,0.0003536405299538,0.0570775815345485);xyz+=R[5]*vec3(0.0232864424191771,0.0009514714056444,0.1136516189362870);xyz+=R[6]*vec3(0.0345594181969747,0.0022822631748318,0.1733587261835500);xyz+=R[7]*vec3(0.0372237901162006,0.0042073290434730,0.1962065755586570);xyz+=R[8]*vec3(0.0324183761091486,0.0066887983719014,0.1860823707062960);xyz+=R[9]*vec3(0.0212332056093810,0.0098883960193565,0.1399504753832070);xyz+=R[10]*vec3(0.0104909907685421,0.0152494514496311,0.0891745294268649);xyz+=R[11]*vec3(0.0032958375797931,0.0214183109449723,0.0478962113517075);xyz+=R[12]*vec3(0.0005070351633801,0.0334229301575068,0.0281456253957952);xyz+=R[13]*vec3(0.0009486742057141,0.0513100134918512,0.0161376622950514);xyz+=R[14]*vec3(0.0062737180998318,0.0704020839399490,0.0077591019215214);xyz+=R[15]*vec3(0.0168646241897775,0.0878387072603517,0.0042961483736618);xyz+=R[16]*vec3(0.0286896490259810,0.0942490536184085,0.0020055092122156);xyz+=R[17]*vec3(0.0426748124691731,0.0979566702718931,0.0008614711098802);xyz+=R[18]*vec3(0.0562547481311377,0.0941521856862608,0.0003690387177652);xyz+=R[19]*vec3(0.0694703972677158,0.0867810237486753,0.0001914287288574);xyz+=R[20]*vec3(0.0830531516998291,0.0788565338632013,0.0001495555858975);xyz+=R[21]*vec3(0.0861260963002257,0.0635267026203555,0.0000923109285104);xyz+=R[22]*vec3(0.0904661376847769,0.0537414167568200,0.0000681349182337);xyz+=R[23]*vec3(0.0850038650591277,0.0426460643574120,0.0000288263655696);xyz+=R[24]*vec3(0.0709066691074488,0.0316173492792708,0.0000157671820553);xyz+=R[25]*vec3(0.0506288916373645,0.0208852059213910,0.0000039406041027);xyz+=R[26]*vec3(0.0354739618852640,0.0138601101360152,0.0000015840125870);xyz+=R[27]*vec3(0.0214682102597065,0.0081026402038399,0.0000000000000000);xyz+=R[28]*vec3(0.0125164567619117,0.0046301022588030,0.0000000000000000);xyz+=R[29]*vec3(0.0068045816390165,0.0024913800051319,0.0000000000000000);xyz+=R[30]*vec3(0.0034645657946526,0.0012593033677378,0.0000000000000000);xyz+=R[31]*vec3(0.0014976097506959,0.0005416465221680,0.0000000000000000);xyz+=R[32]*vec3(0.0007697004809280,0.0002779528920067,0.0000000000000000);xyz+=R[33]*vec3(0.0004073680581315,0.0001471080673854,0.0000000000000000);xyz+=R[34]*vec3(0.0001690104031614,0.0000610327472927,0.0000000000000000);xyz+=R[35]*vec3(0.0000952245150365,0.0000343873229523,0.0000000000000000);xyz+=R[36]*vec3(0.0000490309872958,0.0000177059860053,0.0000000000000000);xyz+=R[37]*vec3(0.0000199961492222,0.0000072209749130,0.0000000000000000);return xyz;}float KS(float R){return pow(1.0-R,2.0)/(2.0*R);}float KM(float KS){return 1.0+KS-sqrt(pow(KS,2.0)+2.0*KS);}vec3 spectral_mix(vec3 color1,float tintingStrength1,float factor1,vec3 color2,float tintingStrength2,float factor2){vec3 lrgb1=spectral_srgb_to_linear(color1);vec3 lrgb2=spectral_srgb_to_linear(color2);float R1[SPECTRAL_SIZE];float R2[SPECTRAL_SIZE];spectral_linear_to_reflectance(lrgb1,R1);spectral_linear_to_reflectance(lrgb2,R2);float luminance1=spectral_reflectance_to_xyz(R1)[1];float luminance2=spectral_reflectance_to_xyz(R2)[1];float R[SPECTRAL_SIZE];for(int i=0;i<SPECTRAL_SIZE;i++){float concentration1=pow(factor1,2.)*pow(tintingStrength1,2.)*luminance1;float concentration2=pow(factor2,2.)*pow(tintingStrength2,2.)*luminance2;float totalConcentration=concentration1+concentration2;float ksMix=0.;ksMix+=KS(R1[i])*concentration1;ksMix+=KS(R2[i])*concentration2;R[i]=KM(ksMix/totalConcentration);}return spectral_xyz_to_srgb(spectral_reflectance_to_xyz(R));}vec3 spectral_mix(vec3 color1,vec3 color2,float factor){return spectral_mix(color1,1.,1.-factor,color2,1.,factor);}vec3 spectral_mix(vec3 color1,float factor1,vec3 color2,float factor2){return spectral_mix(color1,1.,factor1,color2,1.,factor2);}const float GAMMA=2.4,EPSILON=0.0001;const float DARKEN_THRESHOLD=0.7;const float EDGE_MIN=0.05,EDGE_MAX=0.35;void main(void){vec2 uv=0.5*p+0.5;vec2 sourceUV=vec2(uv.x,1.0-uv.y);vec2 maskUV=u_targetIsFramebuffer ? vec2(uv.x,1.0-uv.y): uv;vec4 source=texture(u_source,sourceUV);vec4 maskColor=texture(u_mask,maskUV);if(maskColor.a==0.0){outColor=source;return;}vec4 pigment=vec4(u_color.xyz,1.0);if(u_isBrush&&(maskColor.a>DARKEN_THRESHOLD)){float blacken=0.5*(min(maskColor.a,1.0)-DARKEN_THRESHOLD);pigment=pigment*(1.0-blacken)-vec4(0.5)*blacken;pigment.rgb=max(pigment.rgb,vec3(0.0));}float mixIntensity=min(maskColor.a,1.0);if(!u_isBrush){vec2 texelSize=1.0/vec2(textureSize(u_mask,0));float scaledAlpha=maskColor.a*15.0;float blurEdge=0.0;for(int i=-2;i<=2;i+=2){for(int j=-2;j<=2;j+=2){vec2 neighborUV=maskUV+vec2(float(i),float(j))*texelSize;float neighborAlpha=texture(u_mask,neighborUV).a*15.0;blurEdge+=smoothstep(EDGE_MIN,EDGE_MAX,length(vec2(dFdx(neighborAlpha),dFdy(neighborAlpha))));}}blurEdge/=9.0;mixIntensity=clamp(maskColor.a+blurEdge*0.1,0.0,1.0);}vec3 bgColor=mix(vec3(1.0),source.rgb,source.a);outColor=vec4(spectral_mix(bgColor,pigment.rgb,mixIntensity),1.);}"),this.glMask=ct?.ensureResources?.(q,W,j,K);const e=lt?.ensureResources?.(q,W,j,K,ht)??{mask:null,ctx:null};t&&(q.blendSourceFramebuffer=((...t)=>st.ensureBlendSourceFramebuffer(...t))(q,q.blendSourceFramebuffer,W,j,K)),this.mask=e.mask,this.ctx=e.ctx},blend(t=!1,e=!1){xt();const r=!0===this.isBrush,n=r?this.glMask:this.mask,i=r?this.mask:this.glMask,s=t?._array,a=!!s&&(this.cachedColor?.[0]!==s[0]||this.cachedColor?.[1]!==s[1]||this.cachedColor?.[2]!==s[2]||this.cachedColor?.[3]!==s[3]);!this.isBlending&&s&&(this.isBlending=!0,this.cachedColor=s,o(),this.clearMask(this.glMask)),(e||a)&&(this.justChanged&&(this.applyShader(i,!r),this.justChanged=!1),this.isBlending&&this.applyShader(n,r),s&&(this.cachedColor=s),e&&(this.isBlending=!1,this.cachedColor=null))},applyShader(t,e){if(!t?.isDrawn)return;const r=this.getCompositeRect(t,e);if(!r)return void this.clearMask(t);q.drawingContext;const n=q.shaderProgram,i=rt(),o=((...t)=>st.blitSourceToFramebuffer(...t))({renderer:q,sourceTarget:i??q,sourceFramebuffer:q.blendSourceFramebuffer,dirtyRect:r,isFramebufferTarget:nt,Cwidth:W,Cheight:j,getTargetPixelSize:ut,toScissorBox:dt,withScissor:gt}),s=!!i,a=(e?ct:lt).getShaderMask(q,t,r,mt,ht);((...t)=>{st.runBlendShaderPass(...t)})({renderer:q,shader:n,source:o,mask:a,color:this.cachedColor,isBrushMask:e,Cwidth:W,Cheight:j,dirtyRect:r,targetIsFramebuffer:s,withScissor:gt}),this.clearMask(t)}},vt=(t=!1)=>{((t=!1)=>{Z.load(t)})(t),q.loaded&&pt.load()};let bt=!1;function wt(){bt||(xt(),At=.01*W,Rt=-.5*W,Et=-.5*j,Mt=Math.round(2*W/At),St=Math.round(2*j/At),Dt("hand",(t,e)=>{const r=w(.2,.8),n=R(5,10);return Yt(e,(e,i)=>.5*n*B(r*i*e+R(15,25))*.2*N(t)+p(e,i)*n*.7)}),Dt("curved",(t,e)=>{let r=R(-10,10);return R(0,100)%2==0&&(r*=-1),Yt(e,(e,n)=>3*T(p(.02*e+.03*t,.02*n+.03*t),0,1,-r,r))}),Dt("zigzag",(t,e)=>{let r=R(-30,-15)+Math.abs(44*B(t));R(0,100)%2==0&&(r*=-1);let n=r,i=0;for(let t=0;t<Mt;t++){for(let r=0;r<St;r++)e[t][r]=i,i+=n,n*=-1;i+=n,n*=-1}return e}),Dt("waves",(t,e)=>{const r=R(10,15)+5*B(t),n=R(3,6)+3*N(t),i=R(20,35);return Yt(e,(t,e)=>B(r*t)*i*N(e*n)+R(-3,3))}),Dt("seabed",(t,e)=>{const r=w(.4,.8),n=R(18,26);return Yt(e,(e,i)=>1.1*n*B(r*i*e+R(15,20))*N(t))}),Dt("spiral",(t,e)=>{const r=R(5,10),n=2*R(0,2)-1,i=R(65,80),o=Array.from({length:r},()=>({x:w(.1,.9)*Mt,y:w(.1,.9)*St}));return Yt(e,(t,e)=>{let r=0,s=0;for(const a of o){const o=t-a.x,c=e-a.y,l=1/(o*o+c*c+1),h=Math.atan2(c,o)*(180/Math.PI),u=n*(h+i)*Math.PI/180;r+=l*Math.cos(u),s+=l*Math.sin(u)}return Math.atan2(s,r)*(180/Math.PI)})}),Dt("columns",(t,e)=>{const r=R(3,8),n=R(25,45);return Yt(e,(t,e)=>B(t*r)*n)}),bt=!0)}class _t{constructor(t,e){wt();const r=i();this.mx=r.x,this.my=r.y,this.update(t,e),this.plotted=0}update(t,e){this.x=t,this.y=e,yt.field.isActive&&(this.colIdx=Math.round((t+this.mx-Rt)/At),this.rowIdx=Math.round((e+this.my-Et)/At))}reset(){this.plotted=0}isIn(){return yt.field.isActive?_t.isIn(this.colIdx,this.rowIdx):this.isInCanvas(this.x,this.y)}isInCanvas(){const t=W,e=j,r=this.x+this.mx,n=this.y+this.my;return r>=-.5*t&&r<=1.5*t&&n>=-.5*e&&n<=1.5*e}angle(t=!1){return yt.field.isActive&&(t||this.isIn())?Pt.get(yt.field.current).field[this.colIdx][this.rowIdx]*yt.field.wiggle:0}moveTo(t,e,r=1){const n=X(t);yt.field.isActive?this.movePos(n,e,r):this._moveConstant(n,e,r)}_moveToDegrees(t,e,r=1){yt.field.isActive?this.movePos(t,e,r):this._moveConstant(t,e,r)}_moveConstant(t,e,r){if(!this.isIn())return void(this.plotted+=r);const n=e/r,i=z(-t),o=r*i[0],s=r*i[1];for(let t=0;t<n;t++)this.x+=o,this.y+=s,this.plotted+=r}plotTo(t,e,r,n=1,i=void 0){this.movePos(t,e,r,n,i)}movePos(t,e,r,n=!1,i=void 0){const o=n||1;if(!this.isIn())return void(this.plotted+=r/o);const s=e/r,a=yt.field.isActive,c=!!n;for(let e=0;e<s;e++){const n=c&&void 0!==i&&0===e?i:c?t.angle(this.plotted):t,s=(a?this.angle(!0):0)-n,l=z(s);this.update(this.x+r*l[0],this.y+r*l[1]),this.plotted+=r/o}}static getRowIndex(t,e=1){const r=t+i().y-Et;return Math.round(r/At/e)}static getColIndex(t,e=1){const r=t+i().x-Rt;return Math.round(r/At/e)}static isIn(t,e){return t>=0&&e>=0&&t<Mt&&e<St}}yt.field={isActive:!1,current:null,wiggle:1};let At,Rt,Et,Mt,St,Pt=new Map;const Tt=new Set(["degrees","radians"]);function Ct(t={}){const e=("string"==typeof t?{angleMode:t}:t||{}).angleMode??"degrees";if(!Tt.has(e))throw new Error(`Invalid field angle mode "${e}". Use "degrees" or "radians".`);return e}function kt(t,e){return function(t,e){if("radians"!==e)return t;for(let e=0;e<t.length;e++)for(let r=0;r<t[e].length;r++)t[e][r]=X(t[e][r],!0);return t}(t.gen(e,new Array(Mt).fill(null).map(()=>new Float32Array(St))),t.angleMode)}function It(t=0){if(!yt.field.isActive||!yt.field.current)throw new Error("No field is currently active. Call brush.field('name') to activate one before refreshing.");const e=Pt.get(yt.field.current);e.field=kt(e,t)}function Ft(t){if(yt.field.wiggle||(yt.field.wiggle=1),wt(),!Pt.has(t))throw new Error(`Field "${t}" does not exist. Available fields: ${Array.from(Pt.keys()).join(", ")}.`);yt.field.isActive=!0,yt.field.current=t;const e=Pt.get(t);e.field||(e.field=kt(e,0))}function Lt(){wt(),yt.field.isActive=!1}function Dt(t,e,r={}){Pt.set(t,{gen:e,field:null,angleMode:Ct(r)})}function Nt(){return wt(),Array.from(Pt.keys())}function Bt(t=1){Ft("hand"),yt.field.wiggle=t}function Yt(t,e){for(let r=0;r<Mt;r++)for(let n=0;n<St;n++)t[r][n]=e(r,n);return t}class zt{constructor(t,e=!1){this.a=t,this.vertices=e?t:t.map(([t,e])=>({x:t,y:e})),this.sides=this.vertices.map((t,e,r)=>[t,r[(e+1)%r.length]]),this._intersectionCache={}}intersect(t){const e=`${t.point1.x},${t.point1.y}-${t.point2.x},${t.point2.y}`;if(this._intersectionCache[e])return this._intersectionCache[e];const r=[],n=this.sides,i=n.length;for(let e=0;e<i;e++){const i=H(t.point1,t.point2,n[e][0],n[e][1]);i&&r.push(i)}return this._intersectionCache[e]=r,r}show(){yt.wash&&this.wash(),yt.fill&&this.fill(),yt.mass&&this.mass(),yt.hatch&&this.hatch(),yt.stroke&&this.draw()}}class Ot{constructor(t){this.segments=[],this.angles=[],this.pres=[],this.type=t,this.dir=0,this._cumLen=[],this.length=0,this.index=0,this.suma=0,this.pol=!1}addSegment(t=0,e=0,r=1,n=!1){this.angles.length>0&&this.angles.pop(),t=n?(t%360+360)%360:O(t),this.angles.push(t,t),this.pres.push(r),this._cumLen.push(this.length),this.segments.push(e),this.length+=e}endPlot(t=0,e=1,r=!1){t=r?(t%360+360)%360:O(t),this.angles[this.angles.length-1]=t,this.pres.push(e)}rotate(t){this.dir=O(t)}pressure(t){if(t>this.length)return this.pres[this.pres.length-1];const e=this.pres[this.index],r=this.pres[this.index+1],n=this.segments[this.index];return 0===n?e:e+(t-this.suma)/n*(r-e)}angle(t){if(t>this.length)return this.angles[this.angles.length-1];if(this.calcIndex(t),"curve"!==this.type)return this.angles[this.index]+this.dir;let e=this.angles[this.index],r=this.angles[this.index+1];Math.abs(r-e)>180&&(r>e?r=-(360-r):e=-(360-e));const n=this.segments[this.index];return e+(0===n?0:(t-this.suma)/n)*(r-e)+this.dir}calcIndex(t){const e=this._cumLen,r=e.length;if(0===r)return this.index=0,this.suma=0,0;let n=this.index<r?this.index:r-1;for(;n+1<r&&e[n+1]<=t;)n++;if(e[n]>t){let r=0,i=n-1;for(;r<i;){const n=r+i+1>>1;e[n]<=t?r=n:i=n-1}n=r}return this.index=n,this.suma=e[n],n}genPol(t,e,r=1,n){wt();const i=n<0?4:1,o=[],s=Math.round(this.length/i),a=new _t(t+W/2,e+j/2);let c=0,l=0;for(let t=0;t<s;t++){a.plotTo(this,i,i);const t=this.index;c+=i,(c>=(n<=0?8:Math.max(this.segments[t]*n*w(.7,1.3),20))||t>=l)&&a.x&&(o.push([a.x-W/2,a.y-j/2]),c=0,t>=l&&l++)}return new zt(o)}show(t,e,r=1){yt.wash&&this.wash(t,e,r),yt.fill&&this.fill(t,e,r),yt.mass&&this.mass(t,e,r),yt.hatch&&this.hatch(t,e,r),yt.stroke&&this.draw(t,e,r)}}function Xt(t){const e=new zt(t);return e.show(),e}function Ut(t,e,r,n,i="corner"){"center"===i&&(t-=r/2,e-=n/2),Kt(0),Zt(t,e),Zt(t+r,e),Zt(t+r,e+n),Zt(t,e+n),Jt(!0)}function Gt(t,e,r,n=!1){const i=new Ot("curve"),o=Math.PI*r,s=w(0,360),a=n?()=>1+.2*n*w():()=>1;for(let t=0;t<4;t++){const e=-90*t+s;i.addSegment(e*a(),o/2*a(),1,!0)}if(n){const t=n*R(-5,5);i.addSegment(s,Math.abs(t)*(Math.PI/180)*r,1,!0),i.endPlot(t+s,1,!0)}else i.endPlot(s,1,!0);const c=t-r*B(s),l=e-r*N(-s);return i.show(c,l,1),[i,c,l]}function Vt(t,e,r,n,i){const o=X(n),s=((X(i)-o)%360+360)%360;if(0===s)return null;const a=new Ot("curve"),c=Math.max(1,Math.ceil(s/90)),l=s/c,h=Math.PI*r*l/180;for(let t=0;t<c;t++)a.addSegment(o+t*l+90,h,1,!0);a.endPlot(o+s+90,1,!0);const u=t+r*N(o),f=e-r*B(o);return a.draw(u,f,1),a}let Ht,Wt,jt,$t;class qt{constructor(){this.isClosed=!1,this.curvature=Wt,this.vert=[]}vertex(t,e,r){this.vert.push([t,e,r])}show(){let t=ne(this.vert,this.curvature,this.isClosed);return t.show(),t}}function Kt(t=0){Wt=C(t,0,1),Ht=new qt}function Zt(t,e,r=1){if(!Ht)throw new Error("vertex() called outside of beginShape()/endShape(). Call beginShape() first.");Ht.vertex(t,e,r)}function Jt(t=!1){if(!Ht)throw new Error("endShape() called without beginShape(). Call beginShape() first.");if(Ht.vert.length<2)throw new Error("endShape() requires at least 2 vertices. Add more with vertex().");t&&(Ht.vertex(...Ht.vert[0]),Ht.isClosed=!0);const e=Ht.show();return Ht=!1,e}function Qt(t,e,r){if("curve"!==t&&"segments"!==t)throw new Error(`beginStroke() type must be "curve" or "segments", got "${t}".`);$t=[e,r],jt=new Ot(t)}function te(t,e,r){if(!jt)throw new Error("move() called without beginStroke(). Call beginStroke() first.");jt.addSegment(t,e,r)}function ee(t,e){if(!jt)throw new Error("endStroke() called without beginStroke(). Call beginStroke() first.");jt.endPlot(t,e),jt.draw($t[0],$t[1],1),jt=!1}function re(t,e=.5){if(!t||t.length<2)throw new Error("spline() requires at least 2 points. Each point should be [x, y, pressure].");let r=ne(t,e);return r.show(),r}function ne(t,e=.5,r=!1){const n=new Ot(0===e?"segments":"curve"),i=2*Math.PI;if(r&&0!==e&&t.push(t[1]),t&&t.length>0){let o,s,a,c=0;for(let l=0;l<t.length-1;l++)if(e>0&&l<t.length-2){const h=t[l],u=t[l+1],f=t[l+2],m=G(h[0],h[1],u[0],u[1]),d=G(u[0],u[1],f[0],f[1]),g=V(h[0],h[1],u[0],u[1]),y=V(u[0],u[1],f[0],f[1]),x=e*Math.min(m,d,.5*Math.min(m,d)),p=Math.max(m,d),v=m-x,b=d-x;if(Math.floor(g)===Math.floor(y)){const e=r&&0===l?0:m-c,i=r?0===l?0:d-a:d;n.addSegment(g,e,h[2],!0),l===t.length-3&&n.addSegment(y,i,u[2],!0),c=0,0===l&&(o=m,a=x,s=t[1],c=0)}else{const e={x:u[0]-x*N(-g),y:u[1]-x*B(-g)},f={x:e.x+p*N(90-g),y:e.y+p*B(90-g)},m={x:u[0]+x*N(-y),y:u[1]+x*B(-y)},d={x:m.x+p*N(90-y),y:m.y+p*B(90-y)},w=H(e,f,m,d,!0),_=G(e.x,e.y,w.x,w.y),A=G(e.x,e.y,m.x,m.y)/2,R=i*_*(2*Math.asin(A/_)*(180/Math.PI))/360,E=r&&0===l?0:v-c,M=l===t.length-3?r?o-x:b:0;n.addSegment(g,E,h[2],!0),n.addSegment(g,isNaN(R)?0:R,h[2],!0),n.addSegment(y,M,u[2],!0),c=x,0===l&&(o=v,a=x,s=[e.x,e.y])}l===t.length-3&&n.endPlot(y,u[2],!0)}else if(0===e){const e=t[l],r=t[l+1],i=G(e[0],e[1],r[0],r[1]),o=V(e[0],e[1],r[0],r[1]);n.addSegment(o,i,e[2],!0),l===t.length-2&&n.endPlot(o,r[2],!0)}n.origin=r&&0!==e?s:t[0]}return n}let ie={createTipSurface:()=>{throw new Error("No stroke runtime adapter registered.")},loadImageTip:()=>{throw new Error("No stroke runtime adapter registered.")}},oe=!1;const se=(t,e,r)=>{const n=t.createProgram();for(let[i,o]of[[t.VERTEX_SHADER,e],[t.FRAGMENT_SHADER,r]]){const e=t.createShader(i);t.shaderSource(e,o),t.compileShader(e),t.attachShader(n,e)}return t.linkProgram(n),n};let ae={beginDirectMaskDraw:()=>{throw new Error("No renderer runtime adapter registered.")},endDirectMaskDraw:()=>{throw new Error("No renderer runtime adapter registered.")},resetDirectShaderTracking:()=>{}};const ce=(t,e)=>ae.resetDirectShaderTracking(t,e);let le,he,ue,fe,me,de=!1,ge=0,ye=0,xe=null;const pe={},ve={};let be=1,we=0,_e=0,Ae=1,Re=0,Ee=0,Me=0,Se=0,Pe=1,Te=1,Ce=new Float32Array(8192),ke=0,Ie=2048,Fe=32768,Le=null,De=null,Ne=null,Be=null,Ye=null;const ze={},Oe={};let Xe=0,Ue=new Float32Array(1280),Ge=0,Ve=256,He=null;const We=new Map,je=new Float32Array([-1,-1,1,-1,-1,1,1,1]);function $e(){return he}function qe(){return((t,e,r)=>ae.beginDirectMaskDraw(t,e,r))(q,le,pt.glMask)}function Ke(t){((t,e,r)=>{ae.endDirectMaskDraw(t,e,r)})(q,le,t)}function Ze(t,e,r,n,i){return t?(e<t.minX&&(t.minX=e),r<t.minY&&(t.minY=r),n>t.maxX&&(t.maxX=n),i>t.maxY&&(t.maxY=i),t):{minX:e,minY:r,maxX:n,maxY:i}}function Je(t,e,r,n){if(ke>=Ie){Ie*=2;const t=new Float32Array(4*Ie);t.set(Ce),Ce=t}const i=t-Me,o=e-Se,s=4*ke,a=be*i+_e*o+Re+Me,c=we*i+Ae*o+Ee+Se,l=Te*r*Pe/2;Ce[s]=a,Ce[s+1]=c,Ce[s+2]=l,Ce[s+3]=n/255;const h=a*Te,u=c*Te;Le=Ze(Le,h-l-1,u-l-1,h+l+1,u+l+1),ke++}function Qe(t,e){if(0===Ge)return;pt.glMask.isDrawn=!0;const r=qe();let n=We.get(e);n||(n=le.createTexture(),le.bindTexture(le.TEXTURE_2D,n),le.texImage2D(le.TEXTURE_2D,0,le.RGBA,le.RGBA,le.UNSIGNED_BYTE,t.canvas),le.texParameteri(le.TEXTURE_2D,le.TEXTURE_MIN_FILTER,le.LINEAR),le.texParameteri(le.TEXTURE_2D,le.TEXTURE_MAG_FILTER,le.LINEAR),le.texParameteri(le.TEXTURE_2D,le.TEXTURE_WRAP_S,le.CLAMP_TO_EDGE),le.texParameteri(le.TEXTURE_2D,le.TEXTURE_WRAP_T,le.CLAMP_TO_EDGE),We.set(e,n)),le.useProgram(De),le.enable(le.BLEND),le.blendFunc(le.ONE_MINUS_DST_ALPHA,le.ONE),le.activeTexture(le.TEXTURE0),le.bindTexture(le.TEXTURE_2D,n),le.uniform1i(Oe.u_tex,0);const i=yt.stroke.color._array;le.uniform4f(Oe.u_color,...i),le.uniformMatrix4fv(Oe.u_proj,!1,$e()),le.bindVertexArray(Ne),le.bindBuffer(le.ARRAY_BUFFER,Ye);const o=5*Ge*4,s=Ue.subarray(0,5*Ge);o>Xe?(le.bufferData(le.ARRAY_BUFFER,Ue,le.DYNAMIC_DRAW),Xe=Ue.byteLength):le.bufferSubData(le.ARRAY_BUFFER,0,s),le.drawArraysInstanced(le.TRIANGLE_STRIP,0,4,Ge),le.bindVertexArray(null),le.bindTexture(le.TEXTURE_2D,null),Ke(r),He&&(pt.markDirtyRect(pt.glMask,He),He=null),Ge=0,ce(q,le)}oe||(ct={ensureResources:function(t,e,r,n){return function(t,e,r,n){return!t.glMask||t.glMask.width!==e||t.glMask.height!==r||"function"==typeof t.glMask.pixelDensity&&t.glMask.pixelDensity()!==n}(t,e,r,n)&&(t.glMask?.remove&&t.glMask.remove(),t.glMask=at(t,{width:e,height:r,density:n,antialias:!1,depth:!1,stencil:!1})),t.glMask.dirtyRect??=null,t.glMask.isDrawn??=!1,t.glMask},clearMask:function(t,e){t&&(e(t),t.isDrawn=!1,t.dirtyRect=null)},getCompositeRect:function(t,e,r,n,i){return t?t.dirtyRect?e()?r():i(n(t.dirtyRect,2)):r():null},getShaderMask:function(t,e){return e}},oe=!0),yt.stroke={color:null,weight:1,type:"HB",isActive:!1,opacity:1};let tr=new Map;const er={offset:.08,scale:.08,warp:.06,tilt:.06};function rr(){return{...yt.stroke}}function nr(t){yt.stroke={...t}}function ir(t,e){if(e.type=["marker","custom","image","spray"].includes(e.type)?e.type:"default",void 0===e.markerTip&&(e.markerTip=!0),void 0===e.noise&&(e.noise=.3),e.noise=Math.max(0,Math.min(1,e.noise)),void 0!==e.vibration&&void 0===e.scatter&&(e.scatter=e.vibration),void 0!==e.definition&&void 0===e.sharpness&&(e.sharpness=e.definition),void 0!==e.quality&&void 0===e.grain&&(e.grain=e.quality),e.pressure=function(t){if(!t)return t;if("function"==typeof t)return{type:"custom",min_max:[0,1],curve:t,variation:{...er}};if("object"==typeof t&&!Array.isArray(t)){if("custom"===t.type||"custom"===t.mode){const{mode:e,...r}=t;return{...r,type:"custom",variation:{...er,...r.variation??{}}}}return"gaussian"===t.type||"gaussian"===t.mode||Array.isArray(t.curve)&&Array.isArray(t.min_max)?{...t,type:"gaussian",curve:t.curve,min_max:t.min_max}:t}if(Array.isArray(t)){const[e,r,n]=2===t.length?[t[0],(t[0]+t[1])/2,t[1]]:t,i=Math.min(e,r,n),o=Math.max(e,r,n),s=o-i||1,[a,c,l]=[(e-i)/s,(r-i)/s,(n-i)/s];return{type:"custom",min_max:[i,o],variation:{...er},curve:t=>t<.5?a+(c-a)*t*2:c+(l-c)*(t-.5)*2}}}(e.pressure),"custom"===e.type){if("function"!=typeof e.tip)throw new Error(`Brush "${t}" is type "custom" but is missing a tip function.`);const r=`custom::${t}`;!function(t){const e=We.get(t);e&&le&&le.deleteTexture(e),We.delete(t)}(r);const n=ie.createTipSurface(500,500);return n.pixelDensity(1),n.background(255),n.noSmooth(),n.push(),n.translate(250,250),n.scale(5),n.noStroke(),e.tip(n),n.pop(),Nr.imageToWhite(n),Nr.tips.set(r,n),e.tipKey=r,void tr.set(t,{param:e,colors:[],buffers:[]})}if("image"===e.type){if(!e.image||!e.image.src)throw new Error(`Brush "${t}" is type "image" but is missing params.image.src. Example: image: { src: "./tip.jpg" }`);return Nr.add(e.image.src),tr.set(t,{param:e,colors:[],buffers:[]}),Nr.load()}tr.set(t,{param:e,colors:[],buffers:[]})}function or(){return[...tr.keys()]}function sr(t){return tr.get(t)?.param??null}function ar(t){for(const{param:e}of tr.values())e&&(e.weight*=t,e.scatter*=t,e.spacing*=t)}function cr(t){if(!tr.has(t))throw new Error(`Brush "${t}" not found. Available brushes: ${[...tr.keys()].join(", ")}.`);yt.stroke.type=t}function lr(t,e,r){Q(),yt.stroke.color=n(...arguments),yt.stroke.isActive=!0}function hr(t){yt.stroke.weight=t}function ur(t,e,r=1){cr(t),lr(e),hr(r)}function fr(){yt.stroke.isActive=!1}function mr(t){return Q(),t}function dr(){}let gr,yr,xr,pr,vr=0;const br={};function wr(t,e,r,n=!1){!function(){const t=i();be=t.a,we=t.b,_e=t.c,Ae=t.d,Re=t.x,Ee=t.y,Me=W/2,Se=j/2,Pe=Math.sqrt(be*be+we*we),Te=K}(),gr=new _t(t+W/2,e+j/2),yr=r,xr=n,xr&&xr.calcIndex(0)}const _r=[];function Ar(t,e){e||(pr=t),function(){br.seed=999999*b();const{param:t}=tr.get(yt.stroke.type)??{};if(!t)return;br.p=t;const{pressure:e}=t;if(br.isCustomPressure="custom"===e.type,br.a=br.isCustomPressure?0:b(-1,1),br.b=br.isCustomPressure?0:b(1,1.5),br.isCustomPressure){const t=e.variation??er;br.cp=b(-t.offset,t.offset),br.ct=b(-t.warp,t.warp),br.cs=b(1-t.scale,1+t.scale),br.ck=b(-t.tilt,t.tilt)}else br.cp=b(3,3.5),br.ct=0,br.cs=1,br.ck=0;[br.min,br.max]=e.min_max,xr||(br.cos=N(pr),br.sin=B(pr)),function(){xt(),function(){const t=q?.glMask;t&&(xe=t,xe.dirtyRect??=null,xe.isDrawn??=!1,pt.glMask=xe)}();const t=q.drawingContext,e=!de||le!==t;(e||ge!==W||ye!==j)&&(he=new Float32Array([2/W,0,0,0,0,2/j,0,0,0,0,1,0,-1,-1,0,1]),ge=W,ye=j),e&&(function(){if(le&&!le.isContextLost?.()){for(const t of We.values())le.deleteTexture(t);We.clear(),me&&le.deleteProgram(me),ue&&le.deleteVertexArray(ue),fe&&le.deleteBuffer(fe),De&&le.deleteProgram(De),Ne&&le.deleteVertexArray(Ne),Be&&le.deleteBuffer(Be),Ye&&le.deleteBuffer(Ye),me=ue=fe=null,De=Ne=Be=Ye=null,Fe=0,Xe=0}else We.clear()}(),le=t,me=se(le,"#version 300 es\n#define GLSLIFY 1\nin vec2 a_position;in float a_radius;in float a_alpha;uniform mat4 u_matrix;out float v_alpha;void main(){gl_Position=u_matrix*vec4(a_position,0.0,1.0);v_alpha=a_alpha;gl_PointSize=a_radius*2.0;}","#version 300 es\nprecision highp float;\n#define GLSLIFY 1\nin float v_alpha;out vec4 outColor;uniform vec4 u_color;void main(){vec2 v=gl_PointCoord-vec2(0.5);float f=length(v);float a=fwidth(f);f=1.0-smoothstep(0.5-a,0.5+a,f);if(f<0.01){discard;}outColor=vec4(u_color.xyz,v_alpha*f);}"),le.useProgram(me),le.enable(le.BLEND),le.blendFunc(le.ONE_MINUS_DST_ALPHA,le.ONE),["a_position","a_radius","a_alpha"].forEach(t=>pe[t]=le.getAttribLocation(me,t)),["u_matrix","u_color"].forEach(t=>ve[t]=le.getUniformLocation(me,t)),ue=le.createVertexArray(),le.bindVertexArray(ue),fe=le.createBuffer(),le.bindBuffer(le.ARRAY_BUFFER,fe),le.bufferData(le.ARRAY_BUFFER,Ce.byteLength,le.DYNAMIC_DRAW),Fe=Ce.byteLength,le.enableVertexAttribArray(pe.a_position),le.vertexAttribPointer(pe.a_position,2,le.FLOAT,!1,16,0),le.enableVertexAttribArray(pe.a_radius),le.vertexAttribPointer(pe.a_radius,1,le.FLOAT,!1,16,8),le.enableVertexAttribArray(pe.a_alpha),le.vertexAttribPointer(pe.a_alpha,1,le.FLOAT,!1,16,12),le.bindVertexArray(null),De=se(le,"#version 300 es\n#define GLSLIFY 1\nin vec2 a_corner;in vec2 a_pos;in float a_size;in float a_angle;in float a_alpha;uniform mat4 u_proj;out vec2 v_uv;out float v_alpha;void main(){float c=cos(a_angle);float s=sin(a_angle);vec2 rotated=vec2(c*a_corner.x-s*a_corner.y,s*a_corner.x+c*a_corner.y);gl_Position=u_proj*vec4(a_pos+rotated*a_size,0.0,1.0);v_uv=a_corner*0.5+0.5;v_alpha=a_alpha;}","#version 300 es\nprecision highp float;\n#define GLSLIFY 1\nin vec2 v_uv;in float v_alpha;uniform sampler2D u_tex;uniform vec4 u_color;out vec4 outColor;void main(){float inkAlpha=texture(u_tex,v_uv).a;if(inkAlpha<0.01)discard;outColor=vec4(u_color.rgb,inkAlpha*v_alpha);}"),["a_corner","a_pos","a_size","a_angle","a_alpha"].forEach(t=>ze[t]=le.getAttribLocation(De,t)),["u_proj","u_color","u_tex"].forEach(t=>Oe[t]=le.getUniformLocation(De,t)),Ne=le.createVertexArray(),le.bindVertexArray(Ne),Be=le.createBuffer(),le.bindBuffer(le.ARRAY_BUFFER,Be),le.bufferData(le.ARRAY_BUFFER,je,le.STATIC_DRAW),le.enableVertexAttribArray(ze.a_corner),le.vertexAttribPointer(ze.a_corner,2,le.FLOAT,!1,0,0),Ye=le.createBuffer(),le.bindBuffer(le.ARRAY_BUFFER,Ye),le.bufferData(le.ARRAY_BUFFER,Ue.byteLength,le.DYNAMIC_DRAW),Xe=Ue.byteLength,le.enableVertexAttribArray(ze.a_pos),le.vertexAttribPointer(ze.a_pos,2,le.FLOAT,!1,20,0),le.vertexAttribDivisor(ze.a_pos,1),le.enableVertexAttribArray(ze.a_size),le.vertexAttribPointer(ze.a_size,1,le.FLOAT,!1,20,8),le.vertexAttribDivisor(ze.a_size,1),le.enableVertexAttribArray(ze.a_angle),le.vertexAttribPointer(ze.a_angle,1,le.FLOAT,!1,20,12),le.vertexAttribDivisor(ze.a_angle,1),le.enableVertexAttribArray(ze.a_alpha),le.vertexAttribPointer(ze.a_alpha,1,le.FLOAT,!1,20,16),le.vertexAttribDivisor(ze.a_alpha,1),le.bindVertexArray(null),de=!0)}();const r=!0!==pt.isBrush;pt.isBrush=!0,r&&(pt.justChanged=!0),pt.blend(yt.stroke.color);const n=["default","spray"].includes(br.p.type)?br.p.opacity:br.p.opacity/Math.min(yt.stroke.weight,1.3),i=.1*(br.p.noise??0);br.alpha=i>0?Math.max(0,n*(1+S(0,i))):n,br.overscan=function(){const t=Math.max(1,br.max??1),e=yt.stroke.weight*br.p.scatter,r=yt.stroke.weight*br.p.weight*t;return Math.max(8,1.5*e+.75*r)}(),br.drawFn="spray"===br.p.type?Sr:"marker"===br.p.type?Pr:"custom"===br.p.type||"image"===br.p.type?Tr:Cr,kr()}();const r=br.p?.spacing??1,n=Math.round(yr*(e?t:1)/r);br.pressureCount=10,br.cachedPressure=void 0;const i=2*n;for(;_r.length<i;)_r.push(S());for(let i=0;i<n;i++)e&&(vr=xr.angle(gr.plotted)),Rr(),e?gr.plotTo(xr,r,r,t,vr):gr._moveToDegrees(t,r,r);!function(){kr(),function(){if(0===ke)return;pt.glMask.isDrawn=!0;const t=qe(),e=yt.stroke.color._array;le.useProgram(me),le.enable(le.BLEND),le.blendFunc(le.ONE_MINUS_DST_ALPHA,le.ONE),le.bindVertexArray(ue),le.bindBuffer(le.ARRAY_BUFFER,fe);const r=16*ke,n=Ce.subarray(0,4*ke);r>Fe?(le.bufferData(le.ARRAY_BUFFER,Ce,le.DYNAMIC_DRAW),Fe=Ce.byteLength):le.bufferSubData(le.ARRAY_BUFFER,0,n),le.uniform4f(ve.u_color,...e),le.uniformMatrix4fv(ve.u_matrix,!1,$e()),le.drawArrays(le.POINTS,0,ke),le.bindVertexArray(null),Ke(t),Le&&(pt.markDirtyRect(pt.glMask,Le),Le=null),ke=0,ce(q,le)}();const t=br.p?.type;"image"===t?Qe(Nr.tips.get(br.p.image.src),br.p.image.src):"custom"===t&&Qe(Nr.tips.get(br.p.tipKey),br.p.tipKey)}()}function Rr(){const t=Er();br.drawFn(t)}function Er(){return(br.pressureCount>=10||void 0===br.cachedPressure)&&(br.cachedPressure=xr?Mr()*xr.pressure(gr.plotted):Mr(),br.pressureCount=0),br.pressureCount++,br.cachedPressure}function Mr(){if(!br.isCustomPressure)return function(t=.5+br.p.pressure.curve[0]*br.a,e=1-br.p.pressure.curve[1]*br.b,r=br.cp,n=br.min,i=br.max){const o=t*yr,s=(gr.plotted<o?1.2*e:.8*e)*(yr/2);return T(1/(1+Math.pow(Math.abs((gr.plotted-o)/s),2*r)),0,1,n,i)}();const t=gr.plotted/yr;return T(br.p.pressure.curve(Math.max(0,Math.min(1,.5+(t-.5+br.ct)*br.cs)))+br.cp+br.ck*(t-.5),0,1,br.min,br.max,!0)}function Sr(t){const e=yt.stroke.weight*br.p.scatter*t+yt.stroke.weight*_(_r)*br.p.scatter/3,r=br.p.weight*b(.9,1.1),n=Math.ceil(br.p.grain/t);for(let t=0;t<n;t++){const t=b(.9,1.1),n=t*e*b(-1,1),i=b(-1,1),o=Math.sqrt((t*e)**2-n**2);Je(gr.x+n,gr.y+i*o,r,br.alpha)}}function Pr(t,e=!0,r=br.alpha){const n=e?yt.stroke.weight*br.p.scatter:0,i=e?n*b(-1,1):0,o=e?n*b(-1,1):0;Je(gr.x+i,gr.y+o,yt.stroke.weight*br.p.weight*t,r*Math.max(.8,t)*b(.9,1.1))}function Tr(t,e=br.alpha){const r=yt.stroke.weight*br.p.scatter,n=r*b(-1,1),i=r*b(-1,1),o=br.p.weight*yt.stroke.weight*t,s=br.overscan;let a=0;"random"===br.p.rotate?a=A(0,360)*(Math.PI/180):"natural"===br.p.rotate&&(a=((xr?-vr:-pr)+gr.angle())*(Math.PI/180)),function(t,e,r,n,i,o=0){if(Ge>=Ve){Ve*=2;const t=new Float32Array(5*Ve);t.set(Ue),Ue=t}const s=t-Me,a=e-Se,c=be*s+_e*a+Re+Me,l=we*s+Ae*a+Ee+Se,h=Te*r*Pe/2,u=Te*o*Pe,f=5*Ge;Ue[f]=c,Ue[f+1]=l,Ue[f+2]=h,Ue[f+3]=n,Ue[f+4]=i/255;const m=c*Te,d=l*Te,g=1.42*h+u;He=Ze(He,m-g-1,d-g-1,m+g+1,d+g+1),Ge++}(gr.x+n,gr.y+i,o,a,e*Math.max(.8,t)*b(.9,1.1),s)}function Cr(t){if(b(0,1)>=br.p.grain*t)return;const e=yt.stroke.weight*br.p.scatter*(br.p.sharpness+(1-br.p.sharpness)*_(_r)/t);let r,n;if(xr){const t=vr,i=N(t),o=B(t),s=e*b(-1,1),a=.3*e*b(-1,1);r=s*o+a*i,n=s*i-a*o}else{const t=e*b(-1,1),i=.3*e*b(-1,1);r=t*br.sin+i*br.cos,n=t*br.cos-i*br.sin}const i=t*t*br.p.weight*b(.85,1.15)*yt.stroke.weight,o=Math.max(.9,t)*br.alpha*b(.75,1.1);Je(gr.x+r,gr.y+n,i,o)}function kr(){if(!1===br.p.markerTip)return;let t=Er(),e=br.alpha;if("marker"===br.p.type)for(let r=1;r<10;r++)Pr(t*r/10,!0,8*e);else if("custom"===br.p.type||"image"===br.p.type)for(let r=1;r<5;r++)Tr(t*r/10,2*e)}function Ir(t,e,r,n){if(!yt.stroke.isActive||!yt.stroke.color)throw new Error("No brush or color set. Call brush.set('brushName', color) before drawing.");wt();let i=G(t,e,r,n);0!=i&&(wr(t,e,i),Ar(V(t,e,r,n),!1))}function Fr(t,e,r,n){if(!yt.stroke.isActive||!yt.stroke.color)throw new Error("No brush or color set. Call brush.set('brushName', color) before drawing.");wt(),wr(t,e,r),Ar(O(n),!1)}y(()=>{_r.length=0});const Lr=["weight","scatter","sharpness","grain","opacity","spacing","pressure","type","tip","rotate","markerTip","noise"],Dr=[["pen",[.3,.15,.9,.7,150,.1,{curve:[.15,.2],min_max:[1.2,1]}]],["rotring",[.15,.05,.7,.9,210,.1,{curve:[.35,.2],min_max:[1.3,1]}]],["2B",[.3,.75,.45,.8,180,.1,{curve:[.1,.3],min_max:[1.1,.9]}]],["HB",[.3,.6,.3,.7,170,.1,{curve:[.15,.2],min_max:[1.1,.9]}]],["2H",[.2,.6,.3,.75,120,.1,{curve:[.15,.2],min_max:[1.1,.9]}]],["cpencil",[.35,.55,.8,.7,75,.1,{curve:[.15,.2],min_max:[.95,1.1]}]],["pastel",[.7,5,.91,1,30,.085/3,{mode:"gaussian",curve:[.4,.05],min_max:[1.09,.93]},"default",void 0,"natural",!0,1]],["crayon",[.33,1.9,.75,2,159,.07,[1.1,.9],"default",void 0,"natural",!0,1]],["charcoal",[.35,1.5,.68,2,120,.03,{curve:[.15,.4],min_max:[1.1,.95]}]],["spray",[.2,6,15,40,90,.5,{curve:[.2,.35],min_max:[.7,1]},"spray"]],["marker",[2,.2,null,null,1,.03,{curve:[.35,.25],min_max:[1.2,.85]},"marker"]]];for(let t of Dr){let e={};for(let r=0;r<t[1].length;r++)e[Lr[r]]=t[1][r];ir(t[0],e)}zt.prototype.draw=function(t=!1,e,r){let n=rr();if(t&&ur(t,e,r),n.isActive)for(let t of this.sides)Ir(t[0].x,t[0].y,t[1].x,t[1].y);return nr(n),this},Ot.prototype.draw=function(t,e,r){return rr().isActive&&(this.origin&&(t=this.origin[0],e=this.origin[1],r=1),function(t,e,r,n){wt(),wr(e,r,t.length,t),Ar(n,!0)}(this,t,e,r)),this};const Nr={tips:new Map,add(t){this.tips.has(t)||this.tips.set(t,!1)},imageToWhite(t){t.loadPixels();for(let e=0;e<4*t.width*t.height;e+=4){let r=(t.pixels[e]+t.pixels[e+1]+t.pixels[e+2])/3;t.pixels[e]=t.pixels[e+1]=t.pixels[e+2]=255,t.pixels[e+3]=255-r}t.updatePixels()},async load(){const t=[...this.tips.keys()].filter(t=>!this.tips.get(t));await Promise.all(t.map(t=>((t,e)=>ie.loadImageTip(t,e))(t,Nr.imageToWhite).then(e=>{this.tips.set(t,e)})))}};function Br(){return{...yt.hatch}}function Yr(t){yt.hatch={...t}}function zr(t=5,e=45,r={rand:!1,continuous:!1,gradient:!1}){let n=yt.hatch;n.isActive=!0,n.dist=t,n.angle=X(e),n.options=r}function Or(t,e="black",r=1){yt.hatch.hBrush={brush:t,color:e,weight:r}}function Xr(){yt.hatch.isActive=!1,yt.hatch.hBrush=!1}yt.hatch={isActive:!1,dist:5,angle:45,options:{},hBrush:!1};let Ur=new Float64Array(256),Gr=new Float64Array(256),Vr=new Float64Array(512),Hr=new Float64Array(512),Wr=new Float64Array(512),jr=new Float64Array(512);function $r(t,e,r,n){const i=function(t,e,r,n){Array.isArray(t)||(t=[t]);const i=e*Math.PI/180,o=Math.cos(i),s=Math.sin(i);let a=0;for(const e of t)a+=e.a.length;if(0===a)return[];Ur.length<a&&(Ur=new Float64Array(2*a),Gr=new Float64Array(2*a));let c=Infinity,l=-Infinity,h=0,u=0;Vr.length<a&&(Vr=new Float64Array(2*a),Hr=new Float64Array(2*a),Wr=new Float64Array(2*a),jr=new Float64Array(2*a));for(const e of t){const t=e.a,r=t.length,n=u;for(let e=0;e<r;e++){const r=t[e][0],n=t[e][1];Ur[u]=r*o-n*s,Gr[u]=r*s+n*o,Gr[u]<c&&(c=Gr[u]),Gr[u]>l&&(l=Gr[u]),u++}for(let t=0;t<r;t++){const e=n+t,i=n+(t+1<r?t+1:0),o=Gr[e],s=Gr[i];o!==s&&(Vr[h]=Ur[e],Hr[h]=o,Wr[h]=Ur[i],jr[h]=s,h++)}}const f=[],m=[];let d=c+.5*r,g=r;const y=1!==n;for(;d<l;){m.length=0;for(let t=0;t<h;t++){const e=Hr[t],r=jr[t];e<=d!=r<=d&&m.push(Vr[t]+(d-e)/(r-e)*(Wr[t]-Vr[t]))}const t=m.length;if(2===t){let t=m[0],e=m[1];if(t>e){const r=t;t=e,e=r}f.push({scanY:d,x1:t*o+d*s,y1:-t*s+d*o,x2:e*o+d*s,y2:-e*s+d*o})}else if(t>2){m.sort((t,e)=>t-e);for(let e=0;e<t-1;e+=2){const t=m[e],r=m[e+1];f.push({scanY:d,x1:t*o+d*s,y1:-t*s+d*o,x2:r*o+d*s,y2:-r*s+d*o})}}d+=g,y&&(g*=n)}return f}(t,r,e,n);return i.sort((t,e)=>t.scanY===e.scanY?t.x1-e.x1:t.scanY-e.scanY),i}function qr(t){const{dist:e,options:r,segs:n}=function(t){const e=yt.hatch.dist,r=(yt.hatch.angle%180+180)%180,n=yt.hatch.options;return{dist:e,options:n,segs:$r(t,e,r,n.gradient?T(n.gradient,0,1,1,1.1,!0):1)}}(t),i=r.rand||0,o=[];for(let t=0;t<n.length;t++){const s=n[t];let a=s.x1,c=s.y1,l=s.x2,h=s.y2;i&&(a+=2*i*e*b(-1,1),c+=2*i*e*b(-1,1),l+=2*i*e*b(-1,1),h+=2*i*e*b(-1,1));const u=r.continuous&&t%2==1?{x1:l,y1:h,x2:a,y2:c,scanY:s.scanY,isConnector:!1}:{x1:a,y1:c,x2:l,y2:h,scanY:s.scanY,isConnector:!1};if(o.push(u),t>0&&r.continuous){const t=o[o.length-2];o.push({x1:t.x2,y1:t.y2,x2:u.x1,y2:u.y1,scanY:s.scanY,isConnector:!0})}}return o}function Kr(t){!function(t,e){const r=qr(t);!function(){const t=rr();(()=>{for(let t=0;t<r.length;t++){const n=r[t];e(n.x1,n.y1,n.x2,n.y2,t,r)}})(),nr(t)}()}(t,(t,e,r,n)=>{yt.hatch.hBrush&&ur(yt.hatch.hBrush.brush,yt.hatch.hBrush.color,yt.hatch.hBrush.weight*b(.9,1.1)),Ir(t,e,r,n)})}function Zr(t,e,r={}){yt.mass.brush=t,yt.mass.color=e,yt.mass.options=r,yt.mass.isActive=!0}function Jr(){yt.mass.isActive=!1,yt.mass.brush=null,yt.mass.color=null,yt.mass.options={}}function Qr(t,e,r){return new zt(t.vertices.map(t=>[t.x+e,t.y+r]))}function tn(t,e,r){return t.map(t=>Qr(t,e,r))}function en(t){const e=Array.isArray(t)?t.flatMap(t=>t?.a??[]):t?.a??[];let r=Infinity,n=Infinity,i=-Infinity,o=-Infinity;for(const[t,s]of e)t<r&&(r=t),s<n&&(n=s),t>i&&(i=t),s>o&&(o=s);return{minX:r,minY:n,maxX:i,maxY:o,cx:(r+i)/2,cy:(n+o)/2,size:Math.hypot(i-r,o-n)}}function rn(t,e,r,n,i){const o=(e+n)/2,s=(r+i)/2,a=n-e,c=i-r,l=Math.hypot(a,c);if(!l)return null;const h=-c/l,u=a/l,f=(t.x-o)*h+(t.y-s)*u;return{x:o+h*f,y:s+u*f}}function nn(t,e,r,n,i,o){if(!Array.isArray(t))return e;const s=(r+i)/2,a=(n+o)/2,c=Math.hypot(i-r,o-n),l=Math.max(en(t).size,c,1),h=.08+.18*(1-Math.min(1,c/(.42*l)));return{x:e.x+(s-e.x)*h,y:e.y+(a-e.y)*h}}function on(t,e,r){let n=!1;for(let i=0,o=t.length-1;i<t.length;o=i++){const s=t[i][0],a=t[i][1],c=t[o][0],l=t[o][1];a>r!=l>r&&e<(c-s)*(r-a)/(l-a||Number.EPSILON)+s&&(n=!n)}return n}function sn(t,e,r){if(Array.isArray(t)){let n=!1;for(const i of t)on(i.a,e,r)&&(n=!n);return n}return on(t.a,e,r)}function an(t,e,r,n,i,o,s){const a=o(n),c=(a+((o(i)-a)%360+360)%360*s)*Math.PI/180;return{x:t+r*Math.cos(c),y:e-r*Math.sin(c)}}function cn(t,e,r,n,i,o,s){for(const a of[.125,.25,.375,.5,.625,.75,.875]){const c=an(e,r,n,i,o,s,a);if(!sn(t,c.x,c.y))return!1}return!0}function ln(t,e,r,n,i,o,s,a,c,l){const h=function(t,e,r,n,i,o,s){let a=V(t,e,r,n),c=V(t,e,i,o);return((c-a)%360+360)%360>180&&([a,c]=[c,a]),[s(a),s(c)]}(e,r,i,o,s,a,c),u=[h[1],h[0]],f=cn(t,e,r,n,h[0],h[1],l),m=cn(t,e,r,n,u[0],u[1],l);return f&&!m?h:m&&!f?u:f?h:m?u:null}function hn(t){const e=w(.35,.65),r=t.x1+(t.x2-t.x1)*e,n=t.y1+(t.y2-t.y1)*e,i=t.x2-t.x1,o=t.y2-t.y1,s=Math.hypot(i,o)||1,a=w(.04,.1)*s,c=i/s*a*.5,l=o/s*a*.5;return[{x1:t.x1,y1:t.y1,x2:r-c,y2:n-l},{x1:r+c,y1:n+l,x2:t.x2,y2:t.y2}]}function un(t,e,r,n){const i=function(t,e){const r=en(t),n=r.size*w(.6,1.4),[i,o]=e;return{x:r.cx+i*n,y:r.cy+o*n}}(t,e);for(const e of qr(t)){const o=!e.isConnector&&w()<.35?hn(e):[e];for(const e of o){const o=rn(i,e.x1,e.y1,e.x2,e.y2),s=o?nn(t,o,e.x1,e.y1,e.x2,e.y2):null;if(!s)continue;const a=G(s.x,s.y,e.x1,e.y1);if(!a)continue;const c=ln(t,s.x,s.y,a,e.x1,e.y1,e.x2,e.y2,r,n);if(!c)continue;const[l,h]=c;Vt(s.x,s.y,a,l,h)}}}function fn(t,e,r,n,i,o,s){zr(e,r,Array.isArray(t)?{...n,continuous:!1}:n),un(t,i,o,s)}function mn(t,n,i,o){const s=function(t,e,r,n){const i=!1===e,o=sr(yt.mass.brush)?.scatter??0,s=i?t:t.genPol(e,r,n,.15),a=Math.min(2*o,5),c=[[w(-a,a),w(-a,a)],[w(-a,a),w(-a,a)]];return Array.isArray(s)?[s,tn(s,c[0][0],c[0][1]),tn(s,c[1][0],c[1][1])]:[s,Qr(s,c[0][0],c[0][1]),Qr(s,c[1][0],c[1][1])]}(t,n,i,o),a=Br(),c=rr(),l={...yt.field},h=yt.mass.options?.precision??.5,u=yt.mass.options?.strength??1,f=yt.mass.options?.gradient??.1,m=yt.mass.options?.outline??!1,d=sr(yt.mass.brush)?.scatter??0,g=1.6*w(.65*d,.85*d)-.4*f,y=w(-90,90),x=((y+90)%180+180)%180-90>=0?w()<.5?[1,1]:[-1,-1]:w()<.5?[-1,1]:[1,-1],p=e()?t=>t*Math.PI/180:t=>t,v=e()?t=>180*t/Math.PI:t=>t;ur(yt.mass.brush,yt.mass.color,1),Bt(2-h),m&&function(t){if(Array.isArray(t))for(const e of t)e.draw();else t.draw()}(s[0]),fn(s[0],.9*g,r(y),{gradient:f,rand:2-2*h,continuous:!0},x,p,v),u>.33&&fn(s[1],g,r(y+20*w(-1,1)),{gradient:f,rand:.6-.6*h,continuous:!0},x,p,v),u>.66&&fn(s[2],.8*g,r(y+15*w(-1,1)),{gradient:f,rand:.6-.6*h,continuous:!0},x,p,v),nr(c),Yr(a),yt.field={...l}}function dn(t){return mn(t,!1)}function gn(t,e=null){const r=pt.ctx;e||(e=r.getTransform());const n=e.a,i=e.b,o=e.c,s=e.d,a=e.e,c=e.f;let l=Infinity,h=Infinity,u=-Infinity,f=-Infinity;r.beginPath();const m=t[0];let d=n*m.x+o*m.y+a,g=i*m.x+s*m.y+c;l=u=d,h=f=g,r.moveTo(m.x,m.y);for(let e=1;e<t.length;e++){const m=t[e];d=n*m.x+o*m.y+a,g=i*m.x+s*m.y+c,l=Math.min(l,d),h=Math.min(h,g),u=Math.max(u,d),f=Math.max(f,g),r.lineTo(m.x,m.y)}r.closePath();const y=function(t,e){const r=t.lineWidth||0;return r<=0?1:1+r*Math.max(Math.hypot(e.a,e.c),Math.hypot(e.b,e.d))/2}(r,e);pt.markDirtyRect(pt.mask,{minX:l-y,minY:h-y,maxX:u+y,maxY:f+y})}function yn(t,e,r){const n=2*Math.PI,i=pt.ctx,o=r/2;i.moveTo(t+o,e),i.arc(t,e,o,0,n)}zt.prototype.hatch=function(t=!1,e,r){let n=Br();return t&&zr(t,e,r),yt.hatch.isActive&&Kr(this),Yr(n),this},Ot.prototype.hatch=function(t,e,r){Br().isActive&&(this.origin&&(t=this.origin[0],e=this.origin[1],r=1),this.pol=this.genPol(t,e,r,.3),this.pol.hatch())},yt.mass={isActive:!1,brush:null,color:null,options:{}},zt.prototype.mass=function(){return yt.mass?.isActive&&mn(this,!1),this},Ot.prototype.mass=function(t,e,r){return yt.mass?.isActive&&(this.origin&&(t=this.origin[0],e=this.origin[1],r=1),mn(this,t,e,r)),this};let xn=null,pn=!1;pn||(lt={ensureResources:function(t,e,r,n,i){const o=Math.max(1,Math.round(e*n)),s=Math.max(1,Math.round(r*n));return function(t,e,r){return!t.mask||t.mask.width!==e||t.mask.height!==r}(t,o,s)&&(t.mask=ot(o,s)),function(t,e,r,n){return!t.fillMaskFramebuffer||t.fillMaskFramebuffer.width!==e||t.fillMaskFramebuffer.height!==r||"function"==typeof t.fillMaskFramebuffer.pixelDensity&&t.fillMaskFramebuffer.pixelDensity()!==n}(t,e,r,n)&&(t.fillMaskFramebuffer?.remove&&t.fillMaskFramebuffer.remove(),t.fillMaskFramebuffer=at(t,{width:e,height:r,density:n,antialias:!1,depth:!1,stencil:!1}),i(t.fillMaskFramebuffer)),t.mask.dirtyRect??=null,t.mask.isDrawn??=!1,t.mask.drawingContext.imageSmoothingEnabled=!1,{mask:t.mask,ctx:t.mask.drawingContext}},clearMask:function(t,e){t&&(e(t),t.isDrawn=!1,t.dirtyRect=null)},getCompositeRect:function(t,e,r,n,i){return t?t.dirtyRect?i(n(t.dirtyRect,4)):r():null},getShaderMask:function(t,e,r,n,i){const o=t.fillMaskFramebuffer,s=t.drawingContext,a=r??n(),c=a.maxX-a.minX,l=a.maxY-a.minY;xn&&xn.width===c&&xn.height===l||(xn=ot(c,l));const h=xn.drawingContext;return h.clearRect(0,0,c,l),h.drawImage(e,a.minX,a.minY,c,l,0,0,c,l),i(o),s.bindTexture(s.TEXTURE_2D,o.colorTexture),s.texSubImage2D(s.TEXTURE_2D,0,a.minX,a.minY,s.RGBA,s.UNSIGNED_BYTE,xn),o}},pn=!0);let vn,bn=[],wn=[],_n=[],An=[];yt.fill={opacity:150,bleed_strength:.07,texture_strength:.8,border_strength:.5,direction:"out",angle:null,scatter:!0,isActive:!1};const Rn=()=>({...yt.fill});function En(t,e,r,i){yt.fill.opacity=(arguments.length<4?e:i)||150,yt.fill.color=arguments.length<3?n(t):n(t,e,r),yt.fill.isActive=!0}function Mn(t,e="out",r=null){yt.fill.bleed_strength=C(t,0,1),yt.fill.direction=e,yt.fill.angle=null==r?null:X(r)}function Sn(t=.4,e=.4,r=!0){yt.fill.texture_strength=C(t,0,1),yt.fill.border_strength=C(e,0,1),yt.fill.scatter=r}function Pn(){yt.fill.isActive=!1}let Tn,Cn,kn,In,Fn;const Ln=[[],[]];function Dn(){for(let t=0;t<512;t++)Ln[0][t]=S(.5,.2),Ln[1][t]=S(0,.02)}y(Dn);class Nn{constructor(t,e,r,n=[],i=!1,o,s){if(this.v=t,this.m=e,this.dir=n,this.midP=r,i){let e=0,n=0;const i=[];for(let o=0;o<t.length;o++){const s=Math.abs(r.x-t[o].x),a=Math.abs(r.y-t[o].y);e=Math.max(e,s),n=Math.max(n,a);const c=t[o],l=t[(o+1)%t.length],h={x:l.x-c.x,y:l.y-c.y},u=U(0,0,h.x,h.y,90),f={x:c.x+h.x/2,y:c.y+h.y/2};i.push({v1:c,v2:l,ray:{point1:f,point2:{x:f.x+u.x,y:f.y+u.y}}})}this.sizeX=e,this.sizeY=n;const o=Tn.sides;this.dir=Array(t.length);for(let t=0;t<i.length;t++){const e=i[t],r=e.ray.point1.x,n=e.ray.point1.y,s=e.ray.point2.x-r,a=e.ray.point2.y-n,c=e.v2.x-e.v1.x,l=e.v2.y-e.v1.y,h=-(c*c+l*l);let u=0;for(let t=0;t<o.length;t++){const e=o[t][0],i=o[t][1],c=i.x-e.x,l=i.y-e.y,f=l*s-c*a;if(0===f)continue;const m=(s*(n-e.y)-a*(r-e.x))/f;m<0||m>1||(c*(n-e.y)-l*(r-e.x))/f*h<=.01||u++}this.dir[t]=u%2==0}const s=b(-.6,.6)*e,a=b(-.6,.6)*n;this.midP={x:r.x+s,y:r.y+a}}else this.sizeX=o,this.sizeY=s}trim(t=1){if(t>=1||t<0||this.v.length<=8)return{v:this.v,m:this.m,dir:this.dir};const e=this.v.length,r=~~((1-t)*e),n=~~(e/2-r/2),i=n,o=n+r,s=this.v[(i-1+e)%e],a=this.v[o%e],c=a.x-s.x,l=a.y-s.y,h=Math.hypot(c,l),u=n>=2?~~b(0,n-1):o<e-1?o:0,f=this.v[u],m=this.v[(u+1)%e],d=Math.max(1,Math.hypot(m.x-f.x,m.y-f.y)),g=Math.max(2,Math.ceil(h/d*.05)),y=e-r+g,x=new Array(y),p=new Array(y),v=new Array(y);let w=0;for(let t=0;t<n;t++,w++)x[w]=this.v[t],p[w]=this.m[t],v[w]=this.dir[t];const _=.06*h,A=this.dir[i%this.dir.length];for(let t=0;t<g;t++,w++){const e=(t+1)/(g+1);x[w]={x:s.x+c*e+b(-_,_),y:s.y+l*e+b(-_,_)},p[w]=b(.3,.5),v[w]=A}for(let t=o;t<e;t++,w++)x[w]=this.v[t],p[w]=this.m[t],v[w]=this.dir[t];return{v:x,m:p,dir:v}}scatter(t=.3){const e=this.v.length,r=Math.max(3,~~(e*t)),n=e/r,i=.8*n,o=[],s=[],a=[],c=this.midP,l=Tn.sides;for(let t=0;t<r;t++){const r=~~(t*n+b(0,i))%e;let h=this.v[r],u=!1;if(h.x<Cn||h.x>In||h.y<kn||h.y>Fn)u=!0;else{let t=0;for(const[e,r]of l){const n=e.y,i=r.y;if(n>h.y==i>h.y)continue;const o=(h.y-n)/(i-n);h.x<e.x+o*(r.x-e.x)&&t++}u=t%2==0}u&&(h={x:c.x+(h.x-c.x)*b(.3,.6),y:c.y+(h.y-c.y)*b(.3,.6)}),o.push(h),s.push(this.m[r]),a.push(!this.dir[r])}return new Nn(o,s,this.midP,a,!1,this.sizeX,this.sizeY)}flipDirs(){return new Nn(this.v,this.m,this.midP,this.dir.map(t=>!t),!1,this.sizeX,this.sizeY)}grow(t=1){const{v:e,m:r,dir:n}=this.trim(t),i=e.length,o=2*i;bn.length<i&&(bn=new Array(i),wn=new Array(i)),_n.length<o&&(_n=new Array(o),An=new Array(o));const s=bn,a=wn,c=_n,l=An,h="out"===yt.fill.direction?-90:90;0===Ln[0].length&&Dn();const u=Ln[0],f=u.length,m=Ln[1],d=m.length;let g=0,y=0,x=999===t?b(.6,.8):yt.fill.bleed_strength;const p=vn&&2*i>vn?Math.ceil(2*i/vn):1;if(p>=2&&!(1&p)){for(let e=0;e<i;e++){const n=r[e];t<997&&(x=n),x>=.05&&(b(-1,1),u[~~(b(0,1)*f)],b(.65,1.35),m[~~(b(0,1)*d)])}return new Nn(e,r,this.midP,n,!1,this.sizeX,this.sizeY)}for(let o=0;o<i;o++){const p=e[o],v=e[o+1<i?o+1:0],w=r[o],_=n[o];if(t<997&&(x=w),x<.05){c[g]=w,l[g]=_,g++,s[y]=(p.x+v.x)/2,a[y]=(p.y+v.y)/2,c[g]=w,l[g]=_,g++,y++;continue}const A=(_?h:-h)+5*b(-1,1),R=z(A),E=R[0],M=R[1],S=v.x-p.x,P=v.y-p.y,T=E*S+M*P,C=E*P-M*S,k=u[~~(b(0,1)*f)]*b(.65,1.35)*x,I=w+m[~~(b(0,1)*d)];c[g]=w,l[g]=_,g++,s[y]=p.x+.5*S+T*k,a[y]=p.y+.5*P+C*k,c[g]=I,l[g]=_,g++,y++}let v,w,_;if(vn&&g>vn){const t=Math.ceil(g/vn),r=Math.ceil(g/t);v=new Array(r),w=new Array(r),_=new Array(r);let n=0;for(let r=0;r<g;r+=t,n++)v[n]=r%2==0?e[r>>1]:{x:s[r>>1],y:a[r>>1]},w[n]=c[r],_[n]=l[r]}else{v=new Array(g);for(let t=0;t<g;t++)v[t]=t%2==0?e[t>>1]:{x:s[t>>1],y:a[t>>1]};w=c.slice(0,g),_=l.slice(0,g)}return new Nn(v,w,this.midP,_,!1,this.sizeX,this.sizeY)}fill(t,e,r){const n=3*r,o=2*e*(1+r/2),s=!1!==pt.isBrush;pt.isBrush=!1,s&&(pt.justChanged=!0),pt.blend(t);const a=i();pt.ctx.save(),pt.ctx.setTransform(K*a.a,K*a.b,K*a.c,K*a.d,K*(a.x+W/2),K*(a.y+j/2)),pt.ctx.strokeStyle="rgb(255 0 0 / "+.01*yt.fill.border_strength+")",pt.ctx.lineCap="round",vn=2024*Math.max(.2,2*yt.fill.bleed_strength);const c=pt.ctx.getTransform(),l=Math.max(this.sizeX,this.sizeY),h=b(.15,.7);let u=this.grow();const f=this.scatter(.1).grow().scatter(.75).flipDirs();let m;for(let r=0;r<20;r++){r%4==0&&(u=u.grow()),r%2==0&&(m=[u.grow(1-.0125*r),u.grow(.7-.0125*r),u.grow(.4-.0125*r)]);for(const t of m)t.grow(999).grow(997).layer(r,l,o,c);yt.fill.scatter&&f.grow(999).flipDirs().grow(997).layer(r,l,o*n,c),r%2==0&&u.grow(h).grow(999).layer(r,l,2*o,c),r%8!=0&&19!==r||(0!==n&&u.erase(3*n,e),pt.blend(t,!0))}pt.ctx.restore()}layer(t,e,r,n=null){pt.ctx.lineWidth=T(t,0,24,e/25,e/30,!0)*yt.fill.border_strength,pt.ctx.fillStyle="rgb(255 0 0 / "+r+"%)",gn(this.v,n),pt.ctx.fill(),pt.ctx.stroke()}erase(t,e){pt.ctx.save();const r=~~(b(80,110)*T(t,0,1,2,3.5)),n=this.sizeX/1.3,i=this.sizeY/1.3,o=1.3*Math.min(this.sizeX,this.sizeY),s=.03*o,a=.45*o,{x:c,y:l}=this.midP;pt.ctx.globalCompositeOperation="destination-out";const h=(5-T(e,80,100,.3,.7,!0))*t/255;pt.ctx.fillStyle=`rgb(255 0 0 / ${h})`,pt.ctx.lineWidth=0;for(let t=0;t<r;t++){const e=c+S(0,n),r=l+S(0,i),o=b(s,a);pt.ctx.beginPath(),yn(e,r,o),t%5!=0&&pt.ctx.fill()}pt.ctx.globalCompositeOperation="source-over",pt.ctx.restore()}}function Bn(t,e,r,i){Q(),yt.wash.opacity=(arguments.length<4?e:i)??150,yt.wash.color=arguments.length<3?n(t):n(t,e,r),yt.wash.isActive=!0}function Yn(){yt.wash.isActive=!1}zt.prototype.fill=function(t=!1,e,r,n,i,o,s){let a=Rn();return t&&(En(t,e),Mn(r,o,s),Sn(n,i)),a.isActive&&(wt(),function(t){if(!yt.fill.isActive||!yt.fill.color)throw new Error("No fill color set. Call brush.fill(color) before drawing shapes.");Tn=t,Cn=Infinity,kn=Infinity,In=-Infinity,Fn=-Infinity;for(const[e]of t.sides)e.x<Cn&&(Cn=e.x),e.x>In&&(In=e.x),e.y<kn&&(kn=e.y),e.y>Fn&&(Fn=e.y);const e=[...t.vertices],r=b(0,75),n=~~(.25*e.length*(r<5?1:r<15?2:3)),i=yt.fill.bleed_strength,o=e.map((t,e)=>(e>n?1:.3)*b(.85,1.4)*i),s=null==yt.fill.angle?A(0,e.length):function(t,e){const r=z(e),n=r[0],i=-r[1];let o=0,s=Infinity;for(let e=0;e<t.length;e++){const r=t[e].x*n+t[e].y*i;r<s&&(s=r,o=e)}return o}(e,yt.fill.angle),a=e.length,c=new Array(a);for(let t=0;t<a;t++)c[t]=e[(t+s)%a];const l=function(t){const e=t.length;if(0===e)return{x:0,y:0};if(e<8){let r=0,n=0;for(let i=0;i<e;i++)r+=t[i].x,n+=t[i].y;return{x:r/e,y:n/e}}let r=0,n=0,i=0;for(let o=0;o<e;o++){const s=o+1<e?o+1:0,a=t[o].x,c=t[o].y,l=t[s].x,h=t[s].y,u=a*h-l*c;r+=u,n+=(a+l)*u,i+=(c+h)*u}return r*=.5,r?{x:n/(6*r),y:i/(6*r)}:{x:t[0].x,y:t[0].y}}(c);new Nn(c,o,l,[],!0).fill(yt.fill.color,T(yt.fill.opacity,0,255,0,1,!0),yt.fill.texture_strength)}(this)),(t=>{yt.fill={...t}})(a),this},Ot.prototype.fill=function(t,e,r){Rn().isActive&&(this.origin&&(t=this.origin[0],e=this.origin[1],r=1),this.pol=this.genPol(t,e,r,yt.fill.bleed_strength<.06?0:T(yt.fill.bleed_strength,0,.6,.2,.6,!0)),this.pol.fill())},yt.wash={color:null,opacity:150,isActive:!1},zt.prototype.wash=function(t=!1,e){const r={...yt.wash};return!1!==t&&Bn(t,e),yt.wash.isActive&&function(t){if(!yt.wash?.isActive||!yt.wash.color||t.vertices.length<3)return;const e=!1!==pt.isBrush;pt.isBrush=!1,e&&(pt.justChanged=!0),pt.blend(yt.wash.color);const r=i();pt.ctx.save(),pt.ctx.setTransform(K*r.a,K*r.b,K*r.c,K*r.d,K*(r.x+W/2),K*(r.y+j/2)),gn(t.vertices);const n=yt.wash.opacity/255;pt.ctx.fillStyle="rgb(255 0 0 / "+n+")",pt.ctx.fill(),pt.ctx.restore()}(this),yt.wash={...r},this},Ot.prototype.wash=function(t,e,r){yt.wash?.isActive&&(this.origin&&(t=this.origin[0],e=this.origin[1],r=1),this.pol=this.genPol(t,e,r,0,-1),this.pol.wash())};let zn=null,On=null;const Xn=[];let Un=!1;const Gn=t=>!!t&&"function"==typeof t.begin&&"function"==typeof t.end&&!!t.renderer&&!!t.renderer._pInst,Vn=t=>"number"==typeof t&&Number.isFinite(t)&&t>0,Hn=t=>{try{return t()}catch(t){return}},Wn=t=>!!t&&"webgl2"===Hn(()=>t.webglVersion)&&"function"==typeof t.pixelDensity&&"function"==typeof t.createFramebuffer&&Vn(Hn(()=>t.width))&&Vn(Hn(()=>t.height)),jn=t=>(Wn(t)&&(zn=t),zn),$n=()=>(On&&Wn(On)?jn(On):null)||jn(window.self?.p5?.instance??null)||jn($)||zn,qn=(t=!1)=>{const e=((t=!1)=>{const e=$n();if(!t){if(!e)throw new Error("p5.brush could not resolve an active WEBGL sketch. Call brush.instance(p) and brush.load() after createCanvas(..., WEBGL).");return{renderer:e,width:e.width,height:e.height}}if(Gn(t)){const r=t.renderer._pInst;if(jn(r),e&&r!==e)throw new Error("p5.brush only supports p5.Framebuffer targets created from the active sketch");return{renderer:r,width:t.width,height:t.height}}return{renderer:t,width:t.width,height:t.height}})(t);if(J({Renderer:e.renderer}),jn(t?$n():q),"webgl2"!==q.webglVersion)throw new Error("p5.brush requires a WEBGL canvas");J({Cwidth:e.width,Cheight:e.height}),Un=!0},Kn=t=>{J({Instance:t}),jn(t)},Zn=()=>q??$n?.()??window.self?.p5?.instance??null,Jn={a:1,b:0,c:0,d:1,x:0,y:0},Qn={a:1,b:0,c:0,d:1,x:0,y:0};function ti(){const t=Zn();return!!t&&"function"==typeof t.angleMode&&t.angleMode()===t.RADIANS}function ei(t,e,r){const n=e.isEnabled(e.DEPTH_TEST);n&&e.disable(e.DEPTH_TEST);const i=t._renderer.activeFramebuffer?.()??null;return i?._beforeEnd&&i._beforeEnd(),e.bindFramebuffer(e.FRAMEBUFFER,function(t){return t?"function"==typeof t._framebufferToBind?t._framebufferToBind():t.framebuffer??null:null}(r)),e.viewport(0,0,r.width*r.density,r.height*r.density),{hadDepthTest:n,previousTarget:i}}function ri(t,e,r){r?.hadDepthTest&&e.enable(e.DEPTH_TEST),r?.previousTarget?._beforeBegin?r.previousTarget._beforeBegin():(e.bindFramebuffer(e.FRAMEBUFFER,null),t._renderer.viewport(t._renderer._origViewport.width,t._renderer._origViewport.height),t._renderer._applyStencilTestIfClipping?.())}function ni(t,e){const r=t?._renderer;r&&(r._curShader=null,r._cachedBlendMode=null,r._isBlending=null,e.activeTexture(e.TEXTURE0),e.bindTexture(e.TEXTURE_2D,null),e.blendEquation(e.FUNC_ADD),e.blendFunc(e.ONE,e.ONE_MINUS_SRC_ALPHA))}function ii(t,e,r){if(!e)return;if(r(e))return void e.draw(()=>t.clear());const n=e.drawingContext;n.save(),n.setTransform(1,0,0,1,0,0),n.clearRect(0,0,e.width,e.height),n.restore()}function oi(t,e,r){return t.loaded||(t.shaderProgram??=t.createShader(e,r),t.loaded=!0),t.shaderProgram}function si(t,e,r,n,i){return e?.remove&&e.remove(),t.createFramebuffer({width:r,height:n,density:i,antialias:!1,depth:!1,stencil:!1})}const ai=(t,e)=>t.createFramebuffer(e);function ci({renderer:t,shader:e,source:r,mask:n,color:i,isBrushMask:o,Cwidth:s,Cheight:a,dirtyRect:c,targetIsFramebuffer:l,withScissor:h}){const u=t.drawingContext,f=u.isEnabled(u.DEPTH_TEST);t.push();try{u.disable(u.DEPTH_TEST),t.translate(0,0),t.shader(e),e.setUniform("u_source",r),e.setUniform("u_targetIsFramebuffer",l),e.setUniform("u_isBrush",o),e.setUniform("u_mask",n),e.setUniform("u_color",i),h(u,c,()=>{t.fill(0,0,0,0),t.noStroke(),t.rect(-s/2,-a/2,s,a)},!l)}finally{try{t.pop()}finally{t.resetShader(),f&&u.enable(u.DEPTH_TEST)}}}function li(t){return t.isFramebufferTarget(t.sourceTarget)?((t.sourceTarget.renderer??t.renderer)._renderer?.flushDraw?.(),t.sourceFramebuffer.draw(()=>{t.withScissor(t.renderer.drawingContext,t.dirtyRect,()=>{t.renderer.clear(),t.renderer.push();try{t.renderer.imageMode(t.renderer.CENTER),t.renderer.image(t.sourceTarget,0,0,t.Cwidth,t.Cheight)}finally{t.renderer.pop()}},!1)}),t.sourceFramebuffer):(t.renderer._renderer?.flushDraw?.(),function({renderer:t,sourceFramebuffer:e,dirtyRect:r,getTargetPixelSize:n,toScissorBox:i}){const o=t.drawingContext,{width:s,height:a}=n(),c=r?i(r):null,l=o.getParameter(o.READ_FRAMEBUFFER_BINDING),h=o.getParameter(o.DRAW_FRAMEBUFFER_BINDING);return o.bindFramebuffer(o.READ_FRAMEBUFFER,null),o.bindFramebuffer(o.DRAW_FRAMEBUFFER,e.framebuffer),o.blitFramebuffer(c?.x??0,c?.y??0,c?c.x+c.width:s,c?c.y+c.height:a,c?.x??0,c?.y??0,c?c.x+c.width:s,c?c.y+c.height:a,o.COLOR_BUFFER_BIT,o.NEAREST),o.bindFramebuffer(o.READ_FRAMEBUFFER,l),o.bindFramebuffer(o.DRAW_FRAMEBUFFER,h),e}(t))}const hi=()=>q??$??window.self?.p5?.instance;function ui(t,e){const r=hi();if(!r?.createGraphics)throw new Error("p5 stroke tip creation requires an active renderer or instance.");return r.createGraphics(t,e)}function fi(t,e){return new Promise((r,n)=>{const i=new window.Image;i.onload=()=>{const t=hi();if(!t?.createImage)return void n(new Error("p5 image tip loading requires an active renderer or instance."));const o=t.createImage(i.naturalWidth,i.naturalHeight);o.drawingContext.drawImage(i,0,0),e(o),r(o)},i.onerror=()=>n(new Error(`Failed to load image tip: ${t}`)),i.crossOrigin="anonymous",i.src=t})}const mi=new WeakMap;var di;di={load:qn,syncDensity:()=>{const t=q.pixelDensity();return J({Density:t}),t},isCanvasReady:()=>{Un||qn()},instance:Kn,activateInstance:t=>{Xn.push(On),On=t,Kn(t)},deactivateInstance:()=>{On=Xn.pop()??null},getActiveFramebuffer:()=>q?._renderer?.activeFramebuffer?.()??null,isFramebufferTarget:Gn},Z={...Z,...di},function(){var t;(t={usesRadians:ti,fromDegrees:t=>ti()?t*Math.PI/180:t,createColor:(...t)=>{const e=Zn();if(!e?.color)throw new Error("p5 runtime color creation requires an active renderer.");return e.color(...t)},getAffineMatrix:()=>{const t=Zn(),e=t?._renderer?.uModelMatrix?.mat4;return e?(Qn.a=e[0],Qn.b=e[1],Qn.c=e[4],Qn.d=e[5],Qn.x=e[12],Qn.y=e[13],Qn):Jn}}).usesRadians&&(e=t.usesRadians),t.fromDegrees&&(r=t.fromDegrees),t.createColor&&(n=t.createColor),t.getAffineMatrix&&(i=t.getAffineMatrix),t.notifyDraw&&(o=t.notifyDraw)}(),function(){var t;t={beginDirectMaskDraw:ei,endDirectMaskDraw:ri,resetDirectShaderTracking:ni},ae={...ae,...t}}(),function(){var t;t={clearTarget:ii,ensureBlendShaderProgram:oi,ensureBlendSourceFramebuffer:si,createFramebuffer:ai,runBlendShaderPass:ci,blitSourceToFramebuffer:li},st={...st,...t}}(),function(){var t;t={createTipSurface:ui,loadImageTip:fi},ie={...ie,...t}}(),"undefined"!=typeof p5&&p5.registerAddon(function(t,e,r){r.presetup=function(){et(this)},r.predraw=function(){et(this)},r.postsetup=r.postdraw=function(){try{q!==this&&q?._pInst!==this||(tt(this),(pt.isBlending||pt.justChanged)&&(pt.blend(!1,!0),pt.clearMask(pt.glMask),pt.clearMask(pt.mask),pt.justChanged=!1,pt.isBlending=!1,pt.isBrush=null,pt.cachedColor=null))}finally{Z.deactivateInstance()}};const n=e.push,i=e.pop,o=e.randomSeed,s=e.noiseSeed;e.push=function(){n.call(this),Wn(this)&&function(t=yt){let e=mi.get(t);e||(e=[],mi.set(t,e)),e.push({fill:{...yt.fill},wash:yt.wash?{...yt.wash}:null,stroke:{...yt.stroke},hatch:{...yt.hatch},mass:yt.mass?{...yt.mass}:null,field:{...yt.field}})}(this)},e.pop=function(){i.call(this),Wn(this)&&function(t=yt){const e=mi.get(t)?.pop();e&&(yt.stroke=e.stroke,yt.field=e.field,yt.hatch=e.hatch,yt.fill=e.fill,e.wash&&(yt.wash=e.wash),e.mass?yt.mass=e.mass:yt.mass&&(yt.mass={...yt.mass,isActive:!1,brush:null,color:null,options:{}}))}(this)},e.randomSeed=function(t){o.call(this,t),x(t)},e.noiseSeed=function(t){s.call(this,t),v(t)}});export{Ot as Plot,zt as Polygon,_t as Position,ir as add,Dt as addField,Vt as arc,Kt as beginShape,Qt as beginStroke,or as box,Gt as circle,mr as clip,Jt as endShape,ee as endStroke,Ft as field,En as fill,Mn as fillBleed,Sn as fillTexture,Fr as flowLine,zr as hatch,Kr as hatchArray,Or as hatchStyle,tt as instance,Ir as line,Nt as listFields,vt as load,Zr as mass,dn as massArray,te as move,dr as noClip,Lt as noField,Pn as noFill,Xr as noHatch,Jr as noMass,fr as noStroke,Yn as noWash,v as noiseSeed,cr as pick,Xt as polygon,Ut as rect,It as refreshField,ar as scaleBrushes,x as seed,ur as set,re as spline,lr as stroke,hr as strokeWeight,Zt as vertex,P as wRand,Bn as wash,Bt as wiggle};
//# sourceMappingURL=p5.brush.esm.js.map