2
votes

I'm trying to create a little simulation with the help of HTML5 and Javascript using a canvas. My problem however is, I can't really think of a way to control the behavior of my pixels, without making every single pixel an object, which leads to an awful slowdown of my simulation.

Heres the code so far:

var pixels = [];
class Pixel{
    constructor(color){
        this.color=color;
    }
}

window.onload=function(){
    canv = document.getElementById("canv");
    ctx = canv.getContext("2d");
    createMap();
    setInterval(game,1000/60);
};

function createMap(){
    pixels=[];
    for(i = 0; i <= 800; i++){
        pixels.push(sub_pixels = []);
        for(j = 0; j <= 800; j++){
            pixels[i].push(new Pixel("green"));
        }
    }
    pixels[400][400].color="red";
}

function game(){
    ctx.fillStyle = "white";
    ctx.fillRect(0,0,canv.width,canv.height);
    for(i = 0; i <= 800; i++){
        for(j = 0; j <= 800; j++){
            ctx.fillStyle=pixels[i][j].color;
            ctx.fillRect(i,j,1,1);
        }
    }
    for(i = 0; i <= 800; i++){
        for(j = 0; j <= 800; j++){
            if(pixels[i][j].color == "red"){
                direction = Math.floor((Math.random() * 4) + 1);
                switch(direction){
                    case 1:
                        pixels[i][j-1].color= "red";
                        break;
                    case 2:
                        pixels[i+1][j].color= "red";
                        break;
                    case 3:
                        pixels[i][j+1].color= "red";
                        break;
                    case 4:
                        pixels[i-1][j].color= "red";
                        break;
                }
            }
        }
    }

}

function retPos(){
    return Math.floor((Math.random() * 800) + 1);
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script language="javascript" type="text/javascript" src="game.js"></script>
</head>
<body>
    <canvas width="800px" height="800px" id="canv"></canvas>
</body>

</html>
So my two big questions are, what better way of controlling those pixels is there? And how can I speed up the pixel generation?

Hope you can help me.

2

2 Answers

6
votes

Optimizing pixel manipulation

There are many options to speed up your code

Pixels as 32bit ints

The following will slug most machines with too much work.

    // I removed fixed 800 and replaced with const size 
    for(i = 0; i <= size; i++){
        for(j = 0; j <= size; j++){
            ctx.fillStyle=pixels[i][j].color;
            ctx.fillRect(i,j,1,1);
        }
    }

Don't write each pixel via a rect. Use the pixel data you can get from the canvas API via createImageData and associated functions. It uses typed arrays that are a little quicker than arrays and can have multiple view on the same content.

You can write all the pixels to the canvas in a single call. Not blindingly fast but a zillion times faster than what you are doing.

 const size = 800;
 const imageData = ctx.createImageData(size,size);
 // get a 32 bit view
 const data32 = new Uint32Array(imageData.data.buffer);

 // To set a single pixel
 data32[x+y*size] = 0xFF0000FF;  // set pixel to red

 // to set all pixels 
 data32.fill(0xFF00FF00);  // set all to green

To get a pixel at a pixel coord

 const pixel = data32[x + y * imageData.width];

See Accessing pixel data for more on using the image data.

The pixel data is not displayed until you put it onto the canvas

 ctx.putImageData(imageData,0,0);

That will give you a major improvement.

Better data organization.

When performance is critical you sacrifice memory and simplicity to get more CPU cycles doing what you want and less doing a lot of nothing.

You have red pixels randomly expanding into the scene, you read every pixel and check (via a slow string comparison) if it is red. When you find one you add a random red pixel besides it.

Checking the green pixels is a waste and can be avoided. Expanding red pixels that are completely surrounded by other reds is also pointless. They do nothing.

The only pixels you are interested in are the red pixels that are next to green pixels.

Thus you can create a buffer that holds the location of all active red pixels, An active red has at least one green. Each frame you check all the active reds, spawning new ones if they can, and killing them if they are surrounded in red.

We don't need to store the x,y coordinate of each red, just the memory address so we can use a flat array.

const reds = new Uint32Array(size * size); // max size way over kill but you may need it some time.

You dont want to have to search for reds in your reds array so you need to keep track of how many active reds there are. You want all the active reds to be at the bottom of the array. You need to check each active red only once per frame. If a red is dead than all above it must move down one array index. But you only want to move each red only once per frame.

Bubble array

I dont know what this type of array is called its like a separation tank, dead stuff slowly moves up and live stuff moves down. Or unused items bubble up used items settle to the bottom.

I will show it as functional because it will be easier to understand. but is better implemented as one brute force function

// data32 is the pixel data
const size = 800; // width and height
const red = 0xFF0000FF; // value of a red pixel
const green = 0xFF00FF00; // value of a green pixel
const reds = new Uint32Array(size * size); // max size way over kill but you     var count = 0; // total active reds
var head = 0; // index of current red we are processing
var tail = 0; // after a red has been process it is move to the tail
var arrayOfSpawnS = [] // for each neighbor that is green you want
                      // to select randomly to spawn to. You dont want
                      // to spend time processing so this is a lookup 
                      // that has all the possible neighbor combinations
for(let i = 0; i < 16; i ++){
      let j = 0;
      const combo = [];
      i & 1 && (combo[j++] = 1);  // right
      i & 2 && (combo[j++] = -1);  // left
      i & 4 && (combo[j++] = -size); // top
      i & 5 && (combo[j++] = size);  // bottom
      arrayOfSpawnS.push(combo);
}


function addARed(x,y){   // add a new red
     const pixelIndex = x + y * size;
     if(data32[pixelIndex] === green) { // check if the red can go there
         reds[count++] = pixelIndex;  // add the red with the pixel index
         data32[pixelIndex] = red; // and set the pixel
     }
}
function safeAddRed(pixelIndex) { // you know that some reds are safe at the new pos so a little bit faster 
     reds[count++] = pixelIndex;  // add the red with the pixel index
     data32[pixelIndex] = red; // and set the pixel
}

// a frame in the life of a red. Returns false if red is dead
function processARed(indexOfRed) {
     // get the pixel index 
     var pixelIndex = reds[indexOfRed];
     // check reds neighbors right left top and bottom
     // we fill a bit value with each bit on if there is a green
     var n = data32[pixelIndex + 1] === green ? 1 : 0;
     n += data32[pixelIndex - 1] === green ? 2 : 0;          
     n += data32[pixelIndex - size] === green ? 4 : 0;
     n += data32[pixelIndex + size] === green ? 8 : 0;

     if(n === 0){ // no room to spawn so die
          return false;
     }
     // has room to spawn so pick a random
     var nCount = arrayOfSpawnS[n].length;
     // if only one spawn point then rather than spawn we move
     // this red to the new pos.
     if(nCount === 1){
          reds[indexOfRed] += arrayOfSpawnS[n][0]; // move to next pos
     }else{  // there are several spawn points
          safeAddRed(pixelIndex + arrayOfSpawnS[n][(Math.random() * nCount)|0]);
      }
     // reds frame is done so return still alive to spawn another frame
     return true;
  }

Now to process all the reds.

This is the heart of the bubble array. head is used to index each active red. tail is the index of where to move the current head if no deaths have been encountered tail is equal to head. If however a dead item is encountered the head move up one while the tail remains pointing to the dead item. This moves all the active items to the bottom.

When head === count all active items have been checked. The value of tail now contains the new count which is set after the iteration.

If you were using an object rather than a Integer, instead of moving the active item down you swap the head and tail items. This effectively creates a pool of available objects that can be used when adding new items. This type of array management incurs not GC or Allocation overhead and is hence very quick when compared to stacks and object pools.

   function doAllReds(){
       head = tail = 0; // start at the bottom
       while(head < count){
            if(processARed(head)){ // is red not dead
                reds[tail++] = reds[head++];  // move red down to the tail
             }else{ // red is dead so this creates a gap in the array  
                   // Move the head up but dont move the tail, 
                    // The tail is only for alive reds
                head++;
            }
       }
       // All reads done. The tail is now the new count
       count = tail;
    }

The Demo.

The demo will show you the speed improvement. I used the functional version and there could be some other tweaks.

You can also consider webWorkers to get event more speed. Web worker run on a separate javascript context and provides true concurrent processing.

For the ultimate speed use WebGL. All the logic can be done via a fragment shader on the GPU. This type of task is very well suited to parallel processing for which the GPU is designed.

Will be back later to clean up this answer (got a little too long)

I have also added a boundary to the pixel array as the reds were spawning off the pixel array.

const size = canvas.width;
canvas.height = canvas.width;
const ctx = canvas.getContext("2d");
const red = 0xFF0000FF;
const green = 0xFF00FF00;
const reds = new Uint32Array(size * size);    
const wall = 0xFF000000;
var count = 0;
var head = 0;
var tail = 0;
var arrayOfSpawnS = []
for(let i = 0; i < 16; i ++){
      let j = 0;
      const combo = [];
      i & 1 && (combo[j++] = 1);  // right
      i & 2 && (combo[j++] = -1);  // left
      i & 4 && (combo[j++] = -size); // top
      i & 5 && (combo[j++] = size);  // bottom
      arrayOfSpawnS.push(combo);
}
const imageData = ctx.createImageData(size,size);
const data32 = new Uint32Array(imageData.data.buffer);
function createWall(){//need to keep the reds walled up so they dont run free
    for(let j = 0; j < size; j ++){
         data32[j] = wall;
         data32[j * size] = wall;
         data32[j * size + size - 1] = wall;
         data32[size * (size - 1) +j] = wall;
     }
}
function addARed(x,y){  
     const pixelIndex = x + y * size;
     if (data32[pixelIndex] === green) { 
         reds[count++] = pixelIndex;  
         data32[pixelIndex] = red; 
     }
}
function processARed(indexOfRed) {
     var pixelIndex = reds[indexOfRed];
     var n = data32[pixelIndex + 1] === green ? 1 : 0;
     n += data32[pixelIndex - 1] === green ? 2 : 0;          
     n += data32[pixelIndex - size] === green ? 4 : 0;
     n += data32[pixelIndex + size] === green ? 8 : 0;
     if(n === 0) {  return false }
     var nCount = arrayOfSpawnS[n].length;
     if (nCount === 1) { reds[indexOfRed] += arrayOfSpawnS[n][0] }
     else { 
         pixelIndex += arrayOfSpawnS[n][(Math.random() * nCount)|0]
         reds[count++] = pixelIndex; 
         data32[pixelIndex] = red; 
     }
     return true;
}

function doAllReds(){
   head = tail = 0; 
   while(head < count) {
        if(processARed(head)) { reds[tail++] = reds[head++] }
        else { head++ }
   }
   count = tail;
}
function start(){
	data32.fill(green);  
  createWall();
    var startRedCount = (Math.random() * 5 + 1) | 0;
    for(let i = 0; i < startRedCount; i ++) { addARed((Math.random() * size-2+1) | 0, (Math.random() * size-2+1) | 0) }
    ctx.putImageData(imageData,0,0);
    setTimeout(doItTillAllDead,1000);
    countSameCount = 0;
}
var countSameCount;
var lastCount;
function doItTillAllDead(){
    doAllReds();	
    ctx.putImageData(imageData,0,0);
	if(count === 0 || countSameCount === 100){ // all dead
	    setTimeout(start,1000);
	}else{
        countSameCount += count === lastCount ? 1 : 0;
        lastCount = count; // 

        requestAnimationFrame(doItTillAllDead);
	}
}
start();
<canvas width="800" height="800" id="canvas"></canvas>
2
votes

The main cause of your slow down is your assumption that you need to loop over every pixel for every operation. You do not do this, as that would be 640,000 iterations for every operation you need to do.

You also shouldn't be doing any manipulation logic within the render loop. The only thing that should be there is drawing code. So this should be moved out to preferably a separate thread (Web Workers). If unable to use those a setTimeout/Interval call.

So first a couple of small changes:

  1. Make Pixel class contain the pixel's coordinates along with the color:

    class Pixel{
      constructor(color,x,y){
        this.color=color;
        this.x = x;
        this.y = y;
      }
    }
    
  2. Keep an array of pixels that will end up creating new red pixels. And another one to keep track of what pixels have been updated so we know which ones need drawn.

    var pixels = [];
    var infectedPixesl = [];
    var updatedPixels = [];
    

Now the easiest part of the code to change is the render loop. Since the only thing that it needs to do is draw the pixels it will be only a couple lines.

function render(){
  var numUpdatedPixels = updatedPixels.length;
  for(let i=0; i<numUpdatedPixels; i++){
    let pixel = updatedPixels[i];
    ctx.fillStyle = pixel.color;
    ctx.fillRect(pixel.x,pixel.y,1,1);
  }
  //clear out the updatedPixels as they should no longer be considered updated.
  updatedPixels = [];
  //better method than setTimeout/Interval for drawing
  requestAnimationFrame(render);
}

From there we can move on to the logic. We will loop over the infectedPixels array, and with each pixel we decide a random direction and get that pixel. If this selected pixel is red we do nothing and continue on. Otherwise we change it's color and add it to a temporary array affectedPixels. After which we test to see if all the pixels around the original pixel are all red, if so we can remove it from the infectedPixels as there is no need to check it again. Then add all the pixels from affectedPixels onto the infectedPixels as these are now new pixels that need to be checked. And the last step is to also add affectedPixels onto updatedPixels so that the render loop draws the changes.

function update(){
  var affectedPixels = [];
  //needed as we shouldn't change an array while looping over it
  var stillInfectedPixels = [];

  var numInfected = infectedPixels.length;
  for(let i=0; i<numInfected; i++){
    let pixel = infectedPixels[i];
    let x = pixel.x;
    let y = pixel.y;

    //instead of using a switch statement, use the random number as the index
    //into a surroundingPixels array
    let surroundingPixels = [
      (pixels[x] ? pixels[x][y - 1] : null),
      (pixels[x + 1] ? pixels[x + 1][y] : null),
      (pixels[x] ? pixels[x][y + 1] : null),
      (pixels[x - 1] ? pixels[x - 1][y] : null)
    ].filter(p => p);
    //filter used above to remove nulls, in the cases of edge pixels

    var rand = Math.floor((Math.random() * surroundingPixels.length));
    let selectedPixel = surroundingPixels[rand];

    if(selectedPixel.color == "green"){
      selectedPixel.color = "red";
      affectedPixels.push(selectedPixel);
    }

    if(!surroundingPixels.every(p=>p.color=="red")){
      stillInfectedPixels.push(pixel);  
    }
  }
  infectedPixels = stillInfectedPixel.concat( affectedPixels );
  updatedPixels.push(...affectedPixels);
}

Demo

var pixels = [],
  infectedPixels = [],
  updatedPixels = [],
  canv, ctx;

window.onload = function() {
  canv = document.getElementById("canv");
  ctx = canv.getContext("2d");
  createMap();
  render();
  setInterval(() => {
    update();
  }, 16);
};

function createMap() {
  for (let y = 0; y < 800; y++) {
    pixels.push([]);
    for (x = 0; x < 800; x++) {
      pixels[y].push(new Pixel("green",x,y));
    }
  }
  pixels[400][400].color = "red";
  updatedPixels = [].concat(...pixels);
  infectedPixels.push(pixels[400][400]);
}

class Pixel {
  constructor(color, x, y) {
    this.color = color;
    this.x = x;
    this.y = y;
  }
}

function update() {
  var affectedPixels = [];
  var stillInfectedPixels = [];

  var numInfected = infectedPixels.length;
  for (let i = 0; i < numInfected; i++) {
    let pixel = infectedPixels[i];
    let x = pixel.x;
    let y = pixel.y;

    let surroundingPixels = [
      (pixels[x] ? pixels[x][y - 1] : null),
      (pixels[x + 1] ? pixels[x + 1][y] : null),
      (pixels[x] ? pixels[x][y + 1] : null),
      (pixels[x - 1] ? pixels[x - 1][y] : null)
    ].filter(p => p);
    var rand = Math.floor((Math.random() * surroundingPixels.length));
    let selectedPixel = surroundingPixels[rand];

    if (selectedPixel.color == "green") {
      selectedPixel.color = "red";
      affectedPixels.push(selectedPixel);
    }

    if (!surroundingPixels.every(p => p.color == "red")) {
      stillInfectedPixels.push(pixel);
    }
  }
  infectedPixels = stillInfectedPixels.concat(affectedPixels);
  updatedPixels.push(...affectedPixels);
}

function render() {
  var numUpdatedPixels = updatedPixels.length;
  for (let i = 0; i < numUpdatedPixels; i++) {
    let pixel = updatedPixels[i];
    ctx.fillStyle = pixel.color;
    ctx.fillRect(pixel.x, pixel.y, 1, 1);
  }
  updatedPixels = [];
  requestAnimationFrame(render);
}
<canvas id="canv" width="800" height="800"></canvas>