简体   繁体   中英

how to draw image sprite using canvas?

I want to draw image sprite using canvas.

The code not working. How to improve my code.

I have some Error.

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 );
    }
};

You have to many problems with this code to make this sprite animation works. I wouldn't go to point any of the problems with your code, but I highly recommend to read a little bit about functions and variable scope before try to write this kind of code.

Another simple (and best for newbies) solution can be to use a canvas framework as EaselJS , with this you can do something like this to animate an sprite:

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");

To create a sprite animation it's important to know how it works.

You need your spritesheet make with pixel precision ( 1 pixel can mess up your animation ).

Like here , the character is always in the same size area, make it simple when you make your sprites.

With this you can make an object for each sprite you have like :

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
}

For more animation precision you can add a chrono who will manage the time of your animation :

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;
    }
}

Then the function to animate your sprite may like :

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
                 );
}

I hope I was clear and helpful,

Cheers

PS: Comments for question or optimisation ideas

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM