【问题标题】:Delay function until an asset has loaded?延迟功能直到资产加载?
【发布时间】:2011-11-18 22:38:09
【问题描述】:

我正在玩画布,它似乎在 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 中看到整个内容。)

【问题讨论】:

  • 为时已晚......哦,好吧......你可以在这里看到 onload 方法:jsfiddle.net/6QRtY。我建议首先加载您的图像资源,并为每个资源附加一个加载事件。在每个加载事件中,测试看是否所有资源都加载完毕(flag或类似),如果所有资源都加载完毕,则执行main方法。

标签: javascript html canvas


【解决方案1】:

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

假设您有多个图像,您可以实现一种“加载器”。这基本上只需要一组图像 URL,加载每个 URL,并监听它们的 onload 事件。加载完每个图像后,它会依次调用其他函数,这会发出每个资源都已完成加载的信号。

【讨论】:

    【解决方案2】:

    Image() 对象有一个 onload(and onerror) 事件。如果您需要在加载后执行某些内容,您可以附加一个函数。

    例如

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

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

    【讨论】:

      【解决方案3】:

      您需要对图像使用 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?

      【讨论】:

        【解决方案4】:
        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);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-03-30
          • 1970-01-01
          • 1970-01-01
          • 2019-10-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多