簡體   English   中英

如何使用畫布繪制圖像精靈?

[英]how to draw image sprite using 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 );
    }
};

這段代碼有很多問題才能使這個精靈動畫工作。 我不會指出您的代碼的任何問題,但我強烈建議在嘗試編寫此類代碼之前閱讀一些有關函數和變量范圍的內容。

另一個簡單(並且最適合新手)的解決方案是使用畫布框架作為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 個像素可能會弄亂您的動畫)。

這里一樣,角色總是在相同大小的區域內,制作精靈時要簡單一些。

有了這個,您可以為您擁有的每個精靈制作一個對象:

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:評論問題或優化思路

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM