简体   繁体   English

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

[英]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:另一个简单(并且最适合新手)的解决方案是使用画布框架作为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");

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 ).您需要以像素精度制作精灵表(1 个像素可能会弄乱您的动画)。

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 PS:评论问题或优化思路

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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