Created
June 17, 2023 14:55
-
-
Save sjkp/95163fd5699e5751ce571bb1074a5aeb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<body> | |
<canvas id="mandelbrotCanvas" width="800" height="800"></canvas> | |
<script> | |
const canvas = document.getElementById('mandelbrotCanvas'); | |
const ctx = canvas.getContext('2d'); | |
const imgData = ctx.createImageData(canvas.width, canvas.height); | |
const MAX_ITERATION = 1000; | |
const X_MIN = -2.5, X_MAX = 1.5, Y_MIN = -2, Y_MAX = 2; | |
for (let i = 0; i < canvas.width; i++) { | |
for (let j = 0; j < canvas.height; j++) { | |
let x0 = i / canvas.width * (X_MAX - X_MIN) + X_MIN; | |
let y0 = j / canvas.height * (Y_MAX - Y_MIN) + Y_MIN; | |
let x = 0.0; | |
let y = 0.0; | |
let iteration = 0; | |
while (x*x + y*y <= 4 && iteration < MAX_ITERATION) { | |
let xTemp = x*x - y*y + x0; | |
y = 2*x*y + y0; | |
x = xTemp; | |
iteration++; | |
} | |
let color = iteration === MAX_ITERATION ? 0 : 255; | |
let pixelIndex = (i + j * canvas.width) * 4; | |
imgData.data[pixelIndex] = color; // Red | |
imgData.data[pixelIndex + 1] = color; // Green | |
imgData.data[pixelIndex + 2] = color; // Blue | |
imgData.data[pixelIndex + 3] = 255; // Alpha | |
} | |
} | |
ctx.putImageData(imgData, 0, 0); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment