Guest User

Untitled

a guest
Aug 25th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.06 KB | None | 0 0
  1. // ------------------------------------------------------------------------
  2. // Drawing Mandelbrot Fractals With HTML5 Canvas And JavaScript
  3. // (c) 2015 Rembound.com
  4. // http://rembound.com/articles/drawing-mandelbrot-fractals-with-html5-canvas-and-javascript
  5. // ------------------------------------------------------------------------
  6.  
  7. // The function gets called when the window is fully loaded
  8. window.onload = function() {
  9. // Get the canvas and context
  10. var canvas = document.getElementById("viewport");
  11. var context = canvas.getContext("2d");
  12.  
  13. // Width and height of the image
  14. var imagew = canvas.width;
  15. var imageh = canvas.height;
  16.  
  17. // Image Data (RGBA)
  18. var imagedata = context.createImageData(imagew, imageh);
  19.  
  20. // Pan and zoom parameters
  21. var offsetx = -imagew/2;
  22. var offsety = -imageh/2;
  23. var panx = -100;
  24. var pany = 0;
  25. var zoom = 150;
  26.  
  27. // Palette array of 256 colors
  28. var palette = [];
  29.  
  30. // The maximum number of iterations per pixel
  31. var maxiterations = 250;
  32.  
  33. // Initialize the game
  34. function init() {
  35. // Add mouse events
  36. canvas.addEventListener("mousedown", onMouseDown);
  37.  
  38. // Generate palette
  39. generatePalette();
  40.  
  41. // Generate image
  42. generateImage();
  43.  
  44. // Enter main loop
  45. main(0);
  46. }
  47.  
  48. // Main loop
  49. function main(tframe) {
  50. // Request animation frames
  51. window.requestAnimationFrame(main);
  52.  
  53. // Draw the generate image
  54. context.putImageData(imagedata, 0, 0);
  55. }
  56.  
  57. // Generate palette
  58. function generatePalette() {
  59. // Calculate a gradient
  60. var roffset = 24;
  61. var goffset = 16;
  62. var boffset = 0;
  63. for (var i=0; i<256; i++) {
  64. palette[i] = { r:roffset, g:goffset, b:boffset};
  65.  
  66. if (i < 64) {
  67. roffset += 3;
  68. } else if (i<128) {
  69. goffset += 3;
  70. } else if (i<192) {
  71. boffset += 3;
  72. }
  73. }
  74. }
  75.  
  76. // Generate the fractal image
  77. function generateImage() {
  78. // Iterate over the pixels
  79. for (var y=0; y<imageh; y++) {
  80. for (var x=0; x<imagew; x++) {
  81. iterate(x, y, maxiterations);
  82. }
  83. }
  84. }
  85.  
  86. // Calculate the color of a specific pixel
  87. function iterate(x, y, maxiterations) {
  88. // Convert the screen coordinate to a fractal coordinate
  89. var x0 = (x + offsetx + panx) / zoom;
  90. var y0 = (y + offsety + pany) / zoom;
  91.  
  92. // Iteration variables
  93. var a = 0;
  94. var b = 0;
  95. var rx = 0;
  96. var ry = 0;
  97.  
  98. // Iterate
  99. var iterations = 0;
  100. while (iterations < maxiterations && (rx * rx + ry * ry <= 4)) {
  101. rx = a * a - b * b + x0;
  102. ry = 2 * a * b + y0;
  103.  
  104. // Next iteration
  105. a = rx;
  106. b = ry;
  107. iterations++;
  108. }
  109.  
  110. // Get palette color based on the number of iterations
  111. var color;
  112. if (iterations == maxiterations) {
  113. color = { r:0, g:0, b:0}; // Black
  114. } else {
  115. var index = Math.floor((iterations / (maxiterations-1)) * 255);
  116. color = palette[index];
  117. }
  118.  
  119. // Apply the color
  120. var pixelindex = (y * imagew + x) * 4;
  121. imagedata.data[pixelindex] = color.r;
  122. imagedata.data[pixelindex+1] = color.g;
  123. imagedata.data[pixelindex+2] = color.b;
  124. imagedata.data[pixelindex+3] = 255;
  125. }
  126.  
  127. // Zoom the fractal
  128. function zoomFractal(x, y, factor, zoomin) {
  129. if (zoomin) {
  130. // Zoom in
  131. zoom *= factor;
  132. panx = factor * (x + offsetx + panx);
  133. pany = factor * (y + offsety + pany);
  134. } else {
  135. // Zoom out
  136. zoom /= factor;
  137. panx = (x + offsetx + panx) / factor;
  138. pany = (y + offsety + pany) / factor;
  139. }
  140. }
  141.  
  142. // Mouse event handlers
  143. function onMouseDown(e) {
  144. var pos = getMousePos(canvas, e);
  145.  
  146. // Zoom out with Control
  147. var zoomin = true;
  148. if (e.ctrlKey) {
  149. zoomin = false;
  150. }
  151.  
  152. // Pan with Shift
  153. var zoomfactor = 2;
  154. if (e.shiftKey) {
  155. zoomfactor = 1;
  156. }
  157.  
  158. // Zoom the fractal at the mouse position
  159. zoomFractal(pos.x, pos.y, zoomfactor, zoomin);
  160.  
  161. // Generate a new image
  162. generateImage();
  163. }
  164.  
  165. // Get the mouse position
  166. function getMousePos(canvas, e) {
  167. var rect = canvas.getBoundingClientRect();
  168. return {
  169. x: Math.round((e.clientX - rect.left)/(rect.right - rect.left)*canvas.width),
  170. y: Math.round((e.clientY - rect.top)/(rect.bottom - rect.top)*canvas.height)
  171. };
  172. }
  173.  
  174. // Call init to start the game
  175. init();
  176. };
Advertisement
Add Comment
Please, Sign In to add comment