ImageBitmapLoader.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * @author thespite / http://clicktorelease.com/
  3. */
  4. THREE.ImageBitmapLoader = function ( manager ) {
  5. if ( typeof createImageBitmap === 'undefined' ) {
  6. console.warn( 'THREE.ImageBitmapLoader: createImageBitmap() not supported.' );
  7. }
  8. this.manager = manager !== undefined ? manager : THREE.DefaultLoadingManager;
  9. this.options = undefined;
  10. };
  11. THREE.ImageBitmapLoader.prototype = {
  12. constructor: THREE.ImageBitmapLoader,
  13. setOptions: function setOptions( options ) {
  14. this.options = options;
  15. return this;
  16. },
  17. load: function load( url, onLoad, onProgress, onError ) {
  18. if ( url === undefined ) url = '';
  19. if ( this.path !== undefined ) url = this.path + url;
  20. var scope = this;
  21. var cached = THREE.Cache.get( url );
  22. if ( cached !== undefined ) {
  23. scope.manager.itemStart( url );
  24. setTimeout( function () {
  25. if ( onLoad ) onLoad( cached );
  26. scope.manager.itemEnd( url );
  27. }, 0 );
  28. return cached;
  29. }
  30. fetch( url ).then( function ( res ) {
  31. return res.blob();
  32. } ).then( function ( blob ) {
  33. return createImageBitmap( blob, scope.options );
  34. } ).then( function ( imageBitmap ) {
  35. THREE.Cache.add( url, imageBitmap );
  36. if ( onLoad ) onLoad( imageBitmap );
  37. scope.manager.itemEnd( url );
  38. } ).catch( function ( e ) {
  39. if ( onError ) onError( e );
  40. scope.manager.itemEnd( url );
  41. scope.manager.itemError( url );
  42. } );
  43. }
  44. };