I am playing around with the HTML5 canvas and have begun implementing a tile engine.
However, it's currently very inefficient: I have a 100x100 2D array and my code loops through the whole thing every frame. (Very bad, I know.) I'd like to optimise the rendering loop by using a viewport or camera to determine which parts of the area are view.
Here is an example. I haven’t added the camera offset yet because I’m not sure how:
for (var y = 0; y < state.map_data.length; y++) {
col = state.map_data[y];
for (var x = 0; x < col.length; x++) {
color = (state.map_data[y][x]==1)?'#fff':'#000';
self._draw({
type:'rec',
x: x * state.tile_width,
y: y * state.tile_height,
width: state.tile_width,
height: state.tile_height,
fill: color,
visible: true
},self.ctx);
};
};
Here is a link to the game as it is.
How can I make this more efficient?
