Javascript IE中的HTML5画布内存问题
我正在使用canvas创建一个HTML5游戏。我在IE中运行游戏时出错。错误消息是“没有足够的存储空间来完成此操作”。调用ctx.drawImage时出错。这只会在比赛结束时发生。其他几个图像/精灵在整个游戏中以完全相同的方式绘制,没有任何问题。使用Chrome或Firefox时不会发生这种情况。有什么建议吗 以下是我的流程: 1) 在游戏开始时,我将几个图像加载到一个名为game.sprites的数组中Javascript IE中的HTML5画布内存问题,javascript,html,internet-explorer,html5-canvas,Javascript,Html,Internet Explorer,Html5 Canvas,我正在使用canvas创建一个HTML5游戏。我在IE中运行游戏时出错。错误消息是“没有足够的存储空间来完成此操作”。调用ctx.drawImage时出错。这只会在比赛结束时发生。其他几个图像/精灵在整个游戏中以完全相同的方式绘制,没有任何问题。使用Chrome或Firefox时不会发生这种情况。有什么建议吗 以下是我的流程: 1) 在游戏开始时,我将几个图像加载到一个名为game.sprites的数组中 var imageObj = new Image(); imageObj.src = as
var imageObj = new Image();
imageObj.src = assetDir + "Images/myImage.png";
Game.sprites.myAnimation = new AnimatedSpriteSheet(imageObj, 0, 0, 1200, 1200, 4, 14);
我为大约100张雪碧床单做上述工作
function AnimatedSpriteSheet(img, startX, startY, width, height, imagesPerRow, imageCount){
this.img = img;
this.startX = startX;
this.startY = startY;
this.width = width;
this.height = height;
this.imagesPerRow = imagesPerRow;
this.imageCount = imageCount;
}
AnimatedSpriteSheet.prototype.draw = function(ctx, posX, posY, width, height, imageIndex){
try{
//Determine position of image to draw
var row = Math.floor(imageIndex/this.imagesPerRow);
var column = imageIndex%this.imagesPerRow;
ctx.drawImage(this.img, this.startX + (column*this.width), this.startY + (row*this.height), this.width, this.height, posX, posY, width, height);
return true;
}catch(err){
console.log("Error: AnimatedSpriteSheet.draw for image " + this.img.href + " " + err.message);
return false;
}
}
2) 在游戏期间,我将某些图像添加到名为game.sceneObjects的数组中
Game.sceneObjects.push(new MyAnimationObject("", Game.sprites.myAnimation, Game.cWidth*.3, Game.cHeight*.3, Game.cWidth*.4, Game.cWidth*.4, 0, 2));
function MyAnimationObject(tag, obj, x, y, width, height, startIndex, ticksPerFrame){
this.tag = tag;
this.obj = obj;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.currentIndex = startIndex;
this.tickCount = 0;
this.ticksPerFrame = ticksPerFrame;
};
MyAnimationObject.prototype.draw = function(ctx){
this.obj.draw(ctx, this.x, this.y, this.width, this.height, this.currentIndex);
};
MyAnimationObject.prototype.update = function(){
this.tickCount += 1;
if (this.tickCount > this.ticksPerFrame){
this.tickCount = 0;
if (this.currentIndex < this.obj.imageCount - 1){
this.currentIndex += 1;
}
}
};
4) 在新屏幕/场景开始时,我清除了对象
for (var i = 0; i < Game.sceneObjects.length; i++){
delete Game.sceneObjects[i];
}
Game.sceneObjects = [];
for(变量i=0;i
更新:
如果我在中间或中间开始比赛,结局就不会有这个问题。只有在中场前的某个时候我才能开始比赛。有一些内存问题,但我无法解决。这是因为ie的最大画布大小限制。请阅读本文。您使用哪个版本的IE?您的解决方案不会超过此限制(结果画布宽度和高度)您并没有真正清除
场景对象
delete
命令只会将指定的数组元素设置为undefined
,但不会将其从数组中删除。这相当于:
// element 5362 equals undefined but is still an element in the array
Game.sceneObjects[5362]=undefined;
相反,将数组长度设置为零。这会导致数组没有元素
Game.sceneObjects.length=0;
我的画布尺寸是1020px 680px。MSDN称最大尺寸为8192px 8192px。我在画布上绘制的每个图像都比画布小。但是,没有更多到元素的链接,如果未定义,gc会将其从内存中删除,或者不定义?不用担心…空数组本身(Game.sceneObjects)仍然被引用(仍然存在)不会被垃圾收集。@markE-I删除了delete,并将其替换为数组的长度设置为0。它仍然有同样的问题。我查看了内存配置文件,它仍然显示每一个新场景的内存都在增加。
Game.sceneObjects.length=0;