123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- /**
- * @author thespite / http://clicktorelease.com/
- */
- import { Cache } from './Cache.js';
- import { DefaultLoadingManager } from './LoadingManager.js';
- function ImageBitmapLoader( manager ) {
- if ( typeof createImageBitmap === 'undefined' ) {
- console.warn( 'THREE.ImageBitmapLoader: createImageBitmap() not supported.' );
- }
- if ( typeof fetch === 'undefined' ) {
- console.warn( 'THREE.ImageBitmapLoader: fetch() not supported.' );
- }
- this.manager = manager !== undefined ? manager : DefaultLoadingManager;
- this.options = undefined;
- }
- ImageBitmapLoader.prototype = {
- constructor: ImageBitmapLoader,
- setOptions: function setOptions( options ) {
- this.options = options;
- return this;
- },
- load: function ( url, onLoad, onProgress, onError ) {
- if ( url === undefined ) url = '';
- if ( this.path !== undefined ) url = this.path + url;
- url = this.manager.resolveURL( url );
- var scope = this;
- var cached = Cache.get( url );
- if ( cached !== undefined ) {
- scope.manager.itemStart( url );
- setTimeout( function () {
- if ( onLoad ) onLoad( cached );
- scope.manager.itemEnd( url );
- }, 0 );
- return cached;
- }
- fetch( url ).then( function ( res ) {
- return res.blob();
- } ).then( function ( blob ) {
- if ( scope.options === undefined ) {
- // Workaround for FireFox. It causes an error if you pass options.
- return createImageBitmap( blob );
- } else {
- return createImageBitmap( blob, scope.options );
- }
- } ).then( function ( imageBitmap ) {
- Cache.add( url, imageBitmap );
- if ( onLoad ) onLoad( imageBitmap );
- scope.manager.itemEnd( url );
- } ).catch( function ( e ) {
- if ( onError ) onError( e );
- scope.manager.itemError( url );
- scope.manager.itemEnd( url );
- } );
- scope.manager.itemStart( url );
- },
- setCrossOrigin: function ( /* value */ ) {
- return this;
- },
- setPath: function ( value ) {
- this.path = value;
- return this;
- }
- };
- export { ImageBitmapLoader };
|