twgl.js
twgl.js copied to clipboard
twgl.createTexture does not call the callback if options.src is an html element
Greetings!
Is there a reason to call the callback if options.src
is an element? It's not asynchronous.
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.
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);
}