I need help optimizing my Draw();
function to draw only what is visible in the viewport. Currently I'm drawing the whole Map array in a diamond shape. How can I make my function store only what is visible?
Here is an image explaining it better:
Black tiles are X axis, White tiles Y axis. Green tiles are what I would like the function to store, and the Orange rectangle is the viewport.
I'll include the relevant part of my code aswell:
for(i=0;i<Map.length;i++) {
for (j=0;j<Map[i].length;j++) {
tileX = (i - j) * (tile.width / 2);
tileX += (canvas.width /* 2*/) - (tile.width / 2);
tileY = (i + j) * (tile.height / 2);
tileY -= (canvas.height /* 2*/);
c.drawImage(tile, tileX, tileY);
}
}
I've been trying to figure out how to solve this, but since I'm no genius I can't. Help appreciated.