Is it possible to see the image load progress in js?

Asked

Viewed 92 times

3

Is there any way to know the progress of image uploading? I tried to use onload and onprogress as suggested in a previous tip, but only onload works.

var image = new Image();
image.onload = function () {
    console.log("Carregado");
};

image.onprogress=function(){
    console.log("Em progresso");
};

image.src = imageUrl;
  • 1

    Why the negative one? I recognize that in web design normally images are and should be of quick load and so a mechanism of this does not make sense but in applications gis and medical applications images can have absurd sizes and take a considerable amount of time to load.

1 answer

5


The native elements <img>, interface HTMLImageElement, although it does (inherited from HTMLElement) event properties like onload, do not have native means to merge progress.

You can then use the API XMLHttpRequest to download the image and measure progress during this process.

A very common example:

const url = 'https://images.unsplash.com/photo-1544867160-9a42c8d11d40';

loadImgSrc(url, (e) => {
  console.log('Progresso:', e);
}).then((blobUrl) => {
  console.log('Carregada.');
  const img = new Image();
  img.src = blobUrl;
  document.body.appendChild(img);
}).catch((err) => {
  console.error(err);
});

function loadImgSrc(url, onProgress) {
  const req = new XMLHttpRequest();
  req.open('GET', url, true);
  req.responseType = 'arraybuffer';
  
  req.onprogress = function(e) {
    onProgress(e.loaded / e.total, e);
  };
  
  return new Promise((resolve, reject) => {
    req.onload = function(e) {
      const blob = new Blob([this.response]);
      resolve(window.URL.createObjectURL(blob));
    };
    req.onerror = reject;
    req.send();
  });
}

Note that we are using the event progress (attached to the property onprogress) to calculate a percentage progress. For this, we divide loaded for total, which are properties present in the first parameter (event).

After the image is uploaded, we use the API Blob together with URL.createObjectURL to create a blob URI, which will reference the already loaded image.

This implementation was based on in this Stackoverflow question in English. See it for several other examples.


But be careful that this may end up complicating things. Generally, images are not that heavy and calculating progress in this way can end up being an exaggeration. In most cases, the mere onload already works.

  • 5

    Just remember, there are CORS issues, in the same domain is ok, out will depend on the settings.

  • 1

    Exactly! I had already read this question, however I found the answer a little confusing, but with its explanation, everything became simpler. I understand it seems like an exaggeration, even because I think so myself and I don’t understand the reason for just the onload I’m not meeting my needs. If you are interested in why I asked this question, please visit this link: https://answall.com/questions/479957/como-as-imagens-s%C3%a3o-processadas-no-canvas

  • Yes yes @Guilhermenascimento, you can leave! Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.