Spaces:
Running
Running
File size: 1,427 Bytes
2b7aae2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | import { Cache } from './Cache.js';
import { Loader } from './Loader.js';
import { createElementNS } from '../utils.js';
class ImageLoader extends Loader {
constructor(manager) {
super(manager);
}
load(url, onLoad, onProgress, onError) {
if (this.path !== undefined) url = this.path + url;
url = this.manager.resolveURL(url);
const scope = this;
const cached = Cache.get(url);
if (cached !== undefined) {
scope.manager.itemStart(url);
setTimeout(function () {
if (onLoad) onLoad(cached);
scope.manager.itemEnd(url);
}, 0);
return cached;
}
const image = createElementNS('img');
function onImageLoad() {
removeEventListeners();
Cache.add(url, this);
if (onLoad) onLoad(this);
scope.manager.itemEnd(url);
}
function onImageError(event) {
removeEventListeners();
if (onError) onError(event);
scope.manager.itemError(url);
scope.manager.itemEnd(url);
}
function removeEventListeners() {
image.removeEventListener('load', onImageLoad, false);
image.removeEventListener('error', onImageError, false);
}
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;
}
}
export { ImageLoader };
|