Skip to content
The Chat Archive
command-a-03-2025· Cohere LMArena Human Preference 140K

Create a website with fractals

  1. User

    Create a website with fractals

  2. Assistant

    Creating a website that showcases fractals can be a visually stunning and educational project. Below is a step-by-step guide to building a simple website that displays fractals using HTML, CSS, and JavaScript. We'll focus on generating the Mandelbrot Set, one of the most famous fractals.


    Step 1: Set Up the Project

    Create a folder for your project and add the following files:

    • index.html
    • styles.css
    • script.js

    Step 2: HTML (index.html)

    This file sets up the structure of the website.

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Fractal Explorer</title>
      <link rel="stylesheet" href="styles.css">
    </head>
    <body>
      <h1>Explore the Mandelbrot Set</h1>
      <canvas id="fractalCanvas"></canvas>
      <script src="script.js"></script>
    </body>
    </html>
    

    Step 3: CSS (styles.css)

    Style the website to make it visually appealing.

    body {
      font-family: Arial, sans-serif;
      text-align: center;
      background-color: #1a1a1a;
      color: #fff;
      margin: 0;
      padding: 20px;
    }
    
    h1 {
      margin-bottom: 20px;
    }
    
    canvas {
      border: 2px solid #fff;
      max-width: 100%;
      height: auto;
    }
    

    Step 4: JavaScript (script.js)

    This script generates the Mandelbrot Set and renders it on the canvas.

    const canvas = document.getElementById('fractalCanvas');
    const ctx = canvas.getContext('2d');
    
    // Set canvas size
    canvas.width = 800;
    canvas.height = 600;
    
    // Mandelbrot Set parameters
    const maxIterations = 100;
    const zoom = 1;
    const offsetX = -0.5;
    const offsetY = 0;
    
    // Function to calculate the Mandelbrot Set
    function mandelbrot(x, y) {
      let zx = 0;
      let zy = 0;
      let iteration = 0;
    
      while (iteration < maxIterations) {
        const temp = zx * zx - zy * zy + x;
        zy = 2 * zx * zy + y;
        zx = temp;
    
        if (zx * zx + zy * zy > 4) break;
        iteration++;
      }
    
      return iteration;
    }
    
    // Function to draw the Mandelbrot Set
    function drawMandelbrot() {
      const width = canvas.width;
      const height = canvas.height;
    
      for (let px = 0; px < width; px++) {
        for (let py = 0; py < height; py++) {
          const x = (px - width / 2) * (4.0 / width / zoom) + offsetX;
          const y = (py - height / 2) * (4.0 / height / zoom) + offsetY;
    
          const iteration = mandelbrot(x, y);
    
          const color = iteration === maxIterations ? 0 : (iteration % 8) * 32;
          ctx.fillStyle = `hsl(${color}, 100%, 50%)`;
          ctx.fillRect(px, py, 1, 1);
        }
      }
    }
    
    // Initial draw
    drawMandelbrot();
    

    Step 5: Enhance the Website

    1. Add Interactivity: Implement zooming and panning using mouse events.
    2. Include Explanations: Add a section explaining what fractals and the Mandelbrot Set are.
    3. Optimize Performance: Use WebGL for faster rendering of complex fractals.

    Step 6: Deploy the Website

    Host your website on platforms like:

    • GitHub Pages
    • Netlify
    • Vercel

    Example Enhancements

    • Zooming: Add event listeners for mouse wheel or click-and-drag to zoom into the fractal.
    • Color Palette: Allow users to change the color scheme.
    • Other Fractals: Include other fractals like the Julia Set or Sierpinski Triangle.

    Here’s a simple example of adding zoom functionality:

    let currentZoom = zoom;
    let currentOffsetX = offsetX;
    let currentOffsetY = offsetY;
    
    canvas.addEventListener('wheel', (event) => {
      const zoomFactor = event.deltaY > 0 ? 0.9 : 1.1;
      currentZoom *= zoomFactor;
      drawMandelbrot();
    });
    

    This website will provide an interactive and educational experience for exploring fractals. Happy coding!

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