xxxxxxxxxx
62
/*
Turtle-Graphics (c) Peter Lager 2025
@license MIT
@version 0.9.1
*/
const [DISPLAY,STANDARD,LOGO]=[Symbol.for("display"),Symbol.for("standard"),Symbol.for("logo")],[BUTT,SQUARE,ROUND]=[Symbol.for("butt"),Symbol.for("square"),Symbol.for("round")],[STYLE,POS,STYLE_POS]=[1,2,3],[LT,RT]=[-1,1],[TG]=function(){const [e,n,l,h,q,p,k,z]=[Math.abs,Math.atan2,Math.ceil,Math.cos,Math.max,Math.sign,Math.sin,Math.sqrt],[f,u,r]=[Math.PI,Math.PI/2,2*Math.PI],x=(a,b=r)=>{for(;0>a;)a+=b;for(;a>=b;)a-=b;return a},H=(a,b)=>{let d=(a-b)**2/(a+b)**2;return f*(a+b)*(1+3*d/(10+z(4-3*d)))},
I=(a,b,d,g,m,v)=>{d*=h(v);g*=k(v);v=Math.sin(m);m=Math.cos(m);return[a+d*m-g*v,b+g*m+d*v]},[Q,R,C]=[1,-1,0],[D,K]=[0,1],[L,M]=[1,-1],S=5*f,E=new Map;class A{static clearRecords(){E.clear()}static setRecord(a,b){E.set(a,b)}static hasRecord(a){return E.has(a)}static getRecord(a){if(A.hasRecord(a))return E.get(a).map(b=>b.clone())}static getTurtle(a,b,d){return new A(a,b,d)}constructor(a,b,d=DISPLAY,g=!0){this._b=new OffscreenCanvas(a,b);this._bdc=this._b.getContext("2d");this._interval=25;this._polygons=
new Map;this._snapshots=new Map;this._set_mode(d);this._degrees=g;this._earlier=this._timer=0;this._font="400 normal 14px sans-serif";this.setTurtle(DART);this._reset()}draw(a,b=0,d=0){let g=this._b.width,m=this._b.height,v=g/2,y=m/2;a.save();a.translate(b,d);a.beginPath();a.moveTo(-v,-y);a.lineTo(v,-y);a.lineTo(v,y);a.lineTo(-v,y);a.closePath();a.clip();switch(this._mode){case LOGO:a.rotate(-u);break;case STANDARD:a.scale(1,-1)}a.drawImage(this._b,0,0,g,m,-v,-y,g,m);this._penDown&&this._taskQueue[0]&&
this._taskQueue[0].render&&this._taskQueue[0].render(a);this._csrVisible&&(a.translate(this._penX,this._penY),a.rotate(isNaN(this._tilt)?this._penA:this._tilt),a.drawImage(this._cursor.icon,-this._cursor.hitX,-this._cursor.hitY));a.restore()}_getCopy(){let a=turtle._b,b=a.width,d=a.height,g=b/2,m=d/2,v=new OffscreenCanvas(a.width,a.height),y=v.getContext("2d");y.translate(a.width/2,a.height/2);switch(turtle._mode){case LOGO:y.rotate(-u);break;case STANDARD:y.scale(1,-1)}y.drawImage(a,0,0,b,d,-g,-m,
b,d);return v}x(a,b=!1){return this._addTask(new F(D,a,NaN,b))}y(a,b=!1){return this._addTask(new F(D,NaN,a,b))}xy(a,b,d=!1){return this._addTask(new F(D,a,b,d))}dxy(a,b,d=!1){return this._addTask(new F(K,a,b,d))}f(a){return this._addTask(new w("_penA",this._fix(a)))}face(a){return this.f(a)}fd(a){return this._addTask(new G(a,Q))}forward(a){return this.fd(a)}bk(a){return this._addTask(new G(a,R))}backward(a){return this.bk(a)}back(a){return this.bk(a)}lt(a){return this._addTask(new B(this._fix(a),
LT))}left(a){return this.lt(a)}rt(a){return this._addTask(new B(this._fix(a),RT))}right(a){return this.rt(a)}head(a){return this._addTask(new B(this._fix(a),C))}heading(a){return this.head(a)}home(){return this._addTask(new T)}goto(a,b){return this._addTask(new J(D,a,b))}goby(a,b){return this._addTask(new J(K,a,b))}dot(a=6,b="current",d=.5,g="current"){return this._addTask(new U(a,b,d,g))}arrow(a=6,b="current",d=.5,g="current"){return this._addTask(new V(a,b,d,g))}circle(a,b=0,d=RT,g=0){return this.oval(a,
a,b,d,g)}oval(a,b,d=0,g=RT,m=0){d=e(0===d?r:this._degrees?d*f/180:d);return 0===m?this._addTask(new W(a,b,d,g)):this._addTask(new X(a,b,d,g,l(m)))}sleep(a){return this._addTask(new Y(a))}write(a,b="left",d=!1,g){return this._addTask(new Z(a,b,d,g))}font(a){return this._addTask(new w("_font",a))}up(){return this._addTask(new w("_penDown",!1))}pu(){return this.up()}penup(){return this.up()}down(){return this._addTask(new w("_penDown",!0))}pd(){return this.down()}pendown(){return this.down()}pensize(a=
2){return this._addTask(new w("_penSize",a))}dash(a=[]){this._addTask(new w("_penDash",a));return this}pendash(a=[]){return this.dash(a)}cap(a){return this._addTask(new w("_penCap",a))}pencap(a){return this.cap(a)}pencolor(a){return this._addTask(new w("_penColor",a))}fillcolor(a){return this._addTask(new w("_fillColor",a))}push_pen(){return this._addTask(new N(L))}pop_pen(a=STYLE_POS){return this._addTask(new N(M,a))}turtle(a){return"Cursor"===a?.constructor?.name?this._addTask(new w("_cursor",a)):
this}st(){return this._addTask(new w("_csrVisible",!0))}showturtle(){return this.st()}ht(){return this._addTask(new w("_csrVisible",!1))}hideturtle(){return this.ht()}tilt(a){return this._addTask(new w("_tilt",this._fix(a)))}speed(a,b){isFinite(a)&&0<a&&this._addTask(new w("_lnrSpeed",a));isFinite(b)&&0<b&&this._addTask(new w("_angSpeed",this._degrees?b*f/180:b));return this}begin_fill(){return this._addTask(new O)}end_fill(a=!1){return this._addTask(new P(a))}begin_poly(){return this._addTask(new aa)}end_poly(a){return this._addTask(new ba(a))}begin_record(){return this._addTask(new ca)}end_record(a){return this._addTask(new da(a))}do(a,
b){return"Function"===a.constructor.name?this._addTask(new ea(a,b)):"string"===typeof a?this._addTask(new fa(a)):this}animateon(){return this._addTask(new w("_animate",!0))}animateoff(){return this._addTask(new w("_animate",!1))}clear(){return this._addTask(new ha)}reset(){return this._addTask(new ia)}snapshot(a){return this._addTask(new ja(a))}start(){this._startTurtle();this._autoStart=!0;return this}stop(){this._stopTurtle();this._autoStart=!1;return this}degrees(){this._degrees=!0}radians(){this._degrees=
!1}setUpdateInterval(a){this._interval=a}showTurtle(){this._csrVisible=!0;return this}hideTurtle(){this._csrVisible=!1;return this}doClear(){return this._clear()}doReset(){return this._reset()}getXY(){return this._p_m_XY(this._penX,this._penY)}getX(){return this._p_m_XY(this._penX,this._penY)[0]}getY(){return this._p_m_XY(this._penX,this._penY)[1]}getH(){return this._degrees?180*this._penA/f:this._penA}getHeading(){return this.getH()}setXYH(a,b,d){this.setXY(a,b);this.setH(d);return this}setXY(a,
b){Number.isFinite(a)&&Number.isFinite(b)&&(this._mode===LOGO?(this._penX=b,this._penY=a):(this._penX=a,this._penY=b));return this}setX(a){Number.isFinite(a)&&(this._mode===LOGO?this._penY=a:this._penX=a);return this}setY(a){Number.isFinite(a)&&(this._mode===LOGO?this._penX=a:this._penY=a);return this}setH(a){this._penA=this._degrees?a*f/180:a;return this}getScreenXY(a=0,b=0){return this._m_s_XY(this._penX,this._penY,a,b)}getScreenH(){return this._m_s_Angle(this.getH())}getModeXY(a,b,d=0,g=0){return this._s_m_XY(a,
b,d,g)}getModeH(a){return this._s_m_Angle(a)}setPenSize(a){this._penSize=a;return this}getPenSize(){return this._penSize}setDash(a=[]){Array.isArray(a)&&(this._penDash=a);return this}getDash(){return this._penDash}setPenColor(a){this._penColor=a;return this}getPenColor(){return this._penColor}setCap(a){this._penCap=a;return this}getCap(){return this._penCap}setFillColor(a){this._fillColor=a;return this}getFillColor(){return this._fillColor}setSpeed(a=NaN,b=NaN){Number.isNaN(a)||(this._lnrSpeed=a);
Number.isNaN(b)||(this._angSpeed=this.degrees?b*f/180:b);return this}getSpeed(){return[this._lnrSpeed,this._degrees?180*this._angSpeed/f:this._angSpeed]}getPoly(a){return this._polygons.get(a)}getSnapshot(a){return(a=this._snapshots.get(a))?a:this._getCopy()}setTurtle(a){a&&"string"===typeof a&&CURSORS.has(a)?this._cursor=CURSORS.get(a):"Cursor"===a?.constructor?.name&&(this._cursor=a);return this}setCursor(a){return this.setTurtle(a)}get hasTasks(){return 0<this.nbrTasks}get isActive(){return 0!=
this._timer}get isAnimating(){return this._animate}get isVisible(){return this._csrVisible}get isPenDown(){return this._penDown}get isFilling(){return this._fill_track}get nbrTasks(){return this._taskQueue.length}get nbrStyles(){return this._penStack.length}get mode(){return this._mode}get mode$(){return Symbol.keyFor(this._mode)}get width(){return this._b.width}get height(){return this._b.height}get aspect(){return this._b.width/this._b.height}spawn(){let a=new A(this._b.width,this._b.height,this._mode);
for(let b of Object.keys(this))"object"!=typeof this[b]&&(a[b]=this[b]);a._penDash=[this._penDash];a._cursor=this._cursor;a._poly_points=[];a._poly_track=!1;a._fill_points=[];a._fill_track=!1;a._timer=0;return a}_set_mode(a){switch(a){case LOGO:this._p_m_XY=function(b,d){return[d,b]};this._s_m_XY=function(b,d,g=0,m=0){return[b-g,m-d]};this._m_s_XY=function(b,d,g=0,m=0){return[g+d,m-b]};this._s_m_Angle=function(b){return this._norm(b+(this._degrees?90:u))};this._m_s_Angle=function(b){return this._norm(b+
(this._degrees?-90:-u))};this._mode=a;break;case STANDARD:this._p_m_XY=function(b,d){return[b,d]};this._s_m_XY=function(b,d,g=0,m=0){return[b-g,m-d]};this._m_s_XY=function(b,d,g=0,m=0){return[b+g,-d+m]};this._s_m_Angle=this._m_s_Angle=function(b){return this._norm(-b+(this._degrees?360:r))};this._mode=a;break;default:this._p_m_XY=function(b,d){return[b,d]},this._s_m_XY=function(b,d,g=0,m=0){return[b-g,d-m]},this._m_s_XY=function(b,d,g=0,m=0){return[b+g,d+m]},this._s_m_Angle=this._m_s_Angle=function(b){return this._norm(b)},
this._mode=DISPLAY}return this}_reset(){this._stopTurtle();this._taskQueue=[];this._penStack=[];this._clear();this._autoStart=!1;this._lnrSpeed=200;this._angSpeed=S;this._fillColor="white";this._penColor="black";this._penSize=2;this._penA=this._penY=this._penX=0;this._penDown=!0;this._penDash=[];this._penCap=ROUND;this._animate=this._csrVisible=!0;this._fill_points=[];this._fill_track=!1;this._poly_points=[];this._poly_track=!1;this._record=[];this._recording=!1;this._polygons.clear();this._snapshots.clear();
this._tilt=NaN;return this}_update(){if(0==this.nbrTasks)return this._stopTurtle();var a=performance.now();let b=a-this._earlier;this._earlier=a;do a=this._taskQueue[0],b=a.perform(this,b),a.isDONE()&&(a.render&&a.render(this._bdc,this._b.width,this._b.height),this._taskQueue.shift());while(0<this.nbrTasks&&0<b)}_addTask(a){this._taskQueue.push(a);0===this._timer&&this._autoStart&&this._startTurtle();return this}_injectTasks(a,b=1){this._taskQueue.splice(b,0,a)}_clear(){this._bdc.clearRect(0,0,
this._b.width,this._b.height);return this}_startTurtle(){0===this._timer&&(this._timer=setInterval(()=>this._update.call(this),this._interval),this._earlier=performance.now())}_stopTurtle(){clearInterval(this._timer);this._timer=0}_fix(a){for(this._degrees&&(a=a*f/180);0>a;)a+=r;for(;a>=r;)a-=r;return a}_norm(a){let b=this._degrees?360:r;for(;0>a;)a+=b;for(;a>=b;)a-=b;return a}}class t{constructor(a){this._info=a;this.setDONE()}setWAITING(){this._status=64}setREADY(){this._status=65}setDONE(){this._status=
128}isWAITING(){return 64===this._status}isREADY(){return 65===this._status}isDONE(){return 128===this._status}clone(){return new this.constructor(this._info)}recordManager(a){a._recording&&a._record.push(this.clone())}}class B extends t{constructor(a){super(a);this._ang=a[0];this._dir=a[1];this.setWAITING()}turnDir(a,b){a=x(a);b=x(b);let d;a=b-a;a>-f&&a<=f?d=a:a>f?d=a-r:a<=-f&&(d=a+r);return p(d)}init(a){this.recordManager(a);let b=this._dir;var d=this._ang;this._angSpeed=a._angSpeed;a=
x(a._penA);d=0===b?d:x(a+b*d);0===b&&(b=this.turnDir(a,d));switch(b){case LT:d>a&&(a+=r);break;case RT:a>d&&(d+=r)}this._sa=a;this._ea=d;this._range=d-a;this._dir=b;this._t=0;this.setREADY()}perform(a,b){this.isWAITING()&&this.init(a);let d=0;a._animate&&0!=this._range?(this._t+=e(this._dir*this._angSpeed*b/1E3/this._range),1<=this._t?(this.setDONE(),d=(this._t-1)*this._range*1E3/this._angSpeed,a._penA=x(this._ea)):(b=this._sa,a._penA=b+this._t*(this._ea-b))):(this.setDONE(),d=b,a._penA=x(this._ea));
return d}}class G extends t{constructor(a){super(a);this._length=a[0];this._dir=a[1];this.setWAITING()}init(a){this.recordManager(a);[this._fillStyle,this._strokeStyle,this._lineWidth,this._penDown]=[a._fillColor,a._penColor,a._penSize,a._penDown];this._lineCap=Symbol.keyFor(a._penCap);this._lineDash=[a._penDash];[this._sx,this._sy,this._ang]=[a._penX,a._penY,a._penA];this._ex=this._sx+this._dir*this._length*h(this._ang);this._ey=this._sy+this._dir*this._length*k(this._ang);this._tx=this._sx;
this._ty=this._sy;this._t=0;this.setREADY()}perform(a,b){this.isWAITING()&&this.init(a);let d=0;a._animate?(this._t+=a._lnrSpeed*b*.001/this._length,1<=this._t?(this.setDONE(),d=(this._t-1)*this._length*1E3/a._lnrSpeed,this._tx=this._ex,this._ty=this._ey):(b=this._sx,this._tx=b+this._t*(this._ex-b),b=this._sy,this._ty=b+this._t*(this._ey-b))):(this._tx=this._ex,this._ty=this._ey,d=b,this.setDONE());a._penX=this._tx;a._penY=this._ty;this.isDONE()&&(a._fill_track&&a._fill_points.push(["lineTo",a._penDown?
"lineTo":"moveTo",a._penX,a._penY]),a._poly_track&&a._poly_points.push(a._m_s_XY(a._penX,a._penY)));return d}render(a,b=0,d=0){!this.isWAITING()&&this._penDown&&(a.save(),a.translate(b/2,d/2),a.fillStyle=this._fillStyle,a.strokeStyle=this._strokeStyle,a.lineWidth=this._lineWidth,a.lineCap=this._lineCap,a.setLineDash(this._lineDash),a.beginPath(),a.moveTo(this._sx,this._sy),a.lineTo(this._tx,this._ty),a.stroke(),a.restore())}}class X extends t{constructor(a){super(a);this._a=e(a[0]);this._b=
e(a[1]);this._ext=a[2];this._turn=a[3];this._steps=a[4];this.setWAITING()}init(a){this._cx=a._penX-this._turn*this._b*k(a._penA);this._cy=a._penY+this._turn*this._b*h(a._penA);this._angSpeed=a._lnrSpeed*r/H(this._a,this._b);switch(this._turn){case LT:this._ccw=!0;this._sa=u;this._ea=this._sa-this._ext;break;case RT:this._ccw=!1;this._sa=r-u;this._ea=this._sa+this._ext;break;default:console.log("ERROR illegal turn")}this._deltaAngle=this._ext/this._steps;this._era=a._penA;this._ca=this._sa;this._t=
0;this.setREADY()}perform(a,b){this.isWAITING()&&this.init(a);let d=[];for(let g=1;g<=this._steps;g++){this._ca=this._sa+this._turn*this._deltaAngle*g;let [m,v]=I(this._cx,this._cy,this._a,this._b,this._era,this._ca);if(a._mode===LOGO){let y=m;m=v;v=y}d.push(new J(D,m,v))}d.push(new B(x(a._penA+this._turn*this._ext),C));a._injectTasks(d);this.setDONE();return b}}class W extends t{constructor(a){super(a);this._a=e(a[0]);this._b=e(a[1]);this._ext=a[2];this._turn=a[3];this._lineDash=[];this.setWAITING()}init(a){this.recordManager(a);
[this._fillStyle,this._strokeStyle,this._lineWidth,this._penDown]=[a._fillColor,a._penColor,a._penSize,a._penDown];this._lineCap=Symbol.keyFor(a._penCap);[this._sx,this._sy,this._penA]=[a._penX,a._penY,a._penA];this._lineDash=[a._penDash];this._cx=this._sx-this._turn*this._b*k(this._penA);this._cy=this._sy+this._turn*this._b*h(this._penA);this._angSpeed=a._lnrSpeed*r/H(this._a,this._b);switch(this._turn){case LT:this._ccw=!0;this._sa=u;this._ea=this._sa-this._ext;break;case RT:this._ccw=!1;this._sa=
r-u;this._ea=this._sa+this._ext;break;default:console.log("ERROR illegal turn")}this._era=a._penA;this._ca=this._sa;this._t=0;this.setREADY()}perform(a,b){this.isWAITING()&&this.init(a);var d=b*this._angSpeed/1E3;this._ca+=d*this._turn;var g=this._sa;this._t=(this._ca-g)/(this._ea-g);1<=this._t||!a._animate?(b=(this._t-1)*this._ext*1E3/this._angSpeed,this._t=1,this._ca=this._ea):b-=1E3*d/this._angSpeed;d=I(this._cx,this._cy,this._a,this._b,this._era,this._ca);a._penX=d[0];a._penY=d[1];a._penA=x(this._ca+
this._era+this._turn*u);if(1<=this._t&&(this.setDONE(),a._fill_track&&a._fill_points.push(["ellipse","ellipse",this._cx,this._cy,this._a,this._b,this._era,this._sa,this._ca,this._ccw]),a._poly_track)){d=l(H(this._a,this._b)*this._ext/(20*r));g=this._ext/d;for(let m=1;m<=d;m++){let v=I(this._cx,this._cy,this._a,this._b,this._era,this._sa+m*g*this._turn);a._poly_points.push(a._m_s_XY(v[0],v[1]))}}return b}render(a,b=0,d=0){!this.isWAITING()&&this._penDown&&(a.save(),a.translate(b/2,d/2),a.fillStyle=
this._fillStyle,a.strokeStyle=this._strokeStyle,a.lineWidth=this._lineWidth,a.lineCap=this._lineCap,a.setLineDash(this._lineDash),a.beginPath(),a.ellipse(this._cx,this._cy,this._a,this._b,this._era,this._sa,this._ca,this._ccw),a.stroke(),a.closePath(),a.restore())}}class U extends t{constructor(a){super(a);this._rad=a[0];this._fcol=a[1];this._sw=a[2];this._scol=a[3]}perform(a,b){this.recordManager(a);[this._sx,this._sy,this._penDown]=[a._penX,a._penY,a._penDown];this._fcol="current"==this._fcol?
a._fillColor:this._fcol;this._scol="current"==this._scol?a._penColor:this._scol;this._sw=0<this._sw?Number(this._sw):0==this._sw?a._penSize:0;this._rad=0>=this._rad?q(2*a._penSize,a._penSize+4):this._rad}render(a,b=0,d=0){!this.isWAITING()&&this._penDown&&(a.save(),a.translate(b/2,d/2),0<this._sw&&(a.strokeStyle=this._scol,a.lineWidth=this._sw),a.fillStyle=this._fcol,a.beginPath(),a.ellipse(this._sx,this._sy,this._rad,this._rad,0,0,r,!0),a.fill(),0<this._sw&&a.stroke(),a.closePath(),a.restore())}}
class V extends t{constructor(a){super(a);this._size=a[0];this._fcol=a[1];this._sw=a[2];this._scol=a[3]}perform(a,b){this.recordManager(a);[this._sx,this._sy,this._penDown]=[a._penX,a._penY,a._penDown];this._angle=a._penA;this._fcol="current"==this._fcol?a._fillColor:this._fcol;this._scol="current"==this._scol?a._penColor:this._scol;this._sw=0<this._sw?Number(this._sw):0==this._sw?a._penSize:0;this._xp=-this._size*h(f/6);this._yp=this._size*k(f/6)}render(a,b=0,d=0){!this.isWAITING()&&this._penDown&&
(a.save(),a.translate(b/2,d/2),a.translate(this._sx,this._sy),a.rotate(this._angle),a.fillStyle=this._fcol,0<this._sw&&(a.strokeStyle=this._scol,a.lineWidth=this._sw),a.beginPath(),a.moveTo(0,0),a.lineTo(this._xp,this._yp),a.lineTo(this._xp,-this._yp),a.lineTo(0,0),a.fill(),0<this._sw&&a.stroke(),a.closePath(),a.restore())}}class Z extends t{constructor(a){super(a);this._text=a[0];this._align=a[1];this._move=a[2];this._font=a[3];this.setWAITING()}init(a){this.recordManager(a);[this._sx,this._sy,
this._pa,this._fillColor,this._mode,this._penDown]=[a._penX,a._penY,a._penA,a._fillColor,a._mode,a._penDown];this._align="center"===this._align||"right"===this._align?this._align:"left";this._font=this._font??a._font;this.setREADY()}perform(a,b){this.isWAITING()&&this.init(a);if(this._move&&"right"!==this._align){a._bdc.font=this._font;var d=a._bdc.measureText(this._text).width;"center"===this._align&&(d/=2);b=d*k(a._penA);d*=h(a._penA);switch(this._mode){case LOGO:a._penX=this._sx-b;a._penY=this._sy+
d;break;case STANDARD:a._penX=this._sx+b;a._penY=this._sy-d;break;case DISPLAY:a._penX=this._sx-b,a._penY=this._sy+d}}this.setDONE()}render(a,b=0,d=0){!this.isWAITING()&&this._penDown&&(a.save(),a.translate(this._sx+b/2,this._sy+d/2),a.font=this._font,a.textAlign=this._align,a.textBaseline="bottom",a.fillStyle=this._fillColor,a.rotate(this._pa+u),this._mode===STANDARD&&(a.rotate(-f),a.scale(1,-1)),a.fillText(this._text,0,0),a.restore())}}class N extends t{constructor(a){super(a);this._action=
a[0];this._what=a[1]}perform(a,b){this.recordManager(a);let d;switch(this._action){case L:d={style:[a._penDown,a._penSize,[a._penDash],a._penCap,a._penColor,a._fillColor,a._animate,a._csrVisible,a._tilt,a._lnrSpeed,a._angSpeed],position:[a._penX,a._penY,a._penA]};a._penStack.push(d);break;case M:0<a.nbrStyles&&(d=a._penStack.pop(),STYLE==(this._what&STYLE)&&([a._penDown,a._penSize,a._penDash,a._penCap,a._penColor,a._fillColor,a._animate,a._csrVisible,a._tilt,a._lnrSpeed,a._angSpeed]=d.style),POS===
(this._what&POS)&&([a._penX,a._penY,a._penA]=d.position))}return b}}class ca extends t{constructor(a){super(a)}perform(a,b){a._record=[];a._recording=!0;return b}}class da extends t{constructor(a){super(a);this._id=String(a[0])}perform(a,b){0<this._id.length&&!A.hasRecord(this._id)&&A.setRecord(this._id,a._record);a._recording=!1;a._record=[];return b}}class fa extends t{constructor(a){super(a);this._id=a[0]}perform(a,b){let d=A.getRecord(this._id);d&&a._injectTasks(d);return b}}
class aa extends t{constructor(a){super(a)}perform(a,b){this.recordManager(a);a._poly_track=!0;a._poly_points=[];a._poly_points.push(a._m_s_XY(a._penX,a._penY));return b}}class ba extends t{constructor(a){super(a);this._id=a[0]}perform(a,b){this.recordManager(a);"string"===typeof this._id&&0<this._id.length&&(a._polygons.set(this._id,a._poly_points),a._poly_track=!1,a._poly_points=[]);this.setDONE();return b}}class O extends t{constructor(a){super(a)}perform(a,b){this.recordManager(a);
a._fill_track=!0;a._fill_points=[];a._fill_points.push(["moveTo","moveTo",a._penX,a._penY]);return b}}class P extends t{constructor(a){super(a);this._close=a[0];this._pts=[];this.setWAITING()}init(a){this.recordManager(a);[this._fillStyle,this._strokeStyle,this._lineWidth,this._penDown]=[a._fillColor,a._penColor,a._penSize,a._penDown];[this._lineDash,this._lineCap]=[a._penDash,Symbol.keyFor(a._penCap)];this._pts=a._fill_points;this._close&&0<this._pts.length&&this._pts.push(["lineTo",a._penDown?
"lineTo":"moveTo",this._pts[0][2],this._pts[0][3]]);this.setREADY()}perform(a,b){this.isWAITING()&&this.init(a);a._fill_track=!1;a._fill_points=[];this.setDONE();return b}render(a,b=0,d=0){if(!this.isWAITING()){a.save();a.translate(b/2,d/2);a.fillStyle=this._fillStyle;a.strokeStyle=this._strokeStyle;a.lineWidth=this._lineWidth;a.lineCap=this._lineCap;b=[this._pts];for(a.beginPath();0<b.length;)d=b.shift(),a[d[0]](d.slice(2));a.fill();if(this._penDown){b=[this._pts];a.beginPath();for(a.setLineDash(this._lineDash);0<
b.length;)d=b.shift(),a[d[1]](d.slice(2));a.stroke()}a.restore()}}}class T extends t{constructor(a){super(a);this.setWAITING()}init(a){[this._sx,this._sy]=[a._penX,a._penY];this._angle=n(-this._sy,-this._sx);this._angle=x(this._angle);this._dist=Math.sqrt((0-this._sx)**2+(0-this._sy)**2);this.setREADY()}perform(a,b){this.isWAITING()&&this.init(a);let d=[new B(this._angle,C),new G(this._dist,1),new B(0,C)];a._injectTasks(d);this.setDONE();return b}}class J extends t{constructor(a){super(a);
this._mt=a[0];this._x=a[1];this._y=a[2];this.setWAITING()}init(a){let [b,d]=[a._penX,a._penY],[g,m]=a._mode==LOGO?[this._y,this._x]:[this._x,this._y];g+=this._mt*a._penX;m+=this._mt*a._penY;this._angle=x(n(m-d,g-b));this._dist=Math.sqrt((g-b)**2+(m-d)**2);this.setREADY()}perform(a,b){this.isWAITING()&&this.init(a);let d=[new B(this._angle,C),new G(this._dist,1)];a._injectTasks(d);this.setDONE();return b}}class F extends t{constructor(a){super(a);this._mt=a[0];this._x=a[1];this._y=a[2];this._fill_gap=
a[3]}perform(a,b){let [d,g]=a._mode==LOGO?[this._y,this._x]:[this._x,this._y];d=isNaN(d)?a._penX:d;g=isNaN(g)?a._penY:g;d+=this._mt*a._penX;g+=this._mt*a._penY;let m=[];a.isFilling&&!this._fill_gap?m.push(new P,new w("_penX",d),new w("_penY",g),new O):(m.push(new w("_penX",d),new w("_penY",g)),a._fill_track&&a._fill_points.push(["lineTo","moveTo",d,g]),a._poly_track&&a._poly_points.push(a._m_s_XY(d,g)));a._injectTasks(m);return b}}class w extends t{constructor(a){super(a);this._attr=a[0];this._value=
a[1]}perform(a,b){this.recordManager(a);a[`${this._attr}`]=this._value;return b}}class ha extends t{constructor(a){super(a)}perform(a,b){this.recordManager(a);a._clear();return b}}class ia extends t{constructor(a){super(a)}perform(a,b){this.recordManager(a);a._reset();return b}}class ja extends t{constructor(a){super(a);this._id=a[0]}perform(a,b){this.recordManager(a);"string"===typeof this._id&&0<this._id.length&&a._snapshots.set(this._id,a._getCopy());this.setDONE();return b}}
class Y extends t{constructor(a){super(a);this._sleepTimeLeft=a[0];this.setREADY()}perform(a,b){this.recordManager(a);a._animate?b>=this._sleepTimeLeft?(b-=this._sleepTimeLeft,this.setDONE()):(this._sleepTimeLeft-=b,b=0):this.setDONE();return b}}class ea extends t{constructor(a){super(a);this._function=a[0];this._data=a.slice(1)}perform(a,b){this.recordManager(a);this._function(a,this._data);return b}}return[A]}();
function getImageFromOsc(e,n=p5.instance){if(e){let l=e.getContext("2d").getImageData(0,0,e.width,e.height).data,h=new Uint32Array(l);e=n.createImage(e.width,e.height);e.loadPixels();e.pixels.forEach((q,p,k)=>k[p]=h[p]);e.updatePixels();return e}}function getCursorFromImage(e,n=0,l=0){e.loadPixels();var h=e.width;let q=e.height;e=new ImageData(new Uint8ClampedArray(e.pixels),h,q);h=new OffscreenCanvas(h,q);h.getContext("2d").putImageData(e,0,0);return new Cursor(h,n,l)}
const getArray2D=function(e=1,n=1,l=0){e=Array(e);for(var h=0;h<e.length;h++)e[h]=Array(n);for(n=0;n<e.length;n++)for(h=0;h<e[n].length;h++)e[n][h]=l;return e};class Cursor{constructor(e,n,l){this._icon=e;this._hx=n*e.width;this._hy=l*e.height}get icon(){return this._icon}get hitX(){return this._hx}get hitY(){return this._hy}}
const [ARROW,BALL,DART,DIAMOND,HEXAGON,STAR,TRIANGLE,TURTLE]=function(){let e=[];e.push(csrARROW(20));e.push(csrBALL(10));e.push(csrDART(20));e.push(csrDIAMOND(20));e.push(csrHEXAGON(20));e.push(csrSTAR(24));e.push(csrTRIANGLE(20));e.push(csrTURTLE(24,"limegreen","burlywood",1.5,"saddlebrown"));return e}();function csrARROW(e,n="greenyellow",l=1,h="black"){e=__csrFromVertices(e,.1,[1,4,1,6,5,6,5,8,9,5,5,2,5,4],n,l,h);return new Cursor(e,.5,.5)}
function csrBALL(e,n="greenyellow",l=1,h="black"){let q=e/2,p=new OffscreenCanvas(e,e),k=p.getContext("2d");k.clearRect(0,0,e,e);k.fillStyle=n;k.strokeStyle=h;k.lineWidth=l;k.beginPath();k.translate(q,q);k.ellipse(0,0,q,q,0,0,2*Math.PI);k.fill();0<l&&k.stroke();k.closePath();return new Cursor(p,.5,.5)}function csrDART(e,n="greenyellow",l=1,h="black"){c=[1,1,4,5,1,9,9,5];e=__csrFromVertices(e,.1,c,n,l,h);return new Cursor(e,.5,.5)}
function csrDIAMOND(e,n="greenyellow",l=1,h="black"){e=__csrFromVertices(e,.05,[5,10,10,19,15,10,10,1],n,l,h);return new Cursor(e,.5,.5)}function csrHEXAGON(e,n="greenyellow",l=1,h="black"){nbrSides=6;let q=2*Math.PI/nbrSides,p=[];for(let k=0;k<nbrSides;k++)p.push(.95*Math.cos(k*q)),p.push(.95*Math.sin(k*q));e=__csrFromVertices(e,.5,p,n,l,h,[.5,.5]);return new Cursor(e,.5,.5)}
function csrSTAR(e,n="greenyellow",l=1,h="black"){nbrPts=5;let q=Math.PI/nbrPts,p=[];for(let k=0;k<2*nbrPts;k++){let z=0==k%2?.95:.25;p.push(z*Math.cos(k*q));p.push(z*Math.sin(k*q))}e=__csrFromVertices(e,.5,p,n,l,h,[.5,.5]);return new Cursor(e,.5,.5)}function csrTRIANGLE(e,n="greenyellow",l=1,h="black"){e=__csrFromVertices(e,.1,[3,2,3,8,9,5],n,l,h);return new Cursor(e,.5,.5)}
function csrTURTLE(e,n="limegreen",l="burlywood",h=1,q="saddlebrown"){var p=e/2,k=h/3;let z=new OffscreenCanvas(e,e),f=z.getContext("2d");f.clearRect(0,0,e,e);f.translate(p,p);p=.27*e;var u=.2*e,r=31*Math.PI/180;f.fillStyle=n;f.strokeStyle=q;f.lineWidth=k;f.beginPath();f.ellipse(-p,u,.2*e,.08*e,-r,0,2*Math.PI);f.ellipse(-p,-u,.2*e,.08*e,r,0,2*Math.PI);f.fill();f.stroke();p=.1*e;u=.2*e;r=0*Math.PI/180;f.beginPath();f.ellipse(p,u,.08*e,.2*e,-r,0,2*Math.PI);f.ellipse(p,-u,.08*e,.2*e,r,0,2*Math.PI);f.fill();
f.stroke();f.beginPath();f.ellipse(-.37*e,0,.2*e,.05*e,0,0,2*Math.PI);f.fill();f.stroke();f.beginPath();f.ellipse(.3*e,0,.175*e,.125*e,0,0,2*Math.PI);f.fill();f.stroke();n=-.05*e;p=.35*e;u=.25*e;f.fillStyle=l;f.strokeStyle=q;f.lineWidth=h;f.beginPath();f.ellipse(n,0,p,u,0,0,2*Math.PI);f.fill();f.stroke();l=getArray2D(6,2);h=getArray2D(6,2);e*=.13;for(q=0;q<l.length;q++){var x=(30+60*q)*Math.PI/180;r=Math.sin(x);x=Math.cos(x);l[q][0]=n+e*x;l[q][1]=e*r;h[q][0]=n+p*x;h[q][1]=u*r}f.lineWidth=k;f.beginPath();
for(k=0;k<l.length;k++)f.moveTo(h[k][0],h[k][1]),f.lineTo(l[k][0],l[k][1]);for(k=0;k<l.length;k++)f.lineTo(l[k][0],l[k][1]);f.stroke();return new Cursor(z,.5,.5)}
function __csrFromVertices(e,n,l,h,q=0,p="transparent",k=[0,0]){l=l.map(u=>u*n*e);let z=new OffscreenCanvas(e,e),f=z.getContext("2d");f.clearRect(0,0,e,e);f.translate(e*k[0],e*k[1]);f.fillStyle=h;f.strokeStyle=p;f.lineWidth=q;h=l.shift();p=l.shift();f.beginPath();for(f.moveTo(h,p);2<=l.length;)f.lineTo(l.shift(),l.shift());f.lineTo(h,p);f.fill();0<q&&f.stroke();f.closePath();return z};