mandelbrot.html 1.6 KB

1234567891011121314151617181920212223242526
  1. <!DOCTYPE html><html><head><meta charset="UTF-8"/><title>Benoît was here!</title><style>body{margin:20px;overflow:hidden;text-align:center;}canvas{height:512px;width:512px;display:inline-block;background-color:yellow;}</style></head><body onresize="Z()"><div style="display:inline-block;text-align:center;"><canvas></canvas><div id="c"></div><span id="p"></span></div><script>
  2. D=document;M=Math;g=(x)=>D.querySelector(x);Q=(V=g('canvas')).getContext('2d');
  3. I=2048;U={};G=[-2,-2,4];R=G.slice();H=[];T=[];
  4. X=(x,y)=>[x/W*R[2]+R[0],y/W*R[2]+R[1]];
  5. p=(c,e)=>{b=c.getBoundingClientRect();return[e.x-b.left,e.y-b.top]}
  6. E=(v,x,y)=>v.addEventListener(x,y)
  7. E(V,"mousedown",(e)=>{e.preventDefault();U[0]=p(V,e)})
  8. E(V,"mouseup",(e)=>{e.preventDefault();U[1]=p(V,e);
  9. [x,y]=X(U[0][0],U[0][1]);[w,z]=X(U[1][0],U[1][1]);
  10. [x,y,w,z]=[M.min(x,w),M.min(y,z),M.max(x,w),M.max(y,z)]
  11. d=M.max(w-x,z-y);
  12. if(d==0){d=R[2];x-=d/4;y-=d/4;d/=2}
  13. H.push(R.slice());T=[];R=[x,y,d];A()})
  14. Z=()=>{V.height=V.width=W=512}
  15. Z();
  16. A=()=>{g('#p').innerHTML=`${R[0]}+${R[1]}i<br/>${R[0]+R[2]}+${R[1]+R[2]}i`;
  17. u=new Uint8ClampedArray(W*W*4);for(y=0;y<W;y++){for(x=0;x<W;x++){
  18. v=a=b=0;[c,d]=X(x,y);for(;v++<I&&a*a+b*b<=4;){[a,b]=[a*a-b*b+c,2*a*b+d]}
  19. b=4*(y*W+x);f=(v<I)*255/2;u[b]=f+f*M.cos(v/8);
  20. u[b+1]=f+f*M.cos(v/M.sqrt(15));u[b+2]=0;u[b+3]=255}}
  21. Q.putImageData(new ImageData(u,W,W),0,0)}
  22. [[()=>{R=G.slice();A()},"reset"],[()=>{if(x=H.pop()){T.push(R.slice());R=x}else{R=G.slice();}A()},"<<"],[()=>{if(x=T.pop()){H.push(R.slice());R=x}A()},">>"]].forEach((l)=>{E(s=D.createElement("button"),'click',l[0]);s.innerText=l[1];g('#c').appendChild(s)});A();
  23. </script></body></html>