ImageBitmapLoader.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { Cache } from './Cache.js';
  2. import { Loader } from './Loader.js';
  3. class ImageBitmapLoader extends Loader {
  4. constructor( manager ) {
  5. super( manager );
  6. this.isImageBitmapLoader = true;
  7. if ( typeof createImageBitmap === 'undefined' ) {
  8. console.warn( 'THREE.ImageBitmapLoader: createImageBitmap() not supported.' );
  9. }
  10. if ( typeof fetch === 'undefined' ) {
  11. console.warn( 'THREE.ImageBitmapLoader: fetch() not supported.' );
  12. }
  13. this.options = { premultiplyAlpha: 'none' };
  14. }
  15. setOptions( options ) {
  16. this.options = options;
  17. return this;
  18. }
  19. load( url, onLoad, onProgress, onError ) {
  20. if ( url === undefined ) url = '';
  21. if ( this.path !== undefined ) url = this.path + url;
  22. url = this.manager.resolveURL( url );
  23. const scope = this;
  24. const cached = Cache.get( url );
  25. if ( cached !== undefined ) {
  26. scope.manager.itemStart( url );
  27. setTimeout( function () {
  28. if ( onLoad ) onLoad( cached );
  29. scope.manager.itemEnd( url );
  30. }, 0 );
  31. return cached;
  32. }
  33. const fetchOptions = {};
  34. fetchOptions.credentials = ( this.crossOrigin === 'anonymous' ) ? 'same-origin' : 'include';
  35. fetchOptions.headers = this.requestHeader;
  36. fetch( url, fetchOptions ).then( function ( res ) {
  37. return res.blob();
  38. } ).then( function ( blob ) {
  39. return createImageBitmap( blob, Object.assign( scope.options, { colorSpaceConversion: 'none' } ) );
  40. } ).then( function ( imageBitmap ) {
  41. Cache.add( url, imageBitmap );
  42. if ( onLoad ) onLoad( imageBitmap );
  43. scope.manager.itemEnd( url );
  44. } ).catch( function ( e ) {
  45. if ( onError ) onError( e );
  46. scope.manager.itemError( url );
  47. scope.manager.itemEnd( url );
  48. } );
  49. scope.manager.itemStart( url );
  50. }
  51. }
  52. export { ImageBitmapLoader };
粤ICP备19079148号