candyapplecorn

bacteria.html

Oct 19th, 2014
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.20 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <!--
  6. Created using JS Bin
  7. http://jsbin.com
  8.  
  9. Copyright (c) 2014 by candyapplecorn (http://jsbin.com/jiyuxa/3/edit)
  10.  
  11. Released under the MIT license: http://jsbin.mit-license.org
  12. -->
  13. <title>Candy's Bacteria</title>
  14. <style>
  15. body{
  16. background: black;
  17. }
  18. #myCanvas{
  19. margin-left: 20%;
  20. margin-right: 20%;
  21. }
  22. </style>
  23.  
  24. <style id="jsbin-css">
  25.  
  26. </style>
  27. </head>
  28. <body>
  29. <div id="container">
  30. <canvas id="myCanvas"><script>var c = document.getElementById("myCanvas");
  31. var ctx = c.getContext('2d');
  32. // Adjust size of canvas and add resizer
  33. (function(){ c.width = window.innerWidth, c.height = window.innerHeight; if (c.width > c.height) c.width = c.height; else c.height = c.width; }());
  34. window.addEventListener('resize', function(){ c.width = window.innerWidth, c.height = window.innerHeight; if (c.width > c.height) c.width = c.height; else c.height = c.width; }, true);
  35.  
  36. // functions rolls a dice with "sides" many sides.
  37. function d(sides){ return 1 + Math.floor(Math.random() * sides); }
  38.  
  39. var length = 22, wall = 9999, bacteria = 1, usedbacteria = 32 * 5, fancytable = new Array(Math.pow(length, 2));
  40.  
  41. for(counter = 0; counter < fancytable.length; counter++){
  42. switch(true){
  43. case (counter - length < 0):
  44. case (counter + length > Math.pow(length, 2)):
  45. case (counter % length == 0):
  46. case ((counter + 1) % length == 0):
  47. fancytable[counter] = wall;
  48. break;
  49. default:
  50. fancytable[counter] = 0;
  51. }
  52. }
  53. while(true){ var index = d(fancytable.length) - 1; if(fancytable[index] != 0) continue; fancytable[index] = bacteria; break; }
  54.  
  55. console.clear();
  56. function myObject(){ this.a = 0; }
  57. counter23 = new myObject();
  58. function display(){
  59. var string = "\n";
  60. for(var counter = 0; counter < fancytable.length; counter++){
  61. string += '' + fancytable[counter] + '';
  62. if ((counter + 1) % length == 0) string += "\n";
  63. }
  64. console.log(string);
  65. }
  66. //display();
  67.  
  68. function spread(index){
  69. if(fancytable[index] == bacteria){
  70. if((index - length > 0) && !(fancytable[index - length] == wall) && !(fancytable[index - length] == bacteria) && !(fancytable[index - length] > bacteria && fancytable[index - length] < usedbacteria)){
  71. if(d(6) == 6){
  72. fancytable[index - length] = bacteria;
  73. fancytable[index]++;
  74. }
  75. }
  76. if ((index - 1 > 0) && !(fancytable[index - 1] == wall && !(fancytable[index - 1] == bacteria) && !(fancytable[index - 1] > bacteria && fancytable[index - 1] < usedbacteria))){
  77. if(d(6) == 6){
  78. fancytable[index - 1] = bacteria;
  79. fancytable[index]++;
  80. }
  81. }
  82. if ((1 + index < fancytable.length) && !(fancytable[index + 1] == wall && !(fancytable[index + 1] == bacteria) && !(fancytable[index + 1] > bacteria && fancytable[index + 1] < usedbacteria))){
  83. if(d(6) == 6){
  84. fancytable[index + 1] = bacteria;
  85. fancytable[index]++;
  86. }
  87. }
  88. if((length + index < fancytable.length) && !(fancytable[index + length] == wall && !(fancytable[index + length] == bacteria) && !(fancytable[index + length] > bacteria && fancytable[index + length] < usedbacteria))){
  89. if(d(6) == 6){
  90. fancytable[index + length] = bacteria;
  91. fancytable[index]++;
  92. }
  93. }
  94. }
  95. }
  96.  
  97. setInterval(
  98. function(){
  99. ctx.fillStyle = "lightgray";
  100. ctx.fillRect(0, 0, c.width, c.height);
  101. for(var row = 0; row < length; row++){
  102. for(var column = 0; column < length; column++){
  103. if (fancytable[row * length + column] > bacteria && fancytable[row * length + column] < usedbacteria) fancytable[row * length + column]++;
  104. else if (fancytable[row * length + column] == usedbacteria) fancytable[row * length + column] = 0;
  105. spread(row * length + column);
  106. var color = Math.floor(fancytable[row * length + column]/usedbacteria * 255);
  107. ctx.fillStyle = fancytable[row * length + column] == wall ? "black" : "gray";
  108. ctx.fillStyle = fancytable[row * length + column] == bacteria ? "rgb(0,255,0)" : ctx.fillStyle;
  109. ctx.fillStyle = fancytable[row * length + column] <= usedbacteria && fancytable[row * length + column] > bacteria ? 'rgb(' + Math.floor(color / 1.5) + ', ' + color + ', ' + Math.floor(color / 1.5) + ')' : ctx.fillStyle;
  110. ctx.beginPath();
  111. ctx.rect(column * (c.width / length), row * (c.height / length), c.width / length, c.height / length);
  112. ctx.stroke();
  113.  
  114. ctx.fillRect(column * (c.width / length), row * (c.height / length), c.width / length, c.height / length);
  115. }
  116. }
  117. //if((counter23.a++) == 10) display();
  118. }, 1000/32);
  119.  
  120.  
  121.  
  122.  
  123.  
  124. </script></canvas>
  125. </div>
  126. <script>
  127. // BACTERIA
  128. </script>
  129. <script src="http://static.jsbin.com/js/render/edit.js?3.22.2"></script>
  130. <script>jsbinShowEdit && jsbinShowEdit({"static":"http://static.jsbin.com","root":"http://jsbin.com"});</script>
  131. <script src="http://static.jsbin.com/js/vendor/eventsource.js?3.22.2"></script>
  132. <script src="http://static.jsbin.com/js/spike.js?3.22.2"></script>
  133.  
  134. <script>
  135. (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  136. (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  137. m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  138. })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
  139. ga('create', 'UA-1656750-34', 'jsbin.com');
  140. ga('require', 'linkid', 'linkid.js');
  141. ga('require', 'displayfeatures');
  142. ga('send', 'pageview');
  143.  
  144. </script>
  145.  
  146. </body>
  147. </html>
Advertisement
Add Comment
Please, Sign In to add comment