twgl.js icon indicating copy to clipboard operation
twgl.js copied to clipboard

twgl.createTexture does not call the callback if options.src is an html element

Open alexkh opened this issue 3 years ago • 3 comments

Greetings!

alexkh avatar Feb 26 '21 09:02 alexkh

Is there a reason to call the callback if options.src is an element? It's not asynchronous.

greggman avatar Feb 26 '21 10:02 greggman

In my code I wanted to be able to load image by url or from element, and once the texture is created call some code. So now to handle both cases I need to set a timeout to check if texture was loaded and it's a bit messy... It'd be nice if I specify a callback, the function would automatically become asynchronous in case that I rely on the callback and not having to process both asynchronous and synchronous cases. That'd make the interface more straightforward.

I did notice that simply calling the callback is not the best idea, because if the function is not asynchronous, the return value would not be set at the moment of calling the callback and the callback might rely on that return value being set...

It took me a while to figure out why my code stopped working after switching from url to html element.

alexkh avatar Feb 26 '21 11:02 alexkh

one possible fix in function createTexture:

  setTextureFromElement(gl, tex, src, options);
  width = src.width;
  height = src.height;
  if(callback) {
    setTimeout(function() {callback(null, tex, src)}, 0);
  }

alexkh avatar Feb 26 '21 13:02 alexkh