forked from UL-FRI-LGM/RenderCore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageLoader.js
91 lines (70 loc) · 1.57 KB
/
ImageLoader.js
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* Created by Ziga on 18.4.2016
* Source: three.js
*/
import {Cache} from './Cache.js';
import {LoadingManager} from './LoadingManager.js';
export class ImageLoader {
constructor(manager) {
this.manager = manager || new LoadingManager();
}
load(url, onLoad, onProgress, onError) {
// Self reference
var scope = this;
// Check if root path is set
if (this.path !== undefined) {
url = this.path + url;
}
// Notify img started loading
this.manager.itemStart(url);
// Check if the image is already cached
var cached = Cache.get(url);
if (cached !== undefined) {
if (onLoad) {
// Async
setTimeout(function() {
onLoad(cached);
scope.manager.itemEnd(url);
}, 0);
} else {
scope.manager.itemEnd(url);
}
return cached;
}
// Create new image
var image = new Image();
image.src = url;
// onLoad listener
image.addEventListener('load', function(event) {
// Cache loaded image
Cache.add(url, this);
// Return the loaded image
if (onLoad !== undefined) {
onLoad(image);
}
scope.manager.itemEnd(url);
});
// onProgress listener
image.addEventListener('progress', function(event) {
if (onProgress !== undefined) {
onProgress(event);
}
});
// onError listener
image.addEventListener('error', function(event) {
if (onError) {
onError(event);
}
scope.manager.itemError(url);
});
if (this.crossOrigin !== undefined) {
image.crossOrigin = this.crossOrigin;
}
}
setCrossOrigin(value) {
this.crossOrigin = value;
}
setPath(value) {
this.path = value;
}
};