Unlock custom brushes, natural fill effects and intuitive hatching in p5.js
var t,e;t=this,e=function(t){const e={a:1,b:0,c:0,d:1,x:0,y:0};let r,n,i,a,o=()=>!1,s=t=>t,l=()=>{throw new Error("No runtime color adapter registered.")},c=()=>e,h=()=>{};function u(t){t.usesRadians&&(o=t.usesRadians),t.fromDegrees&&(s=t.fromDegrees),t.createColor&&(l=t.createColor),t.getAffineMatrix&&(c=t.getAffineMatrix),t.notifyDraw&&(h=t.notifyDraw)}let f={load:()=>{throw new Error("No target runtime adapter registered.")},syncDensity:()=>a,isCanvasReady:()=>{throw new Error("No target runtime adapter registered.")},instance:t=>{},activateInstance:t=>{},deactivateInstance:()=>{},getActiveFramebuffer:()=>null,isFramebufferTarget:()=>!1};function d(t){"Cwidth"in t&&(r=t.Cwidth),"Cheight"in t&&(n=t.Cheight),"Instance"in t&&t.Instance,"Renderer"in t&&(i=t.Renderer),"Density"in t&&(a=t.Density)}const m=()=>f.isCanvasReady(),g=()=>f.getActiveFramebuffer(),x=t=>f.isFramebufferTarget(t),y=(t,e=0)=>t?{minX:t.minX-e,minY:t.minY-e,maxX:t.maxX+e,maxY:t.maxY+e}:null,p=(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 b={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 _=(...t)=>b.createFramebuffer(...t);let v=null,w=null;const R=t=>{((...t)=>{b.clearTarget(...t)})(i,t,x)},E=()=>({width:Math.max(1,Math.round(r*a)),height:Math.max(1,Math.round(n*a))}),A=t=>{if(!t)return null;const{width:e,height:r}=E();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))),a=Math.min(e,Math.ceil(Math.max(t.minX,t.maxX))),o=Math.min(r,Math.ceil(Math.max(t.minY,t.maxY)));return a<=n||o<=i?null:{minX:n,minY:i,maxX:a,maxY:o}})(t,e,r)},M=()=>{const{width:t,height:e}=E();return((t,e)=>({minX:0,minY:0,maxX:t,maxY:e}))(t,e)},S=(t,e=!0)=>{const r=A(t);if(!r)return null;const{height:n}=E();return{x:r.minX,y:e?n-r.maxY:r.minY,width:r.maxX-r.minX,height:r.maxY-r.minY}},T=(t,e,r,n=!0)=>{const i=S(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()},P={},C=()=>{i?.loaded||(m(),F.load())},F={isBlending:!1,cachedColor:null,markDirtyRect(t,e){const r=A(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?v:w;e?.clearMask?.(t,R)},getCompositeRect(t,e){const r=e?v:w;return r?.getCompositeRect?.(t,g,M,y,A)},load(){f.syncDensity();const t=!i.blendSourceFramebuffer||i.blendSourceFramebuffer.width!==r||i.blendSourceFramebuffer.height!==n||"function"==typeof i.blendSourceFramebuffer.pixelDensity&&i.blendSourceFramebuffer.pixelDensity()!==a;((...t)=>{b.ensureBlendShaderProgram(...t)})(i,"#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=v?.ensureResources?.(i,r,n,a);const e=w?.ensureResources?.(i,r,n,a,R)??{mask:null,ctx:null};t&&(i.blendSourceFramebuffer=((...t)=>b.ensureBlendSourceFramebuffer(...t))(i,i.blendSourceFramebuffer,r,n,a)),this.mask=e.mask,this.ctx=e.ctx},blend(t=!1,e=!1){C();const r=!0===this.isBrush,n=r?this.glMask:this.mask,i=r?this.mask:this.glMask,a=t?._array,o=!!a&&(this.cachedColor?.[0]!==a[0]||this.cachedColor?.[1]!==a[1]||this.cachedColor?.[2]!==a[2]||this.cachedColor?.[3]!==a[3]);!this.isBlending&&a&&(this.isBlending=!0,this.cachedColor=a,h(),this.clearMask(this.glMask)),(e||o)&&(this.justChanged&&(this.applyShader(i,!r),this.justChanged=!1),this.isBlending&&this.applyShader(n,r),a&&(this.cachedColor=a),e&&(this.isBlending=!1,this.cachedColor=null))},applyShader(t,e){if(!t?.isDrawn)return;const a=this.getCompositeRect(t,e);if(!a)return void this.clearMask(t);i.drawingContext;const o=i.shaderProgram,s=g(),l=((...t)=>b.blitSourceToFramebuffer(...t))({renderer:i,sourceTarget:s??i,sourceFramebuffer:i.blendSourceFramebuffer,dirtyRect:a,isFramebufferTarget:x,Cwidth:r,Cheight:n,getTargetPixelSize:E,toScissorBox:S,withScissor:T}),c=!!s,h=(e?v:w).getShaderMask(i,t,a,M,R);((...t)=>{b.runBlendShaderPass(...t)})({renderer:i,shader:o,source:l,mask:h,color:this.cachedColor,isBrushMask:e,Cwidth:r,Cheight:n,dirtyRect:a,targetIsFramebuffer:c,withScissor:T}),this.clearMask(t)}},k=new WeakMap,I=(t,e,r)=>Math.max(e,Math.min(t,r)),D="degrees",L="radians";let N=null,B=L;const U=[];let O={a:1,b:0,c:0,d:1,x:0,y:0};function X(t,e){return{a:t.a*e.a+t.c*e.b,b:t.b*e.a+t.d*e.b,c:t.a*e.c+t.c*e.d,d:t.b*e.c+t.d*e.d,x:t.a*e.x+t.c*e.y+t.x,y:t.b*e.x+t.d*e.y+t.y}}class Y{constructor(t,e,r){const n=t;let i=1;if(t?._array)return this.r=Math.round(255*t._array[0]),this.g=Math.round(255*t._array[1]),this.b=Math.round(255*t._array[2]),this.hex=this.rgbToHex(this.r,this.g,this.b),this._array=[...t._array],void(this.gl=this._array);if("string"==typeof t){const e=/^rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*([\d.]+))?\s*\)$/i.exec(t);if(e){this.r=I(parseInt(e[1]),0,255),this.g=I(parseInt(e[2]),0,255),this.b=I(parseInt(e[3]),0,255),this.hex=this.rgbToHex(this.r,this.g,this.b);const t=void 0!==e[4]?I(parseFloat(e[4]),0,1):1;return this._array=[this.r/255,this.g/255,this.b/255,t],void(this.gl=this._array)}this.hex=this.standardize(t);const r=this.hexToRgb(this.hex);this.r=r.r,this.g=r.g,this.b=r.b}else Array.isArray(t)&&([t,e,r,i=1]=t),this.r=I(t??0,0,255),this.g=I(e??t??0,0,255),this.b=I(r??t??0,0,255),this.hex=this.rgbToHex(this.r,this.g,this.b);if(!Number.isFinite(this.r)||!Number.isFinite(this.g)||!Number.isFinite(this.b))throw new Error(`Invalid color value "${n}".`);this._array=[this.r/255,this.g/255,this.b/255,I(i,0,1)],this.gl=this._array}rgbToHex(t,e,r){return`#${(1<<24|t<<16|e<<8|r).toString(16).slice(1)}`}hexToRgb(t){t=t.replace(/^#?([a-f\d])([a-f\d])([a-f\d])$/i,(t,e,r,n)=>e+e+r+r+n+n);const e=/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(t);if(!e)throw new Error(`Invalid color value "${t}".`);return{r:parseInt(e[1],16),g:parseInt(e[2],16),b:parseInt(e[3],16)}}standardize(t){const e=function(){if(N)return N;if("undefined"!=typeof document)return N=document.createElement("canvas").getContext("2d"),N;if("undefined"!=typeof OffscreenCanvas)return N=new OffscreenCanvas(1,1).getContext("2d"),N;throw new Error("Standalone color parsing requires CanvasRenderingContext2D support.")}();e.fillStyle="#000000",e.fillStyle=t;const r=e.fillStyle;if(e.fillStyle="#ffffff",e.fillStyle=t,r!==e.fillStyle)throw new Error(`Invalid color value "${t}".`);return r}_getRed(){return this.r}_getGreen(){return this.g}_getBlue(){return this.b}}function z(){u({usesRadians:()=>"radians"===B,fromDegrees:t=>"radians"===B?t*Math.PI/180:t,createColor:(...t)=>1===t.length&&t[0]?._array?t[0]:new Y(...t),getAffineMatrix:()=>O})}let G=!1,V=!1;u({notifyDraw:function(){V||(V=!0,G=!0,requestAnimationFrame(()=>{V=!1,G&&console.warn("[p5.brush] Drawing calls were made but brush.render() was never called. Call brush.render() after your drawing code to flush to the canvas.")}))}});let H=null,W=!1,$=1,q=0,j=0;function K(t,e,r,n){H=t,q=e,j=r,$=n;const i=function(t,e,r,n){const i=function(t){return t.getContext("webgl2",{premultipliedAlpha:!0,preserveDrawingBuffer:!0})??t.getContext("webgl2",{preserveDrawingBuffer:!0})}(t);if(!i)throw new Error("brush.load(target) requires a canvas with a WebGL2 context.");return{canvas:t,drawingContext:i,width:e,height:r,pixelDensity:()=>n}}(t,e,r,n);d({Renderer:i,Cwidth:e,Cheight:r,Density:n}),W=!0}const Z=Math.sqrt(3),J=.5*(Z-1),Q=(3-Z)/6,tt=t=>0|Math.floor(t),et=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 rt(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=>et[t%12*2]),n=new Float64Array(e).map(t=>et[t%12*2+1]);return function(t,i){let a=0,o=0,s=0;const l=(t+i)*J,c=tt(t+l),h=tt(i+l),u=(c+h)*Q,f=t-(c-u),d=i-(h-u);let m,g;f>d?(m=1,g=0):(m=0,g=1);const x=f-m+Q,y=d-g+Q,p=f-1+2*Q,b=d-1+2*Q,_=255&c,v=255&h;let w=.5-f*f-d*d;if(w>=0){const t=_+e[v];w*=w,a=w*w*(r[t]*f+n[t]*d)}let R=.5-x*x-y*y;if(R>=0){const t=_+m+e[v+g];R*=R,o=R*R*(r[t]*x+n[t]*y)}let E=.5-p*p-b*b;if(E>=0){const t=_+1+e[v+1];E*=E,s=E*E*(r[t]*p+n[t]*b)}return 70*(a+o+s)}}function nt(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 it=nt(Math.random()),at=nt(Math.random()+":2");const ot=[],st=t=>ot.push(t);t.noise=rt(nt(Math.random()));let lt=rt(nt(Math.random()+":2"));const ct=(t=0,e=1)=>t+it()*(e-t),ht=(t=0,e=1)=>t+at()*(e-t),ut=t=>t[~~(it()*t.length)],ft=(t,e)=>~~ct(t,e),dt=(t,e)=>~~ht(t,e);let mt=!1,gt=0;const xt=(t=0,e=1)=>{if(mt)return mt=!1,gt*e+t;const r=1-it(),n=it(),i=Math.sqrt(-2*Math.log(r)),a=360*n;return gt=i*At(a),mt=!0,i*Et(a)*e+t},yt=(t,e,r,n,i,a=!1)=>{let o=n+(t-e)/(r-e)*(i-n);return a?n<i?pt(o,n,i):pt(o,i,n):o},pt=(t,e,r)=>Math.max(Math.min(t,r),e),bt=1440,_t=2*Math.PI/bt,vt=new Float32Array(bt),wt=new Float32Array(bt);for(let t=0;t<bt;t++)vt[t]=Math.cos(t*_t),wt[t]=Math.sin(t*_t);const Rt=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)),Et=t=>vt[Rt(t)],At=t=>wt[Rt(t)],Mt=new Float32Array(2),St=t=>{const e=Rt(t);return Mt[0]=vt[e],Mt[1]=wt[e],Mt},Tt=(t,e=!1)=>{if(e||o()){let e=180*t/Math.PI%360;return e<0?e+360:e}return t},Pt=(t,e=!1)=>e||o()?180*t/Math.PI:t,Ct=(t,e,r,n,i)=>{const a=St(i),o=a[0],s=a[1];return{x:o*(r-t)+s*(n-e)+t,y:o*(n-e)-s*(r-t)+e}},Ft=(t,e,r,n)=>Math.hypot(r-t,n-e),kt=(t,e,r,n)=>Tt(Math.atan2(-(n-e),r-t),!0),It=(t,e,r,n,i=!1)=>{const a=t.x,o=t.y,s=e.x,l=e.y,c=r.x,h=r.y,u=s-a,f=l-o,d=n.x-c,m=n.y-h,g=m*u-d*f;if(0===g)return!1;const x=o-h,y=a-c,p=(d*x-m*y)/g,b=(u*x-f*y)/g;return!(!i&&(b<0||b>1))&&{x:a+p*u,y:o+p*f}};let Dt=!1;function Lt(){Dt||(C(),Bt=.01*r,Ut=-.5*r,Ot=-.5*n,Xt=Math.round(2*r/Bt),Yt=Math.round(2*n/Bt),$t("hand",(t,e)=>{const r=ht(.2,.8),n=dt(5,10);return jt(e,(e,i)=>.5*n*At(r*i*e+dt(15,25))*.2*Et(t)+lt(e,i)*n*.7)}),$t("curved",(t,e)=>{let r=dt(-10,10);return dt(0,100)%2==0&&(r*=-1),jt(e,(e,n)=>3*yt(lt(.02*e+.03*t,.02*n+.03*t),0,1,-r,r))}),$t("zigzag",(t,e)=>{let r=dt(-30,-15)+Math.abs(44*At(t));dt(0,100)%2==0&&(r*=-1);let n=r,i=0;for(let t=0;t<Xt;t++){for(let r=0;r<Yt;r++)e[t][r]=i,i+=n,n*=-1;i+=n,n*=-1}return e}),$t("waves",(t,e)=>{const r=dt(10,15)+5*At(t),n=dt(3,6)+3*Et(t),i=dt(20,35);return jt(e,(t,e)=>At(r*t)*i*Et(e*n)+dt(-3,3))}),$t("seabed",(t,e)=>{const r=ht(.4,.8),n=dt(18,26);return jt(e,(e,i)=>1.1*n*At(r*i*e+dt(15,20))*Et(t))}),$t("spiral",(t,e)=>{const r=dt(5,10),n=2*dt(0,2)-1,i=dt(65,80),a=Array.from({length:r},()=>({x:ht(.1,.9)*Xt,y:ht(.1,.9)*Yt}));return jt(e,(t,e)=>{let r=0,o=0;for(const s of a){const a=t-s.x,l=e-s.y,c=1/(a*a+l*l+1),h=Math.atan2(l,a)*(180/Math.PI),u=n*(h+i)*Math.PI/180;r+=c*Math.cos(u),o+=c*Math.sin(u)}return Math.atan2(o,r)*(180/Math.PI)})}),$t("columns",(t,e)=>{const r=dt(3,8),n=dt(25,45);return jt(e,(t,e)=>At(t*r)*n)}),Dt=!0)}class Nt{constructor(t,e){Lt();const r=c();this.mx=r.x,this.my=r.y,this.update(t,e),this.plotted=0}update(t,e){this.x=t,this.y=e,P.field.isActive&&(this.colIdx=Math.round((t+this.mx-Ut)/Bt),this.rowIdx=Math.round((e+this.my-Ot)/Bt))}reset(){this.plotted=0}isIn(){return P.field.isActive?Nt.isIn(this.colIdx,this.rowIdx):this.isInCanvas(this.x,this.y)}isInCanvas(){const t=r,e=n,i=this.x+this.mx,a=this.y+this.my;return i>=-.5*t&&i<=1.5*t&&a>=-.5*e&&a<=1.5*e}angle(t=!1){return P.field.isActive&&(t||this.isIn())?zt.get(P.field.current).field[this.colIdx][this.rowIdx]*P.field.wiggle:0}moveTo(t,e,r=1){const n=Pt(t);P.field.isActive?this.movePos(n,e,r):this._moveConstant(n,e,r)}_moveToDegrees(t,e,r=1){P.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=St(-t),a=r*i[0],o=r*i[1];for(let t=0;t<n;t++)this.x+=a,this.y+=o,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 a=n||1;if(!this.isIn())return void(this.plotted+=r/a);const o=e/r,s=P.field.isActive,l=!!n;for(let e=0;e<o;e++){const n=l&&void 0!==i&&0===e?i:l?t.angle(this.plotted):t,o=(s?this.angle(!0):0)-n,c=St(o);this.update(this.x+r*c[0],this.y+r*c[1]),this.plotted+=r/a}}static getRowIndex(t,e=1){const r=t+c().y-Ot;return Math.round(r/Bt/e)}static getColIndex(t,e=1){const r=t+c().x-Ut;return Math.round(r/Bt/e)}static isIn(t,e){return t>=0&&e>=0&&t<Xt&&e<Yt}}P.field={isActive:!1,current:null,wiggle:1};let Bt,Ut,Ot,Xt,Yt,zt=new Map;const Gt=new Set(["degrees","radians"]);function Vt(t={}){const e=("string"==typeof t?{angleMode:t}:t||{}).angleMode??"degrees";if(!Gt.has(e))throw new Error(`Invalid field angle mode "${e}". Use "degrees" or "radians".`);return e}function Ht(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]=Pt(t[e][r],!0);return t}(t.gen(e,new Array(Xt).fill(null).map(()=>new Float32Array(Yt))),t.angleMode)}function Wt(t){if(P.field.wiggle||(P.field.wiggle=1),Lt(),!zt.has(t))throw new Error(`Field "${t}" does not exist. Available fields: ${Array.from(zt.keys()).join(", ")}.`);P.field.isActive=!0,P.field.current=t;const e=zt.get(t);e.field||(e.field=Ht(e,0))}function $t(t,e,r={}){zt.set(t,{gen:e,field:null,angleMode:Vt(r)})}function qt(t=1){Wt("hand"),P.field.wiggle=t}function jt(t,e){for(let r=0;r<Xt;r++)for(let n=0;n<Yt;n++)t[r][n]=e(r,n);return t}class Kt{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=It(t.point1,t.point2,n[e][0],n[e][1]);i&&r.push(i)}return this._intersectionCache[e]=r,r}show(){P.wash&&this.wash(),P.fill&&this.fill(),P.mass&&this.mass(),P.hatch&&this.hatch(),P.stroke&&this.draw()}}class Zt{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:Tt(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:Tt(t),this.angles[this.angles.length-1]=t,this.pres.push(e)}rotate(t){this.dir=Tt(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,i=1,a){Lt();const o=a<0?4:1,s=[],l=Math.round(this.length/o),c=new Nt(t+r/2,e+n/2);let h=0,u=0;for(let t=0;t<l;t++){c.plotTo(this,o,o);const t=this.index;h+=o,(h>=(a<=0?8:Math.max(this.segments[t]*a*ht(.7,1.3),20))||t>=u)&&c.x&&(s.push([c.x-r/2,c.y-n/2]),h=0,t>=u&&u++)}return new Kt(s)}show(t,e,r=1){P.wash&&this.wash(t,e,r),P.fill&&this.fill(t,e,r),P.mass&&this.mass(t,e,r),P.hatch&&this.hatch(t,e,r),P.stroke&&this.draw(t,e,r)}}function Jt(t,e,r,n,i){const a=Pt(n),o=((Pt(i)-a)%360+360)%360;if(0===o)return null;const s=new Zt("curve"),l=Math.max(1,Math.ceil(o/90)),c=o/l,h=Math.PI*r*c/180;for(let t=0;t<l;t++)s.addSegment(a+t*c+90,h,1,!0);s.endPlot(a+o+90,1,!0);const u=t+r*Et(a),f=e-r*At(a);return s.draw(u,f,1),s}let Qt,te,ee,re;class ne{constructor(){this.isClosed=!1,this.curvature=te,this.vert=[]}vertex(t,e,r){this.vert.push([t,e,r])}show(){let t=se(this.vert,this.curvature,this.isClosed);return t.show(),t}}function ie(t=0){te=pt(t,0,1),Qt=new ne}function ae(t,e,r=1){if(!Qt)throw new Error("vertex() called outside of beginShape()/endShape(). Call beginShape() first.");Qt.vertex(t,e,r)}function oe(t=!1){if(!Qt)throw new Error("endShape() called without beginShape(). Call beginShape() first.");if(Qt.vert.length<2)throw new Error("endShape() requires at least 2 vertices. Add more with vertex().");t&&(Qt.vertex(...Qt.vert[0]),Qt.isClosed=!0);const e=Qt.show();return Qt=!1,e}function se(t,e=.5,r=!1){const n=new Zt(0===e?"segments":"curve"),i=2*Math.PI;if(r&&0!==e&&t.push(t[1]),t&&t.length>0){let a,o,s,l=0;for(let c=0;c<t.length-1;c++)if(e>0&&c<t.length-2){const h=t[c],u=t[c+1],f=t[c+2],d=Ft(h[0],h[1],u[0],u[1]),m=Ft(u[0],u[1],f[0],f[1]),g=kt(h[0],h[1],u[0],u[1]),x=kt(u[0],u[1],f[0],f[1]),y=e*Math.min(d,m,.5*Math.min(d,m)),p=Math.max(d,m),b=d-y,_=m-y;if(Math.floor(g)===Math.floor(x)){const e=r&&0===c?0:d-l,i=r?0===c?0:m-s:m;n.addSegment(g,e,h[2],!0),c===t.length-3&&n.addSegment(x,i,u[2],!0),l=0,0===c&&(a=d,s=y,o=t[1],l=0)}else{const e={x:u[0]-y*Et(-g),y:u[1]-y*At(-g)},f={x:e.x+p*Et(90-g),y:e.y+p*At(90-g)},d={x:u[0]+y*Et(-x),y:u[1]+y*At(-x)},m={x:d.x+p*Et(90-x),y:d.y+p*At(90-x)},v=It(e,f,d,m,!0),w=Ft(e.x,e.y,v.x,v.y),R=Ft(e.x,e.y,d.x,d.y)/2,E=i*w*(2*Math.asin(R/w)*(180/Math.PI))/360,A=r&&0===c?0:b-l,M=c===t.length-3?r?a-y:_:0;n.addSegment(g,A,h[2],!0),n.addSegment(g,isNaN(E)?0:E,h[2],!0),n.addSegment(x,M,u[2],!0),l=y,0===c&&(a=b,s=y,o=[e.x,e.y])}c===t.length-3&&n.endPlot(x,u[2],!0)}else if(0===e){const e=t[c],r=t[c+1],i=Ft(e[0],e[1],r[0],r[1]),a=kt(e[0],e[1],r[0],r[1]);n.addSegment(a,i,e[2],!0),c===t.length-2&&n.endPlot(a,r[2],!0)}n.origin=r&&0!==e?o:t[0]}return n}let le={createTipSurface:()=>{throw new Error("No stroke runtime adapter registered.")},loadImageTip:()=>{throw new Error("No stroke runtime adapter registered.")}},ce=!1;const he=(t,e,r)=>{const n=t.createProgram();for(let[i,a]of[[t.VERTEX_SHADER,e],[t.FRAGMENT_SHADER,r]]){const e=t.createShader(i);t.shaderSource(e,a),t.compileShader(e),t.attachShader(n,e)}return t.linkProgram(n),n};let ue={beginDirectMaskDraw:()=>{throw new Error("No renderer runtime adapter registered.")},endDirectMaskDraw:()=>{throw new Error("No renderer runtime adapter registered.")},resetDirectShaderTracking:()=>{}};const fe=(t,e)=>ue.resetDirectShaderTracking(t,e);let de,me,ge,xe,ye,pe=!1,be=0,_e=0,ve=null;const we={},Re={};let Ee=1,Ae=0,Me=0,Se=1,Te=0,Pe=0,Ce=0,Fe=0,ke=1,Ie=1,De=new Float32Array(8192),Le=0,Ne=2048,Be=32768,Ue=null,Oe=null,Xe=null,Ye=null,ze=null;const Ge={},Ve={};let He=0,We=new Float32Array(1280),$e=0,qe=256,je=null;const Ke=new Map,Ze=new Float32Array([-1,-1,1,-1,-1,1,1,1]);function Je(){return me}function Qe(){return((t,e,r)=>ue.beginDirectMaskDraw(t,e,r))(i,de,F.glMask)}function tr(t){((t,e,r)=>{ue.endDirectMaskDraw(t,e,r)})(i,de,t)}function er(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 rr(t,e,r,n){if(Le>=Ne){Ne*=2;const t=new Float32Array(4*Ne);t.set(De),De=t}const i=t-Ce,a=e-Fe,o=4*Le,s=Ee*i+Me*a+Te+Ce,l=Ae*i+Se*a+Pe+Fe,c=Ie*r*ke/2;De[o]=s,De[o+1]=l,De[o+2]=c,De[o+3]=n/255;const h=s*Ie,u=l*Ie;Ue=er(Ue,h-c-1,u-c-1,h+c+1,u+c+1),Le++}function nr(t,e){if(0===$e)return;F.glMask.isDrawn=!0;const r=Qe();let n=Ke.get(e);n||(n=de.createTexture(),de.bindTexture(de.TEXTURE_2D,n),de.texImage2D(de.TEXTURE_2D,0,de.RGBA,de.RGBA,de.UNSIGNED_BYTE,t.canvas),de.texParameteri(de.TEXTURE_2D,de.TEXTURE_MIN_FILTER,de.LINEAR),de.texParameteri(de.TEXTURE_2D,de.TEXTURE_MAG_FILTER,de.LINEAR),de.texParameteri(de.TEXTURE_2D,de.TEXTURE_WRAP_S,de.CLAMP_TO_EDGE),de.texParameteri(de.TEXTURE_2D,de.TEXTURE_WRAP_T,de.CLAMP_TO_EDGE),Ke.set(e,n)),de.useProgram(Oe),de.enable(de.BLEND),de.blendFunc(de.ONE_MINUS_DST_ALPHA,de.ONE),de.activeTexture(de.TEXTURE0),de.bindTexture(de.TEXTURE_2D,n),de.uniform1i(Ve.u_tex,0);const a=P.stroke.color._array;de.uniform4f(Ve.u_color,...a),de.uniformMatrix4fv(Ve.u_proj,!1,Je()),de.bindVertexArray(Xe),de.bindBuffer(de.ARRAY_BUFFER,ze);const o=5*$e*4,s=We.subarray(0,5*$e);o>He?(de.bufferData(de.ARRAY_BUFFER,We,de.DYNAMIC_DRAW),He=We.byteLength):de.bufferSubData(de.ARRAY_BUFFER,0,s),de.drawArraysInstanced(de.TRIANGLE_STRIP,0,4,$e),de.bindVertexArray(null),de.bindTexture(de.TEXTURE_2D,null),tr(r),je&&(F.markDirtyRect(F.glMask,je),je=null),$e=0,fe(i,de)}ce||(v={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=_(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}},ce=!0),P.stroke={color:null,weight:1,type:"HB",isActive:!1,opacity:1};let ir=new Map;const ar={offset:.08,scale:.08,warp:.06,tilt:.06};function or(){return{...P.stroke}}function sr(t){P.stroke={...t}}function lr(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:{...ar}};if("object"==typeof t&&!Array.isArray(t)){if("custom"===t.type||"custom"===t.mode){const{mode:e,...r}=t;return{...r,type:"custom",variation:{...ar,...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),a=Math.max(e,r,n),o=a-i||1,[s,l,c]=[(e-i)/o,(r-i)/o,(n-i)/o];return{type:"custom",min_max:[i,a],variation:{...ar},curve:t=>t<.5?s+(l-s)*t*2:l+(c-l)*(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=Ke.get(t);e&&de&&de.deleteTexture(e),Ke.delete(t)}(r);const n=le.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(),Dr.imageToWhite(n),Dr.tips.set(r,n),e.tipKey=r,void ir.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 Dr.add(e.image.src),ir.set(t,{param:e,colors:[],buffers:[]}),Dr.load()}ir.set(t,{param:e,colors:[],buffers:[]})}function cr(t){return ir.get(t)?.param??null}function hr(t){if(!ir.has(t))throw new Error(`Brush "${t}" not found. Available brushes: ${[...ir.keys()].join(", ")}.`);P.stroke.type=t}function ur(t,e,r){m(),P.stroke.color=l(...arguments),P.stroke.isActive=!0}function fr(t){P.stroke.weight=t}function dr(t,e,r=1){hr(t),ur(e),fr(r)}let mr,gr,xr,yr,pr=0;const br={};function _r(t,e,i,o=!1){!function(){const t=c();Ee=t.a,Ae=t.b,Me=t.c,Se=t.d,Te=t.x,Pe=t.y,Ce=r/2,Fe=n/2,ke=Math.sqrt(Ee*Ee+Ae*Ae),Ie=a}(),mr=new Nt(t+r/2,e+n/2),gr=i,xr=o,xr&&xr.calcIndex(0)}const vr=[];function wr(t,e){e||(yr=t),function(){br.seed=999999*ct();const{param:t}=ir.get(P.stroke.type)??{};if(!t)return;br.p=t;const{pressure:e}=t;if(br.isCustomPressure="custom"===e.type,br.a=br.isCustomPressure?0:ct(-1,1),br.b=br.isCustomPressure?0:ct(1,1.5),br.isCustomPressure){const t=e.variation??ar;br.cp=ct(-t.offset,t.offset),br.ct=ct(-t.warp,t.warp),br.cs=ct(1-t.scale,1+t.scale),br.ck=ct(-t.tilt,t.tilt)}else br.cp=ct(3,3.5),br.ct=0,br.cs=1,br.ck=0;[br.min,br.max]=e.min_max,xr||(br.cos=Et(yr),br.sin=At(yr)),function(){C(),function(){const t=i?.glMask;t&&(ve=t,ve.dirtyRect??=null,ve.isDrawn??=!1,F.glMask=ve)}();const t=i.drawingContext,e=!pe||de!==t;(e||be!==r||_e!==n)&&(me=new Float32Array([2/r,0,0,0,0,2/n,0,0,0,0,1,0,-1,-1,0,1]),be=r,_e=n),e&&(function(){if(de&&!de.isContextLost?.()){for(const t of Ke.values())de.deleteTexture(t);Ke.clear(),ye&&de.deleteProgram(ye),ge&&de.deleteVertexArray(ge),xe&&de.deleteBuffer(xe),Oe&&de.deleteProgram(Oe),Xe&&de.deleteVertexArray(Xe),Ye&&de.deleteBuffer(Ye),ze&&de.deleteBuffer(ze),ye=ge=xe=null,Oe=Xe=Ye=ze=null,Be=0,He=0}else Ke.clear()}(),de=t,ye=he(de,"#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);}"),de.useProgram(ye),de.enable(de.BLEND),de.blendFunc(de.ONE_MINUS_DST_ALPHA,de.ONE),["a_position","a_radius","a_alpha"].forEach(t=>we[t]=de.getAttribLocation(ye,t)),["u_matrix","u_color"].forEach(t=>Re[t]=de.getUniformLocation(ye,t)),ge=de.createVertexArray(),de.bindVertexArray(ge),xe=de.createBuffer(),de.bindBuffer(de.ARRAY_BUFFER,xe),de.bufferData(de.ARRAY_BUFFER,De.byteLength,de.DYNAMIC_DRAW),Be=De.byteLength,de.enableVertexAttribArray(we.a_position),de.vertexAttribPointer(we.a_position,2,de.FLOAT,!1,16,0),de.enableVertexAttribArray(we.a_radius),de.vertexAttribPointer(we.a_radius,1,de.FLOAT,!1,16,8),de.enableVertexAttribArray(we.a_alpha),de.vertexAttribPointer(we.a_alpha,1,de.FLOAT,!1,16,12),de.bindVertexArray(null),Oe=he(de,"#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=>Ge[t]=de.getAttribLocation(Oe,t)),["u_proj","u_color","u_tex"].forEach(t=>Ve[t]=de.getUniformLocation(Oe,t)),Xe=de.createVertexArray(),de.bindVertexArray(Xe),Ye=de.createBuffer(),de.bindBuffer(de.ARRAY_BUFFER,Ye),de.bufferData(de.ARRAY_BUFFER,Ze,de.STATIC_DRAW),de.enableVertexAttribArray(Ge.a_corner),de.vertexAttribPointer(Ge.a_corner,2,de.FLOAT,!1,0,0),ze=de.createBuffer(),de.bindBuffer(de.ARRAY_BUFFER,ze),de.bufferData(de.ARRAY_BUFFER,We.byteLength,de.DYNAMIC_DRAW),He=We.byteLength,de.enableVertexAttribArray(Ge.a_pos),de.vertexAttribPointer(Ge.a_pos,2,de.FLOAT,!1,20,0),de.vertexAttribDivisor(Ge.a_pos,1),de.enableVertexAttribArray(Ge.a_size),de.vertexAttribPointer(Ge.a_size,1,de.FLOAT,!1,20,8),de.vertexAttribDivisor(Ge.a_size,1),de.enableVertexAttribArray(Ge.a_angle),de.vertexAttribPointer(Ge.a_angle,1,de.FLOAT,!1,20,12),de.vertexAttribDivisor(Ge.a_angle,1),de.enableVertexAttribArray(Ge.a_alpha),de.vertexAttribPointer(Ge.a_alpha,1,de.FLOAT,!1,20,16),de.vertexAttribDivisor(Ge.a_alpha,1),de.bindVertexArray(null),pe=!0)}();const a=!0!==F.isBrush;F.isBrush=!0,a&&(F.justChanged=!0),F.blend(P.stroke.color);const o=["default","spray"].includes(br.p.type)?br.p.opacity:br.p.opacity/Math.min(P.stroke.weight,1.3),s=.1*(br.p.noise??0);br.alpha=s>0?Math.max(0,o*(1+xt(0,s))):o,br.overscan=function(){const t=Math.max(1,br.max??1),e=P.stroke.weight*br.p.scatter,r=P.stroke.weight*br.p.weight*t;return Math.max(8,1.5*e+.75*r)}(),br.drawFn="spray"===br.p.type?Mr:"marker"===br.p.type?Sr:"custom"===br.p.type||"image"===br.p.type?Tr:Pr,Cr()}();const a=br.p?.spacing??1,o=Math.round(gr*(e?t:1)/a);br.pressureCount=10,br.cachedPressure=void 0;const s=2*o;for(;vr.length<s;)vr.push(xt());for(let r=0;r<o;r++)e&&(pr=xr.angle(mr.plotted)),Rr(),e?mr.plotTo(xr,a,a,t,pr):mr._moveToDegrees(t,a,a);!function(){Cr(),function(){if(0===Le)return;F.glMask.isDrawn=!0;const t=Qe(),e=P.stroke.color._array;de.useProgram(ye),de.enable(de.BLEND),de.blendFunc(de.ONE_MINUS_DST_ALPHA,de.ONE),de.bindVertexArray(ge),de.bindBuffer(de.ARRAY_BUFFER,xe);const r=16*Le,n=De.subarray(0,4*Le);r>Be?(de.bufferData(de.ARRAY_BUFFER,De,de.DYNAMIC_DRAW),Be=De.byteLength):de.bufferSubData(de.ARRAY_BUFFER,0,n),de.uniform4f(Re.u_color,...e),de.uniformMatrix4fv(Re.u_matrix,!1,Je()),de.drawArrays(de.POINTS,0,Le),de.bindVertexArray(null),tr(t),Ue&&(F.markDirtyRect(F.glMask,Ue),Ue=null),Le=0,fe(i,de)}();const t=br.p?.type;"image"===t?nr(Dr.tips.get(br.p.image.src),br.p.image.src):"custom"===t&&nr(Dr.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?Ar()*xr.pressure(mr.plotted):Ar(),br.pressureCount=0),br.pressureCount++,br.cachedPressure}function Ar(){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 a=t*gr,o=(mr.plotted<a?1.2*e:.8*e)*(gr/2);return yt(1/(1+Math.pow(Math.abs((mr.plotted-a)/o),2*r)),0,1,n,i)}();const t=mr.plotted/gr;return yt(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 Mr(t){const e=P.stroke.weight*br.p.scatter*t+P.stroke.weight*ut(vr)*br.p.scatter/3,r=br.p.weight*ct(.9,1.1),n=Math.ceil(br.p.grain/t);for(let t=0;t<n;t++){const t=ct(.9,1.1),n=t*e*ct(-1,1),i=ct(-1,1),a=Math.sqrt((t*e)**2-n**2);rr(mr.x+n,mr.y+i*a,r,br.alpha)}}function Sr(t,e=!0,r=br.alpha){const n=e?P.stroke.weight*br.p.scatter:0,i=e?n*ct(-1,1):0,a=e?n*ct(-1,1):0;rr(mr.x+i,mr.y+a,P.stroke.weight*br.p.weight*t,r*Math.max(.8,t)*ct(.9,1.1))}function Tr(t,e=br.alpha){const r=P.stroke.weight*br.p.scatter,n=r*ct(-1,1),i=r*ct(-1,1),a=br.p.weight*P.stroke.weight*t,o=br.overscan;let s=0;"random"===br.p.rotate?s=ft(0,360)*(Math.PI/180):"natural"===br.p.rotate&&(s=((xr?-pr:-yr)+mr.angle())*(Math.PI/180)),function(t,e,r,n,i,a=0){if($e>=qe){qe*=2;const t=new Float32Array(5*qe);t.set(We),We=t}const o=t-Ce,s=e-Fe,l=Ee*o+Me*s+Te+Ce,c=Ae*o+Se*s+Pe+Fe,h=Ie*r*ke/2,u=Ie*a*ke,f=5*$e;We[f]=l,We[f+1]=c,We[f+2]=h,We[f+3]=n,We[f+4]=i/255;const d=l*Ie,m=c*Ie,g=1.42*h+u;je=er(je,d-g-1,m-g-1,d+g+1,m+g+1),$e++}(mr.x+n,mr.y+i,a,s,e*Math.max(.8,t)*ct(.9,1.1),o)}function Pr(t){if(ct(0,1)>=br.p.grain*t)return;const e=P.stroke.weight*br.p.scatter*(br.p.sharpness+(1-br.p.sharpness)*ut(vr)/t);let r,n;if(xr){const t=pr,i=Et(t),a=At(t),o=e*ct(-1,1),s=.3*e*ct(-1,1);r=o*a+s*i,n=o*i-s*a}else{const t=e*ct(-1,1),i=.3*e*ct(-1,1);r=t*br.sin+i*br.cos,n=t*br.cos-i*br.sin}const i=t*t*br.p.weight*ct(.85,1.15)*P.stroke.weight,a=Math.max(.9,t)*br.alpha*ct(.75,1.1);rr(mr.x+r,mr.y+n,i,a)}function Cr(){if(!1===br.p.markerTip)return;let t=Er(),e=br.alpha;if("marker"===br.p.type)for(let r=1;r<10;r++)Sr(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 Fr(t,e,r,n){if(!P.stroke.isActive||!P.stroke.color)throw new Error("No brush or color set. Call brush.set('brushName', color) before drawing.");Lt();let i=Ft(t,e,r,n);0!=i&&(_r(t,e,i),wr(kt(t,e,r,n),!1))}st(()=>{vr.length=0});const kr=["weight","scatter","sharpness","grain","opacity","spacing","pressure","type","tip","rotate","markerTip","noise"],Ir=[["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 Ir){let e={};for(let r=0;r<t[1].length;r++)e[kr[r]]=t[1][r];lr(t[0],e)}Kt.prototype.draw=function(t=!1,e,r){let n=or();if(t&&dr(t,e,r),n.isActive)for(let t of this.sides)Fr(t[0].x,t[0].y,t[1].x,t[1].y);return sr(n),this},Zt.prototype.draw=function(t,e,r){return or().isActive&&(this.origin&&(t=this.origin[0],e=this.origin[1],r=1),function(t,e,r,n){Lt(),_r(e,r,t.length,t),wr(n,!0)}(this,t,e,r)),this};const Dr={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)=>le.loadImageTip(t,e))(t,Dr.imageToWhite).then(e=>{this.tips.set(t,e)})))}};function Lr(){return{...P.hatch}}function Nr(t){P.hatch={...t}}function Br(t=5,e=45,r={rand:!1,continuous:!1,gradient:!1}){let n=P.hatch;n.isActive=!0,n.dist=t,n.angle=Pt(e),n.options=r}P.hatch={isActive:!1,dist:5,angle:45,options:{},hBrush:!1};let Ur=new Float64Array(256),Or=new Float64Array(256),Xr=new Float64Array(512),Yr=new Float64Array(512),zr=new Float64Array(512),Gr=new Float64Array(512);function Vr(t,e,r,n){const i=function(t,e,r,n){Array.isArray(t)||(t=[t]);const i=e*Math.PI/180,a=Math.cos(i),o=Math.sin(i);let s=0;for(const e of t)s+=e.a.length;if(0===s)return[];Ur.length<s&&(Ur=new Float64Array(2*s),Or=new Float64Array(2*s));let l=Infinity,c=-Infinity,h=0,u=0;Xr.length<s&&(Xr=new Float64Array(2*s),Yr=new Float64Array(2*s),zr=new Float64Array(2*s),Gr=new Float64Array(2*s));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*a-n*o,Or[u]=r*o+n*a,Or[u]<l&&(l=Or[u]),Or[u]>c&&(c=Or[u]),u++}for(let t=0;t<r;t++){const e=n+t,i=n+(t+1<r?t+1:0),a=Or[e],o=Or[i];a!==o&&(Xr[h]=Ur[e],Yr[h]=a,zr[h]=Ur[i],Gr[h]=o,h++)}}const f=[],d=[];let m=l+.5*r,g=r;const x=1!==n;for(;m<c;){d.length=0;for(let t=0;t<h;t++){const e=Yr[t],r=Gr[t];e<=m!=r<=m&&d.push(Xr[t]+(m-e)/(r-e)*(zr[t]-Xr[t]))}const t=d.length;if(2===t){let t=d[0],e=d[1];if(t>e){const r=t;t=e,e=r}f.push({scanY:m,x1:t*a+m*o,y1:-t*o+m*a,x2:e*a+m*o,y2:-e*o+m*a})}else if(t>2){d.sort((t,e)=>t-e);for(let e=0;e<t-1;e+=2){const t=d[e],r=d[e+1];f.push({scanY:m,x1:t*a+m*o,y1:-t*o+m*a,x2:r*a+m*o,y2:-r*o+m*a})}}m+=g,x&&(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 Hr(t){const{dist:e,options:r,segs:n}=function(t){const e=P.hatch.dist,r=(P.hatch.angle%180+180)%180,n=P.hatch.options;return{dist:e,options:n,segs:Vr(t,e,r,n.gradient?yt(n.gradient,0,1,1,1.1,!0):1)}}(t),i=r.rand||0,a=[];for(let t=0;t<n.length;t++){const o=n[t];let s=o.x1,l=o.y1,c=o.x2,h=o.y2;i&&(s+=2*i*e*ct(-1,1),l+=2*i*e*ct(-1,1),c+=2*i*e*ct(-1,1),h+=2*i*e*ct(-1,1));const u=r.continuous&&t%2==1?{x1:c,y1:h,x2:s,y2:l,scanY:o.scanY,isConnector:!1}:{x1:s,y1:l,x2:c,y2:h,scanY:o.scanY,isConnector:!1};if(a.push(u),t>0&&r.continuous){const t=a[a.length-2];a.push({x1:t.x2,y1:t.y2,x2:u.x1,y2:u.y1,scanY:o.scanY,isConnector:!0})}}return a}function Wr(t){!function(t,e){const r=Hr(t);!function(){const t=or();(()=>{for(let t=0;t<r.length;t++){const n=r[t];e(n.x1,n.y1,n.x2,n.y2,t,r)}})(),sr(t)}()}(t,(t,e,r,n)=>{P.hatch.hBrush&&dr(P.hatch.hBrush.brush,P.hatch.hBrush.color,P.hatch.hBrush.weight*ct(.9,1.1)),Fr(t,e,r,n)})}function $r(t,e,r){return new Kt(t.vertices.map(t=>[t.x+e,t.y+r]))}function qr(t,e,r){return t.map(t=>$r(t,e,r))}function jr(t){const e=Array.isArray(t)?t.flatMap(t=>t?.a??[]):t?.a??[];let r=Infinity,n=Infinity,i=-Infinity,a=-Infinity;for(const[t,o]of e)t<r&&(r=t),o<n&&(n=o),t>i&&(i=t),o>a&&(a=o);return{minX:r,minY:n,maxX:i,maxY:a,cx:(r+i)/2,cy:(n+a)/2,size:Math.hypot(i-r,a-n)}}function Kr(t,e,r,n,i){const a=(e+n)/2,o=(r+i)/2,s=n-e,l=i-r,c=Math.hypot(s,l);if(!c)return null;const h=-l/c,u=s/c,f=(t.x-a)*h+(t.y-o)*u;return{x:a+h*f,y:o+u*f}}function Zr(t,e,r,n,i,a){if(!Array.isArray(t))return e;const o=(r+i)/2,s=(n+a)/2,l=Math.hypot(i-r,a-n),c=Math.max(jr(t).size,l,1),h=.08+.18*(1-Math.min(1,l/(.42*c)));return{x:e.x+(o-e.x)*h,y:e.y+(s-e.y)*h}}function Jr(t,e,r){let n=!1;for(let i=0,a=t.length-1;i<t.length;a=i++){const o=t[i][0],s=t[i][1],l=t[a][0],c=t[a][1];s>r!=c>r&&e<(l-o)*(r-s)/(c-s||Number.EPSILON)+o&&(n=!n)}return n}function Qr(t,e,r){if(Array.isArray(t)){let n=!1;for(const i of t)Jr(i.a,e,r)&&(n=!n);return n}return Jr(t.a,e,r)}function tn(t,e,r,n,i,a,o){const s=a(n),l=(s+((a(i)-s)%360+360)%360*o)*Math.PI/180;return{x:t+r*Math.cos(l),y:e-r*Math.sin(l)}}function en(t,e,r,n,i,a,o){for(const s of[.125,.25,.375,.5,.625,.75,.875]){const l=tn(e,r,n,i,a,o,s);if(!Qr(t,l.x,l.y))return!1}return!0}function rn(t,e,r,n,i,a,o,s,l,c){const h=function(t,e,r,n,i,a,o){let s=kt(t,e,r,n),l=kt(t,e,i,a);return((l-s)%360+360)%360>180&&([s,l]=[l,s]),[o(s),o(l)]}(e,r,i,a,o,s,l),u=[h[1],h[0]],f=en(t,e,r,n,h[0],h[1],c),d=en(t,e,r,n,u[0],u[1],c);return f&&!d?h:d&&!f?u:f?h:d?u:null}function nn(t){const e=ht(.35,.65),r=t.x1+(t.x2-t.x1)*e,n=t.y1+(t.y2-t.y1)*e,i=t.x2-t.x1,a=t.y2-t.y1,o=Math.hypot(i,a)||1,s=ht(.04,.1)*o,l=i/o*s*.5,c=a/o*s*.5;return[{x1:t.x1,y1:t.y1,x2:r-l,y2:n-c},{x1:r+l,y1:n+c,x2:t.x2,y2:t.y2}]}function an(t,e,r,n){const i=function(t,e){const r=jr(t),n=r.size*ht(.6,1.4),[i,a]=e;return{x:r.cx+i*n,y:r.cy+a*n}}(t,e);for(const e of Hr(t)){const a=!e.isConnector&&ht()<.35?nn(e):[e];for(const e of a){const a=Kr(i,e.x1,e.y1,e.x2,e.y2),o=a?Zr(t,a,e.x1,e.y1,e.x2,e.y2):null;if(!o)continue;const s=Ft(o.x,o.y,e.x1,e.y1);if(!s)continue;const l=rn(t,o.x,o.y,s,e.x1,e.y1,e.x2,e.y2,r,n);if(!l)continue;const[c,h]=l;Jt(o.x,o.y,s,c,h)}}}function on(t,e,r,n,i,a,o){Br(e,r,Array.isArray(t)?{...n,continuous:!1}:n),an(t,i,a,o)}function sn(t,e,r,n){const i=function(t,e,r,n){const i=!1===e,a=cr(P.mass.brush)?.scatter??0,o=i?t:t.genPol(e,r,n,.15),s=Math.min(2*a,5),l=[[ht(-s,s),ht(-s,s)],[ht(-s,s),ht(-s,s)]];return Array.isArray(o)?[o,qr(o,l[0][0],l[0][1]),qr(o,l[1][0],l[1][1])]:[o,$r(o,l[0][0],l[0][1]),$r(o,l[1][0],l[1][1])]}(t,e,r,n),a=Lr(),l=or(),c={...P.field},h=P.mass.options?.precision??.5,u=P.mass.options?.strength??1,f=P.mass.options?.gradient??.1,d=P.mass.options?.outline??!1,m=cr(P.mass.brush)?.scatter??0,g=1.6*ht(.65*m,.85*m)-.4*f,x=ht(-90,90),y=((x+90)%180+180)%180-90>=0?ht()<.5?[1,1]:[-1,-1]:ht()<.5?[-1,1]:[1,-1],p=o()?t=>t*Math.PI/180:t=>t,b=o()?t=>180*t/Math.PI:t=>t;dr(P.mass.brush,P.mass.color,1),qt(2-h),d&&function(t){if(Array.isArray(t))for(const e of t)e.draw();else t.draw()}(i[0]),on(i[0],.9*g,s(x),{gradient:f,rand:2-2*h,continuous:!0},y,p,b),u>.33&&on(i[1],g,s(x+20*ht(-1,1)),{gradient:f,rand:.6-.6*h,continuous:!0},y,p,b),u>.66&&on(i[2],.8*g,s(x+15*ht(-1,1)),{gradient:f,rand:.6-.6*h,continuous:!0},y,p,b),sr(l),Nr(a),P.field={...c}}function ln(t,e=null){const r=F.ctx;e||(e=r.getTransform());const n=e.a,i=e.b,a=e.c,o=e.d,s=e.e,l=e.f;let c=Infinity,h=Infinity,u=-Infinity,f=-Infinity;r.beginPath();const d=t[0];let m=n*d.x+a*d.y+s,g=i*d.x+o*d.y+l;c=u=m,h=f=g,r.moveTo(d.x,d.y);for(let e=1;e<t.length;e++){const d=t[e];m=n*d.x+a*d.y+s,g=i*d.x+o*d.y+l,c=Math.min(c,m),h=Math.min(h,g),u=Math.max(u,m),f=Math.max(f,g),r.lineTo(d.x,d.y)}r.closePath();const x=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);F.markDirtyRect(F.mask,{minX:c-x,minY:h-x,maxX:u+x,maxY:f+x})}function cn(t,e,r){const n=2*Math.PI,i=F.ctx,a=r/2;i.moveTo(t+a,e),i.arc(t,e,a,0,n)}Kt.prototype.hatch=function(t=!1,e,r){let n=Lr();return t&&Br(t,e,r),P.hatch.isActive&&Wr(this),Nr(n),this},Zt.prototype.hatch=function(t,e,r){Lr().isActive&&(this.origin&&(t=this.origin[0],e=this.origin[1],r=1),this.pol=this.genPol(t,e,r,.3),this.pol.hatch())},P.mass={isActive:!1,brush:null,color:null,options:{}},Kt.prototype.mass=function(){return P.mass?.isActive&&sn(this,!1),this},Zt.prototype.mass=function(t,e,r){return P.mass?.isActive&&(this.origin&&(t=this.origin[0],e=this.origin[1],r=1),sn(this,t,e,r)),this};let hn=null,un=!1;un||(w={ensureResources:function(t,e,r,n,i){const a=Math.max(1,Math.round(e*n)),o=Math.max(1,Math.round(r*n));return function(t,e,r){return!t.mask||t.mask.width!==e||t.mask.height!==r}(t,a,o)&&(t.mask=p(a,o)),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=_(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 a=t.fillMaskFramebuffer,o=t.drawingContext,s=r??n(),l=s.maxX-s.minX,c=s.maxY-s.minY;hn&&hn.width===l&&hn.height===c||(hn=p(l,c));const h=hn.drawingContext;return h.clearRect(0,0,l,c),h.drawImage(e,s.minX,s.minY,l,c,0,0,l,c),i(a),o.bindTexture(o.TEXTURE_2D,a.colorTexture),o.texSubImage2D(o.TEXTURE_2D,0,s.minX,s.minY,o.RGBA,o.UNSIGNED_BYTE,hn),a}},un=!0);let fn,dn=[],mn=[],gn=[],xn=[];P.fill={opacity:150,bleed_strength:.07,texture_strength:.8,border_strength:.5,direction:"out",angle:null,scatter:!0,isActive:!1};const yn=()=>({...P.fill});function pn(t,e,r,n){P.fill.opacity=(arguments.length<4?e:n)||150,P.fill.color=arguments.length<3?l(t):l(t,e,r),P.fill.isActive=!0}function bn(t,e="out",r=null){P.fill.bleed_strength=pt(t,0,1),P.fill.direction=e,P.fill.angle=null==r?null:Pt(r)}function _n(t=.4,e=.4,r=!0){P.fill.texture_strength=pt(t,0,1),P.fill.border_strength=pt(e,0,1),P.fill.scatter=r}let vn,wn,Rn,En,An;const Mn=[[],[]];function Sn(){for(let t=0;t<512;t++)Mn[0][t]=xt(.5,.2),Mn[1][t]=xt(0,.02)}st(Sn);class Tn{constructor(t,e,r,n=[],i=!1,a,o){if(this.v=t,this.m=e,this.dir=n,this.midP=r,i){let e=0,n=0;const i=[];for(let a=0;a<t.length;a++){const o=Math.abs(r.x-t[a].x),s=Math.abs(r.y-t[a].y);e=Math.max(e,o),n=Math.max(n,s);const l=t[a],c=t[(a+1)%t.length],h={x:c.x-l.x,y:c.y-l.y},u=Ct(0,0,h.x,h.y,90),f={x:l.x+h.x/2,y:l.y+h.y/2};i.push({v1:l,v2:c,ray:{point1:f,point2:{x:f.x+u.x,y:f.y+u.y}}})}this.sizeX=e,this.sizeY=n;const a=vn.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,o=e.ray.point2.x-r,s=e.ray.point2.y-n,l=e.v2.x-e.v1.x,c=e.v2.y-e.v1.y,h=-(l*l+c*c);let u=0;for(let t=0;t<a.length;t++){const e=a[t][0],i=a[t][1],l=i.x-e.x,c=i.y-e.y,f=c*o-l*s;if(0===f)continue;const d=(o*(n-e.y)-s*(r-e.x))/f;d<0||d>1||(l*(n-e.y)-c*(r-e.x))/f*h<=.01||u++}this.dir[t]=u%2==0}const o=ct(-.6,.6)*e,s=ct(-.6,.6)*n;this.midP={x:r.x+o,y:r.y+s}}else this.sizeX=a,this.sizeY=o}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,a=n+r,o=this.v[(i-1+e)%e],s=this.v[a%e],l=s.x-o.x,c=s.y-o.y,h=Math.hypot(l,c),u=n>=2?~~ct(0,n-1):a<e-1?a:0,f=this.v[u],d=this.v[(u+1)%e],m=Math.max(1,Math.hypot(d.x-f.x,d.y-f.y)),g=Math.max(2,Math.ceil(h/m*.05)),x=e-r+g,y=new Array(x),p=new Array(x),b=new Array(x);let _=0;for(let t=0;t<n;t++,_++)y[_]=this.v[t],p[_]=this.m[t],b[_]=this.dir[t];const v=.06*h,w=this.dir[i%this.dir.length];for(let t=0;t<g;t++,_++){const e=(t+1)/(g+1);y[_]={x:o.x+l*e+ct(-v,v),y:o.y+c*e+ct(-v,v)},p[_]=ct(.3,.5),b[_]=w}for(let t=a;t<e;t++,_++)y[_]=this.v[t],p[_]=this.m[t],b[_]=this.dir[t];return{v:y,m:p,dir:b}}scatter(t=.3){const e=this.v.length,r=Math.max(3,~~(e*t)),n=e/r,i=.8*n,a=[],o=[],s=[],l=this.midP,c=vn.sides;for(let t=0;t<r;t++){const r=~~(t*n+ct(0,i))%e;let h=this.v[r],u=!1;if(h.x<wn||h.x>En||h.y<Rn||h.y>An)u=!0;else{let t=0;for(const[e,r]of c){const n=e.y,i=r.y;if(n>h.y==i>h.y)continue;const a=(h.y-n)/(i-n);h.x<e.x+a*(r.x-e.x)&&t++}u=t%2==0}u&&(h={x:l.x+(h.x-l.x)*ct(.3,.6),y:l.y+(h.y-l.y)*ct(.3,.6)}),a.push(h),o.push(this.m[r]),s.push(!this.dir[r])}return new Tn(a,o,this.midP,s,!1,this.sizeX,this.sizeY)}flipDirs(){return new Tn(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,a=2*i;dn.length<i&&(dn=new Array(i),mn=new Array(i)),gn.length<a&&(gn=new Array(a),xn=new Array(a));const o=dn,s=mn,l=gn,c=xn,h="out"===P.fill.direction?-90:90;0===Mn[0].length&&Sn();const u=Mn[0],f=u.length,d=Mn[1],m=d.length;let g=0,x=0,y=999===t?ct(.6,.8):P.fill.bleed_strength;const p=fn&&2*i>fn?Math.ceil(2*i/fn):1;if(p>=2&&!(1&p)){for(let e=0;e<i;e++){const n=r[e];t<997&&(y=n),y>=.05&&(ct(-1,1),u[~~(ct(0,1)*f)],ct(.65,1.35),d[~~(ct(0,1)*m)])}return new Tn(e,r,this.midP,n,!1,this.sizeX,this.sizeY)}for(let a=0;a<i;a++){const p=e[a],b=e[a+1<i?a+1:0],_=r[a],v=n[a];if(t<997&&(y=_),y<.05){l[g]=_,c[g]=v,g++,o[x]=(p.x+b.x)/2,s[x]=(p.y+b.y)/2,l[g]=_,c[g]=v,g++,x++;continue}const w=(v?h:-h)+5*ct(-1,1),R=St(w),E=R[0],A=R[1],M=b.x-p.x,S=b.y-p.y,T=E*M+A*S,P=E*S-A*M,C=u[~~(ct(0,1)*f)]*ct(.65,1.35)*y,F=_+d[~~(ct(0,1)*m)];l[g]=_,c[g]=v,g++,o[x]=p.x+.5*M+T*C,s[x]=p.y+.5*S+P*C,l[g]=F,c[g]=v,g++,x++}let b,_,v;if(fn&&g>fn){const t=Math.ceil(g/fn),r=Math.ceil(g/t);b=new Array(r),_=new Array(r),v=new Array(r);let n=0;for(let r=0;r<g;r+=t,n++)b[n]=r%2==0?e[r>>1]:{x:o[r>>1],y:s[r>>1]},_[n]=l[r],v[n]=c[r]}else{b=new Array(g);for(let t=0;t<g;t++)b[t]=t%2==0?e[t>>1]:{x:o[t>>1],y:s[t>>1]};_=l.slice(0,g),v=c.slice(0,g)}return new Tn(b,_,this.midP,v,!1,this.sizeX,this.sizeY)}fill(t,e,i){const o=3*i,s=2*e*(1+i/2),l=!1!==F.isBrush;F.isBrush=!1,l&&(F.justChanged=!0),F.blend(t);const h=c();F.ctx.save(),F.ctx.setTransform(a*h.a,a*h.b,a*h.c,a*h.d,a*(h.x+r/2),a*(h.y+n/2)),F.ctx.strokeStyle="rgb(255 0 0 / "+.01*P.fill.border_strength+")",F.ctx.lineCap="round",fn=2024*Math.max(.2,2*P.fill.bleed_strength);const u=F.ctx.getTransform(),f=Math.max(this.sizeX,this.sizeY),d=ct(.15,.7);let m=this.grow();const g=this.scatter(.1).grow().scatter(.75).flipDirs();let x;for(let r=0;r<20;r++){r%4==0&&(m=m.grow()),r%2==0&&(x=[m.grow(1-.0125*r),m.grow(.7-.0125*r),m.grow(.4-.0125*r)]);for(const t of x)t.grow(999).grow(997).layer(r,f,s,u);P.fill.scatter&&g.grow(999).flipDirs().grow(997).layer(r,f,s*o,u),r%2==0&&m.grow(d).grow(999).layer(r,f,2*s,u),r%8!=0&&19!==r||(0!==o&&m.erase(3*o,e),F.blend(t,!0))}F.ctx.restore()}layer(t,e,r,n=null){F.ctx.lineWidth=yt(t,0,24,e/25,e/30,!0)*P.fill.border_strength,F.ctx.fillStyle="rgb(255 0 0 / "+r+"%)",ln(this.v,n),F.ctx.fill(),F.ctx.stroke()}erase(t,e){F.ctx.save();const r=~~(ct(80,110)*yt(t,0,1,2,3.5)),n=this.sizeX/1.3,i=this.sizeY/1.3,a=1.3*Math.min(this.sizeX,this.sizeY),o=.03*a,s=.45*a,{x:l,y:c}=this.midP;F.ctx.globalCompositeOperation="destination-out";const h=(5-yt(e,80,100,.3,.7,!0))*t/255;F.ctx.fillStyle=`rgb(255 0 0 / ${h})`,F.ctx.lineWidth=0;for(let t=0;t<r;t++){const e=l+xt(0,n),r=c+xt(0,i),a=ct(o,s);F.ctx.beginPath(),cn(e,r,a),t%5!=0&&F.ctx.fill()}F.ctx.globalCompositeOperation="source-over",F.ctx.restore()}}function Pn(t,e,r,n){m(),P.wash.opacity=(arguments.length<4?e:n)??150,P.wash.color=arguments.length<3?l(t):l(t,e,r),P.wash.isActive=!0}function Cn(t,e,r){const n=e.isEnabled(e.DEPTH_TEST);return n&&e.disable(e.DEPTH_TEST),e.bindFramebuffer(e.FRAMEBUFFER,r.framebuffer),e.viewport(0,0,r.width*r.density,r.height*r.density),{hadDepthTest:n}}function Fn(t,e,i){i?.hadDepthTest&&e.enable(e.DEPTH_TEST),e.bindFramebuffer(e.FRAMEBUFFER,null),e.viewport(0,0,Math.max(1,Math.round(r*a)),Math.max(1,Math.round(n*a)))}function kn(t,e){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 In(t,e,r){if(!e)return;if(r(e)){const r=t.drawingContext,n=r.getParameter(r.FRAMEBUFFER_BINDING),i=r.getParameter(r.VIEWPORT);return r.bindFramebuffer(r.FRAMEBUFFER,e.framebuffer),r.viewport(0,0,e.width*e.density,e.height*e.density),r.clearColor(0,0,0,0),r.clear(r.COLOR_BUFFER_BIT),r.bindFramebuffer(r.FRAMEBUFFER,n),void r.viewport(i[0],i[1],i[2],i[3])}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 Dn(t,e,r){return t.shaderProgram??=function(t,e,r){const n=he(t,e,r),i=new Map,a=t.createVertexArray(),o=t.getUniformLocation(n,"u_source"),s=t.getUniformLocation(n,"u_mask"),l=t.getUniformLocation(n,"u_color");return{program:n,quadVao:a,loc_source:o,loc_mask:s,loc_color:l,setUniform(e,r){const a=(e=>(i.has(e)||i.set(e,t.getUniformLocation(n,e)),i.get(e)))(e);a&&("boolean"!=typeof r?"number"!=typeof r?Array.isArray(r)&&(3===r.length?t.uniform3f(a,r[0],r[1],r[2]):4===r.length&&t.uniform4f(a,r[0],r[1],r[2],r[3])):t.uniform1f(a,r):t.uniform1i(a,r?1:0))}}}(t.drawingContext,e,r),t.shaderProgram}function Ln(t,e){const r=t.drawingContext,n=e.density??1,i=r.createFramebuffer(),a=Math.max(1,e.width),o=Math.max(1,e.height),s=Math.max(1,Math.round(a*n)),l=Math.max(1,Math.round(o*n)),c=function(t,e,r){const n=t.createTexture();return t.bindTexture(t.TEXTURE_2D,n),t.texImage2D(t.TEXTURE_2D,0,t.RGBA,e,r,0,t.RGBA,t.UNSIGNED_BYTE,null),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MIN_FILTER,t.LINEAR),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MAG_FILTER,t.LINEAR),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_S,t.CLAMP_TO_EDGE),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_T,t.CLAMP_TO_EDGE),t.bindTexture(t.TEXTURE_2D,null),n}(r,s,l);return r.bindFramebuffer(r.FRAMEBUFFER,i),r.framebufferTexture2D(r.FRAMEBUFFER,r.COLOR_ATTACHMENT0,r.TEXTURE_2D,c,0),r.bindFramebuffer(r.FRAMEBUFFER,null),{__brushFramebuffer:!0,framebuffer:i,colorTexture:c,width:a,height:o,density:n,pixelDensity:()=>n,remove(){r.deleteFramebuffer(i),r.deleteTexture(c)}}}function Nn(t,e,r,n,i){return e?.remove?.(),Ln(t,{width:r,height:n,density:i})}function Bn({renderer:t,shader:e,source:r,mask:n,color:i,isBrushMask:a,dirtyRect:o,targetIsFramebuffer:s,withScissor:l}){const c=t.drawingContext,h=c.isEnabled(c.DEPTH_TEST),u=c.getParameter(c.FRAMEBUFFER_BINDING),f=c.getParameter(c.CURRENT_PROGRAM),d=c.getParameter(c.VERTEX_ARRAY_BINDING);c.bindVertexArray(e.quadVao),c.useProgram(e.program),c.disable(c.DEPTH_TEST),c.enable(c.BLEND),c.blendEquation(c.FUNC_ADD),c.blendFunc(c.ONE,c.ONE_MINUS_SRC_ALPHA),c.activeTexture(c.TEXTURE0),c.bindTexture(c.TEXTURE_2D,r.colorTexture),c.uniform1i(e.loc_source,0),c.activeTexture(c.TEXTURE1),c.bindTexture(c.TEXTURE_2D,n.colorTexture),c.uniform1i(e.loc_mask,1),e.setUniform("u_targetIsFramebuffer",s),e.setUniform("u_isBrush",a),c.uniform3f(e.loc_color,i[0],i[1],i[2]),c.bindFramebuffer(c.FRAMEBUFFER,null),l(c,o,()=>{c.drawArrays(c.TRIANGLES,0,3)},!s),c.bindTexture(c.TEXTURE_2D,null),c.activeTexture(c.TEXTURE0),c.bindVertexArray(d),c.useProgram(f),c.bindFramebuffer(c.FRAMEBUFFER,u),h&&c.enable(c.DEPTH_TEST)}function Un(t){return function({renderer:t,sourceFramebuffer:e,dirtyRect:r,getTargetPixelSize:n,toScissorBox:i}){const a=t.drawingContext,{width:o,height:s}=n(),l=r?i(r):null,c=a.getParameter(a.READ_FRAMEBUFFER_BINDING),h=a.getParameter(a.DRAW_FRAMEBUFFER_BINDING);return a.bindFramebuffer(a.READ_FRAMEBUFFER,null),a.bindFramebuffer(a.DRAW_FRAMEBUFFER,e.framebuffer),a.blitFramebuffer(l?.x??0,l?.y??0,l?l.x+l.width:o,l?l.y+l.height:s,l?.x??0,l?.y??0,l?l.x+l.width:o,l?l.y+l.height:s,a.COLOR_BUFFER_BIT,a.NEAREST),a.bindFramebuffer(a.READ_FRAMEBUFFER,c),a.bindFramebuffer(a.DRAW_FRAMEBUFFER,h),e}(t)}function On(t){if("number"==typeof t){const e=Math.max(0,Math.min(255,t));return`rgb(${e} ${e} ${e})`}return t??"black"}function Xn(t){const e=((t,e=!1)=>(t.drawingContext??=t.getContext("2d",e?{willReadFrequently:!0}:void 0),t.drawingContext))(t,!0),r={canvas:t,drawingContext:e,width:t.width,height:t.height,pixels:null,_fillStyle:"#ffffff",_strokeStyle:"transparent",_lineWidth:1,pixelDensity:()=>1,background(r){e.save(),e.setTransform(1,0,0,1,0,0),e.fillStyle=On(r),e.fillRect(0,0,t.width,t.height),e.restore()},noSmooth(){e.imageSmoothingEnabled=!1},push(){e.save()},pop(){e.restore()},translate(t,r){e.translate(t,r)},scale(t,r=t){e.scale(t,r)},rotate(t){e.rotate(t)},noStroke(){r._strokeStyle="transparent"},stroke(t){r._strokeStyle=On(t)},noFill(){r._fillStyle="transparent"},fill(t){r._fillStyle=On(t)},strokeWeight(t){r._lineWidth=t},rect(t,n,i,a){e.beginPath(),e.rect(t,n,i,a),r._paint()},circle(t,n,i){e.beginPath(),e.arc(t,n,i/2,0,2*Math.PI),r._paint()},ellipse(t,n,i,a){e.beginPath(),e.ellipse(t,n,i/2,a/2,0,0,2*Math.PI),r._paint()},line(t,n,i,a){e.beginPath(),e.moveTo(t,n),e.lineTo(i,a),e.strokeStyle=r._strokeStyle,e.lineWidth=r._lineWidth,e.stroke()},beginShape(){e.beginPath(),r._shapeStarted=!1},vertex(t,n){r._shapeStarted?e.lineTo(t,n):(e.moveTo(t,n),r._shapeStarted=!0)},endShape(t=!1){t&&e.closePath(),r._paint()},loadPixels(){r.pixels=e.getImageData(0,0,t.width,t.height).data},updatePixels(){const n=e.getImageData(0,0,t.width,t.height);n.data.set(r.pixels),e.putImageData(n,0,0)},_paint(){"transparent"!==r._fillStyle&&(e.fillStyle=r._fillStyle,e.fill()),"transparent"!==r._strokeStyle&&(e.strokeStyle=r._strokeStyle,e.lineWidth=r._lineWidth,e.stroke())}};return r}function Yn(t,e){return Xn(p(t,e,!0))}function zn(t,e){return new Promise((r,n)=>{const i=globalThis.Image;if(!i)return void n(new Error("Standalone image brush loading requires Image support."));const a=new i;a.onload=()=>{const t=Xn(p(a.naturalWidth,a.naturalHeight,!0));t.drawingContext.drawImage(a,0,0),e(t),r(t)},a.onerror=()=>n(new Error(`Failed to load image tip: ${t}`)),a.crossOrigin="anonymous",a.src=t})}var Gn;Kt.prototype.fill=function(t=!1,e,r,n,i,a,o){let s=yn();return t&&(pn(t,e),bn(r,a,o),_n(n,i)),s.isActive&&(Lt(),function(t){if(!P.fill.isActive||!P.fill.color)throw new Error("No fill color set. Call brush.fill(color) before drawing shapes.");vn=t,wn=Infinity,Rn=Infinity,En=-Infinity,An=-Infinity;for(const[e]of t.sides)e.x<wn&&(wn=e.x),e.x>En&&(En=e.x),e.y<Rn&&(Rn=e.y),e.y>An&&(An=e.y);const e=[...t.vertices],r=ct(0,75),n=~~(.25*e.length*(r<5?1:r<15?2:3)),i=P.fill.bleed_strength,a=e.map((t,e)=>(e>n?1:.3)*ct(.85,1.4)*i),o=null==P.fill.angle?ft(0,e.length):function(t,e){const r=St(e),n=r[0],i=-r[1];let a=0,o=Infinity;for(let e=0;e<t.length;e++){const r=t[e].x*n+t[e].y*i;r<o&&(o=r,a=e)}return a}(e,P.fill.angle),s=e.length,l=new Array(s);for(let t=0;t<s;t++)l[t]=e[(t+o)%s];const c=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 a=0;a<e;a++){const o=a+1<e?a+1:0,s=t[a].x,l=t[a].y,c=t[o].x,h=t[o].y,u=s*h-c*l;r+=u,n+=(s+c)*u,i+=(l+h)*u}return r*=.5,r?{x:n/(6*r),y:i/(6*r)}:{x:t[0].x,y:t[0].y}}(l);new Tn(l,a,c,[],!0).fill(P.fill.color,yt(P.fill.opacity,0,255,0,1,!0),P.fill.texture_strength)}(this)),(t=>{P.fill={...t}})(s),this},Zt.prototype.fill=function(t,e,r){yn().isActive&&(this.origin&&(t=this.origin[0],e=this.origin[1],r=1),this.pol=this.genPol(t,e,r,P.fill.bleed_strength<.06?0:yt(P.fill.bleed_strength,0,.6,.2,.6,!0)),this.pol.fill())},P.wash={color:null,opacity:150,isActive:!1},Kt.prototype.wash=function(t=!1,e){const i={...P.wash};return!1!==t&&Pn(t,e),P.wash.isActive&&function(t){if(!P.wash?.isActive||!P.wash.color||t.vertices.length<3)return;const e=!1!==F.isBrush;F.isBrush=!1,e&&(F.justChanged=!0),F.blend(P.wash.color);const i=c();F.ctx.save(),F.ctx.setTransform(a*i.a,a*i.b,a*i.c,a*i.d,a*(i.x+r/2),a*(i.y+n/2)),ln(t.vertices);const o=P.wash.opacity/255;F.ctx.fillStyle="rgb(255 0 0 / "+o+")",F.ctx.fill(),F.ctx.restore()}(this),P.wash={...i},this},Zt.prototype.wash=function(t,e,r){P.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())},Gn={load:function(t=H){if(!function(t){return function(t){return"undefined"!=typeof HTMLCanvasElement&&t instanceof HTMLCanvasElement}(t)||function(t){return"undefined"!=typeof OffscreenCanvas&&t instanceof OffscreenCanvas}(t)}(t))throw new Error("Standalone brush.load(target) requires an HTMLCanvasElement or OffscreenCanvas.");K(t,t.width,t.height,1)},syncDensity:function(){return d({Cwidth:q,Cheight:j,Density:$}),$},isCanvasReady:function(){if(!W)throw new Error("No standalone target loaded. Call brush.load(canvasOrOffscreenCanvas) first.")},instance:function(){},activateInstance:function(){},deactivateInstance:function(){},getActiveFramebuffer:function(){return null},isFramebufferTarget:function(){return!0===arguments[0]?.__brushFramebuffer}},f={...f,...Gn},z(),function(){var t;t={beginDirectMaskDraw:Cn,endDirectMaskDraw:Fn,resetDirectShaderTracking:kn},ue={...ue,...t}}(),function(){var t;t={clearTarget:In,ensureBlendShaderProgram:Dn,ensureBlendSourceFramebuffer:Nn,createFramebuffer:Ln,runBlendShaderPass:Bn,blitSourceToFramebuffer:Un},b={...b,...t}}(),function(){var t;t={createTipSurface:Yn,loadImageTip:zn},le={...le,...t}}(),t.Color=Y,t.DEGREES=D,t.Plot=Zt,t.Polygon=Kt,t.Position=Nt,t.RADIANS=L,t.add=lr,t.addField=$t,t.angleMode=function(t){if(t!==D&&t!==L)throw new Error(`Invalid angle mode "${t}". Use "degrees" or "radians".`);B=t},t.arc=Jt,t.beginShape=ie,t.beginStroke=function(t,e,r){if("curve"!==t&&"segments"!==t)throw new Error(`beginStroke() type must be "curve" or "segments", got "${t}".`);re=[e,r],ee=new Zt(t)},t.box=function(){return[...ir.keys()]},t.circle=function(t,e,r,n=!1){const i=new Zt("curve"),a=Math.PI*r,o=ht(0,360),s=n?()=>1+.2*n*ht():()=>1;for(let t=0;t<4;t++){const e=-90*t+o;i.addSegment(e*s(),a/2*s(),1,!0)}if(n){const t=n*dt(-5,5);i.addSegment(o,Math.abs(t)*(Math.PI/180)*r,1,!0),i.endPlot(t+o,1,!0)}else i.endPlot(o,1,!0);const l=t-r*At(o),c=e-r*Et(-o);return i.show(l,c,1),[i,l,c]},t.clear=function(...t){m(),F.glMask&&F.clearMask(F.glMask),F.mask&&F.clearMask(F.mask),F.justChanged=!1,F.isBlending=!1,F.isBrush=null,F.cachedColor=null;const e=i.drawingContext,r=0===t.length?[1,1,1,0]:[...l(...t)._array.slice(0,3),1];e.bindFramebuffer(e.FRAMEBUFFER,null),e.disable(e.SCISSOR_TEST),e.clearColor(r[0],r[1],r[2],r[3]),e.clear(e.COLOR_BUFFER_BIT|e.DEPTH_BUFFER_BIT)},t.clip=function(t){return m(),t},t.createCanvas=function(t,e,r={}){if("undefined"==typeof document)throw new Error("brush.createCanvas() requires a browser document.");const n=Math.max(1,Math.round(t)),i=Math.max(1,Math.round(e)),a=Math.max(1,Number(r.pixelDensity)||1),o=document.createElement("canvas");o.width=Math.max(1,Math.round(n*a)),o.height=Math.max(1,Math.round(i*a)),o.style.width=`${n}px`,o.style.height=`${i}px`,o.id=r.id??"brush-canvas";const s=void 0!==r.parent?r.parent:document.body;if(s){const t="string"==typeof s?document.querySelector(s):s;if(!t)throw new Error(`Could not find parent "${r.parent}" for brush.createCanvas().`);t.appendChild(o)}return K(o,n,i,a),o},t.endShape=oe,t.endStroke=function(t,e){if(!ee)throw new Error("endStroke() called without beginStroke(). Call beginStroke() first.");ee.endPlot(t,e),ee.draw(re[0],re[1],1),ee=!1},t.field=Wt,t.fill=pn,t.fillBleed=bn,t.fillTexture=_n,t.flowLine=function(t,e,r,n){if(!P.stroke.isActive||!P.stroke.color)throw new Error("No brush or color set. Call brush.set('brushName', color) before drawing.");Lt(),_r(t,e,r),wr(Tt(n),!1)},t.getAngleMode=function(){return B},t.hatch=Br,t.hatchArray=Wr,t.hatchStyle=function(t,e="black",r=1){P.hatch.hBrush={brush:t,color:e,weight:r}},t.initStandaloneRuntime=z,t.instance=t=>f.instance(t),t.line=Fr,t.listFields=function(){return Lt(),Array.from(zt.keys())},t.load=(t=!1)=>{((t=!1)=>{f.load(t)})(t),i.loaded&&F.load()},t.mass=function(t,e,r={}){P.mass.brush=t,P.mass.color=e,P.mass.options=r,P.mass.isActive=!0},t.massArray=function(t){return sn(t,!1)},t.move=function(t,e,r){if(!ee)throw new Error("move() called without beginStroke(). Call beginStroke() first.");ee.addSegment(t,e,r)},t.noClip=function(){},t.noField=function(){Lt(),P.field.isActive=!1},t.noFill=function(){P.fill.isActive=!1},t.noHatch=function(){P.hatch.isActive=!1,P.hatch.hBrush=!1},t.noMass=function(){P.mass.isActive=!1,P.mass.brush=null,P.mass.color=null,P.mass.options={}},t.noStroke=function(){P.stroke.isActive=!1},t.noWash=function(){P.wash.isActive=!1},t.noiseSeed=e=>{t.noise=rt(nt(e)),lt=rt(nt(`${e}:2`))},t.pick=hr,t.polygon=function(t){const e=new Kt(t);return e.show(),e},t.pop=function(){0!==U.length&&(O=U.pop(),function(t=P){const e=k.get(t)?.pop();e&&(P.stroke=e.stroke,P.field=e.field,P.hatch=e.hatch,P.fill=e.fill,e.wash&&(P.wash=e.wash),e.mass?P.mass=e.mass:P.mass&&(P.mass={...P.mass,isActive:!1,brush:null,color:null,options:{}}))}())},t.push=function(){!function(t=P){let e=k.get(t);e||(e=[],k.set(t,e)),e.push({fill:{...P.fill},wash:P.wash?{...P.wash}:null,stroke:{...P.stroke},hatch:{...P.hatch},mass:P.mass?{...P.mass}:null,field:{...P.field}})}(),U.push({...O})},t.random=function(t=0,e=1){return Array.isArray(t)?ut(t):1===arguments.length?at()*t:ht(t,e)},t.rect=function(t,e,r,n,i="corner"){"center"===i&&(t-=r/2,e-=n/2),ie(0),ae(t,e),ae(t+r,e),ae(t+r,e+n),ae(t,e+n),oe(!0)},t.refreshField=function(t=0){if(!P.field.isActive||!P.field.current)throw new Error("No field is currently active. Call brush.field('name') to activate one before refreshing.");const e=zt.get(P.field.current);e.field=Ht(e,t)},t.render=function(){G=!1,(F.isBlending||F.justChanged)&&(F.blend(!1,!0),F.clearMask(F.glMask),F.clearMask(F.mask),F.justChanged=!1,F.isBlending=!1,F.isBrush=null,F.cachedColor=null)},t.rotate=function(t){const e=B===L?t:t*Math.PI/180,r=Math.cos(e),n=Math.sin(e);O=X(O,{a:r,b:n,c:-n,d:r,x:0,y:0})},t.scale=function(t,e=t){O=X(O,{a:t,b:0,c:0,d:e,x:0,y:0})},t.scaleBrushes=function(t){for(const{param:e}of ir.values())e&&(e.weight*=t,e.scatter*=t,e.spacing*=t)},t.seed=t=>{it=nt(t),at=nt(`${t}:2`),mt=!1;for(const t of ot)t()},t.set=dr,t.spline=function(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=se(t,e);return r.show(),r},t.stroke=ur,t.strokeWeight=fr,t.translate=function(t,e){O=X(O,{a:1,b:0,c:0,d:1,x:t,y:e})},t.vertex=ae,t.wRand=t=>{let e=0;const r=[];for(const n in t)e+=t[n],r.push({key:n,cumulative:e});const n=it()*e;for(const{key:t,cumulative:e}of r)if(n<e)return isNaN(t)?t:parseInt(t)},t.wash=Pn,t.wiggle=qt},"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).brush={});
//# sourceMappingURL=brush.js.map