Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Mandelbrot_Set.html
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>Mandelbrot Set</title>
- <style>
- canvas {
- border: 1px solid black;
- }
- </style>
- </head>
- <body>
- <canvas id="canvas" width="500" height="500"></canvas>
- <script>
- var canvas = document.getElementById("canvas");
- var ctx = canvas.getContext("2d");
- var xRange = 3.5;
- var yRange = 2.0;
- var xOffset = -0.75;
- var yOffset = 0.0;
- var colors = [];
- function z(r, g, b) {
- colors.push([r, g, b]);
- }
- let r = 255, g = 0, b = 0;
- for (let g = 0; g < 256; g++) {
- z(r, g, b);
- }
- for (r = 254; r >= 0; r--) {
- z(r, g, b);
- }
- for (let b = 0; b < 256; b++) {
- z(r, g, b);
- }
- for (g = 254; g >= 0; g--) {
- z(r, g, b);
- }
- for (r = 0; r < 256; r++) {
- z(r, g, b);
- }
- for (let b = 254; b >= 0; b--) {
- z(r, g, b);
- }
- var numColors = colors.length;
- function calculateMandelbrot(xRange, yRange, xOffset, yOffset) {
- var imageData = ctx.createImageData(canvas.width, canvas.height);
- var data = imageData.data;
- for (var y = 0; y < canvas.height; y++) {
- for (var x = 0; x < canvas.width; x++) {
- var zx = 0;
- var zy = 0;
- var cx = (x - canvas.width / 2) * xRange / canvas.width + xOffset;
- var cy = (y - canvas.height / 2) * yRange / canvas.height + yOffset;
- var i = 0;
- while (zx * zx + zy * zy < 4 && i < 500) {
- var xt = zx * zx - zy * zy + cx;
- zy = 2 * zx * zy + cy;
- zx = xt;
- i++;
- }
- var i = Math.floor(i * 20 + yRange) % numColors;
- var index = (x + y * canvas.width) * 4;
- data[index + 0] = colors[i][0];
- data[index + 1] = colors[i][1];
- data[index + 2] = colors[i][2];
- data[index + 3] = 255;
- }
- }
- ctx.putImageData(imageData, 0, 0);
- }
- calculateMandelbrot(xRange, yRange, xOffset, yOffset);
- var zoomFactor = 1.12;
- var dragStart = null;
- canvas.addEventListener("wheel", function(event) {
- event.preventDefault();
- var x = event.clientX - canvas.offsetLeft;
- var y = event.clientY - canvas.offsetTop;
- var zx = (x - canvas.width / 2) * xRange / canvas.width + xOffset;
- var zy = (y - canvas.height / 2) * yRange / canvas.height + yOffset;
- if (event.deltaY < 0) {
- xRange /= zoomFactor;
- yRange /= zoomFactor;
- } else {
- xRange *= zoomFactor;
- yRange *= zoomFactor;
- }
- xOffset = zx - (x - canvas.width / 2) * xRange / canvas.width;
- yOffset = zy - (y - canvas.height / 2) * yRange / canvas.height;
- calculateMandelbrot(xRange, yRange, xOffset, yOffset);
- });
- canvas.addEventListener("mousedown", function(event) {
- dragStart = {
- x: event.clientX,
- y: event.clientY,
- xOffset: xOffset,
- yOffset: yOffset
- };
- });
- canvas.addEventListener("mouseup", function(event) {
- dragStart = null;
- });
- canvas.addEventListener("mousemove", function(event) {
- if (dragStart) {
- var xDelta = event.clientX - dragStart.x;
- var yDelta = event.clientY - dragStart.y;
- xOffset = dragStart.xOffset - xDelta * xRange / canvas.width;
- yOffset = dragStart.yOffset - yDelta * yRange / canvas.height;
- calculateMandelbrot(xRange, yRange, xOffset, yOffset);
- }
- });
- document.addEventListener("keydown", function(event) {
- if (event.keyCode == 38) {
- xRange /= zoomFactor;
- yRange /= zoomFactor;
- calculateMandelbrot(xRange, yRange, xOffset, yOffset);
- } else if (event.keyCode == 40) {
- xRange *= zoomFactor;
- yRange *= zoomFactor;
- calculateMandelbrot(xRange, yRange, xOffset, yOffset);
- }
- });
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement