Conveyor Belt in Cellular Automata

c

var canvas = document.getElementById("canvas");
var canvas = document.getElementById("canvas"); 
var ctx = canvas.getContext("2d"); 
var img; var current, prev, imageData;

function RunOnce()  
{ 
   for (var y = 1; y < img.height - 1; ++y) 
   { 
      for (var x = 1; x < img.width - 1; ++x)
      { 
          var n = (y * img.width + x) * 4; // pixel address 
          var c = prev[n]; // color var neighbours = 0; 
          for (var a = -1; a < 2; ++a) 
              for (var b = -1; b < 2; ++b) 
                 if (!(a == 0 && b == 0) && prev[((y + b) * img.width + (x + a)) * 4] == 240) 
                     neighbours++; 

          if (c == 240 && neighbours >= 3 && neighbours <= 5) 
              continue; 

          if (c >= 80) 
              current[n] = c - 80; 
          else 
              current[n] = neighbours == 2 ? 240 : 0; 
       } 
    } 

    ctx.putImageData(imageData, 0, 0); 
    prev.set(current);  
    setTimeout(function() { RunOnce(); }, 10); 
}

function loadImageFromFile(event)
{
    document.getElementById("label").style.visibility = "hidden";
    var reader = new FileReader();
    img = document.getElementById("world");
    reader.onload = function(event) 
    {
       img.onload = function() 
       { 
           ctx.drawImage(img, 0, 0); 
           imageData = ctx.getImageData(0, 0, img.width, img.height);
           current = imageData.data;
           prev = new Uint8ClampedArray(imageData.data);
           RunOnce();
       };
       img.src = event.target.result;
    } 
    reader.readAsDataURL(event.target.files[0]);
}
conveyor

Source image for conveyor belt example. Should be in 24bit bmp format

glider_gun

Source image for glider gun

burning_arrow

Source image for burning arow

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s