ex1.html  返回列表

001.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
002.<html xmlns="http://www.w3.org/1999/xhtml">
003.    <head>
004.        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
005.        <title>ExplorerCanvas Example 1</title>
006.        <script type="text/javascript" src="jquery-1.4.2.min.js">
007.        </script>
008.        <script type="text/javascript" src="canvas3.js">
009.        </script>
010.        <!--[if IE]><script type="text/javascript" src="excanvas_r73.js"></script><![endif]-->
011.        <script type="text/javascript">
012.            var D;
013.            var width, height;
014.            var maxIterations = 20;
015.            var minRe = -2.0;
016.            var maxRe = 1.0;
017.            var minIm = -1;
018.            var maxIm;
019.            var animate;
020.            var step=5;
021.            $(function(){
022.                D = new canvas('canvas');
023.                width = D.width;
024.                height = D.height;
025.                maxIm = minIm + (maxRe - minRe) * height / width;
026.                draw();
027.                D.cvs.onmousedown = function(evt){
028.                    var x0 = evt.pageX - D.cvs.offsetLeft;
029.                    var y0 = evt.pageY - D.cvs.offsetTop;
030.                     
031.                    var x1, y1, w, h;
032.                    var imgd = D.ctx.getImageData(0, 0, width, height);
033.                     
034.                    update(evt);
035.                     
036.                    function update(evt){
037.                        x1 = evt.pageX - D.cvs.offsetLeft;
038.                        y1 = evt.pageY - D.cvs.offsetTop;
039.                        w = Math.abs(x1 - x0), h = Math.abs(y1 - y0);
040.                        //h=w;
041.                         
042.                    }
043.                     
044.                    function clear(evt){
045.                        if (w && h) {
046.                            D.clear();
047.                            D.ctx.putImageData(imgd, 0, 0);
048.                        }
049.                    }
050.                     
051.                    D.cvs.onmousemove = function(evt){
052.                        clear(evt);
053.                        update(evt);
054.                        D.set({
055.                            "strokeStyle": "red"
056.                        });
057.                        D.rect(x0 < x1 ? x0 : x1, y0 < y1 ? y0 : y1, w, h);
058.                    }
059.                     
060.                    D.cvs.onmouseup = function(evt){
061.                        clear(evt);
062.                        D.cvs.onmousemove = D.cvs.onmouseup = null;
063.                         
064.                        minRe = minRe + (maxRe - minRe) / width * (x0 < x1 ? x0 : x1);
065.                        maxRe = minRe + (maxRe - minRe) / width * w;
066.                        minIm = minIm + (maxIm - minIm) / height * (y0 < y1 ? y0 : y1);
067.                        maxIm = minIm + (maxIm - minIm) / height * h;
068.                         
069.                        draw();
070.                    }
071.                };
072.                $('#reset').click(function(){
073.                    minRe = -2.0;
074.                    maxRe = 1.0;
075.                    minIm = -1;
076.                    maxIm = minIm + (maxRe - minRe) * height / width;
077.                     
078.                    draw();
079.                });
080.            });
081.            function draw(){
082.                /*
083.                D.set({
084.                    "fillStyle": "rgba(" + parseInt(Math.random() * 255) + "," + parseInt(Math.random() * 255) + "," + parseInt(Math.random() * 255) + ",75)"
085.                });
086.                */
087.                D.set({
088.                    "fillStyle": "rgba(30,90,150,15)"
089.                });
090.                D.rect(0, 0, width, height, true);
091.                 
092.                //var imgd = D.ctx.getImageData(0, 0, width, height);
093.                //var pix = imgd.data;
094.                var drawPixel = function(x, y, itr){
095.                    //var i = (y * width + x) * 4;
096.                    //pix[i] = pix[i + 1] = pix[i + 2] = pix[i + 3]=Math.round(itr * 255 / maxIterations);
097.                    D.circle(x,y,1);
098.                }
099.                //animate=setInterval("mandelbrot2("+width+", "+height+");",1);
100.                mandelbrot(width, height, drawPixel);
101.                //D.ctx.putImageData(imgd, 0, 0);
102.            }
103.            var x=1,y=1,n=1;
104.            var z_im=false,z_re;
105.            var _x=false;
106.            function mandelbrot2(imageWidth, imageHeight){
107.                var re_factor = (maxRe - minRe) / (imageWidth - 1);
108.                var im_factor = (maxIm - minIm) / (imageHeight - 1);
109.                if(n<maxIterations){
110.                    n+=step;
111.                }else{
112.                    n=1;
113.                    if(x<imageWidth){
114.                        x+=step;
115.                    }else{
116.                        x=1;
117.                        if(y<imageHeight){
118.                            y+=step;
119.                        }else{
120.                            clearInterval(animate);
121.                            alert("over");
122.                        }
123.                    }
124.                }
125.                //console.log(n,x,y);
126.                var c_im = maxIm - y * im_factor;
127.                 
128.                var c_re = minRe + x * re_factor;
129.                if((!z_im && !_x) || (_x && _x!=x)){
130.                    z_re = c_re, z_im = c_im;
131.                    _x=x;
132.                }
133.                 
134.                 
135.                var z_re2 = z_re * z_re, z_im2 = z_im * z_im;
136.                if (z_re2 + z_im2 > 4) {
137.                    D.set({
138.                    "fillStyle": "rgb(0,0,0)"
139.                });
140.                    D.circle(x,y,step,true);
141.                }else{
142.                    z_im = 2 * z_re * z_im + c_im;
143.                    z_re = z_re2 - z_im2 + c_re;
144.                }
145.            }
146.             
147.            function mandelbrot(imageWidth, imageHeight, drawPixel){
148.                var re_factor = (maxRe - minRe) / (imageWidth - 1);
149.                var im_factor = (maxIm - minIm) / (imageHeight - 1);
150.                for (var y = 0; y < imageHeight; ++y) {
151.                    var c_im = maxIm - y * im_factor;
152.                    for (var x = 0; x < imageWidth; ++x) {
153.                        var c_re = minRe + x * re_factor;
154.                         
155.                        var z_re = c_re, z_im = c_im;
156.                        var isInside = true;
157.                        var n = 0;
158.                        for (; n < maxIterations; ++n) {
159.                            var z_re2 = z_re * z_re, z_im2 = z_im * z_im;
160.                            if (z_re2 + z_im2 > 4) {
161.                                isInside = false;
162.                                break;
163.                            }
164.                            z_im = 2 * z_re * z_im + c_im;
165.                            z_re = z_re2 - z_im2 + c_re;
166.                        }
167.                         
168.                        if (!isInside) {
169.                            drawPixel(x, y, n);
170.                        }
171.                    }
172.                }
173.            }
174.             
175.        </script>
176.    </head>
177.    <body>
178.        <canvas id="canvas" width="800" height="680" style="" title="canvas">
179.            <p>
180.                Your browser does not support the canvas element.
181.            </p>
182.        </canvas>
183.        <p>
184.            <input id="reset" type="button" value="Reset" />
185.        </p>
186.    </body>
187.</html>
Add New Content