diff --git a/src/textures.js b/src/textures.js index 6e6b19df..a8f2210e 100644 --- a/src/textures.js +++ b/src/textures.js @@ -1700,6 +1700,30 @@ function createTexture(gl, options, callback) { return tex; } +/** + * Value returned by createTextureAsync + * + * @typedef {Object} CreateTextureInfo + * @param {WebGLTexture} texture the texture. + * @param {module:twgl.TextureSrc} source image(s) used to as the src for the texture + * @memberOf module:twgl + */ + +/** + * Creates a texture based on the options passed in. + * + * see {@link module:twgl/textures.createTexture}. + * The only difference is this function returns a promise + * where as the other returns a texture and takes a callback. + * + * Note: this is here for completeness. It is probably better to use + * the non-async version as it returns a usable texture immediately + * where as this one you have to wait for it to load. + * + * @param {WebGLRenderingContext} gl the WebGLRenderingContext + * @param {module:twgl.TextureOptions} [options] A TextureOptions object with whatever parameters you want set. + * @return {Promise} The created texture and source. + */ function createTextureAsync(gl, options) { return new Promise((resolve, reject) => { createTexture(gl, options, (err, texture, source) => { @@ -1888,6 +1912,42 @@ function createTextures(gl, textureOptions, callback) { return textures; } +/** + * Value returned by createTextureAsync + * + * @typedef {Object} CreateTexturesInfo + * @param {Object.} textures the created textures by name. Same as returned by {@link module:twgl.createTextures}. + * @param {Object.} sources the image(s) used for the texture by name. + * @memberOf module:twgl + */ + +/** + * Creates textures based on the options passed in. + * + * see {@link module:twgl/textures.createTextures}. + * The only difference is this function returns a promise + * where as the other returns a texture and takes a callback. + * + * Note: this is here for completeness. It is probably better to use + * the non-async version as it returns usable textures immediately + * where as this one you have to wait for them to load. + * + * @param {WebGLRenderingContext} gl the WebGLRenderingContext + * @param {Object.} options A object of TextureOptions one per texture. + * @return {Promise} The created textures and sources. + */ +function createTexturesAsync(gl, options) { + return new Promise((resolve, reject) => { + createTexture(gl, options, (err, textures, sources) => { + if (err) { + reject(err); + } else { + resolve({ textures, sources }); + } + }); + }); +} + export { setDefaults as setTextureDefaults_, @@ -1905,6 +1965,7 @@ export { setTextureParameters, setDefaultTextureColor, createTextures, + createTexturesAsync, resizeTexture, canGenerateMipmap,