Chrome 认为图像不是负载,但实际上它们是负载?

发布于 2024-12-23 04:17:22 字数 774 浏览 2 评论 0原文

我试图在画布上绘制一些图像,为了确保在绘制图像时加载图像,我使用数组预加载它们。

var num = 0,
    length = 3,
    images = {
        img1: '/img/img1.png',
        img2: '/img/img2.png',
        img3: '/img/img3.png'
    }


for(var i in images)
{
    var tmp = new Image();
    tmp.src = images[i];
    tmp.onload = function()
    {
        num ++;
        if(num == length)
        {
            startDoingStuff();
        }
    }
}

预加载后,startDoingStuff 被触发,我再次使用图像,

<snip>
this.img = new Image();
img.src = images[id].src;
</snip>

这在 Firefox 和 Opera 中有效,但在 Chrome 中无效。 Chrome 可能认为图像未加载,因为如果我再进行一次加载,它就会起作用。这是愚蠢的,因为图像已经加载:/

我发现解决这个问题的唯一方法是将加载的图像放入另一个数组中(或覆盖原始数组),从而保留引用。

为什么 Chrome 没有意识到他已经在缓存中保存了图像并在需要时使用它们?

I'm trying to draw a few images on a canvas, to make sure the images are loaded when I draw them, I preload them using an array.

var num = 0,
    length = 3,
    images = {
        img1: '/img/img1.png',
        img2: '/img/img2.png',
        img3: '/img/img3.png'
    }


for(var i in images)
{
    var tmp = new Image();
    tmp.src = images[i];
    tmp.onload = function()
    {
        num ++;
        if(num == length)
        {
            startDoingStuff();
        }
    }
}

After preloading the startDoingStuff is fired where I use the image again

<snip>
this.img = new Image();
img.src = images[id].src;
</snip>

This works in Firefox and Opera but not in Chrome. Chrome probably thinks the images aren't loaded because if I put yet another onload it will work. Which is stupid because the images were already loaded :/

Only thing I found to solve this is by putting the loaded images in another array (or overwriting the original array) thus keeping reference.

Why doesn't Chrome not realize he already has the images in cache and use them when asked for?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉风有信 2024-12-30 04:17:22

您预加载图像的代码不太正确。尝试下一个函数:

function preloadImages(images,callback){
  var img,imgs,finished=0,onLoadError;

  if(images instanceof Array)imgs=images;
  else{
    imgs=[];
    for(var p in images)if(images.hasOwnProperty(p))imgs.push(images[p]);
  }

  onLoadError=function(ev){
    //ev=ev||window.event;
    // ev.type can be used to check 
    // when image is loaded succ (ev.type==="load")
    // or fail (ev.type==="error")
    finished++;
    if(finished===imgs.length)callback();
  };

  for(var i=0;i<imgs.length;i++){
    img=new Image();
    img.onload=img.onerror=onLoadError;
    img.src=imgs[i]; // NOTE: .src must be assigned after .onload and .onerror!
  }
}

您可以像下面的示例一样使用此函数:

preloadImages({ img1: '/img/img1.png',
                img2: '/img/img2.png',
                img3: '/img/img3.png'  },
              startDoingStuff);

// or

preloadImages([ '/img/img1.png',
                '/img/img2.png',
                '/img/img3.png'  ],
              startDoingStuff);

Your code to preload the images are not very correct. Try next function:

function preloadImages(images,callback){
  var img,imgs,finished=0,onLoadError;

  if(images instanceof Array)imgs=images;
  else{
    imgs=[];
    for(var p in images)if(images.hasOwnProperty(p))imgs.push(images[p]);
  }

  onLoadError=function(ev){
    //ev=ev||window.event;
    // ev.type can be used to check 
    // when image is loaded succ (ev.type==="load")
    // or fail (ev.type==="error")
    finished++;
    if(finished===imgs.length)callback();
  };

  for(var i=0;i<imgs.length;i++){
    img=new Image();
    img.onload=img.onerror=onLoadError;
    img.src=imgs[i]; // NOTE: .src must be assigned after .onload and .onerror!
  }
}

You can use this function like in next examples:

preloadImages({ img1: '/img/img1.png',
                img2: '/img/img2.png',
                img3: '/img/img3.png'  },
              startDoingStuff);

// or

preloadImages([ '/img/img1.png',
                '/img/img2.png',
                '/img/img3.png'  ],
              startDoingStuff);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文