|
@@ -1,10 +1,11 @@
|
|
|
-import { FileLoader } from './FileLoader';
|
|
|
-import { DefaultLoadingManager } from './LoadingManager';
|
|
|
-
|
|
|
/**
|
|
|
* @author mrdoob / http://mrdoob.com/
|
|
|
*/
|
|
|
|
|
|
+import { Cache } from './Cache';
|
|
|
+import { DefaultLoadingManager } from './LoadingManager';
|
|
|
+
|
|
|
+
|
|
|
function ImageLoader( manager ) {
|
|
|
|
|
|
this.manager = ( manager !== undefined ) ? manager : DefaultLoadingManager;
|
|
@@ -15,56 +16,64 @@ Object.assign( ImageLoader.prototype, {
|
|
|
|
|
|
load: function ( url, onLoad, onProgress, onError ) {
|
|
|
|
|
|
+ if ( url === undefined ) url = '';
|
|
|
+
|
|
|
+ if ( this.path !== undefined ) url = this.path + url;
|
|
|
+
|
|
|
var scope = this;
|
|
|
|
|
|
- var image = document.createElementNS( 'http://www.w3.org/1999/xhtml', 'img' );
|
|
|
- image.onload = function () {
|
|
|
+ var cached = Cache.get( url );
|
|
|
|
|
|
- image.onload = null;
|
|
|
+ if ( cached !== undefined ) {
|
|
|
|
|
|
- URL.revokeObjectURL( image.src );
|
|
|
+ scope.manager.itemStart( url );
|
|
|
|
|
|
- if ( onLoad ) onLoad( image );
|
|
|
+ setTimeout( function () {
|
|
|
|
|
|
- scope.manager.itemEnd( url );
|
|
|
+ if ( onLoad ) onLoad( cached );
|
|
|
+
|
|
|
+ scope.manager.itemEnd( url );
|
|
|
+
|
|
|
+ }, 0 );
|
|
|
+
|
|
|
+ return cached;
|
|
|
|
|
|
- };
|
|
|
- image.onerror = onError;
|
|
|
+ }
|
|
|
+
|
|
|
+ var image = document.createElementNS( 'http://www.w3.org/1999/xhtml', 'img' );
|
|
|
|
|
|
- if ( url.indexOf( 'data:' ) === 0 ) {
|
|
|
+ image.addEventListener( 'load', function () {
|
|
|
|
|
|
- image.src = url;
|
|
|
+ THREE.Cache.add( url, this );
|
|
|
|
|
|
- } else if ( this.crossOrigin !== undefined ) {
|
|
|
+ if ( onLoad ) onLoad( this );
|
|
|
|
|
|
- // crossOrigin doesn't work with URL.createObjectURL()?
|
|
|
+ scope.manager.itemEnd( url );
|
|
|
|
|
|
- image.crossOrigin = this.crossOrigin;
|
|
|
- image.src = url;
|
|
|
+ }, false );
|
|
|
|
|
|
- } else {
|
|
|
+ /*
|
|
|
+ image.addEventListener( 'progress', function ( event ) {
|
|
|
|
|
|
- var loader = new FileLoader();
|
|
|
- loader.setPath( this.path );
|
|
|
- loader.setResponseType( 'blob' );
|
|
|
- loader.setWithCredentials( this.withCredentials );
|
|
|
+ if ( onProgress ) onProgress( event );
|
|
|
|
|
|
- // By default the FileLoader requests files to be loaded with a MIME
|
|
|
- // type of `text/plain`. Using `URL.createObjectURL()` with SVGs that
|
|
|
- // have a MIME type of `text/plain` results in an error, so explicitly
|
|
|
- // set the SVG MIME type.
|
|
|
- if ( /\.svg$/.test( url ) ) loader.setMimeType( 'image/svg+xml' );
|
|
|
+ }, false );
|
|
|
+ */
|
|
|
|
|
|
- loader.load( url, function ( blob ) {
|
|
|
+ image.addEventListener( 'error', function ( event ) {
|
|
|
|
|
|
- image.src = URL.createObjectURL( blob );
|
|
|
+ if ( onError ) onError( event );
|
|
|
|
|
|
- }, onProgress, onError );
|
|
|
+ scope.manager.itemError( url );
|
|
|
|
|
|
- }
|
|
|
+ }, false );
|
|
|
+
|
|
|
+ if ( this.crossOrigin !== undefined ) image.crossOrigin = this.crossOrigin;
|
|
|
|
|
|
scope.manager.itemStart( url );
|
|
|
|
|
|
+ image.src = url;
|
|
|
+
|
|
|
return image;
|
|
|
|
|
|
},
|
|
@@ -76,13 +85,6 @@ Object.assign( ImageLoader.prototype, {
|
|
|
|
|
|
},
|
|
|
|
|
|
- setWithCredentials: function ( value ) {
|
|
|
-
|
|
|
- this.withCredentials = value;
|
|
|
- return this;
|
|
|
-
|
|
|
- },
|
|
|
-
|
|
|
setPath: function ( value ) {
|
|
|
|
|
|
this.path = value;
|