0
的地形設置http://mystikrpg.com/images/all_tiles.pngputImageData不會畫到畫布
它仍然不會畫分手了到我知道,因爲它在console.log(tileData[1])
輸出的ImageData它被裝進tileData[]
的<canvas>
後。
$(document).ready(function() {
var tileWidth, tileHeight
ImageWidth = 736;
ImageHeight = 672;
tileWidth = 32;
tileHeight = 32;
console.log("Client setup...");
canvas = document.getElementById("canvas");
canvas.width = 512;
canvas.height = 352;
context = canvas.getContext("2d");
canvas.tabIndex = 0;
canvas.focus();
console.log("Client focused.");
var imageObj = new Image();
imageObj.src = "./images/all_tiles.png";
imageObj.onload = function() {
context.drawImage(imageObj, ImageWidth, ImageHeight);
var allLoaded = 0;
var tilesX = ImageWidth/tileWidth;
var tilesY = ImageHeight/tileHeight;
var totalTiles = tilesX * tilesY;
for(var i=0; i<tilesY; i++)
{
for(var j=0; j<tilesX; j++)
{
// Store the image data of each tile in the array.
tileData.push(context.getImageData(j*tileWidth, i*tileHeight, tileWidth, tileHeight));
allLoaded++;
}
}
if (allLoaded == totalTiles) {
console.log("All done: " + allLoaded); // 483
console.log(tileData[1]); // > ImageData
startGame();
}
};
});
也
var startGame = function() {
console.log("Trying to paint test tile onto convas...");
try {
context.putImageData(tileData[0], 0, 0);
} catch(e) {
console.log(e);
}
}
'context.drawImage(imageObj,0,0);'只是在畫布上畫出一個大的未裁剪的圖像。我真的不知道我爲什麼在那裏。 – nn2 2012-07-19 04:29:15
您想要在畫布上繪製大圖,以便您可以逐個像素並將所有圖塊添加到您的圖塊數組中。但你並不是特別想把它放在原來的位置。 – sq2 2012-07-19 05:35:36
好吧..所以它畫在畫布上......然後呢? – nn2 2012-07-19 05:37:41