2

it is possible to overwrite the charging method of an image so that this loading is done by a WebWorker?

Example:

Image.prototype.contructor = function(arguments){  // Webworker implementation }

//or

Image.prototype.setSrc = function(arguments){  // Webworker implementation }

var image = new Image('somePath.jpg'); //or 
image.setSrc('somePath.jpg');

I need it because I need to carry many images on a single screen only once using ionic framework, and loading it stands today is fighting the UI Thread and perhaps using a WebWorker I improve performance

Steeve Pitis
  • 4,283
  • 1
  • 21
  • 24
1fabiopereira
  • 542
  • 2
  • 4
  • 18

1 Answers1

1

You can use XMLHttpRequest() within Worker, postMessage() of ArrayBuffer, Blob, or data URI representation of image to main thread when request is successful.

at main thread

var worker = new Worker("/path/to/worker.js");

worker.addEventListener("message", function(e) {
  // do stuff with `Blob` of image
  console.log(e.data);
  var img = new Image;
  img.onload = function() {
    document.body.appendChild(this)
  }
  img.src = URL.createObjectURL(e.data);
});

worker.postMessage("request" /* or, path to image */);

worker.js

self.addEventListener("message", function(e) {
  if (e.data === "request") {
    var request = new XMLHttpRequest();
    request.open("GET", "/path/to/image" /* or, `e.data` */);
    request.responseType = "blob";
    request.onload = function() {
      self.postMessage(request.response);
    }
    request.send();
  }
});

plnkr http://plnkr.co/edit/smqVzIMSOJR3GNSBBxul?p=preview

guest271314
  • 1
  • 15
  • 104
  • 177
  • Hi, is it possible to have a graph that show the impact of this practice ? – Steeve Pitis Oct 11 '16 at 08:30
  • @SteevePitis _"is it possible to have a graph that show the impact of this practice ?"_ Yes. At linked plnkr at firefox 45 you can press `Ctrl+Shift+I` to toggle developer tools; select `Performance`; select `Waterfall`; click `Start Recording Performance`; click `Stop`, then `Run` at plnkr Editor; click `Stop Recording Performance` when image is rendered at plnkr panel; filter results displayed at list and graph at developer tools panel. – guest271314 Oct 11 '16 at 23:57