ImageBitmapLoader.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * @author thespite / http://clicktorelease.com/
  3. */
  4. function detectCreateImageBitmap() {
  5. var url = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==';
  6. return new Promise( function ( resolve, reject ) {
  7. if ( ! ( 'createImageBitmap' in window ) ) {
  8. reject();
  9. return;
  10. }
  11. fetch( url ).then( function ( res ) {
  12. return res.blob();
  13. } ).then( function ( blob ) {
  14. Promise.all( [
  15. createImageBitmap( blob, { imageOrientation: 'none', premultiplyAlpha: 'none' } ),
  16. createImageBitmap( blob, { imageOrientation: 'flipY', premultiplyAlpha: 'none' } ),
  17. createImageBitmap( blob, { imageOrientation: 'none', premultiplyAlpha: 'premultiply' } ),
  18. createImageBitmap( blob, { imageOrientation: 'flipY', premultiplyAlpha: 'premultiply' } )
  19. ] ).then( function () {
  20. resolve();
  21. } ).catch( function () {
  22. reject();
  23. } );
  24. } );
  25. } );
  26. }
  27. var canUseImageBitmap = detectCreateImageBitmap();
  28. canUseImageBitmap.then( function () {
  29. console.log( 'THREE.ImageBitmapLoader: createImageBitmap() supported.' );
  30. } ).catch( function () {
  31. console.warn( 'THREE.ImageBitmapLoader: createImageBitmap() not supported.' );
  32. } );
  33. THREE.ImageBitmapLoader = function ( manager ) {
  34. this.manager = manager !== undefined ? manager : THREE.DefaultLoadingManager;
  35. this.options = {};
  36. };
  37. THREE.ImageBitmapLoader.prototype = {
  38. constructor: THREE.ImageBitmapLoader,
  39. setOptions: function setOptions( options ) {
  40. this.options = options;
  41. return this;
  42. },
  43. load: function load( url, onLoad, onProgress, onError ) {
  44. if ( url === undefined ) url = '';
  45. if ( this.path !== undefined ) url = this.path + url;
  46. var scope = this;
  47. var cached = THREE.Cache.get( url );
  48. if ( cached !== undefined ) {
  49. scope.manager.itemStart( url );
  50. setTimeout( function () {
  51. if ( onLoad ) onLoad( cached );
  52. scope.manager.itemEnd( url );
  53. }, 0 );
  54. return cached;
  55. }
  56. fetch( url ).then( function ( res ) {
  57. return res.blob();
  58. } ).then( function ( res ) {
  59. return createImageBitmap( res, scope.options );
  60. } ).then( function ( res ) {
  61. THREE.Cache.add( url, res );
  62. if ( onLoad ) onLoad( res );
  63. scope.manager.itemEnd( url );
  64. } ).catch( function ( e ) {
  65. if ( onError ) onError( e );
  66. scope.manager.itemEnd( url );
  67. scope.manager.itemError( url );
  68. } );
  69. }
  70. };