ImageLoader.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { FileLoader } from './FileLoader';
  2. import { DefaultLoadingManager } from './LoadingManager';
  3. /**
  4. * @author mrdoob / http://mrdoob.com/
  5. */
  6. function ImageLoader( manager ) {
  7. this.manager = ( manager !== undefined ) ? manager : DefaultLoadingManager;
  8. }
  9. Object.assign( ImageLoader.prototype, {
  10. load: function ( url, onLoad, onProgress, onError ) {
  11. var scope = this;
  12. var image = document.createElementNS( 'http://www.w3.org/1999/xhtml', 'img' );
  13. image.onload = function () {
  14. image.onload = null;
  15. URL.revokeObjectURL( image.src );
  16. if ( onLoad ) onLoad( image );
  17. scope.manager.itemEnd( url );
  18. };
  19. image.onerror = onError;
  20. if ( url.indexOf( 'data:' ) === 0 ) {
  21. image.src = url;
  22. } else if ( this.crossOrigin !== undefined ) {
  23. // crossOrigin doesn't work with URL.createObjectURL()?
  24. image.crossOrigin = this.crossOrigin;
  25. image.src = url;
  26. } else {
  27. var loader = new FileLoader();
  28. loader.setPath( this.path );
  29. loader.setResponseType( 'blob' );
  30. loader.setWithCredentials( this.withCredentials );
  31. // By default the FileLoader requests files to be loaded with a MIME
  32. // type of `text/plain`. Using `URL.createObjectURL()` with SVGs that
  33. // have a MIME type of `text/plain` results in an error, so explicitly
  34. // set the SVG MIME type.
  35. if ( /\.svg$/.test( url ) ) loader.setMimeType( 'image/svg+xml' );
  36. loader.load( url, function ( blob ) {
  37. image.src = URL.createObjectURL( blob );
  38. }, onProgress, onError );
  39. }
  40. scope.manager.itemStart( url );
  41. return image;
  42. },
  43. setCrossOrigin: function ( value ) {
  44. this.crossOrigin = value;
  45. return this;
  46. },
  47. setWithCredentials: function ( value ) {
  48. this.withCredentials = value;
  49. return this;
  50. },
  51. setPath: function ( value ) {
  52. this.path = value;
  53. return this;
  54. }
  55. } );
  56. export { ImageLoader };
粤ICP备19079148号