0

有没有办法清除只有 1 个元素的画布(html5)?我在画布上有一个动态图像,当我擦除图像时,背景颜色也会消失。有没有办法只删除图像而不是整个背景。我的背景只是一个简单的颜色,但将来会更复杂。

这也很棘手,因为无法从属性中获取图像 x,y pos。

  ClassLoadImages.prototype.m_move = function(){
     this.x=++img1_x;
     this.y=++img1_y; 
     //img1_x++;
   //img1_y++;
  // alert(img.x);
      ctx.drawImage(img.imgElement,  this.x,   this.y);
     // ctx.fillText("finished loading " ,10,40);
  };


 function doGameLoop() {

    ctx.clearRect(0,0,600,400);
    img.m_move();
     if (img.x>30)
     {
          clearInterval(gameLoop);

     }
 }




  var img= new ClassLoadImages('images/image4.jpg');
 gameLoop = setInterval(doGameLoop, 100);
</script>
4

1 回答 1

2

简单的答案是否定的。画布是平面位图,而不是对象的分层集合。一旦你画到它,你就会失去你画的东西背后的背景。

您可以尝试自己实现该功能,方法是首先记录您用于创建画布的步骤,然后在使用或不使用相关图像的情况下重新创建它。

于 2013-04-09T12:48:23.653 回答