Create a website with fractals
- User
Create a website with fractals
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Create a website with fractals
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Creating a website that displays fractals can be a fun and visually stunning project! Below is a simple example of how you can create a basic HTML/JavaScript-based website that generates and displays fractals using the HTML5 Canvas API.
We’ll start with a Mandelbrot set fractal, one of the most famous fractals.
/fractal-website/
│
├── index.html
├── style.css
└── script.js
index.html<!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="style.css" />
</head>
<body>
<h1>Mandelbrot Fractal Explorer</h1>
<canvas id="fractalCanvas"></canvas>
<script src="script.js"></script>
</body>
</html>
style.cssbody {
margin: 0;
font-family: sans-serif;
text-align: center;
background-color: #111;
color: white;
}
h1 {
margin-top: 10px;
}
canvas {
display: block;
margin: 0 auto;
border: 1px solid #444;
cursor: crosshair;
}
script.js – Mandelbrot Rendererconst canvas = document.getElementById('fractalCanvas');
const ctx = canvas.getContext('2d');
let width = window.innerWidth;
let height = window.innerHeight;
canvas.width = width;
canvas.height = height;
// Fractal settings
let minRe = -2.5;
let maxRe = 1;
let minIm = -1;
let maxIm = 1;
const maxIterations = 300;
function drawFractal() {
let reFactor = (maxRe - minRe) / (width - 1);
let imFactor = (maxIm - minIm) / (height - 1);
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
let c_re = minRe + x * reFactor;
let c_im = minIm + y * imFactor;
let z_re = c_re,
z_im = c_im;
let isInside = true;
let n;
for (n = 0; n < maxIterations; n++) {
let z_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) {
ctx.fillStyle = "black";
} else {
ctx.fillStyle = `hsl(${(n * 10) % 360}, 100%, 50%)`;
}
ctx.fillRect(x, y, 1, 1);
}
}
}
// Zoom functionality
canvas.addEventListener("click", (e) => {
const zoomFactor = 0.5;
let x = e.offsetX;
let y = e.offsetY;
let clickRe = minRe + x * (maxRe - minRe) / width;
let clickIm = minIm + y * (maxIm - minIm) / height;
let newWidth = (maxRe - minRe) * (1 - zoomFactor);
let newHeight = (maxIm - minIm) * (1 - zoomFactor);
minRe = clickRe - newWidth / 2;
maxRe = clickRe + newWidth / 2;
minIm = clickIm - newHeight / 2;
maxIm = clickIm + newHeight / 2;
drawFractal();
});
drawFractal();
index.html, style.css, script.js) into a folder.index.html in your browser.You can extend this project by:
Want to put it online? Try:
Just upload your code and share the link!
Would you like me to help you add features like Julia set switching, color customization, or even WebGL acceleration?