Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <script>
- $(document).ready(
- function() {
- //Complex number object for use with the fractal rendering.
- function Complex(real, imag) {
- this.r = real;
- this.i = imag;
- }
- Complex.prototype.square = function() {
- return(new Complex(this.r * this.r,this.i * -this.i));
- }
- Complex.prototype.add = function(c) {
- return(new Complex(this.r + c.r, this.i + c.i));
- }
- Complex.prototype.abs = function() {
- return(Math.sqrt(this.c*this.c+this.i*this.i))
- }
- //gather canvas information
- var canvas = document.getElementById("myCanvas");
- var ctx = canvas.getContext("2d");
- var maxItr = 4; //maximum iterations to go through for rendering.
- var maxCol = 256; //maximum allowed colors.
- var Data = ctx.createImageData(canvas.width, canvas.height);
- for (var x = 0; x < Data.width; x++) { //pixel loop
- for (var y = 0; y < Data.height; y++) {
- //CALCULATING MANDELBROT SET
- var mx = (x/(Data.width/4))-2; //get the relevent location in the set.
- var my = (y/(Data.width/4))-2;
- var z = new Complex(0,0);
- var c = new Complex(mx,my);
- var itr = 0;
- while((z.abs() < 2) && (itr < maxItr)) {
- z = z.square.add(c);
- itr++;
- }
- var col = maxCol % itr;
- //SETTING IMAGE DATA
- var idx = (x + y * Data.width) * 4; // Index of the pixel in the array
- Data.data[idx] = col; // Red channel
- Data.data[idx + 1] = col; // Green channel
- Data.data[idx + 2] = col; // Blue channel
- Data.data[idx + 3] = col; // Alpha channel
- }
- }
- var Data = ctx.putImageData(Data, 0, 0);
- }
- );
- </script>
Add Comment
Please, Sign In to add comment