I would..
- Determine how many tiles are wide and high.
- Draw the image on the canvas in memory and use the context to get the image data.
- Scroll and create each fragment, saving in an array.
Assuming:
imageWidth, imageHeight, tileWidth, tileHeight
Everyone describes what their names offer, and:
EDIT: added loading of the image according to the comment, the name ImageWidth
and ImageHeight
to ImageWidth
and ImageHeight
EDIT: Code execution inside imageObj.onload
, as shown in the picture, drawImage () from the beginning (0,0)
var canvas = document.getElementById("myCanvas"); var ctx = canvas.getContext("2d"); var imageObj = new Image(); imageObj.src = "tilesetImageLocationHere"; imageObj.onload = function() { ctx.drawImage(imageObj, 0, 0);
Then split your image into a list of tile data.
var tilesX = imageWidth / tileWidth; var tilesY = imageHeight / tileHeight; var totalTiles = tilesX * tilesY; var tileData = new Array(); for(var i=0; i<tilesY; i++) { for(var j=0; j<tilesX; j++) {
source share