/
ImageBitmapLoader.js
109 lines (57 loc) · 1.8 KB
/
ImageBitmapLoader.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* @author thespite / http://clicktorelease.com/
*/
import { Cache } from './Cache.js';
import { Loader } from './Loader.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.' );
}
Loader.call( this, manager );
this.options = undefined;
}
ImageBitmapLoader.prototype = Object.assign( Object.create( Loader.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 );
}
} );
export { ImageBitmapLoader };