0
点赞
收藏
分享

微信扫一扫

图像绘制器28

//...V1..这个版本的实现存在一些问题,它没有考虑图像的加载是否完成。V2版本会修正这个问题
var ImagePainter = function (imageUrl) {
   this.image = new Image();
   this.image.src = imageUrl;
};

ImagePainter.prototype = {
   image: undefined,

   paint: function (sprite, context) {
      if (this.image !== undefined) {
         if ( ! this.image.complete) {
            this.image.onload = function (e) {
               sprite.width = this.width;
               sprite.height = this.height;
               
               context.drawImage(this,  // this is image
                  sprite.left, sprite.top,
                  sprite.width, sprite.height);
            };
         }
         else {
           context.drawImage(this.image, sprite.left, sprite.top,
                             sprite.width, sprite.height); 
         }
      }
   }
};

//......................................
var bomb = new Sprite('bomb', new ImagePainter('../../shared/images/bomb.png'));
bomb.paint(context);

举报

相关推荐

0 条评论