如何使用画布绘制图像精灵?

时间:2013-02-14 05:33:31

标签: javascript image html5 canvas

我想用画布绘制图像精灵。

代码无效。如何改进我的代码。

我有一些错误。

var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');

var Game = {

 draw_image: function(img, sourceX, sourceY, sourceW, sourceH, destX, destY, destW, destH){

var img = new Image();   // Create new img element
img.src = 'images/background.png'; // Set source path
img.onload = function(){
            canvas.width = 1000;
            canvas.height = 500;
            ctx.drawImage(img, sourceX, sourceY, sourceW, sourceH, destX, destY, destW, destH);
};
}

var BACKGROUND = {
  image_top:    { x:   5, y:   5, w: 1280, h: 480 , dx:0 ,dy:0   ,dw:500 ,dh:500 },
  image_body:   { x:   5, y: 495, w: 1280, h: 480 , dx:0 ,dy:150 ,dw:500 ,dh:350},
  image_bottom: { x:   5, y: 985, w: 1280, h: 480 , dx:0 ,dy:300 ,dw:500 ,dh:200 }
};

for(var n = 0 ; n < BACKGROUND.length ; n++) {
     draw_image(nameImage, BACKGROUND[n].x,BACKGROUND[n].y, BACKGROUND[n].w, BACKGROUND[n].h, BACKGROUND[n].dx, BACKGROUND[n].dy, BACKGROUND[n].dw, BACKGROUND[n].dh );
    }
};

2 个答案:

答案 0 :(得分:1)

使用此代码时,您必须遇到许多问题才能使此精灵动画生效。我不会指出你的代码有任何问题,但我强烈建议你在尝试编写这种代码之前先阅读functions and variable scope

另一个简单的(也是最好的新手)解决方案可以使用画布框架作为EaselJS,这样你可以做这样的事情来动画精灵:

var data = {
     images: ["images/background.png"],
     frames: {width:50, height:50},
     animations: {run:[0,4], jump:[5,8,"run"]}
 };
 var animation = new createjs.BitmapAnimation(data);
 animation.gotoAndPlay("run");

答案 1 :(得分:1)

要创建精灵动画,了解其工作原理非常重要。

你需要你的spritesheet make像素精度(1个像素会弄乱你的动画)。

here一样,角色总是在相同大小的区域,让你的精灵变得简单。

通过这个你可以为你喜欢的每个精灵创建一个对象:

function Sprite(_position, _numberFrame, _framesize, _image, _duration){
    this.position = _position;      //Array like { x : 0, y : 0 }
    this.rendersize = _rendersize;  //Array like { width : 50, height : 80 }
    this.framesize = _framesize;    //Array like { width : 50, height : 80 }
    this.image = _image;            //Image object
    this.chrono = new Chrono(_duration); //Explanation below
}

要获得更多动画精确度,您可以添加一个管理动画时间的计时器:

function Chrono(_duration){
    this.currentTime = 0;
    this.lastTime = 0;
    this.timeElapse = 0;
    this.duration = _duration;
}

Chrono.prototype.countTime = function(){
    this.currentTime = Date.now();

    if(this.lastTime != 0)
        this.timeElapse += this.currentTime - this.lastTime;

        this.lastTime = Date.now();

    if(this.timeElpase >= this.duration && this.lastTime != 0){
        this.timeElapse = 0;
        return TRUE;
    } else {
        return FALSE;
    }
}

然后,为精灵设置动画的功能可能是:

Sprite.prototype.render = function(){
    if(this.position.x <= this.image.width && this.chrono.countTime()){
        this.position.x += this.framesize.x;
    } else {
        this.position.x = 0;
    }
    ctx.drawImage(this.image, 
                  this.position.x, this.position.y, 
                  this.framesize.width, this.framesize.height,
                  this.rendersize.width, this.rendersize.height
                 );
}

我希望我很清楚,乐于助人,

干杯

PS:对问题或优化想法的评论

相关问题