2012-04-04 11 views
0

私はゲームを作っています。その中に、フレームごとにイメージを回転させるのではなく、キャッシュされたキャンバスを使うことができます。私は意味をなさない画像を追加する機能を作ったと思うが、キャンバスオブジェクトはもはや利用できないと私に知らせるすべてのフレームでエラーが発生している。キャンバスでの回転とキャッシングのバグ

INVALID_STATE_ERR:DOM例外11:試みはない オブジェクトを使用するために行われていない、または、もはや使用可能であるました。

私はobject.set()を使用していることを知る必要があります。その画像をrenderArrayに追加してください。キャンバスオブジェクトがまだ使用可能かどうかに影響する可能性がありますか?ここで

は、キャッシュされたキャンバスを返す関数である、(私はこのウェブサイト上のポストからそれを取った:D):

var game = { 
    render:function(){ 
    //gets called every frame 
     context.clearRect(0, 0, canvas.width, canvas.height); 
     for(i = 0; i < game.renderArray.length; i++){ 
      switch(game.renderArray[i].type){ 
       case "image": 
        context.save(); 
        context.translate(game.renderArray[i].x, game.renderArray[i].y); 
        context.rotate(Math.PI*game.renderArray[i].rotation/180); 
        context.translate(-game.renderArray[i].x, -game.renderArray[i].y); 
        context.drawImage(game.renderArray[i].image, game.renderArray[i].x, game.renderArray[i].y); 
        context.restore(); 
        break; 
      } 
      if(game.renderArray[i].remove == true){ 
       game.renderArray.splice(i,1); 
       if(i > 1){ 
        i--; 
       }else{ 
        break; 
       } 
      } 
     } 
    }, 
    size:function(width, height){ 
     canvas.height = height; 
     canvas.width = width; 
     return height + "," + width; 
    }, 
    renderArray:new Array(), 
    //initialize the renderArray 
    image:function(src, angle){ 
     if(angle != undefined){ 
        //if the argument 'angle' was given 
      this.tmp = new Image(); 
      this.tmp.src = src; 
        //sets 'this.image' (peach.image) to the canvas. It then should get rendered in the next frame, but apparently it doesn't work... 
      this.image = rotateAndCache(this.tmp, angle); 
     }else{ 
      this.image = new Image(); 
      this.image.src = src; 
     } 
     this.x = 0; 
     this.y = 0; 
     this.rotation = 0; 
     this.destroy = function(){ 
      this.remove = true; 
      return "destroyed"; 
     }; 
     this.remove = false; 
     this.type = "image"; 
     this.set = function(){ 
      game.renderArray.push(this);  
     } 
    } 
}; 

var canvas, context, peach; 

$(document).ready(function(){ 
    canvas = document.getElementById("canvas"); 
    context = canvas.getContext("2d"); 
    //make the variable peach a new game.image with src of meme.jpg and an angle of 20. 
    peach = new game.image('meme.jpg', 20); 
    peach.set(); 
    game.size(700,500); 
    animLoop(); 
}); 

rotateAndCache = function(image, angle){ 
    var offscreenCanvas = document.createElement('canvas'); 
    var offscreenCtx = offscreenCanvas.getContext('2d'); 
    var size = Math.max(image.width, image.height); 
    offscreenCanvas.width = size; 
    offscreenCanvas.height = size; 
    offscreenCtx.translate(size/2, size/2); 
    offscreenCtx.rotate(angle + Math.PI/2); 
    offscreenCtx.drawImage(image, -(image.width/2), -(image.height/2)); 
    return offscreenCanvas; 
} 

そしてここでは、いくつかのより多くのマークアップされたコードですあなたが望むなら、私のサイトでホストされているこのプロジェクトはここにあります: http://keirp.com/zap

答えて

1

私は見ることができますか、それ以上はあなたのページにエラーはありません。

問題は読み込みが完了していない可能性があります。たとえば、まだ完成していないイメージからキャンバスパターンを作成しようとすると、エラーが発生します。描画を開始する前に、pxloaderや独自の画像読み込み機能を使用して、すべての画像が完成していることを確認してください。

とにかく、あなたのコードが実際に何のエラーも与えていないので、何が起きているのか、起きているのかは分かりません。

関連する問題