Unlock custom brushes, natural fill effects and intuitive hatching in p5.js
Check
Alejandroee3c7163e0a8
diff --git a/.babelrc b/.babelrcnew file mode 100644index 0000000..ff3059c--- /dev/null+++ b/.babelrc@@ -0,0 +1,3 @@+{+ "presets": ["@babel/preset-env"]+}\ No newline at end of filediff --git a/example/index.html b/example/index.htmlindex e913cd1..626c0ec 100644--- a/example/index.html+++ b/example/index.html@@ -3,6 +3,7 @@<head><title>p5.brush.js Example</title><script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/p5.js"></script>+ <script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/p5.brush.min.js"></script><link rel="stylesheet" href="./style.css"></head><body>diff --git a/lib/p5.brush.min.js b/lib/p5.brush.min.jsnew file mode 100644index 0000000..377120b--- /dev/null+++ b/lib/p5.brush.min.js@@ -0,0 +1,2 @@+/*! For license information please see p5.brush.min.js.LICENSE.txt */+!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.$=e():t.$=e()}(self,(()=>(()=>{"use strict";var t={d:(e,i)=>{for(var s in i)t.o(i,s)&&!t.o(e,s)&&Object.defineProperty(e,s,{enumerable:!0,get:i[s]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},e={};t.r(e),t.d(e,{default:()=>Ct});var i={};let s;t.r(i),t.d(i,{Plot:()=>rt,Polygon:()=>et,Position:()=>D,add:()=>O,addField:()=>P,beginShape:()=>ht,beginStroke:()=>mt,bleed:()=>wt,box:()=>G,circle:()=>ot,clip:()=>q,colorCache:()=>b,config:()=>a,endShape:()=>ct,endStroke:()=>ut,erase:()=>gt,field:()=>S,fill:()=>yt,fillAnimatedMode:()=>At,fillTexture:()=>kt,flowLine:()=>U,hatch:()=>Q,hatchArray:()=>tt,line:()=>$,listFields:()=>T,load:()=>h,noClip:()=>W,noErase:()=>xt,noField:()=>C,noFill:()=>bt,noHatch:()=>Y,noStroke:()=>V,pick:()=>N,plot:()=>K,polygon:()=>it,pop:()=>g,preload:()=>c,push:()=>v,reBlend:()=>_,reDraw:()=>z,rect:()=>st,refreshField:()=>I,remove:()=>l,rotate:()=>y,scale:()=>k,scaleBrushes:()=>F,segment:()=>dt,set:()=>L,setHatch:()=>X,spline:()=>ft,stroke:()=>j,strokeWeight:()=>H,vertex:()=>lt});let r=!1,o=!1,n=!1;function a(t={}){t.R&&(d.source=t.R)}function h(t=!1){let e=!(!o||!t)&&n;r&&_remove(!1),!t&&o&&(t=n),s=t||window.self,r||(r=!0,B.create(),F(s.width/250)),A.load(e)}function l(t=!0){r&&(A.masks[0].remove(),A.masks[0]=null,A.masks[1].remove(),A.masks[1]=null,A.masks[2].remove(),A.masks[2]=null,t&&brush.load())}function c(){J.load()}function m(){r||h()}const d={source:()=>s.random(),random(t=0,e=1){return 1===arguments.length?this.map(this.source(),0,1,0,t):this.map(this.source(),0,1,t,e)},randInt(t,e){return Math.floor(this.random(t,e))},gaussian(t=0,e=1){const i=1-d.random(),s=d.random();return Math.sqrt(-2*Math.log(i))*Math.cos(2*Math.PI*s)*e+t},weightedRand(t){let e,i,s=[];for(e in t)for(i=0;i<10*t[e];i++)s.push(e);return s[Math.floor(this.source()*s.length)]},map(t,e,i,s,r,o=!1){let n=s+(t-e)/(i-e)*(r-s);return o?s<r?this.constrain(n,s,r):this.constrain(n,r,s):n},constrain:(t,e,i)=>Math.max(Math.min(t,i),e),cos(t){return this.c[Math.floor((t%360+360)%360*4)]},sin(t){return this.s[Math.floor((t%360+360)%360*4)]},isPrecalculationDone:!1,preCalculation(){if(this.isPrecalculationDone)return;const t=1440,e=2*Math.PI/t;this.c=new Float64Array(t),this.s=new Float64Array(t);for(let i=0;i<t;i++){const t=i*e;d.c[i]=Math.cos(t),d.s[i]=Math.sin(t)}this.isPrecalculationDone=!0},isNumber:t=>!isNaN(t),toDegrees:t=>(("radians"===s.angleMode()?180*t/Math.PI:t)%360+360)%360,dist:(t,e,i,s)=>Math.hypot(i-t,s-e)};function u(t,e,i,s,r=!1){let o=t.x,n=t.y,a=e.x,h=e.y,l=i.x,c=i.y,m=s.x,d=s.y;if(o===a&&n===h||l===m&&c===d)return!1;let u=a-o,p=h-n,f=m-l,v=d-c,g=v*u-f*p;if(0===g)return!1;let x=(f*(n-c)-v*(o-l))/g,y=(u*(n-c)-p*(o-l))/g;return!(!r&&(y<0||y>1))&&{x:o+x*u,y:n+x*p}}function p(t,e,i,s){return(Math.atan2(-(s-e),i-t)*(180/Math.PI)%360+360)%360}d.preCalculation();const f={field:{},stroke:{},hatch:{},fill:{},others:{}};function v(){f.field.isActive=B.isActive,f.field.current=B.current,f.stroke.isActive=R.isActive,f.stroke.brush=R.name,f.stroke.color=R.c,f.stroke.weight=R.w,f.stroke.clip=R.cr,f.hatch.isActive=Z.isActive,f.hatch.hatchingParams=Z.hatchingParams,f.hatch.hatchingBrush=Z.hatchingBrush,f.fill.isActive=zt.isActive,f.fill.color=zt.color,f.fill.opacity=zt.opacity,f.fill.bleed_strength=zt.bleed_strength,f.fill.texture_strength=zt.texture_strength,f.fill.border_strength=zt.border_strength,f.others.rotate=x.rotation}function g(){B.isActive=f.field.isActive,B.current=f.field.current,R.isActive=f.stroke.isActive,R.name=f.stroke.brush,R.c=f.stroke.color,R.w=f.stroke.weight,R.cr=f.stroke.clip,Z.isActive=f.hatch.isActive,Z.hatchingParams=f.hatch.hatchingParams,Z.hatchingBrush=f.hatch.hatchingBrush,zt.isActive=f.fill.isActive,zt.color=f.fill.color,zt.opacity=f.fill.opacity,zt.bleed_strength=f.fill.bleed_strength,zt.texture_strength=f.fill.texture_strength,zt.border_strength=f.fill.border_strength,x.rotation=f.others.rotate}const x={translation:[0,0],rotation:0,trans(){return this.translation=[s._renderer.uMVMatrix.mat4[12],s._renderer.uMVMatrix.mat4[13]],this.translation}};function y(t=0){if(!d.isNumber(t))return console.error("rotate(): Value must be numeric"),null;x.rotation=d.toDegrees(t)}let w=1;function k(t){w*=t}function b(t=!0){A.isCaching=t}const A={loaded:!1,isBlending:!1,isCaching:!0,currentColor:new Float32Array(3),load(t){switch(this.type=o&&!t?0:t?2:1,this.masks=[],this.type){case 0:this.masks[0]=s.createGraphics(s.width,s.height),this.masks[1]=s.createGraphics(s.width,s.height,s.WEBGL),this.masks[2]=s.createGraphics(s.width,s.height);break;case 1:this.masks[0]=createGraphics(s.width,s.height),this.masks[1]=createGraphics(s.width,s.height,WEBGL),this.masks[2]=createGraphics(s.width,s.height);break;case 2:this.masks[0]=t.createGraphics(t.width,t.height),this.masks[1]=t.createGraphics(t.width,t.height,t.WEBGL),this.masks[2]=t.createGraphics(t.width,t.height)}for(let t of this.masks)t.pixelDensity(s.pixelDensity()),t.clear(),t.angleMode(s.DEGREES),t.noSmooth();this.shader=s.createShader(this.vert,this.frag),A.loaded=!0},getPigment(t){let e=t.levels,i=new Float32Array(3);return i[0]=e[0]/255,i[1]=e[1]/255,i[2]=e[2]/255,i},color1:new Float32Array(3),color2:new Float32Array(3),blending1:!1,blending2:!1,blend(t=!1,e=!1,i=!1){if(this.isBlending=i?this.blending1:this.blending2,this.currentColor=i?this.color1:this.color2,!this.isBlending)if(t)this.currentColor=this.getPigment(t),i?(this.blending1=!0,this.color1=this.currentColor):(this.blending2=!0,this.color2=this.currentColor);else if(e)return void(i||this.reDraw());if((t?this.getPigment(t):this.currentColor).toString()!==this.currentColor.toString()||e||!this.isCaching){if(this.reDraw(),this.isBlending){s.push(),s.translate(-x.trans()[0],-x.trans()[1]),s.shader(this.shader),this.shader.setUniform("addColor",this.currentColor),this.shader.setUniform("source",s._renderer),this.shader.setUniform("active",A.watercolor),this.shader.setUniform("random",[d.random(),d.random(),d.random()]);let t=i?this.masks[1]:this.masks[0];this.shader.setUniform("mask",t),s.fill(0,0,0,0),s.noStroke(),s.rect(-s.width/2,-s.height/2,s.width,s.height),s.pop(),t.clear()}e||(this.currentColor=this.getPigment(t),i?this.color1=this.currentColor:this.color2=this.currentColor)}e&&(this.isBlending=!1,i?this.blending1=this.isBlending:this.blending2=this.isBlending)},vert:"precision highp float;attribute vec3 aPosition;attribute vec2 aTexCoord;uniform mat4 uModelViewMatrix,uProjectionMatrix;varying vec2 vVertTexCoord;void main(){gl_Position=uProjectionMatrix*uModelViewMatrix*vec4(aPosition,1);vVertTexCoord=aTexCoord;}",frag:"precision highp float;varying vec2 vVertTexCoord;uniform sampler2D source,mask;uniform vec4 addColor;uniform vec3 random;uniform bool active;\n #ifndef SPECTRAL\n #define SPECTRAL\n float x(float v){return v<.04045?v/12.92:pow((v+.055)/1.055,2.4);}float v(float v){return v<.0031308?v*12.92:1.055*pow(v,1./2.4)-.055;}vec3 m(vec3 v){return vec3(x(v[0]),x(v[1]),x(v[2]));}vec3 f(vec3 f){return clamp(vec3(v(f[0]),v(f[1]),v(f[2])),0.,1.);}void f(vec3 v,out float m,out float f,out float x,out float y,out float z,out float i,out float r){m=min(v.x,min(v.y,v.z));v-=m;f=min(v.y,v.z);x=min(v.x,v.z);y=min(v.x,v.y);z=min(max(0.,v.x-v.z),max(0.,v.x-v.y));i=min(max(0.,v.y-v.z),max(0.,v.y-v.x));r=min(max(0.,v.z-v.y),max(0.,v.z-v.x));}void f(vec3 v,inout float i[38]){float x,y,d,z,o,m,e;f(v,x,y,d,z,o,m,e);i[0]=max(1e-4,x+y*.96853629+d*.51567122+z*.02055257+o*.03147571+m*.49108579+e*.97901834);i[1]=max(1e-4,x+y*.96855103+d*.5401552+z*.02059936+o*.03146636+m*.46944057+e*.97901649);i[2]=max(1e-4,x+y*.96859338+d*.62645502+z*.02062723+o*.03140624+m*.4016578+e*.97901118);i[3]=max(1e-4,x+y*.96877345+d*.75595012+z*.02073387+o*.03119611+m*.2449042+e*.97892146);i[4]=max(1e-4,x+y*.96942204+d*.92826996+z*.02114202+o*.03053888+m*.0682688+e*.97858555);i[5]=max(1e-4,x+y*.97143709+d*.97223624+z*.02233154+o*.02856855+m*.02732883+e*.97743705);i[6]=max(1e-4,x+y*.97541862+d*.98616174+z*.02556857+o*.02459485+m*.013606+e*.97428075);i[7]=max(1e-4,x+y*.98074186+d*.98955255+z*.03330189+o*.0192952+m*.01000187+e*.96663223);i[8]=max(1e-4,x+y*.98580992+d*.98676237+z*.05185294+o*.01423112+m*.01284127+e*.94822893);i[9]=max(1e-4,x+y*.98971194+d*.97312575+z*.10087639+o*.01033111+m*.02636635+e*.89937713);i[10]=max(1e-4,x+y*.99238027+d*.91944277+z*.24000413+o*.00765876+m*.07058713+e*.76070164);i[11]=max(1e-4,x+y*.99409844+d*.32564851+z*.53589066+o*.00593693+m*.70421692+e*.4642044);i[12]=max(1e-4,x+y*.995172+d*.13820628+z*.79874659+o*.00485616+m*.85473994+e*.20123039);i[13]=max(1e-4,x+y*.99576545+d*.05015143+z*.91186529+o*.00426186+m*.95081565+e*.08808402);i[14]=max(1e-4,x+y*.99593552+d*.02912336+z*.95399623+o*.00409039+m*.9717037+e*.04592894);i[15]=max(1e-4,x+y*.99564041+d*.02421691+z*.97137099+o*.00438375+m*.97651888+e*.02860373);i[16]=max(1e-4,x+y*.99464769+d*.02660696+z*.97939505+o*.00537525+m*.97429245+e*.02060067);i[17]=max(1e-4,x+y*.99229579+d*.03407586+z*.98345207+o*.00772962+m*.97012917+e*.01656701);i[18]=max(1e-4,x+y*.98638762+d*.04835936+z*.98553736+o*.0136612+m*.9425863+e*.01451549);i[19]=max(1e-4,x+y*.96829712+d*.0001172+z*.98648905+o*.03181352+m*.99989207+e*.01357964);i[20]=max(1e-4,x+y*.89228016+d*8.554e-5+z*.98674535+o*.10791525+m*.99989891+e*.01331243);i[21]=max(1e-4,x+y*.53740239+d*.85267882+z*.98657555+o*.46249516+m*.13823139+e*.01347661);i[22]=max(1e-4,x+y*.15360445+d*.93188793+z*.98611877+o*.84604333+m*.06968113+e*.01387181);i[23]=max(1e-4,x+y*.05705719+d*.94810268+z*.98559942+o*.94275572+m*.05628787+e*.01435472);i[24]=max(1e-4,x+y*.03126539+d*.94200977+z*.98507063+o*.96860996+m*.06111561+e*.01479836);i[25]=max(1e-4,x+y*.02205445+d*.91478045+z*.98460039+o*.97783966+m*.08987709+e*.0151525);i[26]=max(1e-4,x+y*.01802271+d*.87065445+z*.98425301+o*.98187757+m*.13656016+e*.01540513);i[27]=max(1e-4,x+y*.0161346+d*.78827548+z*.98403909+o*.98377315+m*.22169624+e*.01557233);i[28]=max(1e-4,x+y*.01520947+d*.65738359+z*.98388535+o*.98470202+m*.32176956+e*.0156571);i[29]=max(1e-4,x+y*.01475977+d*.59909403+z*.98376116+o*.98515481+m*.36157329+e*.01571025);i[30]=max(1e-4,x+y*.01454263+d*.56817268+z*.98368246+o*.98537114+m*.4836192+e*.01571916);i[31]=max(1e-4,x+y*.01444459+d*.54031997+z*.98365023+o*.98546685+m*.46488579+e*.01572133);i[32]=max(1e-4,x+y*.01439897+d*.52110241+z*.98361309+o*.98550011+m*.47440306+e*.01572502);i[33]=max(1e-4,x+y*.0143762+d*.51041094+z*.98357259+o*.98551031+m*.4857699+e*.01571717);i[34]=max(1e-4,x+y*.01436343+d*.50526577+z*.98353856+o*.98550741+m*.49267971+e*.01571905);i[35]=max(1e-4,x+y*.01435687+d*.5025508+z*.98351247+o*.98551323+m*.49625685+e*.01571059);i[36]=max(1e-4,x+y*.0143537+d*.50126452+z*.98350101+o*.98551563+m*.49807754+e*.01569728);i[37]=max(1e-4,x+y*.01435408+d*.50083021+z*.98350852+o*.98551547+m*.49889859+e*.0157002);}vec3 t(vec3 x){mat3 i;i[0]=vec3(3.24306333,-1.53837619,-.49893282);i[1]=vec3(-.96896309,1.87542451,.04154303);i[2]=vec3(.05568392,-.20417438,1.05799454);float v=dot(i[0],x),y=dot(i[1],x),o=dot(i[2],x);return f(vec3(v,y,o));}vec3 d(float m[38]){vec3 i=vec3(0);i+=m[0]*vec3(6.469e-5,1.84e-6,.00030502);i+=m[1]*vec3(.00021941,6.21e-6,.00103681);i+=m[2]*vec3(.00112057,3.101e-5,.00531314);i+=m[3]*vec3(.00376661,.00010475,.01795439);i+=m[4]*vec3(.01188055,.00035364,.05707758);i+=m[5]*vec3(.02328644,.00095147,.11365162);i+=m[6]*vec3(.03455942,.00228226,.17335873);i+=m[7]*vec3(.03722379,.00420733,.19620658);i+=m[8]*vec3(.03241838,.0066888,.18608237);i+=m[9]*vec3(.02123321,.0098884,.13995048);i+=m[10]*vec3(.01049099,.01524945,.08917453);i+=m[11]*vec3(.00329584,.02141831,.04789621);i+=m[12]*vec3(.00050704,.03342293,.02814563);i+=m[13]*vec3(.00094867,.05131001,.01613766);i+=m[14]*vec3(.00627372,.07040208,.0077591);i+=m[15]*vec3(.01686462,.08783871,.00429615);i+=m[16]*vec3(.02868965,.09424905,.00200551);i+=m[17]*vec3(.04267481,.09795667,.00086147);i+=m[18]*vec3(.05625475,.09415219,.00036904);i+=m[19]*vec3(.0694704,.08678102,.00019143);i+=m[20]*vec3(.08305315,.07885653,.00014956);i+=m[21]*vec3(.0861261,.0635267,9.231e-5);i+=m[22]*vec3(.09046614,.05374142,6.813e-5);i+=m[23]*vec3(.08500387,.04264606,2.883e-5);i+=m[24]*vec3(.07090667,.03161735,1.577e-5);i+=m[25]*vec3(.05062889,.02088521,3.94e-6);i+=m[26]*vec3(.03547396,.01386011,1.58e-6);i+=m[27]*vec3(.02146821,.00810264,0);i+=m[28]*vec3(.01251646,.0046301,0);i+=m[29]*vec3(.00680458,.00249138,0);i+=m[30]*vec3(.00346457,.0012593,0);i+=m[31]*vec3(.00149761,.00054165,0);i+=m[32]*vec3(.0007697,.00027795,0);i+=m[33]*vec3(.00040737,.00014711,0);i+=m[34]*vec3(.00016901,6.103e-5,0);i+=m[35]*vec3(9.522e-5,3.439e-5,0);i+=m[36]*vec3(4.903e-5,1.771e-5,0);i+=m[37]*vec3(2e-5,7.22e-6,0);return i;}float d(float y,float m,float v){float z=m*pow(v,2.);return z/(y*pow(1.-v,2.)+z);}vec3 f(vec3 v,vec3 y,float z){vec3 x=m(v),o=m(y);float i[38],a[38];f(x,i);f(o,a);float r=d(i)[1],e=d(a)[1];z=d(r,e,z);float s[38];for(int u=0;u<38;u++){float p=(1.-z)*(pow(1.-i[u],2.)/(2.*i[u]))+z*(pow(1.-a[u],2.)/(2.*a[u]));s[u]=1.+p-sqrt(pow(p,2.)+2.*p);}return t(d(s));}vec4 f(vec4 v,vec4 x,float y){return vec4(f(v.xyz,x.xyz,y),mix(v.w,x.w,y));}\n #endif\n float d(vec2 m,vec2 v,float y,out vec2 i){vec2 f=vec2(m.x+m.y*.5,m.y),x=floor(f),o=fract(f);float z=step(o.y,o.x);vec2 d=vec2(z,1.-z),r=x+d,e=x+1.,a=vec2(x.x-x.y*.5,x.y),p=vec2(a.x+d.x-d.y*.5,a.y+d.y),s=vec2(a.x+.5,a.y+1.),w=m-a,g=m-p,k=m-s;vec3 u,c,t,A;if(any(greaterThan(v,vec2(0)))){t=vec3(a.x,p.x,s);A=vec3(a.y,p.y,s.y);if(v.x>0.)t=mod(vec3(a.x,p.x,s),v.x);if(v.y>0.)A=mod(vec3(a.y,p.y,s.y),v.y);u=floor(t+.5*A+.5);c=floor(A+.5);}else u=vec3(x.x,r.x,e),c=vec3(x.y,r.y,e.y);vec3 S=mod(u,289.);S=mod((S*51.+2.)*S+c,289.);S=mod((S*34.+10.)*S,289.);vec3 b=S*.07482+y,C=cos(b),D=sin(b);vec2 h=vec2(C.x,D),B=vec2(C.y,D.y),E=vec2(C.z,D.z);vec3 F=.8-vec3(dot(w,w),dot(g,g),dot(k,k));F=max(F,0.);vec3 G=F*F,H=G*G,I=vec3(dot(h,w),dot(B,g),dot(E,k)),J=G*F,K=-8.*J*I;i=10.9*(H.x*h+K.x*w+(H.y*B+K.y*g)+(H.z*E+K.z*k));return 10.9*dot(H,I);}vec4 d(vec3 v,float x){return vec4(mix(v,vec3(dot(vec3(.299,.587,.114),v)),x),1);}float f(vec2 v,float x,float y,float f){return fract(sin(dot(v,vec2(x,y)))*f);}void main(){vec4 v=texture2D(mask,vVertTexCoord);if(v.x>0.){vec2 x=vec2(12.9898,78.233),o=vec2(7.9898,58.233),m=vec2(17.9898,3.233);float y=f(vVertTexCoord,x.x,x.y,43358.5453)*2.-1.,z=f(vVertTexCoord,o.x,o.y,43213.5453)*2.-1.,e=f(vVertTexCoord,m.x,m.y,33358.5453)*2.-1.;const vec2 i=vec2(0);vec2 s;vec4 r;if(active){float a=d(vVertTexCoord*5.,i,10.*random.x,s),p=d(vVertTexCoord*5.,i,10.*random.y,s),g=d(vVertTexCoord*5.,i,10.*random.z,s),k=.25+.25*d(vVertTexCoord*4.,i,3.*random.x,s);r=vec4(d(addColor.xyz,k).xyz+vec3(a,p,g)*.03*abs(addColor.x-addColor.y-addColor.z),1);}else r=vec4(addColor.xyz,1);if(v.w>.7){float a=.5*(v.w-.7);r=r*(1.-a)-vec4(.5)*a;}vec3 a=f(texture2D(source,vVertTexCoord).xyz,r.xyz,.9*v.w);gl_FragColor=vec4(a+.01*vec3(y,z,e),1);}}"};function z(){s.push(),s.translate(-x.trans()[0],-x.trans()[1]),s.image(A.masks[2],-s.width/2,-s.height/2),A.masks[2].clear(),s.pop()}function _(){v(),L("marker","white",1),$(-10,-10,-5,-5),g()}function M(t){t.registerMethod("afterSetup",(()=>A.blend(!1,!0))),t.registerMethod("afterSetup",(()=>A.blend(!1,!0,!0))),t.registerMethod("post",(()=>A.blend(!1,!0))),t.registerMethod("post",(()=>A.blend(!1,!0,!0)))}function S(t){if(m(),!B.list.has(t))return console.error("field(): No VectorField with that name"),null;B.isActive=!0,B.current=t}function C(){B.isActive=!1}function P(t,e){m(),B.list.set(t,{gen:e}),B.current=t,B.refresh()}function I(t){if(!d.isNumber(t))return console.error("refreshField(): Value for refreshField must be numeric"),null;B.refresh(t)}function T(){return Array.from(B.list.keys())}"undefined"!=typeof p5&&M(p5.prototype),exports.instance=function(t){o=!0,n=t,M(t)};const B={isActive:!1,list:new Map,current:"",step_length:()=>Math.min(s.width,s.height)/1e3,create(){this.R=.01*s.width,this.left_x=-1*s.width,this.top_y=-1*s.height,this.num_columns=Math.round(2*s.width/this.R),this.num_rows=Math.round(2*s.height/this.R),this.addStandard()},flow_field(){return this.list.get(this.current).field},refresh(t=0){this.list.get(this.current).field=this.list.get(this.current).gen(t,this.genField())},genField(){let t=new Array(this.num_columns);for(let e=0;e<this.num_columns;e++)t[e]=new Float64Array(this.num_rows);return t},addStandard(){P("curved",(function(t,e){let i=d.randInt(-25,-15);d.randInt(0,100)%2==0&&(i*=-1);for(let r=0;r<B.num_columns;r++)for(let o=0;o<B.num_rows;o++){let n=s.noise(.02*r+.03*t,.02*o+.03*t),a=d.map(n,0,1,-i,i);e[r][o]=3*a}return e})),P("truncated",(function(t,e){let i=d.randInt(-25,-15)+5*d.sin(t);d.randInt(0,100)%2==0&&(i*=-1);let r=d.randInt(5,10);for(let t=0;t<B.num_columns;t++)for(let o=0;o<B.num_rows;o++){let n=s.noise(.02*t,.02*o),a=Math.round(d.map(n,0,1,-i,i)/r)*r;e[t][o]=4*a}return e})),P("zigzag",(function(t,e){let i=d.randInt(-30,-15)+Math.abs(44*d.sin(t));d.randInt(0,100)%2==0&&(i*=-1);let s=i,r=0;for(let t=0;t<B.num_columns;t++){for(let i=0;i<B.num_rows;i++)e[t][i]=r,r+=s,s*=-1;r+=s,s*=-1}return e})),P("waves",(function(t,e){let i=d.randInt(10,15)+5*d.sin(t),s=d.randInt(3,6)+3*d.cos(t),r=d.randInt(20,35);for(let t=0;t<B.num_columns;t++)for(let o=0;o<B.num_rows;o++){let n=d.sin(i*t)*(r*d.cos(o*s))+d.randInt(-3,3);e[t][o]=n}return e})),P("seabed",(function(t,e){let i=d.random(.4,.8),s=d.randInt(18,26);for(let r=0;r<B.num_columns;r++)for(let o=0;o<B.num_rows;o++){let n=d.randInt(15,20),a=s*d.sin(i*o*r+n);e[r][o]=1.1*a*d.cos(t)}return e}))}};class D{constructor(t,e){this.update(t,e),this.plotted=0}update(t,e){this.x=t,this.y=e,B.isActive&&(this.x_offset=this.x-B.left_x+x.trans()[0],this.y_offset=this.y-B.top_y+x.trans()[1],this.column_index=Math.round(this.x_offset/B.R),this.row_index=Math.round(this.y_offset/B.R))}reset(){this.plotted=0}isIn(){return B.isActive?this.column_index>=0&&this.row_index>=0&&this.column_index<B.num_columns&&this.row_index<B.num_rows:this.isInCanvas()}isInCanvas(){let t=s.width,e=s.height;return this.x>=-t-x.trans()[0]&&this.x<=t-x.trans()[0]&&this.y>=-e-x.trans()[1]&&this.y<=e-x.trans()[1]}angle(){return this.isIn()&&B.isActive?B.flow_field()[this.column_index][this.row_index]:0}moveTo(t,e,i=B.step_length(),s=!0){if(this.isIn()){let r,o;s||(r=d.cos(-e),o=d.sin(-e));for(let n=0;n<t/i;n++){if(s){let t=this.angle();r=d.cos(t-e),o=d.sin(t-e)}let t=i*r,n=i*o;this.plotted+=i,this.update(this.x+t,this.y+n)}}else this.plotted+=i}plotTo(t,e,i,s){if(this.isIn()){const r=1/s;for(let s=0;s<e/i;s++){let e=this.angle(),s=t.angle(this.plotted),o=i*d.cos(e-s),n=i*d.sin(e-s);this.plotted+=i*r,this.update(this.x+o,this.y+n)}}else this.plotted+=i/k}}function F(t){for(let e of St){let i=R.list.get(e[0]).param;i.weight*=t,i.vibration*=t,i.spacing*=t}E=t}let E=1;function V(){R.isActive=!1}function G(){return Array.from(R.list.keys())}const R={isActive:!0,list:new Map,c:"#000000",w:1,cr:null,name:"HB",spacing(){return this.p=this.list.get(this.name).param,"default"===this.p.type||"spray"===this.p.type?this.p.spacing/this.w:this.p.spacing},initializeDrawingState(t,e,i,s,r){this.position=new D(t,e),this.length=i,this.flow=s,this.plot=r,r&&r.calcIndex(0)},draw(t,e){e||(this.dir=t),this.pushState();const i=this.spacing(),s=e?Math.round(this.length*t/i):Math.round(this.length/i);for(let r=0;r<s;r++)this.tip(),e?this.position.plotTo(this.plot,i,i,t):this.position.moveTo(i,t,i,this.flow);this.popState()},drawTip(t){this.pushState(!0),this.tip(t),this.popState(!0)},pushState(t=!1){if(this.p=this.list.get(this.name).param,!t){this.a="custom"!==this.p.pressure.type?d.random(-1,1):0,this.b="custom"!==this.p.pressure.type?d.random(1,1.5):0,this.cp="custom"!==this.p.pressure.type?d.random(3,3.5):d.random(-.2,.2);const[t,e]=this.p.pressure.min_max;this.min=t,this.max=e}this.c=s.color(this.c),this.mask=this.p.blend?"image"===this.p.type?A.masks[1]:A.masks[0]:A.masks[2],x.trans(),this.mask.push(),this.mask.noStroke(),"image"===this.p.type?this.mask.translate(x.translation[0],x.translation[1]):this.mask.translate(x.translation[0]+s.width/2,x.translation[1]+s.height/2),this.mask.rotate(-x.rotation),this.mask.scale(w),this.p.blend&&(A.watercolor=!1,"image"!==this.p.type?A.blend(this.c):A.blend(this.c,!1,!0),t||this.markerTip()),this.alpha=this.calculateAlpha(),this.applyColor(this.alpha)},popState(t=!1){this.p.blend&&!t&&this.markerTip(),this.mask.pop()},tip(t=!1){let e=t||this.calculatePressure();if(this.isInsideClippingArea())switch(this.p.type){case"spray":this.drawSpray(e);break;case"marker":this.drawMarker(e);break;case"custom":case"image":this.drawCustomOrImage(e,this.alpha);break;default:this.drawDefault(e)}},calculatePressure(){return this.plot?this.simPressure()*this.plot.pressure(this.position.plotted):this.simPressure()},simPressure(){return"custom"===this.p.pressure.type?d.map(this.p.pressure.curve(this.position.plotted/this.length)+this.cp,0,1,this.min,this.max,!0):this.gauss()},gauss(t=.5+R.p.pressure.curve[0]*R.a,e=1-R.p.pressure.curve[1]*R.b,i=R.cp,s=R.min,r=R.max){return d.map(1/(1+Math.pow(Math.abs((this.position.plotted-t*this.length)/(e*this.length/2)),2*i)),0,1,s,r)},calculateAlpha(){return"default"!==this.p.type&&"spray"!==this.p.type?this.p.opacity/this.w:this.p.opacity},applyColor(t){this.p.blend?this.mask.fill(255,0,0,t/2):(this.c.setAlpha(t),this.mask.fill(this.c))},isInsideClippingArea(){if(R.cr)return this.position.x>=R.cr[0]&&this.position.x<=R.cr[2]&&this.position.y>=R.cr[1]&&this.position.y<=R.cr[3];{let t=.55*s.width,e=.55*s.height;return this.position.x>=-t-x.trans()[0]&&this.position.x<=t-x.trans()[0]&&this.position.y>=-e-x.trans()[1]&&this.position.y<=e-x.trans()[1]}},drawSpray(t){let e=this.w*this.p.vibration*t+this.w*d.gaussian()*this.p.vibration/3,i=this.p.weight*d.random(.9,1.1);const s=this.p.quality/t;for(let t=0;t<s;t++){let t=d.random(.9,1.1),s=t*e*d.random(-1,1),r=d.random(-1,1),o=Math.pow(t*e,2),n=Math.sqrt(o-Math.pow(s,2));this.mask.circle(this.position.x+s,this.position.y+r*n,i)}},drawMarker(t,e=!0){let i=e?this.w*this.p.vibration:0,s=e?i*d.random(-1,1):0,r=e?i*d.random(-1,1):0;this.mask.circle(this.position.x+s,this.position.y+r,this.w*this.p.weight*t)},drawCustomOrImage(t,e,i=!0){this.mask.push();let s=i?this.w*this.p.vibration:0,r=i?s*d.random(-1,1):0,o=i?s*d.random(-1,1):0;this.mask.translate(this.position.x+r,this.position.y+o),this.adjustSizeAndRotation(this.w*t,e),this.p.tip(this.mask),this.mask.pop()},drawDefault(t){let e=this.w*this.p.vibration*(this.p.definition+(1-this.p.definition)*d.gaussian()*this.gauss(.5,.9,5,.2,1.2)/t);d.random(0,this.p.quality*t)>.4&&this.mask.circle(this.position.x+.7*e*d.random(-1,1),this.position.y+e*d.random(-1,1),t*this.p.weight*d.random(.85,1.15))},adjustSizeAndRotation(t,e){if(this.mask.scale(t),"image"===this.p.type&&(this.p.blend?this.mask.tint(255,0,0,e/2):this.mask.tint(this.mask.red(this.c),this.mask.green(this.c),this.mask.blue(this.c),e)),"random"===this.p.rotate)this.mask.rotate(d.randInt(0,360));else if("natural"===this.p.rotate){let t=(this.plot?-this.plot.angle(this.position.plotted):-this.dir)+(this.flow?this.position.angle():0);this.mask.rotate(t)}},markerTip(){if(this.isInsideClippingArea()){let t=this.calculatePressure(),e=this.calculateAlpha(t);if(this.mask.fill(255,0,0,e/1.5),"marker"===R.p.type)for(let e=1;e<5;e++)this.drawMarker(t*e/5,!1);else if("custom"===R.p.type||"image"===R.p.type)for(let i=1;i<5;i++)this.drawCustomOrImage(t*i/5,e,!1)}}};function O(t,e){const i="marker"===e.type||"custom"===e.type||"image"===e.type;i||"spray"===e.type||(e.type="default"),"image"===e.type&&(J.add(e.image.src),e.tip=()=>R.mask.image(J.tips.get(R.p.image.src),-R.p.weight/2,-R.p.weight/2,R.p.weight,R.p.weight)),e.blend=!!(i&&!1!==e.blend||e.blend),R.list.set(t,{param:e,colors:[],buffers:[]})}function L(t,e,i=1){N(t),R.c=e,R.w=i,R.isActive=!0}function N(t){if(!R.list.has(t))return console.error("set() / setBrush() / setHatch():\n No brush with that name"),null;R.name=t}function j(t,e,i){arguments.length>0&&(R.c=arguments.length<2?t:[t,e,i]),R.isActive=!0}function H(t){R.w=t}function q(t){R.cr=t}function W(){R.cr=null}function $(t,e,i,s){m();let r=d.dist(t,e,i,s);if(0==r)return;R.initializeDrawingState(t,e,r,!1,!1);let o=p(t,e,i,s);R.draw(o,!1)}function U(t,e,i,s){m(),R.initializeDrawingState(t,e,i,!0,!1),R.draw(d.toDegrees(s),!1)}function K(t,e,i,s){m(),R.initializeDrawingState(e,i,t.length,!0,t),R.draw(s,!0)}const J={tips:new Map,add(t){this.tips.set(t,!1)},imageToWhite(t){t.loadPixels();for(let e=0;e<4*t.width*t.height;e+=4){let i=(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-i}t.updatePixels()},load(){for(let t of this.tips.keys()){let e=loadImage(t,(()=>J.imageToWhite(e)));this.tips.set(t,e)}}};function Q(t=5,e=45,i={rand:!1,continuous:!1,gradient:!1}){Z.isActive=!0,Z.hatchingParams=[t,e,i]}function X(t,e="black",i=1){Z.hatchingBrush=[t,e,i]}function Y(){Z.isActive=!1,Z.hatchingBrush=!1}const Z={isActive:!1,hatchingParams:[5,45,{}],hatchingBrush:!1,hatch(t){let e=Z.hatchingParams[0],i=Z.hatchingParams[1],s=Z.hatchingParams[2],r=R.c,o=R.name,n=R.w,a=R.isActive;Z.hatchingBrush&&L(Z.hatchingBrush[0],Z.hatchingBrush[1],Z.hatchingBrush[2]),i=d.toDegrees(i)%180;let h=1/0,l=-1/0,c=1/0,m=-1/0,u=t=>{for(let e of t.a)h=e[0]<h?e[0]:h,l=e[0]>l?e[0]:l,c=e[1]<c?e[1]:c,m=e[1]>m?e[1]:m};Array.isArray(t)||(t=[t]);for(let e of t)u(e);let p=new et([[h,c],[l,c],[l,m],[h,m]]),f=i<=90&&i>=0?c:m,v=s.gradient?d.map(s.gradient,0,1,1,1.1,!0):1,g=[],x=0,y=e,w=t=>({point1:{x:h+y*t*d.cos(90-i),y:f+y*t*d.sin(90-i)},point2:{x:h+y*t*d.cos(90-i)+d.cos(-i),y:f+y*t*d.sin(90-i)+d.sin(-i)}});for(;p.intersect(w(x)).length>0;){let e=[];for(let i of t)e.push(i.intersect(w(x)));g[x]=e.flat().sort(((t,e)=>t.x===e.x?t.y-e.y:t.x-e.x)),y*=v,x++}let k=[];for(let t of g)void 0!==t[0]&&k.push(t);let b=s.rand?s.rand:0;for(let t=0;t<k.length;t++){let i=k[t],r=t>0&&s.continuous;for(let s=0;s<i.length-1;s+=2)0!==b&&(i[s].x+=b*e*d.random(-10,10),i[s].y+=b*e*d.random(-10,10),i[s+1].x+=b*e*d.random(-10,10),i[s+1].y+=b*e*d.random(-10,10)),$(i[s].x,i[s].y,i[s+1].x,i[s+1].y),r&&$(k[t-1][1].x,k[t-1][1].y,i[s].x,i[s].y)}L(o,r,n),R.isActive=a}},tt=Z.hatch;class et{constructor(t,e=!1){this.a=t,this.vertices=t.map((t=>({x:t[0],y:t[1]}))),e&&(this.vertices=t),this.sides=this.vertices.map(((t,e,i)=>[t,i[(e+1)%i.length]]))}intersect(t){let e=`${t.point1.x},${t.point1.y}-${t.point2.x},${t.point2.y}`;if(this._intersectionCache&&this._intersectionCache[e])return this._intersectionCache[e];let i=[];for(let e of this.sides){let s=u(t.point1,t.point2,e[0],e[1]);!1!==s&&i.push(s)}return this._intersectionCache||(this._intersectionCache={}),this._intersectionCache[e]=i,i}draw(t=!1,e,i){let s=R.isActive;if(t&&L(t,e,i),R.isActive){m();for(let t of this.sides)$(t[0].x,t[0].y,t[1].x,t[1].y)}R.isActive=s}fill(t=!1,e,i,s,r,o){let n=zt.isActive;t&&(yt(t,e),wt(i,o),kt(s,r)),zt.isActive&&(m(),zt.fill(this)),zt.isActive=n}hatch(t=!1,e,i){let s=Z.isActive;t&&Q(t,e,i),Z.isActive&&(m(),Z.hatch(this)),Z.isActive=s}erase(t=!1,e=vt.a){if(vt.isActive||t){A.masks[2].push(),A.masks[2].noStroke();let i=s.color(t||vt.c);i.setAlpha(e),A.masks[2].fill(i),A.masks[2].beginShape();for(let t of this.vertices)A.masks[2].vertex(t.x,t.y);A.masks[2].endShape(s.CLOSE),A.masks[2].pop()}}}function it(t){if(!Array.isArray(t)||t.length<3)return void console.error("Invalid input for polygon: An array with at least 3 points is required.");let e=new et(t);e.fill(),e.hatch(),e.draw()}function st(t,e,i,r,o=s.CORNER){if(o==s.CENTER&&(t-=i/2,e-=r/2),B.isActive)ht(0),lt(t,e),lt(t+i,e),lt(t+i,e+r),lt(t,e+r),ct(s.CLOSE);else{let s=new et([[t,e],[t+i,e],[t+i,e+r],[t,e+r]]);s.fill(),s.hatch(),s.draw(),s.erase()}}class rt{constructor(t){this.segments=[],this.angles=[],this.pres=[],this.type=t,this.dir=0,this.calcIndex(0),this.pol=!1}addSegment(t=0,e=0,i=1,s=!1){this.angles.length>0&&this.angles.splice(-1),t=s?(t%360+360)%360:d.toDegrees(t),this.angles.push(t),this.pres.push(i),this.segments.push(e),this.length=this.segments.reduce(((t,e)=>t+e),0),this.angles.push(t)}endPlot(t=0,e=1,i=!1){t=i?(t%360+360)%360:d.toDegrees(t),this.angles.splice(-1),this.angles.push(t),this.pres.push(e)}rotate(t){this.dir=d.toDegrees(t)}pressure(t){return t>this.length?this.pres[this.pres.length-1]:this.curving(this.pres,t)}angle(t){return t>this.length?this.angles[this.angles.length-1]:(this.calcIndex(t),"curve"===this.type?this.curving(this.angles,t)+this.dir:this.angles[this.index]+this.dir)}curving(t,e){let i=t[this.index],s=t[this.index+1];return void 0===s&&(s=i),Math.abs(s-i)>180&&(s>i?s=-(360-s):i=-(360-i)),d.map(e-this.suma,0,this.segments[this.index],i,s,!0)}calcIndex(t){this.index=-1,this.suma=0;let e=0;for(;e<=t;)this.suma=e,e+=this.segments[this.index+1],this.index++}genPol(t,e,i=1,s=!1){m();let r=0,o=9999;for(let t of this.segments)t*=i,0!==t&&(r=Math.max(r,t),o=Math.min(o,t));let n=R.spacing(),a=[],h=d.constrain(zt.bleed_strength,.075,1),l=(r+o)*(s?.03:zt.isAnimated?.25:h),c=new D(t,e),u=Math.round(this.length/n),p=0,f=0,v=l*d.random(.6,1.5);for(let t=0;t<u;t++)c.plotTo(this,n,n,1),p+=n,this.calcIndex(c.plotted),this.index>=f&&c.x&&(a.push([c.x,c.y]),f++),p>=v&&c.x&&(a.push([c.x,c.y]),v=l*d.random(.7,1.3),p=0);return this.calcIndex(0),new et(a)}draw(t,e,i){R.isActive&&(m(),this.origin&&(t=this.origin[0],e=this.origin[1],i=1),K(this,t,e,i))}fill(t,e,i){zt.isActive&&(m(),this.origin&&(t=this.origin[0],e=this.origin[1],i=1),this.pol=this.genPol(t,e,i),this.pol.fill())}hatch(t,e,i){Z.isActive&&(m(),this.origin&&(t=this.origin[0],e=this.origin[1],i=1),this.pol=this.genPol(t,e,i,!0),this.pol.hatch())}erase(t,e,i){this.origin&&(t=this.origin[0],e=this.origin[1],i=1),this.pol=this.genPol(t,e,i,!0),A.masks[2].push(),A.masks[2].noStroke();let r=s.color(vt.c);r.setAlpha(vt.a),A.masks[2].fill(r),A.masks[2].beginShape();for(let t of this.pol.vertices)A.masks[2].vertex(t.x,t.y);A.masks[2].endShape(s.CLOSE),A.masks[2].pop()}}function ot(t,e,i,s=!1){let r=new rt("curve"),o=Math.PI*i/2,n=d.random(0,360),a=()=>s?d.random(-1,1):0;r.addSegment(0+n+a(),o+a(),1,!0),r.addSegment(-90+n+a(),o+a(),1,!0),r.addSegment(-180+n+a(),o+a(),1,!0),r.addSegment(-270+n+a(),o+a(),1,!0);let h=s?d.randInt(-5,5):0;s&&r.addSegment(0+n,h*(Math.PI/180)*i,!0),r.endPlot(h+n,1,!0);let l=[t-i*d.sin(n),e-i*d.cos(-n),1];r.fill(l[0],l[1]),r.hatch(l[0],l[1]),r.draw(l[0],l[1]),r.erase(l[0],l[1])}let nt,at=!1;function ht(t=0){nt=d.constrain(t,0,1),at=[]}function lt(t,e,i){at.push([t,e,i])}function ct(t){t===s.CLOSE&&(at.push(at[0]),at.push(at[1]));let e=0!=nt||B.isActive?pt(at,nt,t===s.CLOSE):new et(at);(zt.isActive||Z.isActive)&&(e.fill(),e.hatch()),R.isActive&&e.draw(),vt.isActive&&e.erase(),at=!1}function mt(t,e,i){nt=[e,i],at=new rt(t)}function dt(t,e,i){at.addSegment(t,e,i)}function ut(t,e){at.endPlot(t,e),at.draw(nt[0],nt[1],1),at=!1}function pt(t,e=.5,i=!1){let s=new rt(0===e?"segments":"curve");if(t&&t.length>0){let r,o,n,a=0;for(let h=0;h<t.length-1;h++)if(e>0&&h<t.length-2){let l=t[h],c=t[h+1],m=t[h+2],f=d.dist(l[0],l[1],c[0],c[1]),v=d.dist(c[0],c[1],m[0],m[1]),g=p(l[0],l[1],c[0],c[1]),x=p(c[0],c[1],m[0],m[1]),y=e*Math.min(Math.min(f,v),.5*Math.min(f,v)),w=Math.max(f,v),k=f-y,b=v-y;if(Math.floor(g)===Math.floor(x)){let e=i&&0===h?0:f-a,m=i?0===h?0:v-n:v;s.addSegment(g,e,l[2],!0),h===t.length-3&&s.addSegment(x,m,c[2],!0),a=0,0===h&&(r=f,n=y,o=t[1],a=0)}else{let e={x:c[0]-y*d.cos(-g),y:c[1]-y*d.sin(-g)},m={x:e.x+w*d.cos(90-g),y:e.y+w*d.sin(90-g)},p={x:c[0]+y*d.cos(-x),y:c[1]+y*d.sin(-x)},f=u(e,m,p,{x:p.x+w*d.cos(90-x),y:p.y+w*d.sin(90-x)},!0),v=d.dist(e.x,e.y,f.x,f.y),A=d.dist(e.x,e.y,p.x,p.y)/2,z=2*Math.asin(A/v)*(180/Math.PI),_=2*Math.PI*v*z/360,M=i&&0===h?0:k-a,S=h===t.length-3?i?r-y:b:0;s.addSegment(g,M,l[2],!0),s.addSegment(g,isNaN(_)?0:_,l[2],!0),s.addSegment(x,S,c[2],!0),a=y,0===h&&(r=k,n=y,o=[e.x,e.y])}h==t.length-3&&s.endPlot(x,c[2],!0)}else if(0===e){0===h&&i&&t.pop();let e=t[h],r=t[h+1],o=d.dist(e[0],e[1],r[0],r[1]),n=p(e[0],e[1],r[0],r[1]);s.addSegment(n,o,1,!0),h==t.length-2&&s.endPlot(n,1,!0)}s.origin=i&&0!==e?o:t[0]}return s}function ft(t,e=.5){pt(t,e).draw()}const vt={};function gt(t="white",e=255){vt.isActive=!0,vt.c=t,vt.a=e}function xt(){vt.isActive=!1}function yt(t,e,i,r){m(),zt.opacity=arguments.length<4?arguments.length<3?e:1:r,zt.color=arguments.length<3?s.color(t):s.color(t,e,i),zt.isActive=!0}function wt(t,e="out"){m(),zt.bleed_strength=d.constrain(t,0,.6),zt.direction=e}function kt(t=.4,e=.4){m(),zt.texture_strength=d.constrain(t,0,1),zt.border_strength=d.constrain(e,0,1)}function bt(){zt.isActive=!1}function At(t){zt.isAnimated=t}const zt={isActive:!1,isAnimated:!1,color:"#002185",opacity:80,bleed_strength:.07,texture_strength:.4,border_strength:.4,fill(t){this.polygon=t,this.v=t.a.map((t=>s.createVector(t[0],t[1])));const e=this.v.length*d.random(.4);zt.m=this.v.map(((t,i)=>{let s=d.random(.8,1.2)*this.bleed_strength;return i<e?d.constrain(2*s,0,.9):s}));let i=d.randInt(0,this.v.length);this.v=[...this.v.slice(i),...this.v.slice(0,i)],new _t(this.v,this.m,this.calcCenter(),[],!0).fill(this.color,Math.floor(d.map(this.opacity,0,155,0,20,!0)),this.texture_strength)},calcCenter(){let t=0,e=0;for(let i=0;i<this.v.length;++i)t+=this.v[i].x,e+=this.v[i].y;return t/=this.v.length,e/=this.v.length,s.createVector(t,e)}};class _t{constructor(t,e,i,r,o=!1){this.pol=new et(t,!0),this.v=t,this.dir=r,this.m=e,this.midP=i,this.size=-1/0;for(let t of this.v){let e=d.dist(this.midP.x,this.midP.y,t.x,t.y);e>this.size&&(this.size=e)}if(o){const t="radians"===s.angleMode()?Math.PI/180:1;for(let e=0;e<this.v.length;e++){const i=this.v[e],s=this.v[(e+1)%this.v.length];let r=s.copy().sub(i),o=r.copy();o.rotate(90*t);let n={point1:{x:i.x+r.x/2,y:i.y+r.y/2},point2:{x:i.x+r.x/2+o.x,y:i.y+r.y/2+o.y}};const a=(t,e,i)=>(e.x-t.x)*(i.y-t.y)-(e.y-t.y)*(i.x-t.x)>.01;let h=0;for(let t of zt.polygon.intersect(n))a(i,s,t)&&h++;this.dir[e]=h%2==0}}}grow(t,e=!1){const i=[],r=[],o=[];let n=t>=.2&&this.v.length>=10?Math.floor(t*this.v.length):this.v.length;const a=s.angleMode();s.angleMode(s.RADIANS);const h=e?-.5:1,l=t=>t+.1*(d.gaussian(.5,.1)-.5);let c=s.createVector();for(let e=0;e<n;e++){const s=this.v[e],a=this.v[(e+1)%n];let m=.1===t?zt.bleed_strength<=.1?.25:.75:this.m[e];m*=h,i.push(s),r.push(l(m));let u=a.x-s.x,p=a.y-s.y,f=Math.sqrt(u*u+p*p);c.set(u,p).normalize();let v=this.dir[e],g="out"==zt.direction?90:-90,x=(v?g:-g)+45*d.gaussian(0,.4);c.rotate(x*Math.PI/180),c.mult(d.gaussian(.5,.2)*d.random(.6,1.4)*f*m);let y=s.copy().lerp(a,d.constrain(d.gaussian(.5,.2),.1,.9));y.add(c),i.push(y),r.push(l(m)),o.push(v,v)}return s.angleMode(a),new _t(i,r,this.midP,o)}fill(t,e,i){let r=d.map(zt.bleed_strength,0,.15,.6,1,!0);const o=24*r,n=e/5+i*e/6,a=e/4+i*e/3,h=e/7+i*e/3,l=e/5,c=3*i;A.watercolor=!0,x.trans(),A.blend(t,!1,!1,!0),A.masks[0].push(),A.masks[0].noStroke(),A.masks[0].translate(x.translation[0]+s.width/2,x.translation[1]+s.height/2),A.masks[0].rotate(x.rotation),A.masks[0].scale(w);let m=this.grow(),u=m.grow().grow(.9),p=u.grow(.75),f=this.grow(.6);for(let t=0;t<o;t++)t!==Math.floor(o/4)&&t!==Math.floor(o/2)&&t!==Math.floor(3*o/4)||(m=m.grow(),1!==r&&t!==Math.floor(o/2)||(u=u.grow(.75),p=p.grow(.75),f=f.grow(.1,!0))),m.grow().layer(t,l),f.grow(.1,!0).grow(.1).layer(t,h,!1),u.grow(.1).grow(.1).layer(t,a,!1),p.grow(.8).grow(.1).layer(t,n,!1),0!==c&&m.erase(c,e);A.masks[0].pop()}layer(t,e,i=!0){A.masks[0].fill(255,0,0,e),i?(A.masks[0].stroke(255,0,0,.5+1.5*zt.border_strength),A.masks[0].strokeWeight(d.map(t,0,24,6,.5))):A.masks[0].noStroke(),A.masks[0].beginShape();for(let t of this.v)A.masks[0].vertex(t.x,t.y);A.masks[0].endShape(s.CLOSE)}erase(t,e){const i=d.random(130,200),s=this.size/2,r=.025*this.size,o=.19*this.size;A.masks[0].erase(3.5*t-d.map(e,80,120,.3,1,!0),0);for(let t=0;t<i;t++){const t=this.midP.x+d.gaussian(0,s),e=this.midP.y+d.gaussian(0,s),i=d.random(r,o);A.masks[0].circle(t,e,i)}A.masks[0].noErase()}}exports.end=function(){A.blend(!1,!0),A.blend(!1,!0,!0)};const Mt=["weight","vibration","definition","quality","opacity","spacing","pressure","type","tip","rotate"],St=[["pen",[.35,.12,.5,8,200,.3,{curve:[.15,.2],min_max:[1.4,.9]}]],["rotring",[.2,.05,1,3,250,.15,{curve:[.05,.2],min_max:[1.7,.8]}]],["2B",[.35,.5,.1,8,180,.2,{curve:[.15,.2],min_max:[1.3,1]}]],["HB",[.3,.5,.4,4,180,.25,{curve:[.15,.2],min_max:[1.2,.9]}]],["2H",[.2,.4,.3,2,150,.2,{curve:[.15,.2],min_max:[1.2,.9]}]],["cpencil",[.4,.6,.8,7,120,.15,{curve:[.15,.2],min_max:[.95,1.2]}]],["charcoal",[.5,2,.8,300,110,.06,{curve:[.15,.2],min_max:[1.3,.8]}]],["hatch_brush",[.2,.4,.3,2,150,.15,{curve:[.5,.7],min_max:[1,1.5]}]],["spray",[.3,12,15,40,80,.65,{curve:[0,.1],min_max:[.15,1.2]},"spray"]],["marker",[2.5,.12,null,null,25,.4,{curve:[.35,.25],min_max:[1.5,1]},"marker"]],["marker2",[2.5,.12,null,null,25,.35,{curve:[.35,.25],min_max:[1.3,.95]},"custom",function(t){let e=E;t.rect(-1.5*e,-1.5*e,3*e,3*e),t.rect(1*e,1*e,1*e,1*e)},"natural"]]];for(let t of St){let e={};for(let i=0;i<t[1].length;i++)e[Mt[i]]=t[1][i];O(t[0],e)}const Ct=i;return e})()));\ No newline at end of filediff --git a/lib/p5.brush.min.js.LICENSE.txt b/lib/p5.brush.min.js.LICENSE.txtnew file mode 100644index 0000000..f5803d4--- /dev/null+++ b/lib/p5.brush.min.js.LICENSE.txt@@ -0,0 +1,58 @@+/**+ * @fileoverview p5.brush - A comprehensive toolset for brush management in p5.js.+ * @version 1.0.6+ * @license MIT+ * @author Alejandro Campos Uribe+ *+ * @description+ * p5.brush is a p5.js library dedicated to the creation and management of custom brushes.+ * It extends the drawing capabilities of p5.js by allowing users to simulate a wide range+ * of brush strokes, vector fields, hatching patterns, and fill textures. These features+ * are essential for emulating the nuanced effects found in traditional sketching and painting.+ * Whether for digital art applications or procedural generation of graphics, p5.brush provides+ * a robust framework for artists and developers to create rich, dynamic, and textured visuals.+ *+ * @example+ * // Basic usage:+ * brush.pick('marker'); // Select brush type+ * brush.stroke(255, 0, 0); // Set brush color+ * brush.strokeWeight(10); // Set brush size+ * brush.line(25, 25, 75, 75); // Draw a line+ *+ * // Add a new brush type:+ * brush.add('customBrush', { /* parameters for the brush *\/ });+ * brush.pick('customBrush');+ *+ * // Use the custom brush for a vector-field line:+ * brush.field('field_name') // Pick a flowfield+ * brush.flowLine(50, 50, 100, PI / 4); // Draw a line within the vector-field+ *+ * // Fill textures:+ * brush.noStroke();+ * brush.fill('#FF0000', 90); // Set fill color to red and opacity to 90+ * brush.bleed(0.3); // Set bleed effect for a watercolor-like appearance+ * brush.rect(100, 100, 50, 50); // Fill a rectangle with the bleed effect+ *+ * @license+ * MIT License+ *+ * Copyright (c) 2023 Alejandro Campos Uribe+ *+ * Permission is hereby granted, free of charge, to any person obtaining a copy+ * of this software and associated documentation files (the "Software"), to deal+ * in the Software without restriction, including without limitation the rights+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+ * copies of the Software, and to permit persons to whom the Software is+ * furnished to do so, subject to the following conditions:+ *+ * The above copyright notice and this permission notice shall be included in+ * all copies or substantial portions of the Software.+ *+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER+ * DEALINGS IN THE SOFTWARE.+ */diff --git a/package.json b/package.jsonindex 2d84714..f1360a7 100644--- a/package.json+++ b/package.json@@ -29,6 +29,9 @@},"homepage": "https://p5-brush.cargo.site/","devDependencies": {+ "@babel/core": "^7.23.7",+ "@babel/preset-env": "^7.23.8",+ "babel-loader": "^9.1.3","webpack": "^5.89.0","webpack-cli": "^5.1.4"},diff --git a/src/standalone.js b/src/standalone.jsnew file mode 100644index 0000000..2c337d3--- /dev/null+++ b/src/standalone.js@@ -0,0 +1,2 @@+import * as brush from './index.js';+export default brush\ No newline at end of filediff --git a/webpack.config.js b/webpack.config.jsindex 9796766..97bbec3 100644--- a/webpack.config.js+++ b/webpack.config.js@@ -1,16 +1,31 @@const path = require("path")-module.exports = {- entry: path.resolve(__dirname, "src/index.js"),- output: {- path: path.resolve(__dirname, "dist"),- filename: "p5.brush.js",- library: "$",- libraryTarget: "umd",+module.exports = [+ {+ entry: path.resolve(__dirname, "src/index.js"),+ output: {+ path: path.resolve(__dirname, "dist"),+ filename: "p5.brush.js",+ library: "$",+ libraryTarget: "umd",+ },+ mode: "development",+ devtool: false,+ optimization: {+ usedExports: true,+ }},- mode: "production",- devtool: false,- optimization: {- usedExports: true,+ {+ entry: path.resolve(__dirname, "src/standalone.js"),+ output: {+ path: path.resolve(__dirname, "lib"),+ filename: "p5.brush.min.js",+ library: "$",+ libraryTarget: "umd",+ },+ mode: "production",+ optimization: {+ usedExports: true,+ }}-}\ No newline at end of file+]