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