ImageBitmapLoader.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /**
  2. * @author thespite / http://clicktorelease.com/
  3. */
  4. function detectCreateImageBitmap( optionsList ) {
  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. var pendingImages = [];
  15. for ( var i = 0; i < optionsList.length; i ++ ) {
  16. var pendingImage = optionsList[ i ] === undefined
  17. ? createImageBitmap( blob )
  18. : createImageBitmap( blob, optionsList[ i ] );
  19. pendingImages.push( pendingImage );
  20. }
  21. Promise.all( pendingImages ).then( function () {
  22. resolve();
  23. } ).catch( function () {
  24. reject();
  25. } );
  26. } );
  27. } );
  28. }
  29. var canUseImageBitmap = detectCreateImageBitmap( [ undefined ] );
  30. var canUseImageBitmapOptions = detectCreateImageBitmap( [
  31. { imageOrientation: 'none', premultiplyAlpha: 'none' },
  32. { imageOrientation: 'flipY', premultiplyAlpha: 'none' },
  33. { imageOrientation: 'none', premultiplyAlpha: 'premultiply' },
  34. { imageOrientation: 'flipY', premultiplyAlpha: 'premultiply' }
  35. ] );
  36. THREE.ImageBitmapLoader = function ( manager ) {
  37. canUseImageBitmap.catch( function () {
  38. console.warn( 'THREE.ImageBitmapLoader: createImageBitmap() not supported.' );
  39. } );
  40. this.manager = manager !== undefined ? manager : THREE.DefaultLoadingManager;
  41. this.options = undefined;
  42. };
  43. THREE.ImageBitmapLoader.prototype = {
  44. constructor: THREE.ImageBitmapLoader,
  45. setOptions: function setOptions( options ) {
  46. canUseImageBitmapOptions.catch( function () {
  47. console.warn( 'THREE.ImageBitmapLoader: createImageBitmap() options not supported.' );
  48. } );
  49. this.options = options;
  50. return this;
  51. },
  52. load: function load( url, onLoad, onProgress, onError ) {
  53. if ( url === undefined ) url = '';
  54. if ( this.path !== undefined ) url = this.path + url;
  55. var scope = this;
  56. var cached = THREE.Cache.get( url );
  57. if ( cached !== undefined ) {
  58. scope.manager.itemStart( url );
  59. setTimeout( function () {
  60. if ( onLoad ) onLoad( cached );
  61. scope.manager.itemEnd( url );
  62. }, 0 );
  63. return cached;
  64. }
  65. fetch( url ).then( function ( res ) {
  66. return res.blob();
  67. } ).then( function ( blob ) {
  68. return scope.options === undefined
  69. ? createImageBitmap( blob )
  70. : createImageBitmap( blob, scope.options );
  71. } ).then( function ( imageBitmap ) {
  72. THREE.Cache.add( url, imageBitmap );
  73. if ( onLoad ) onLoad( imageBitmap );
  74. scope.manager.itemEnd( url );
  75. } ).catch( function ( e ) {
  76. if ( onError ) onError( e );
  77. scope.manager.itemEnd( url );
  78. scope.manager.itemError( url );
  79. } );
  80. }
  81. };