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