延迟功能直到资产加载?

时间:2011-09-16 19:10:22

标签: javascript html5 canvas

我正在使用画布,它似乎在FF6中运行良好,但在Chrome 13中,我绘制的精灵并不可靠。我已完成some research并发现问题源于资产完全加载之前的函数触发。

小提琴: http://jsfiddle.net/LqHY9/

相关的Javascript:

    function sprite(ipath, sh, sw, ih, iw){
    /* BASIC INFO FOR SPRITE */
        this.frameWidth = sw;
        this.frameHeight= sh;
        frame_rows = ih/sh;
        frame_columns = iw/sw;
        num_frames = frame_columns*frame_rows ;
        this.frame = new Array();
        frameNumber = 0;
        for(row = 0; row<frame_rows; row++){
            for(i=0;i<frame_columns;i++){
                this.frame[frameNumber] = {};
                this.frame[frameNumber].offsetX = this.frameWidth*i;
                this.frame[frameNumber].offsetY = this.frameHeight*row;
                frameNumber++
            }
        }
        this.sheight = sh;
        this.swidth = sw;
        this.raw = new Image();
        this.raw.src = ipath;
    }
    animation=new sprite("http://www.melonjs.org/tutorial/tutorial_final/data/sprite/gripe_run_right.png",64,64,64,512);

context.drawImage(animation.raw, animation.frame[0].offsetX, animation.frame[0].offsetY, animation.frameWidth, animation.frameHeight, 0, 0, animation.frameWidth,animation.frameHeight)

(别担心,我的上下文变量是定义的,我只是把那一点切掉了,你可以在JSFiddle中看到整个事情。)

4 个答案:

答案 0 :(得分:1)

Image对象有一个你应该挂钩的onload事件。

假设您有多个图像,则可以实现一种“加载程序”。这基本上只需要一组图像URL,加载它们,并听取它们的onload事件。一旦每个图像加载完毕,它就会调用其他一些函数,这将表示每个资源都已完成加载。

答案 1 :(得分:1)

Image()对象有一个onload(和onerror)事件。如果你需要在加载后执行某些操作,可以附加一个函数。

e.g。

var img = new Image();
img.onload = function() {
//do something
};

请确保在设置src之前附加onload。

答案 2 :(得分:1)

您需要为图像使用onload处理程序。您必须在为对象设置.src之前设置处理程序,因为在某些浏览器中,如果图像位于浏览器缓存中,则在设置.src时可能会立即触发加载事件。这是一段伪代码:

var img = new Image();
img.onload = function () {
    // image is now loaded and ready for handling
    // you can safely start your sprite animation
}
img.src = "xxx";

您可以在我写的另一个答案中看到相关的示例代码:jQuery: How to check when all images in an array are loaded?

答案 3 :(得分:0)

function Sprite(urls, speed, box)
{
    var that = this, running = false, interval = 0, loaded = false;

    that.urls = urls;
    that.speed = speed;
    that.images = new Array();
    that.box = box || { x: 0.0, y: 0.0, w: 64, h: 64 };

    for(var i = 0; i < that.urls.length; ++i)
    {
        that.images[i] = new Image();
        that.images[i].src = that.urls[i];
        that.images[i].id = i;
        var len = that.urls.length;
        that.images[i].onload = function(){ if(parseInt(this.id) === len) { loaded = true; } };
    }
    that.current = 0;

    var Draw = function(ctx)
    {
        if(loaded)
        {
            var curr = that.images[that.current];
            ctx.drawImage(curr, 0.0, 0.0, curr.width, curr.height, that.box.x, that.box.y, that.box.w, that.box.h);
        }  
    };

    that.Run = function(ctx)
    {
        if(!running)
        {
            running = true;
            interval = setInterval(function(){
                Draw(ctx);
                if(that.current < that.urls.length) 
                { 
                    that.current++; 
                } 
                else 
                {
                    that.current = 0;  
                }
            }, that.speed);
        }
    };

    that.Clear = function()
    {
        if(running)
        {
            running = false;
            clearInterval(interval);
        }
    };
}

// Exemple

var test = new Sprite(["image1.png", "image2.png", "image3.png"], 250);
test.Run(myContext);