Guest User

Untitled

a guest
May 24th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1. var conway = function() {
  2. var context, running, canvas, board, cellWidth,
  3. cellHeight, cellX, cellY, lastUpdate;
  4. var cells = 25;
  5.  
  6. var draw = function() {
  7. ctx.clearRect(0, 0, canvas.width, canvas.height);
  8.  
  9. drawBoard();
  10. var now = new Date().getTime();
  11. if (running && ((now - lastUpdate) > 1000)) {
  12. board = simulate();
  13. lastUpdate = now;
  14. }
  15. drawGrid();
  16. };
  17.  
  18. var clickHandler = function(e) {
  19. var elementX = elementY = 0;
  20. var parent=e.currentTarget;
  21. while (parent) {
  22. elementX += parent.offsetLeft;
  23. elementY += parent.offsetTop;
  24. parent = parent.offsetParent;
  25. }
  26.  
  27. var clickX = e.clientX + canvas.offsetParent.scrollLeft - elementX;
  28. var clickY = e.clientY + canvas.offsetParent.scrollTop - elementY;
  29.  
  30. var cellX = parseInt(clickX / cellWidth);
  31. var cellY = parseInt(clickY / cellHeight);
  32.  
  33. board[cellX][cellY] = !board[cellX][cellY];
  34. };
  35.  
  36. var keyHandler = function(e) {
  37. var keynum;
  38. var keychar;
  39.  
  40. if (window.event) { // IE
  41. keynum = e.keyCode;
  42. } else if (e.which) { // Others
  43. keynum = e.which;
  44. }
  45.  
  46. keychar = String.fromCharCode(keynum);
  47.  
  48. if (keychar == "s") {
  49. running = !running;
  50. } else if (keychar == "r") {
  51. board = makeBoard();
  52. } else if (keychar == "n") {
  53. board = simulate();
  54. }
  55. };
  56.  
  57. var drawGrid = function() {
  58. ctx.strokeStyle = "#0000FF";
  59. ctx.beginPath();
  60. for (i = 1; i < cells; i++) {
  61. ctx.moveTo(cellWidth * i, 0);
  62. ctx.lineTo(cellWidth * i, canvas.height);
  63. }
  64. for (i = 1; i < cells; i++) {
  65. ctx.moveTo(0, cellHeight * i);
  66. ctx.lineTo(canvas.width, cellHeight * i);
  67. }
  68. ctx.closePath();
  69. ctx.stroke();
  70. }
  71.  
  72. var makeBoard = function() {
  73. var newBoard = new Array();
  74. for (i = -1; i <= cells; i++) {
  75. newBoard[i] = new Array();
  76. for (j = -1; j <= cells; j++) {
  77. newBoard[i][j] = false;
  78. }
  79. }
  80. return newBoard;
  81. }
  82.  
  83. var countNeighbors = function(x, y) {
  84. newCount = 0;
  85. // uses javascript's negative indexable array :)
  86. if (board[x][y-1]) newCount++;
  87. if (board[x][y+1]) newCount++;
  88. if (board[x-1][y]) newCount++;
  89. if (board[x+1][y]) newCount++;
  90. if (board[x-1][y-1]) newCount++;
  91. if (board[x+1][y-1]) newCount++;
  92. if (board[x-1][y+1]) newCount++;
  93. if (board[x+1][y+1]) newCount++;
  94. return newCount;
  95. }
  96.  
  97. var simulate = function() {
  98. var newBoard = makeBoard();
  99.  
  100. for (i = 0; i < cells; i++) {
  101. for (j = 0; j < cells; j++) {
  102. var neighbors = countNeighbors(i, j);
  103. if (board[i][j]) {
  104. newBoard[i][j] = (neighbors == 2 || neighbors == 3);
  105. } else {
  106. newBoard[i][j] = (neighbors == 3);
  107. }
  108. }
  109. }
  110.  
  111. // Now copy edge cells to make simulation wrap around
  112. for (i = -1; i <= cells; i++) {
  113. newBoard[i][-1] = newBoard[i][cells-1];
  114. newBoard[i][cells] = newBoard[i][-1];
  115. newBoard[-1][i] = newBoard[cells-1][i];
  116. newBoard[cells][i] = newBoard[-1][i];
  117. }
  118.  
  119. return newBoard;
  120. }
  121.  
  122. var drawBoard = function() {
  123. for (i = 0; i < cells; i++) {
  124. for (j = 0; j < cells; j++) {
  125. if (board[i][j]) {
  126. fillCell(i, j);
  127. }
  128. }
  129. }
  130. }
  131.  
  132. var circle = function(cellX, cellY) {
  133. var radius = cellWidth / 2;
  134. var centerX = cellX * cellWidth + radius;
  135. var centerY = cellY * cellHeight + radius;
  136.  
  137. ctx.beginPath();
  138. ctx.arc(centerX, centerY, radius - 1, 0, 2 * Math.PI, false);
  139. ctx.closePath();
  140. ctx.fill();
  141. }
  142.  
  143. var square = function(cellX, cellY) {
  144. ctx.fillRect(cellX * cellWidth, cellY * cellHeight, cellWidth, cellHeight);
  145. }
  146.  
  147. var fillCell = function(cellX, cellY) {
  148. ctx.fillStyle = "#FF0000";
  149. circle(cellX, cellY);
  150. }
  151.  
  152. lastUpdate = new Date().getTime();
  153. canvas = document.getElementById('conway');
  154. cellWidth = canvas.width / cells;
  155. cellHeight = canvas.height / cells;
  156. ctx = canvas.getContext('2d');
  157.  
  158. board = makeBoard();
  159. running = false;
  160. document.onkeypress = keyHandler;
  161. canvas.onclick = clickHandler;
  162.  
  163. return setInterval(draw, 100);
  164. };
Add Comment
Please, Sign In to add comment