|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
既然话题已经抄起,我打算今晚发篇博文再引导一下舆论方向,使它再火两天,抛砖引玉,而且赵劼先生一直在跟帖,使.NET阵营的我感到万分难得。为了测试一下js和Canvas的盘算才能,做了一个Mandelbrot的分形图,撑持鼠标ZoomIn。Mandelbrot的界说很复杂,虚数立体的每一个点(x,y),经由过程重复盘算zn+1=zn2+c,z0=0。只要了局收敛才属于Mandelbrot,不然依据n的设定一个色彩,越年夜越深,代表靠近属于汇合的点,能够把汇合中的点了解为n=无量年夜。保举在chrome中翻开本页,还撑持firefox和ie9。拖动鼠标能够分明发明chrome是最流利的。
更加专业的Mandelbrot请check这里http://www.atopon.org/mandel/
- <canvasid="canvas"width="600"height="480"style="margin-left:100px"><p>Yourbrowserdoesnotsupportthecanvaselement.</p></canvas><p><inputid="reset"type="button"value="Reset"/></p><script>varcanvas=document.getElementById(canvas);varcontext=canvas.getContext(2d);varwidth=canvas.width,height=canvas.height;varmaxIterations=100;varminRe=-2.0;varmaxRe=1.0;varminIm=-1;varmaxIm=minIm+(maxRe-minRe)*height/width;reset();document.getElementById("reset").onclick=reset;canvas.onmousedown=function(evt){varx0=evt.pageX-canvas.offsetLeft;vary0=evt.pageY-canvas.offsetTop;varx1,y1,w,h;varimgd=context.getImageData(0,0,width,height);update(evt);functionupdate(evt){x1=evt.pageX-canvas.offsetLeft;y1=evt.pageY-canvas.offsetTop;w=Math.abs(x1-x0),h=Math.abs(y1-y0);}functionclear(evt){if(w&&h){context.clearRect(0,0,width,height);context.putImageData(imgd,0,0);}}canvas.onmousemove=function(evt){clear(evt);update(evt);context.strokeStyle="red";context.strokeRect(x0<x1?x0:x1,y0<y1?y0:y1,w,h);}canvas.onmouseup=function(evt){clear(evt);canvas.onmousemove=canvas.onmouseup=null;minRe=minRe+(maxRe-minRe)/width*(x0<x1?x0:x1);maxRe=minRe+(maxRe-minRe)/width*w;minIm=minIm+(maxIm-minIm)/height*(y0<y1?y0:y1);maxIm=minIm+(maxIm-minIm)/height*h;draw();}}functionreset(){minRe=-2.0;maxRe=1.0;minIm=-1;maxIm=minIm+(maxRe-minRe)*height/width;draw();}functiondraw(){context.fillRect(0,0,width,height);varimgd=context.getImageData(0,0,width,height)varpix=imgd.data;vardrawPixel=function(x,y,itr){vari=(y*width+x)*4;pix[i]=pix[i+1]=pix[i+2]=Math.round(itr*255/maxIterations);}mandelbrot(width,height,drawPixel);context.putImageData(imgd,0,0);}functionmandelbrot(imageWidth,imageHeight,drawPixel){varre_factor=(maxRe-minRe)/(imageWidth-1);varim_factor=(maxIm-minIm)/(imageHeight-1);for(vary=0;y<imageHeight;++y){varc_im=maxIm-y*im_factor;for(varx=0;x<imageWidth;++x){varc_re=minRe+x*re_factor;varz_re=c_re,z_im=c_im;varisInside=true;varn=0;for(;n<maxIterations;++n){varz_re2=z_re*z_re,z_im2=z_im*z_im;if(z_re2+z_im2>4){isInside=false;break;}z_im=2*z_re*z_im+c_im;z_re=z_re2-z_im2+c_re;}if(!isInside){drawPixel(x,y,n);}}}}</script>
复制代码
另外,小型软件代码重用价值低,没有必要跨平台;大型软件,有严格的规划、部署,不可以随意跨平台。 |
|