Skip to content
TheChatArchiveThe Chat Archive
o3-mini· OpenAIHugging Face LMArena Human Preference 140K

please, code me an interactive Mandelbrot zoom website. all in one .html file

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Qwenqwen3-235b-a22b-instruct-2507’s answer.
  1. User

    please, code me an interactive Mandelbrot zoom website. all in one .html file

  2. Assistant

    Below is a complete self‐contained HTML file that displays an interactive Mandelbrot explorer. Simply save the code below as (for example) index.html and open it in your browser. You can click and drag on the canvas to select a region you want to zoom into.

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>Interactive Mandelbrot Zoom</title>
      <style>
        html, body {
          margin: 0;
          padding: 0;
          background: #000;
          overflow: hidden;
        }
        canvas {
          display: block;
          background: #000;
          cursor: crosshair;
        }
      </style>
    </head>
    <body>
      <canvas id="mandelbrot"></canvas>
      <script>
        // Get canvas and set its size to full-window
        const canvas = document.getElementById("mandelbrot");
        const ctx = canvas.getContext("2d");
    
        canvas.width = window.innerWidth;
        canvas.height = window.innerHeight;
    
        // Mandelbrot view parameters: initial region in the complex plane.
        let view = {
          xMin: -2.5,
          xMax: 1,
          yMin: -1,
          yMax: 1
        };
    
        let maxIter = 100; // Maximum iterations for the escape-time algorithm
    
        // Convert HSV to RGB for coloring.
        function hsvToRgb(h, s, v) {
          let r, g, b;
          let i = Math.floor(h * 6);
          let f = h * 6 - i;
          let p = v * (1 - s);
          let q = v * (1 - f * s);
          let t = v * (1 - (1 - f) * s);
          switch (i % 6) {
            case 0: r = v, g = t, b = p; break;
            case 1: r = q, g = v, b = p; break;
            case 2: r = p, g = v, b = t; break;
            case 3: r = p, g = q, b = v; break;
            case 4: r = t, g = p, b = v; break;
            case 5: r = v, g = p, b = q; break;
          }
          return [Math.floor(r * 255), Math.floor(g * 255), Math.floor(b * 255)];
        }
    
        // Function to render the Mandelbrot set onto the canvas.
        function mandelbrot() {
          const width = canvas.width;
          const height = canvas.height;
          const imgData = ctx.createImageData(width, height);
          const data = imgData.data;
    
          for (let py = 0; py < height; py++) {
            // Map pixel y-coordinate to imaginary part
            let y0 = view.yMin + (py / height) * (view.yMax - view.yMin);
            for (let px = 0; px < width; px++) {
              // Map pixel x-coordinate to real part
              let x0 = view.xMin + (px / width) * (view.xMax - view.xMin);
              let x = 0, y = 0;
              let iteration = 0;
              while ((x * x + y * y <= 4) && (iteration < maxIter)) {
                // Mandelbrot iteration: z = z² + c.
                let xTemp = x * x - y * y + x0;
                y = 2 * x * y + y0;
                x = xTemp;
                iteration++;
              }
              // Calculate the pixel's index in the ImageData array.
              const index = (py * width + px) * 4;
              if (iteration === maxIter) {
                // Inside the Mandelbrot set: color black.
                data[index] = data[index + 1] = data[index + 2] = 0;
              } else {
                // Outside: color based on escape iteration.
                const color = hsvToRgb(iteration / maxIter, 1, 1);
                data[index] = color[0];
                data[index + 1] = color[1];
                data[index + 2] = color[2];
              }
              // Fully opaque
              data[index + 3] = 255;
            }
          }
    
          ctx.putImageData(imgData, 0, 0);
        }
    
        // Initial rendering.
        mandelbrot();
    
        // Variables for handling mouse drag for zooming.
        let isDragging = false;
        let dragStart = null;
        let dragEnd = null;
    
        // Mouse event listeners for interactive zoom selection.
        canvas.addEventListener("mousedown", (e) => {
          isDragging = true;
          dragStart = { x: e.offsetX, y: e.offsetY };
          dragEnd = null;
        });
    
        canvas.addEventListener("mousemove", (e) => {
          if (isDragging) {
            dragEnd = { x: e.offsetX, y: e.offsetY };
            // Redraw the Mandelbrot and overlay selection rectangle.
            mandelbrot();
            drawSelection();
          }
        });
    
        canvas.addEventListener("mouseup", (e) => {
          if (isDragging) {
            dragEnd = { x: e.offsetX, y: e.offsetY };
            isDragging = false;
            zoomToSelection();
          }
        });
    
        // When the window resizes, adjust the canvas and redraw.
        window.addEventListener("resize", () => {
          canvas.width = window.innerWidth;
          canvas.height = window.innerHeight;
          mandelbrot();
        });
    
        // Draws a white rectangle over the canvas to indicate the selected zoom region.
        function drawSelection() {
          if (dragStart && dragEnd) {
            const x = Math.min(dragStart.x, dragEnd.x);
            const y = Math.min(dragStart.y, dragEnd.y);
            const w = Math.abs(dragStart.x - dragEnd.x);
            const h = Math.abs(dragStart.y - dragEnd.y);
            ctx.strokeStyle = "white";
            ctx.lineWidth = 2;
            ctx.strokeRect(x, y, w, h);
          }
        }
    
        // Update the view region based on the selected rectangle and redraw.
        function zoomToSelection() {
          if (!dragStart || !dragEnd) return;
    
          // Coordinates in the canvas.
          const x1 = Math.min(dragStart.x, dragEnd.x);
          const x2 = Math.max(dragStart.x, dragEnd.x);
          const y1 = Math.min(dragStart.y, dragEnd.y);
          const y2 = Math.max(dragStart.y, dragEnd.y);
    
          // Ignore very small drags.
          if ((x2 - x1) < 5 || (y2 - y1) < 5) return;
    
          // Map canvas coordinates to complex plane coordinates.
          const newXMin = view.xMin + (x1 / canvas.width) * (view.xMax - view.xMin);
          const newXMax = view.xMin + (x2 / canvas.width) * (view.xMax - view.xMin);
          const newYMin = view.yMin + (y1 / canvas.height) * (view.yMax - view.yMin);
          const newYMax = view.yMin + (y2 / canvas.height) * (view.yMax - view.yMin);
    
          view = { xMin: newXMin, xMax: newXMax, yMin: newYMin, yMax: newYMax };
    
          // Optionally, increase the iteration count for more detail when zoomed in.
          maxIter = Math.min(maxIter + 50, 1000);
    
          mandelbrot();
          dragStart = dragEnd = null;
        }
      </script>
    </body>
    </html>
    

    How It Works

    • Rendering the Mandelbrot Set:
      The mandelbrot() function cycles through each pixel, maps it to the corresponding complex number, iteratively computes whether it escapes, and colors it accordingly.

    • Coloring:
      Outside points are colored using the hsvToRgb() helper function, turning the iteration count into a hue value, while points inside the set are black.

    • Interactivity:
      You can click and drag anywhere on the canvas. A white rectangle shows the selected region. When you release the mouse button, the view region is updated to the selected area and the Mandelbrot set is re-rendered with increased iterations for more detail.

    • Responsive Canvas:
      The canvas resizes with the browser window and redraws the current view.

    Enjoy exploring the Mandelbrot set!

  3. User

    thanks! do you think you could make it a bit more efficient and a bit more higher resolution when zoomed in? right now, resolution drops drastically when I zoom in

  4. Assistant

    Below is an updated version that aims to improve both efficiency and on‐screen resolution when zoomed in. The changes include:

    1. High‑Resolution Canvas: The code now uses the device’s pixel ratio to render off‑screen at a higher resolution (so details at high zoom are sharper) and then scales down to fit the viewport.

    2. Minor Loop Optimizations: Some loop variables are cached, and inner loops have been slightly re‐organized for performance.

    Simply save this code as an HTML file and open it in your browser.


    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>Interactive Mandelbrot Zoom - High Resolution</title>
      <style>
        html, body {
          margin: 0;
          padding: 0;
          background: #000;
          overflow: hidden;
        }
        canvas {
          display: block;
          background: #000;
          cursor: crosshair;
        }
      </style>
    </head>
    <body>
      <canvas id="mandelbrot"></canvas>
      <script>
        const canvas = document.getElementById("mandelbrot");
        const ctx = canvas.getContext("2d");
    
        // Keep track of canvas dimensions
        let logicalWidth = window.innerWidth;
        let logicalHeight = window.innerHeight;
    
        // Set up high resolution canvas based on device pixel ratio.
        function resizeCanvas() {
          const ratio = window.devicePixelRatio || 1;
          logicalWidth = window.innerWidth;
          logicalHeight = window.innerHeight;
          canvas.width = logicalWidth * ratio;
          canvas.height = logicalHeight * ratio;
          canvas.style.width = logicalWidth + "px";
          canvas.style.height = logicalHeight + "px";
          // Scale the context so we can use logical coordinates.
          ctx.setTransform(ratio, 0, 0, ratio, 0, 0);
          mandelbrot();
        }
        window.addEventListener("resize", resizeCanvas);
        resizeCanvas();
    
        // Mandelbrot view parameters.
        let view = {
          xMin: -2.5,
          xMax: 1,
          yMin: -1,
          yMax: 1
        };
    
        let maxIter = 100; // Maximum iterations
    
        // Convert HSV to RGB.
        function hsvToRgb(h, s, v) {
          let r, g, b;
          let i = Math.floor(h * 6);
          let f = h * 6 - i;
          let p = v * (1 - s);
          let q = v * (1 - f * s);
          let t = v * (1 - (1 - f) * s);
          switch (i % 6) {
            case 0: r = v; g = t; b = p; break;
            case 1: r = q; g = v; b = p; break;
            case 2: r = p; g = v; b = t; break;
            case 3: r = p; g = q; b = v; break;
            case 4: r = t; g = p; b = v; break;
            case 5: r = v; g = p; b = q; break;
          }
          return [Math.floor(r * 255), Math.floor(g * 255), Math.floor(b * 255)];
        }
    
        // Render the Mandelbrot set.
        function mandelbrot() {
          const width = logicalWidth;
          const height = logicalHeight;
          const imgData = ctx.createImageData(width, height);
          const data = imgData.data;
    
          // Pre-calculate pixel-to-complex mapping increments.
          const dx = (view.xMax - view.xMin) / width;
          const dy = (view.yMax - view.yMin) / height;
    
          let index = 0;
          for (let py = 0; py < height; py++) {
            const y0 = view.yMin + py * dy;
            for (let px = 0; px < width; px++) {
              const x0 = view.xMin + px * dx;
              let x = 0, y = 0;
              let iteration = 0;
              // Iterate the Mandelbrot function.
              while (x * x + y * y <= 4 && iteration < maxIter) {
                const xtemp = x * x - y * y + x0;
                y = 2 * x * y + y0;
                x = xtemp;
                iteration++;
              }
              if (iteration === maxIter) {
                // Inside the set → black.
                data[index] = data[index + 1] = data[index + 2] = 0;
              } else {
                const color = hsvToRgb(iteration / maxIter, 1, 1);
                data[index]     = color[0];
                data[index + 1] = color[1];
                data[index + 2] = color[2];
              }
              data[index + 3] = 255;
              index += 4;
            }
          }
          ctx.putImageData(imgData, 0, 0);
        }
    
        // Variables for zoom selection
        let isDragging = false;
        let dragStart = null;
        let dragEnd = null;
    
        canvas.addEventListener("mousedown", (e) => {
          isDragging = true;
          dragStart = { x: e.offsetX, y: e.offsetY };
          dragEnd = null;
        });
    
        canvas.addEventListener("mousemove", (e) => {
          if (isDragging) {
            dragEnd = { x: e.offsetX, y: e.offsetY };
            mandelbrot();
            drawSelection();
          }
        });
    
        canvas.addEventListener("mouseup", (e) => {
          if (isDragging) {
            dragEnd = { x: e.offsetX, y: e.offsetY };
            isDragging = false;
            zoomToSelection();
          }
        });
    
        // Draw selection rectangle.
        function drawSelection() {
          if (dragStart && dragEnd) {
            const x = Math.min(dragStart.x, dragEnd.x);
            const y = Math.min(dragStart.y, dragEnd.y);
            const w = Math.abs(dragStart.x - dragEnd.x);
            const h = Math.abs(dragStart.y - dragEnd.y);
            ctx.strokeStyle = "white";
            ctx.lineWidth = 2;
            ctx.strokeRect(x, y, w, h);
          }
        }
    
        // Update view region based on selection and redraw.
        function zoomToSelection() {
          if (!dragStart || !dragEnd) return;
    
          const x1 = Math.min(dragStart.x, dragEnd.x);
          const x2 = Math.max(dragStart.x, dragEnd.x);
          const y1 = Math.min(dragStart.y, dragEnd.y);
          const y2 = Math.max(dragStart.y, dragEnd.y);
    
          // Avoid accidental small drags.
          if ((x2 - x1) < 5 || (y2 - y1) < 5) return;
    
          const newXMin = view.xMin + (x1 / logicalWidth) * (view.xMax - view.xMin);
          const newXMax = view.xMin + (x2 / logicalWidth) * (view.xMax - view.xMin);
          const newYMin = view.yMin + (y1 / logicalHeight) * (view.yMax - view.yMin);
          const newYMax = view.yMin + (y2 / logicalHeight) * (view.yMax - view.yMin);
    
          view = {
            xMin: newXMin,
            xMax: newXMax,
            yMin: newYMin,
            yMax: newYMax
          };
    
          // Increase iterations when zooming in for more detail (capped).
          maxIter = Math.min(maxIter + 50, 1000);
    
          // Render the new view.
          mandelbrot();
          dragStart = dragEnd = null;
        }
      </script>
    </body>
    </html>
    

    Explanation of the Improvements

    1. High‑Resolution Canvas Rendering:
      The resizeCanvas() function considers the devicePixelRatio so the canvas’s actual pixel dimensions are scaled up while the style dimensions remain the same. We then use ctx.setTransform to draw using logical coordinates. This ensures that when you zoom in, the details remain sharp rather than looking pixelated.

    2. Optimized Pixel Mapping:
      The mapping from screen pixels to complex plane coordinates is pre‑computed for both x and y directions (using dx and dy) so that we don’t calculate the same information repeatedly inside the inner loop.

    3. Adaptive Detail:
      Each zoom increases the maximum iterations (up to a capped value) so more detail is computed for zoomed regions.

    Enjoy exploring a sharper and slightly more efficient Mandelbrot experience!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.