我想破坏一张spritesheet,并且不会像它应该将它放入二维数组中。在我的代码结尾,console.log附近,它吐出ImageData ...但是当它试图putImageData ...我得到Uncaught TypeError: Type error
JavaScript - 未捕获TypeError:类型错误
帮助?
,这里是我的代码:
$(document).ready(function() {
console.log("Client setup...");
canvas = document.getElementById("canvas");
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;
var tileData = [totalTiles]; // Array with reserved size
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);
console.log(tileData[1]);
context.putImageData(tileData[0], 0 ,0); // Sample paint
}
});
错误发生在context.putImageData(tileData[0], 0 ,0); // Sample paint
下面是代码示例小提琴上述http://jsfiddle.net/47XUA/
那......什么都没做。还是一样。 – nn2 2012-07-18 18:56:04
澄清,它仍然产生相同的'类型错误'?我的叉不(在Chrome和FF):http://jsfiddle.net/HBzH7/ – apsillers 2012-07-18 18:57:48
我没有看到'context.putImageData(tileData [1],0,0);'画布上的图像? – nn2 2012-07-18 19:12:32