Files
boundless/public/painterly/vendor/p5.brush.js
T
吴璨 fad9760265 自部署多端同步:服务端、实时协作、口令登录与油画加载动画
- 新增 Node 服务端(Hono + Hocuspocus + node:sqlite),数据保存在服务器
- 基于 Yjs 的多端实时同步,本机 IndexedDB 离线缓存,撤销只针对本机操作
- 空间(标签)有独立地址,增删改通过接口同步到所有设备
- 口令登录(签名 Cookie 会话、登录限流、同源校验)
- 图片上传到服务器,过大照片上传前自动压缩
- 历史版本(自动/手动保存、一键恢复)、跨空间搜索、在线设备
- 随机油画加载动画(Painting Loaders,本地收录 p5 / p5.brush)
- 选中样式与拖拽性能优化:拖动结束后才同步位置
- Docker / docker-compose 部署,README 与 CLAUDE.md 更新
2026-10-21 18:09:03 +08:00

9 lines
70 KiB
JavaScript

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