123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- /**
- * @author mrdoob / http://mrdoob.com/
- */
- import { Cache } from './Cache.js';
- import { DefaultLoadingManager } from './LoadingManager.js';
- function ImageLoader( manager ) {
- this.manager = ( manager !== undefined ) ? manager : DefaultLoadingManager;
- }
- Object.assign( ImageLoader.prototype, {
- crossOrigin: 'anonymous',
- 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;
- }
- var image = document.createElementNS( 'http://www.w3.org/1999/xhtml', 'img' );
- function onImageLoad() {
- image.removeEventListener( 'load', onImageLoad, false );
- image.removeEventListener( 'error', onImageError, false );
- Cache.add( url, this );
- if ( onLoad ) onLoad( this );
- scope.manager.itemEnd( url );
- }
- function onImageError( event ) {
- image.removeEventListener( 'load', onImageLoad, false );
- image.removeEventListener( 'error', onImageError, false );
- if ( onError ) onError( event );
- scope.manager.itemError( url );
- scope.manager.itemEnd( url );
- }
- image.addEventListener( 'load', onImageLoad, false );
- image.addEventListener( 'error', onImageError, false );
- if ( url.substr( 0, 5 ) !== 'data:' ) {
- if ( this.crossOrigin !== undefined ) image.crossOrigin = this.crossOrigin;
- }
- scope.manager.itemStart( url );
- image.src = url;
- return image;
- },
- setCrossOrigin: function ( value ) {
- this.crossOrigin = value;
- return this;
- },
- setPath: function ( value ) {
- this.path = value;
- return this;
- }
- } );
- export { ImageLoader };
|