Why don't you just store it as a matrix?I'm running into an issue with a JavaScript program I've been working on in my spare time. The concept is that the program will draw a "pixel grid" on the screen then later on iterate through and change the color of the "pixels" based on various parameters.
Code:
function pixelMatrixGenerator(numRows, numCols) {
var i, j, matrix = [];
for (i = 0; i < numRows; i++) {
matrix[i] = [];
for (j = 0; j < numCols; j++) {
// create a new pixel (just the color) at the given coordinate
matrix[i][j] = "#000000";
}
return matrix;
}
var grid = pixelMatrixGenerator(WIDTH, HEIGHT);
// now you can access a pixel by the given coordianate
console.log(grid[3][5]); // x -> 3, y -> 5
Now you can cycle through grid (independent of heights and widths) and draw the pixels. You can also later update the grid, and draw it again.