var JsBonsai=(()=>{var m=Object.defineProperty;var k=Object.getOwnPropertyDescriptor;var Y=Object.getOwnPropertyNames;var A=Object.prototype.hasOwnProperty;var Z=(r,t)=>{for(var o in t)m(r,o,{get:t[o],enumerable:!0})},T=(r,t,o,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let e of Y(t))!A.call(r,e)&&e!==o&&m(r,e,{get:()=>t[e],enumerable:!(i=k(t,e))||i.enumerable});return r};var y=r=>T(m({},"__esModule",{value:!0}),r);var P={};Z(P,{default:()=>N});var x=document.createElement("div"),R=["trunk","shootleft","shootright","dying","dead"];function B(r){x.style.color=r,x.style.display="none",document.body.appendChild(x);let t=getComputedStyle(x).color;document.body.removeChild(x);let o=t.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*([\d.]+))?\)$/);return o?{r:parseInt(o[1],10),g:parseInt(o[2],10),b:parseInt(o[3],10),a:o[4]!==void 0?parseFloat(o[4]):1}:null}function L(r,t){let o=B(r),i=Math.max(0,Math.min(255,Math.floor(o.r*(1-t)))),e=Math.max(0,Math.min(255,Math.floor(o.g*(1-t)))),s=Math.max(0,Math.min(255,Math.floor(o.b*(1-t))));return o.a!==1?`rgba(${i}, ${e}, ${s}, ${o.a})`:`rgb(${i}, ${e}, ${s})`}function _(r){return Math.floor(Math.random()*r)}function b(r){return new Promise(t=>setTimeout(t,r))}function I(r){return Math.floor(Math.random()*r)}function G(r,t,o){switch(t<4&&(r="dying"),r){case"trunk":case"shootLeft":case"shootRight":return Math.random()<.7?o.branchColor:L(o.branchColor,.25);case"dying":return Math.random()<.5?o.leavesColor:L(o.leavesColor,.2);case"dead":return Math.random()<.1?o.deadLeavesColor:L(o.deadLeavesColor,.15);default:return o.branchColor}}function z(r,t,o,i){let e=0,s=0,a;switch(r){case"trunk":o<=2||t<4?(s=0,e=_(3)-1):o=1&&a<=3?e=-1:a>=4&&a<=5?e=0:a>=6&&a<=8?e=1:a===9&&(e=2)):(a=_(10),s=a>2?-1:0,e=_(3)-1);break;case"shootleft":a=_(10),a<=1?s=-1:a<=7?s=0:s=1,a=_(10),a<=1?e=-2:a<=5?e=-1:a<=8?e=0:e=1;break;case"shootright":a=_(10),a<=1?s=-1:a<=7?s=0:s=1,a=_(10),a<=1?e=2:a<=5?e=1:a<=8?e=0:e=-1;break;case"dying":a=_(10),a<=1?s=-1:a<=8?s=0:s=1,a=_(15),a===0?e=-3:a<=2?e=-2:a<=5?e=-1:a<=8?e=0:a<=11?e=1:a<=13?e=2:e=3;break;case"dead":a=_(10),a<=2?s=-1:a<=6?s=0:s=1,e=_(3)-1;break}return{dx:e,dy:s}}function $(r,t,o,i,e=["&"]){let s="?";switch(t<4&&(r="dying"),r){case"trunk":i===0?s="/~":o<0?s="\\|":o===0?s="/|\\":o>0&&(s="|/");break;case"shootleft":i>0?s="\\":i===0?s="\\_":o<0?s="\\|":o===0?s="/|":o>0&&(s="/");break;case"shootright":i>0?s="/":i===0?s="_/":o<0?s="\\|":o===0?s="/|":o>0&&(s="/");break;case"dying":case"dead":e.length>0?s=e[Math.floor(Math.random()*e.length)]:s="&";break}return s}function u(r,t,o,i){r.font=`${t.CELL_SIZE}px monospace`,r.textBaseline="top",r.textAlign="left",r.fillStyle=i.potColor;let e=o,s=t.maxY-e.length,a=Math.floor((t.maxX-e[0].length)/2);e.forEach((h,c)=>{for(let l=0;l=i.MAX_BRANCHES||t<=0)return;a.branch++;let n=0,f=0,d=0,C=h*2;for(;t>0;){if(t--,d=o-t,{dx:n,dy:f}=z(r,t,d,h),f>0&&s>i.maxY-2&&f--,t<3)await E("dead",t,t,i,e,s,a,h,c,l);else if((r==="trunk"||r==="shootleft"||r==="shootright")&&t7){C=h*2;let S=t+I(5)-2;await E("trunk",S,S,i,e,s,a,h,c,l)}else if(C<=0){C=h*2;let S=t+h;a.shoots++,a.shootCounter++;let X=R[a.shootCounter%2+1];await E(X,S,S,i,e,s,a,h,c,l)}}C--,e+=n,s+=f,e=Math.min(e,i.maxX-3),s=Math.min(s,i.maxY);let g=i.CELL_SIZE,w=G(r,t,c),p=$(r,t,n,f,["&"]);i.ctx.font=`${g}px monospace`,i.ctx.fillStyle=w,i.ctx.textBaseline="top",i.ctx.textAlign="left",i.ctx.clearRect(e*i.CELL_SIZE+i.offsetX,s*i.CELL_SIZE+i.offsetY,g*p.length,g),i.ctx.fillText(p,e*i.CELL_SIZE+i.offsetX,s*i.CELL_SIZE+i.offsetY),await b(l)}}var H=document.querySelectorAll("[data-bonsai]"),M=[["(---./~~~\\.---)"," ( ) "," (_________) "],[":________./~~~\\.________:"," \\ / "," \\___________________/ "," (_) (_)"]],v=class{constructor(t){this.canvas=t,this.ctx=this.canvas.getContext("2d"),this.CELL_SIZE=parseInt(this.canvas.getAttribute("data-size"))||12,this.getDimensions(),this.cols=Math.floor(this.canvas.width/this.CELL_SIZE),this.rows=Math.floor(this.canvas.height/this.CELL_SIZE),this.alignX=this.canvas.getAttribute("data-align").split(" ")[1]||"center",this.alignY=this.canvas.getAttribute("data-align").split(" ")[0]||"center",this.offsetX=this.calculateOffset("x",this.alignX),this.offsetY=this.calculateOffset("y",this.alignY),this.maxX=this.offsetX+this.cols*this.CELL_SIZE,this.maxY=this.offsetY+this.rows*this.CELL_SIZE,this.treeGridSize=M[1][0].length,Object.assign(this,this.getUserInput())}getDimensions(){let t=getComputedStyle(this.canvas),o=parseInt(t.width),i=parseInt(t.height);this.canvas.width=o,this.canvas.height=i}getUserInput(){let t=parseInt(this.canvas.getAttribute("data-base-type"))||1,o=t<=2&&t>0?t-1:0,i=this.canvas.getAttribute("data-pot-color")||"grey",e=this.canvas.getAttribute("data-leaves-color")||"#5cbd9b",s=this.canvas.getAttribute("data-dead-leaves-color")||"#e26554",a=this.canvas.getAttribute("data-branch-color")||"#7c5c5c",h=this.canvas.getAttribute("data-repeat"),c=h==="true"||h==="",l=this.canvas.getAttribute("data-loop"),n=l==="true"||l==="",d=150-((parseInt(this.canvas.getAttribute("data-speed"))||7)-1)*(130/9),C=parseInt(this.canvas.getAttribute("data-life"))||22,g=C;return{multiplier:parseInt(this.canvas.getAttribute("data-multiplier"))||5,baseType:o,potColor:i,leavesColor:e,deadLeavesColor:s,branchColor:a,repeat:c,loop:n,sleepDuration:d,MAX_BRANCHES:500,life:C,startingLife:g}}calculateOffset(t,o){let i=t==="x"?this.cols:this.rows,e=t==="x"?this.canvas.width:this.canvas.height,s=i*this.CELL_SIZE,a=e-s;switch(o){case"left":case"top":return 0;case"right":case"bottom":return a;case"center":default:return Math.floor(a/2)}}getTreeGridOffset(t,o,i){let e=this.offsetX,s=this.offsetY,[a,h]=i.split(" ");switch(h){case"left":e=this.offsetX;break;case"center":e=this.offsetY+Math.floor((this.cols-t)*this.CELL_SIZE/2);break;case"right":e=this.maxX-t*this.CELL_SIZE;break;default:console.warn("Unknown horizontal align:",h)}switch(a){case"top":s=this.offsetX;break;case"center":s=this.offsetY+Math.floor((this.rows-o)*this.CELL_SIZE/2);break;case"bottom":s=this.maxY-o*this.CELL_SIZE;break;default:console.warn("Unknown vertical align:",a)}return{offsetX:e,offsetY:s,cols:t,rows:o}}drawTreeGrid(){let t=this.treeGridSize,o=this.treeGridSize,{offsetX:i,offsetY:e,cols:s,rows:a}=this.getTreeGridOffset(t,o,align);this.ctx.strokeStyle="rgba(255, 255, 255, 0.2)";for(let h=0;h<=a;h++){let c=e+h*this.CELL_SIZE;this.ctx.beginPath(),this.ctx.moveTo(i,c),this.ctx.lineTo(i+s*this.CELL_SIZE,c),this.ctx.stroke()}for(let h=0;h<=s;h++){let c=i+h*this.CELL_SIZE;this.ctx.beginPath(),this.ctx.moveTo(c,e),this.ctx.lineTo(c,e+a*this.CELL_SIZE),this.ctx.stroke()}}async drawTree(){let t=`${this.alignY} ${this.alignX}`,o=this.treeGridSize,i=this.treeGridSize,{offsetX:e,offsetY:s,cols:a,rows:h}=this.getTreeGridOffset(o,i,t),c=Math.floor(a/2),n={offsetX:e,offsetY:s,maxX:a,maxY:h,pointerX:c,pointerY:h,CELL_SIZE:this.CELL_SIZE,ctx:this.ctx,MAX_BRANCHES:this.MAX_BRANCHES||500},f=M[0],d={potColor:this.potColor,leavesColor:this.leavesColor,darkLeavesColor:L(this.leavesColor),branchColor:this.branchColor},C={branch:0,shoots:0,shootCounter:0},g=3;u(this.ctx,n,f,d),await E("trunk",this.life,this.startingLife,n,n.pointerX,n.pointerY,C,g,d,50)}async drawRepeatedTrees(){let t=[],o=this.treeGridSize,i=this.treeGridSize,e=o*this.CELL_SIZE,s=i*this.CELL_SIZE,a=Math.ceil(this.canvas.width/e),h=Math.ceil(this.canvas.height/s),c=M[this.baseType],l={potColor:this.potColor,leavesColor:this.leavesColor,darkLeavesColor:L(this.leavesColor),branchColor:this.branchColor};for(let n=0;n{new v(r).start()});var N=v;return y(P);})();