【问题标题】:how to draw image sprite using canvas?如何使用画布绘制图像精灵?
【发布时间】:2013-01-29 21:00:43
【问题描述】:

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

代码不工作。如何改进我的代码。

我有一些错误。

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

【问题讨论】:

  • 哪些错误,哪些行? nameImage 是什么?
  • Uncaught SyntaxError: Unexpected token var in line var BACKGROUND = {
  • 我希望精灵 image_top,image_body,image-bottom 的名称为nameImage
  • 伙计,你有标点符号问题,而且我认为你不知道var x = function()function x() 之间的区别,如果你是 Javascript 新手,有更好的方法来实现这一点。你知道任何画布动画框架吗?

标签: javascript image html canvas


【解决方案1】:

要创建精灵动画,了解它的工作原理很重要。

您需要以像素精度制作您的 spritesheet(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:问题或优化想法的评论

【讨论】:

    【解决方案2】:

    要使这个精灵动画正常工作,这段代码有很多问题。我不会指出您的代码有任何问题,但我强烈建议您在尝试编写此类代码之前先阅读一些关于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");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-02
      • 2013-02-13
      • 2022-08-13
      • 2023-03-29
      • 1970-01-01
      相关资源
      最近更新 更多