Advertisement
nzisaacnz

A simple view of evolution

Jul 28th, 2013
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5.  
  6. </style>
  7. <script>
  8. var Gens=[];
  9. var maxChilds = 0;
  10. var currentGen = 0;
  11. var cell;
  12. function Cell(par,gen,hsl)
  13. {
  14.  this.hsl=hsl+Math.random()*30-15;
  15.  this.gen=gen;
  16.  this.par = par;
  17.  this.bold = false;
  18.  this.childCount=parseInt(Math.random()*3);
  19.  if(this.gen<2)this.childCount=2;
  20.  this.children = new Array(this.childCount);
  21.  currentGen=Math.max(currentGen,gen);
  22.  Gens[this.gen]=(Gens[this.gen]+1)|1;
  23.  this.pos = Gens[this.gen];
  24.  this.Calc = function()
  25.  {
  26.   for(var I=0; I<this.childCount; I++)
  27.   {
  28.    this.children[I] = new Cell(this,this.gen+1,this.hsl);
  29.   }
  30.   return this.children;
  31.  }
  32. }
  33. function boldFamilyLine(cell)
  34. {
  35.  if(!cell.bold)
  36.  {
  37.   cell.bold = true;
  38.   if(cell.par)boldFamilyLine(cell.par);
  39.  }
  40. }
  41. function max(arr)
  42. {
  43. var copy = [];
  44. for(var a=0,b=arr.length; a<b; a++)
  45. {
  46. copy[a] = arr[a];
  47. }
  48. return copy.sort()[copy.length-1];
  49. }
  50. function main()
  51. {
  52. maxChilds = 0;
  53. currentGen =0;
  54. Gens=[];
  55. cell = new Cell(null,0,0);
  56. var childs = cell.Calc();
  57. var lastChildren = [];
  58. for(var b=0; b<200&&childs.length>0; b++)
  59. {
  60. lastChildren = childs;
  61. var c = [];
  62. for(var a=0; a<childs.length; a++)
  63. {
  64.  var d = childs[a].Calc();
  65.  for(var e=0; e<d.length; e++)
  66.  {
  67.   c[c.length] = d[e];
  68.  }
  69. }
  70. childs = c;
  71. }
  72. for(var a=0; a<lastChildren.length; a++)
  73. {
  74.  boldFamilyLine(lastChildren[a]);
  75. }
  76. redraw();
  77. }
  78.  
  79. function Draw(G,cell)
  80. {
  81. if(cell)
  82. {
  83.  if(cell.childCount>0)
  84.  {
  85.   for(var a=0; a<cell.children.length; a++)
  86.   {
  87.    G.lineWidth=1;
  88.    var hsl = cell.children[a].hsl;
  89.    G.strokeStyle="hsl("+hsl+",100%,50%)";
  90.    if(cell.children[a].bold)
  91.    {
  92.     var rgb=0;//parseInt(Gens[cell.gen]/maxChilds*64);
  93.     G.strokeStyle="rgba("+rgb+","+rgb+","+rgb+",1)";
  94.     G.lineWidth = 1;
  95.    }
  96.    
  97.    G.beginPath();
  98.    G.moveTo(cell.gen*5,cvs.height/2+(Gens[cell.gen]-cell.pos-Gens[cell.gen]/2-1)*5);
  99.    G.lineTo(cell.children[a].gen*5,cvs.height/2+(Gens[cell.children[a].gen]-cell.children[a].pos-Gens[cell.children[a].gen]/2-1)*5);
  100.    G.stroke();
  101.   try
  102.   {
  103.  
  104.   Draw(G,cell.children[a]);
  105.   }catch(e){}
  106.  }
  107.  }
  108. }
  109. }
  110. function redraw(event)
  111. {
  112. maxChilds = 5;
  113. if(event)maxChilds = event.target.value;
  114. var G = cvs.getContext("2d");
  115. G.clearRect(0,0,cvs.width,cvs.height);
  116. G.strokeStyle="rgba(0,0,0,0.3)";
  117. Draw(G,cell);
  118. }
  119. window.onload = main;
  120. </script>
  121. </head>
  122. <body onclick="main()">
  123. <canvas id="cvs" width="2000" height="2000" style="border:1px solid;" ></canvas>
  124. </body>
  125. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement