ImageBitmapLoader.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. if ( scope.options === undefined ) {
  40. // Workaround for FireFox. It causes an error if you pass options.
  41. return createImageBitmap( blob );
  42. } else {
  43. return createImageBitmap( blob, scope.options );
  44. }
  45. } ).then( function ( imageBitmap ) {
  46. Cache.add( url, imageBitmap );
  47. if ( onLoad ) onLoad( imageBitmap );
  48. scope.manager.itemEnd( url );
  49. } ).catch( function ( e ) {
  50. if ( onError ) onError( e );
  51. scope.manager.itemError( url );
  52. scope.manager.itemEnd( url );
  53. } );
  54. scope.manager.itemStart( url );
  55. },
  56. setCrossOrigin: function ( /* value */ ) {
  57. return this;
  58. },
  59. setPath: function ( value ) {
  60. this.path = value;
  61. return this;
  62. }
  63. };
  64. export { ImageBitmapLoader };