ImageBitmapLoader.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. * @author thespite / http://clicktorelease.com/
  3. */
  4. import { Cache } from './Cache.js';
  5. import { DefaultLoadingManager } from './LoadingManager.js';
  6. function ImageBitmapLoader( manager ) {
  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.manager = manager !== undefined ? manager : DefaultLoadingManager;
  14. this.options = undefined;
  15. }
  16. ImageBitmapLoader.prototype = {
  17. constructor: ImageBitmapLoader,
  18. setOptions: function setOptions( options ) {
  19. this.options = options;
  20. return this;
  21. },
  22. load: function ( url, onLoad, onProgress, onError ) {
  23. if ( url === undefined ) url = '';
  24. if ( this.path !== undefined ) url = this.path + url;
  25. url = this.manager.resolveURL( url );
  26. var scope = this;
  27. var cached = Cache.get( url );
  28. if ( cached !== undefined ) {
  29. scope.manager.itemStart( url );
  30. setTimeout( function () {
  31. if ( onLoad ) onLoad( cached );
  32. scope.manager.itemEnd( url );
  33. }, 0 );
  34. return cached;
  35. }
  36. fetch( url ).then( function ( res ) {
  37. return res.blob();
  38. } ).then( function ( blob ) {
  39. return createImageBitmap( blob, scope.options );
  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. },
  50. setCrossOrigin: function ( /* value */ ) {
  51. return this;
  52. },
  53. setPath: function ( value ) {
  54. this.path = value;
  55. return this;
  56. }
  57. };
  58. export { ImageBitmapLoader };