Thread: Canvas Problem
View Single Post
Old 12-05-2012, 07:21 PM   PM User | #40
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
The beauty of this solution is that you could expand it to many types of "terrain".

Example:
Code:
var terrain {
   "g" : "grass.png",
   "c" : "cementjpg",
   "m" : "mountain.gif",
   "w" : "water.jpg",
   "t" : "town.png"
};

var map = [
    "mmmmmwwwww",
    "mmggcctttw",
    .... etc. ...
];

var cellWidth = 25;
var cellHeight = 25;

// replace strings with image objects:
for ( var c in terrain )
{
     var pic = new Image();
     pic.src = "new/" + terrain[c];
     pic.width = cellWidth; 
     pic.height = cellHeight;
     terrain[c] = pic; 
}
// draw terrain:
for( row=0; row < map.length; ++row)
{
    var line = map[row];
    for( column=0; column < line.length; ++column )
    {
        var image = terrain[ line[ column ] ];
	context.drawImage( image, column*cellWidth,  row*cellHeight );
    }
}
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote