Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. <!doctype html>
  2. <html lang="en">
  3.  
  4. <head>
  5. <meta charset="UTF-8">
  6.  
  7. <title>HTML5 Canvas</title>
  8.  
  9. <script type="text/javascript">
  10.  
  11. window.addEventListener("load", canvasApp, false);
  12.  
  13. function canvasApp() {
  14.  
  15. //The whole program should be placed inside this canvasApp function. This function executes only when the page is fully loaded.
  16.  
  17. //code below sets up variables so that the canvas context can be referenced in JavaScript.
  18. var displayCanvas = document.getElementById("displayCanvas");
  19. var context = displayCanvas.getContext("2d");
  20. var displayWidth = displayCanvas.width;
  21. var displayHeight = displayCanvas.height;
  22. var Xmin;
  23. var Xmax;
  24. var Ymin;
  25. var Ymax;
  26. var maxIterations;
  27.  
  28. init();
  29.  
  30. function init() {
  31. Xmin = -2;
  32. Xmax = 2;
  33. Ymin = -2
  34. Ymax = 2;
  35. maxIterations = 2000;
  36. fillCanvas();
  37. }
  38.  
  39. function fillCanvas() {
  40. var image = context.getImageData(0, 0, displayWidth, displayHeight);
  41. var pixelData = image.data;
  42. var len = pixelData.length;
  43. var i;
  44. var x, y;
  45. var a, b;
  46. var pixX, pixY, temp;
  47. var iterations;
  48. var maxIterations = 2000;
  49. var red, green ,blue;
  50. var dx, dy;
  51. var rad;
  52. var maxRad = Math.sqrt(displayWidth*displayWidth + displayHeight*displayHeight)/2;
  53. var grays = [64, 128, 192, 255];
  54. var gray;
  55. var escaped;
  56. for (i = 0; i < len; i = i + 4) {
  57. pixX = (i/4 % displayWidth);
  58. pixY = Math.floor(i/(4*displayWidth));
  59.  
  60. // figure out what x and y are represented by this pixX, pixY
  61. a = Xmin + ((Xmax-Xmin)/displayWidth)*pixX
  62. b = Ymin + ((Ymax-Ymin)/displayHeight)*pixY
  63. // start the Mandelbrot iteration to decide what color this pixel should be
  64. // most code here
  65. x = 0;
  66. y = 0;
  67. iterations = 0;
  68. escaped = false;
  69. while (true){
  70. temp = (x*x) - (y*y) + a;
  71. y = 2*x*y + b;
  72. x = temp;
  73. iterations++;
  74. if (iterations > maxIterations){
  75. break;
  76. }
  77. if ((x*x) + (y*y) > 4){
  78. red = green = blue = 255;
  79. escaped = true;
  80. break;
  81. }
  82. }
  83. if (escaped) {
  84. gray = grays[iterations % 4];
  85. }
  86. else {
  87. gray = 0;
  88. }
  89. // color the pixel the correct color
  90.  
  91. pixelData[i ] = gray; //red CHANGE THIS
  92. pixelData[i+1] = gray; //green CHANGE THIS
  93. pixelData[i+2] = gray; //blue CHANGE THIS
  94. pixelData[i+3] = 255; //alpha
  95. }
  96. context.putImageData(image,0,0);
  97. }
  98.  
  99. function changeXmin() {
  100. Xmin = document.getElementById("Xmin").value;
  101. location.reload();
  102.  
  103. }
  104. function changeXmax() {
  105. Xmax = document.getElementById("Xmax").value;
  106. location.reload();
  107.  
  108. }
  109. function changeYmin() {
  110. Ymin = document.getElementById("Ymin").value;
  111. location.reload();
  112.  
  113. }
  114. function changeYmax() {
  115. Ymax = document.getElementById("Ymax").value;
  116. location.reload();
  117.  
  118. }
  119. function changeIterations() {
  120. maxIterations = document.getElementById("Iterations").value;
  121. location.reload();
  122.  
  123. }
  124.  
  125. }
  126.  
  127. </script>
  128.  
  129. <title>HTML5 Canvas Example</title>
  130.  
  131. <style type="text/css">
  132. body {background-color:#ffffff; color:#333333;}
  133. a {font-family: sans-serif; color:#d15423; text-decoration:none;}
  134. /*#displayCanvas {position:absolute; top:10px; z-index:0;border: 1px solid #000;}*/
  135. /*#container {width:820px; height:820px; margin:auto;}*/
  136. </style>
  137.  
  138. </head>
  139. <body>
  140. <div id="container">
  141. <canvas id="displayCanvas" width="1000px" height="1000px"></canvas>
  142. </div>
  143. <div id="xmin">
  144. <input type="number" placeholder="X Min" id="Xmin" oninput="changeXmin()">
  145. </div>
  146. <div id="xmax">
  147. <input type="number" placeholder="X Max" id="Xmax" oninput="changeXmax()">
  148. </div>
  149. <div id="ymin">
  150. <input type="number" placeholder="Y Min" id="Ymin" oninput="changeYmin()">
  151. </div>
  152. <div id="ymax">
  153. <input type="number" placeholder="Y Max" id="Ymax" oninput="changeYmax()">
  154. </div>
  155. <div id="iterations">
  156. <input type="number" placeholder="Iterations" id="Iterations" oninput="changeIterations()">
  157. </div>
  158. </body>
  159. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement