Skip to content

Commit 106b7cf

Browse files
committed
Add createTextureAsync and createTexturesAsync
I'm not really sure I should add these. I think I thought about it before. The issue is, createTexture and createTextures both return usable textures immediately. No waiting. If the data for the texture needs to be loaded asynchronously, they'll return a 1x1 pixel texture. createTextureAsync and createTexturesAsync on the other hand, only return the texture after their async sources have loaded. These are more here for completeness as sometimes you actually do want to wait until the texture's data is loaded and it's now common to use promises for this purpose.
1 parent 364ddd6 commit 106b7cf

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

src/textures.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,6 +1700,30 @@ function createTexture(gl, options, callback) {
17001700
return tex;
17011701
}
17021702

1703+
/**
1704+
* Value returned by createTextureAsync
1705+
*
1706+
* @typedef {Object} CreateTextureInfo
1707+
* @param {WebGLTexture} texture the texture.
1708+
* @param {module:twgl.TextureSrc} source image(s) used to as the src for the texture
1709+
* @memberOf module:twgl
1710+
*/
1711+
1712+
/**
1713+
* Creates a texture based on the options passed in.
1714+
*
1715+
* see {@link module:twgl/textures.createTexture}.
1716+
* The only difference is this function returns a promise
1717+
* where as the other returns a texture and takes a callback.
1718+
*
1719+
* Note: this is here for completeness. It is probably better to use
1720+
* the non-async version as it returns a usable texture immediately
1721+
* where as this one you have to wait for it to load.
1722+
*
1723+
* @param {WebGLRenderingContext} gl the WebGLRenderingContext
1724+
* @param {module:twgl.TextureOptions} [options] A TextureOptions object with whatever parameters you want set.
1725+
* @return {Promise<CreateTextureInfo>} The created texture and source.
1726+
*/
17031727
function createTextureAsync(gl, options) {
17041728
return new Promise((resolve, reject) => {
17051729
createTexture(gl, options, (err, texture, source) => {
@@ -1888,6 +1912,42 @@ function createTextures(gl, textureOptions, callback) {
18881912
return textures;
18891913
}
18901914

1915+
/**
1916+
* Value returned by createTextureAsync
1917+
*
1918+
* @typedef {Object} CreateTexturesInfo
1919+
* @param {Object.<string, WebGLTexture>} textures the created textures by name. Same as returned by {@link module:twgl.createTextures}.
1920+
* @param {Object.<string, module:twgl.TextureSrc>} sources the image(s) used for the texture by name.
1921+
* @memberOf module:twgl
1922+
*/
1923+
1924+
/**
1925+
* Creates textures based on the options passed in.
1926+
*
1927+
* see {@link module:twgl/textures.createTextures}.
1928+
* The only difference is this function returns a promise
1929+
* where as the other returns a texture and takes a callback.
1930+
*
1931+
* Note: this is here for completeness. It is probably better to use
1932+
* the non-async version as it returns usable textures immediately
1933+
* where as this one you have to wait for them to load.
1934+
*
1935+
* @param {WebGLRenderingContext} gl the WebGLRenderingContext
1936+
* @param {Object.<string,module:twgl.TextureOptions>} options A object of TextureOptions one per texture.
1937+
* @return {Promise<CreateTexturesInfo>} The created textures and sources.
1938+
*/
1939+
function createTexturesAsync(gl, options) {
1940+
return new Promise((resolve, reject) => {
1941+
createTexture(gl, options, (err, textures, sources) => {
1942+
if (err) {
1943+
reject(err);
1944+
} else {
1945+
resolve({ textures, sources });
1946+
}
1947+
});
1948+
});
1949+
}
1950+
18911951
export {
18921952
setDefaults as setTextureDefaults_,
18931953

@@ -1905,6 +1965,7 @@ export {
19051965
setTextureParameters,
19061966
setDefaultTextureColor,
19071967
createTextures,
1968+
createTexturesAsync,
19081969
resizeTexture,
19091970

19101971
canGenerateMipmap,

0 commit comments

Comments
 (0)