Advertisement
FallenEagle

The Infinite Fight of Pixels [CSS Part]

Nov 26th, 2015
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
CSS 4.54 KB | None | 0 0
  1. <style>
  2.       footer {
  3. position: absolute;
  4. bottom: 10;
  5.       }
  6.       </style>
  7.   <script type="application/javascript">
  8.  
  9. var canvas, ctx;
  10. var interval;
  11. var width, height;
  12. var size,step;
  13. var Sum_1;
  14.       color1 = Math.floor(Math.random()*16777215).toString(16)
  15.       color1 = "#"+("000000" + color1).slice(-6)
  16.       color2 = Math.floor(Math.random()*16777215).toString(16)
  17.       color2 = "#"+("000000" + color2).slice(-6)
  18.  
  19. function setup() {
  20.         width = 500;
  21.         height = 500;
  22.         canvas = document.getElementById("scrawl");
  23.         ctx = canvas.getContext("2d");
  24.         size=125;
  25.         step=500/size;
  26.         Sum_1=0;
  27.  
  28.  
  29.         initialize();
  30.         interval = setInterval(run,2);
  31.  
  32. }
  33.  
  34.  
  35.  
  36.  
  37. // Intiiales Setzen der Boards
  38. function initialize() {
  39.         Old = new Array (size);
  40.         New = new Array (size);
  41.         //Anzahl der Nachbarn
  42.         Neigh = new Array (size);
  43.         Ratio1 = new Array (size);
  44.  
  45.         for (i = 0; i < Old . length; ++ i){
  46.                 Old [i] = new Array (size);
  47.                 New [i] = new Array (size);
  48.                 Neigh [i] = new Array (size);
  49.                 Ratio1 [i] = new Array (size);
  50.  
  51.         }
  52.         for (i = 0; i < size; ++ i){
  53.                 for (j = 0; j < size; ++ j){
  54.                         Ratio1 [i][j]=0;
  55.                         Neigh[i][j]=8;
  56.                         if (i===0 || i===size-1) {
  57.                                 Neigh[i][j]=5;
  58.                                 if(j===0 || j===size-1) {
  59.                                         Neigh[i][j]=3
  60.                                 }
  61.                         }
  62.                         if (j===0 || j===size-1) {
  63.                                 Neigh[i][j]=5;
  64.                                 if(i===0 || i===size-1) {
  65.                                         Neigh[i][j]=3
  66.                                 }
  67.                         }
  68.  
  69.  
  70.                         if (i<size/2){
  71.                                 Old [i][j] = 1;
  72.                                 Sum_1+=1
  73.                         }
  74.                         else {
  75.                                 Old [i][j] = 0;
  76.                         }
  77.                         New [i][j] = Old [i][j];
  78.                 }
  79.         }
  80.         Sum_1=Sum_1/(size*size);
  81. }
  82.  
  83.  
  84. function ratio() {
  85.         for (i = 0; i < size; ++ i){
  86.                 for (j = 0; j < size; ++ j){
  87.                         Ratio1[i][j]=0;
  88.                         if(i>0){
  89.                                 if(j>0){                Ratio1[i][j]+=Old[i-1][j-1];}
  90.                                                                 Ratio1[i][j]+=Old[i-1][j];
  91.                                 if(j<size-1){   Ratio1[i][j]+=Old[i-1][j+1];}
  92.                         }
  93.  
  94.                         if(j>0){                Ratio1[i][j]+=Old[i][j-1];}
  95.                         if(j<size-1){   Ratio1[i][j]+=Old[i][j+1];}
  96.  
  97.                         if(i<size-1){
  98.                                 if(j>0){                Ratio1[i][j]+=Old[i+1][j-1];}
  99.                                                                 Ratio1[i][j]+=Old[i+1][j];
  100.                                 if(j<size-1){   Ratio1[i][j]+=Old[i+1][j+1];}
  101.                         }
  102.  
  103.                         Ratio1[i][j]=Ratio1[i][j]/Neigh[i][j];
  104.                 }
  105.         }
  106.                 console.log(Neigh[1][1]);
  107.         console.log(Ratio1[1][1]);
  108.                         console.log(Sum_1);
  109.  
  110. }
  111.  
  112.  
  113.  
  114.  
  115.  
  116. function draw() {
  117.        
  118.  
  119.         for (i = 0; i < size; ++ i){
  120.                 for (j = 0; j < size; ++ j){
  121.                                         ctx.fillStyle = color1;
  122.                                         if(Old[i][j]===1){ctx.fillStyle = color2;}                                            
  123.                                 ctx.fillRect (i*step, j*step, step, step);
  124.                         }
  125.                 }
  126. }
  127.  
  128. function calculate() {
  129.  
  130.         for (i = 0; i < size; ++ i){
  131.                 for (j = 0; j < size; ++ j){
  132.                         help=Math.random();
  133.  
  134.                         if ((Ratio1[i][j])>help){
  135.                                 Old[i][j]=1;
  136.                         } else{Old[i][j]=0;
  137.                         }
  138.                 }
  139.         }
  140.        
  141.  
  142.         Sum_1=0;
  143.         for (i = 0; i < size; ++ i){
  144.                 for (j = 0; j < size; ++ j){
  145.                         if (Old[i][j]==1) Sum_1+=1;
  146.                 }
  147.         }
  148.         Sum_1=Sum_1/(size*size);
  149.  
  150.  
  151. }
  152.  
  153.  
  154. function run() {
  155.         ratio();
  156.         draw();
  157.         calculate();
  158.          
  159.  
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement