src/textures.js
function loadImage(url, crossOrigin, callback) {
  callback = callback || noop;
  let img;
  crossOrigin = crossOrigin !== undefined ? crossOrigin : defaults.crossOrigin;
  crossOrigin = setToAnonymousIfUndefinedAndURLIsNotSameOrigin(url, crossOrigin);
  if (typeof Image !== 'undefined') {
    img = new Image();
    if (crossOrigin !== undefined) {
      img.crossOrigin = crossOrigin;
    }

    const clearEventHandlers = function clearEventHandlers() {
      img.removeEventListener('error', onError);  // eslint-disable-line
      img.removeEventListener('load', onLoad);  // eslint-disable-line
      img = null;
    };

    const onError = function onError() {
      const msg = "couldn't load image: " + url;
      helper.error(msg);
      callback(msg, img);
      clearEventHandlers();
    };

    const onLoad = function onLoad() {
      callback(null, img);
      clearEventHandlers();
    };

    img.addEventListener('error', onError);
    img.addEventListener('load', onLoad);
    img.src = url;
    return img;
  } else if (typeof ImageBitmap !== 'undefined') {
    let err;
    let bm;
    const cb = function cb() {
      callback(err, bm);
    };

    const options = {};
    if (crossOrigin) {
      options.mode = 'cors'; // TODO: not sure how to translate image.crossOrigin
    }
    fetch(url, options).then(function(response) {
      if (!response.ok) {
        throw response;
      }
      return response.blob();
    }).then(function(blob) {
      return createImageBitmap(blob, {
        premultiplyAlpha: 'none',
        colorSpaceConversion: 'none',
      });
    }).then(function(bitmap) {
      // not sure if this works. We don't want
      // to catch the user's error. So, call
      // the callback in a timeout so we're
      // not in this scope inside the promise.
      bm = bitmap;
      setTimeout(cb);
    }).catch(function(e) {
      err = e;
      setTimeout(cb);
    });
    img = null;
  }
  return img;
}
function loadImage(url, crossOrigin, callback) {
  callback = callback || noop;
  let img;
  crossOrigin = crossOrigin !== undefined ? crossOrigin : defaults.crossOrigin;
  crossOrigin = setToAnonymousIfUndefinedAndURLIsNotSameOrigin(url, crossOrigin);
  if (typeof Image !== 'undefined') {
    img = new Image();
    if (crossOrigin !== undefined) {
      img.crossOrigin = crossOrigin;
    }

    const clearEventHandlers = function clearEventHandlers() {
      img.removeEventListener('error', onError);  // eslint-disable-line
      img.removeEventListener('load', onLoad);  // eslint-disable-line
      img = null;
    };

    const onError = function onError() {
      const msg = "couldn't load image: " + url;
      helper.error(msg);
      callback(msg, img);
      clearEventHandlers();
    };

    const onLoad = function onLoad() {
      callback(null, img);
      clearEventHandlers();
    };

    img.addEventListener('error', onError);
    img.addEventListener('load', onLoad);
    img.src = url;
    return img;
  } else if (typeof ImageBitmap !== 'undefined') {
    let err;
    let bm;
    const cb = function cb() {
      callback(err, bm);
    };

    const options = {};
    if (crossOrigin) {
      options.mode = 'cors'; // TODO: not sure how to translate image.crossOrigin
    }
    fetch(url, options).then(function(response) {
      if (!response.ok) {
        throw response;
      }
      return response.blob();
    }).then(function(blob) {
      return createImageBitmap(blob, {
        premultiplyAlpha: 'none',
        colorSpaceConversion: 'none',
      });
    }).then(function(bitmap) {
      // not sure if this works. We don't want
      // to catch the user's error. So, call
      // the callback in a timeout so we're
      // not in this scope inside the promise.
      bm = bitmap;
      setTimeout(cb);
    }).catch(function(e) {
      err = e;
      setTimeout(cb);
    });
    img = null;
  }
  return img;
}
function loadImage ( url , crossOrigin , callback ) { callback = callback || noop ; let img ; crossOrigin = crossOrigin !== undefined ? crossOrigin : defaults . crossOrigin ; crossOrigin = setToAnonymousIfUndefinedAndURLIsNotSameOrigin ( url , crossOrigin ) ; if ( typeof Image !== 'undefined' ) { img = new Image ( ) ; if ( crossOrigin !== undefined ) { img . crossOrigin = crossOrigin ; } const clearEventHandlers = function clearEventHandlers ( ) { img . removeEventListener ( 'error' , onError ) ; // eslint-disable-line img . removeEventListener ( 'load' , onLoad ) ; // eslint-disable-line img = null ; } ; const onError = function onError ( ) { const msg = "couldn't load image: " + url ; helper . error ( msg ) ; callback ( msg , img ) ; clearEventHandlers ( ) ; } ; const onLoad = function onLoad ( ) { callback ( null , img ) ; clearEventHandlers ( ) ; } ; img . addEventListener ( 'error' , onError ) ; img . addEventListener ( 'load' , onLoad ) ; img . src = url ; return img ; } else if ( typeof ImageBitmap !== 'undefined' ) { let err ; let bm ; const cb = function cb ( ) { callback ( err , bm ) ; } ; const options = { } ; if ( crossOrigin ) { options . mode = 'cors' ; // TODO: not sure how to translate image.crossOrigin } fetch ( url , options ) . then ( function ( response ) { if ( ! response . ok ) { throw response ; } return response . blob ( ) ; } ) . then ( function ( blob ) { return createImageBitmap ( blob , { premultiplyAlpha : 'none' , colorSpaceConversion : 'none' , } ) ; } ) . then ( function ( bitmap ) { // not sure if this works. We don't want // to catch the user's error. So, call // the callback in a timeout so we're // not in this scope inside the promise. bm = bitmap ; setTimeout ( cb ) ; } ) . catch ( function ( e ) { err = e ; setTimeout ( cb ) ; } ) ; img = null ; } return img ; }
Loads an image
@param {string} url url to image
@param {string} crossOrigin
@param {function(err, img)} [callback] a callback that's passed an error and the image. The error will be non-null
if there was an error
@return {HTMLImageElement} the image being loaded.
@private
Loads an image