1 |
|
2 |
|
3 |
|
4 |
|
5 | import { Cache } from './Cache.js';
|
6 | import { DefaultLoadingManager } from './LoadingManager.js';
|
7 |
|
8 |
|
9 | function ImageBitmapLoader( manager ) {
|
10 |
|
11 | if ( typeof createImageBitmap === 'undefined' ) {
|
12 |
|
13 | console.warn( 'THREE.ImageBitmapLoader: createImageBitmap() not supported.' );
|
14 |
|
15 | }
|
16 |
|
17 | if ( typeof fetch === 'undefined' ) {
|
18 |
|
19 | console.warn( 'THREE.ImageBitmapLoader: fetch() not supported.' );
|
20 |
|
21 | }
|
22 |
|
23 | this.manager = manager !== undefined ? manager : DefaultLoadingManager;
|
24 | this.options = undefined;
|
25 |
|
26 | }
|
27 |
|
28 | ImageBitmapLoader.prototype = {
|
29 |
|
30 | constructor: ImageBitmapLoader,
|
31 |
|
32 | setOptions: function setOptions( options ) {
|
33 |
|
34 | this.options = options;
|
35 |
|
36 | return this;
|
37 |
|
38 | },
|
39 |
|
40 | load: function load( url, onLoad, onProgress, onError ) {
|
41 |
|
42 | if ( url === undefined ) url = '';
|
43 |
|
44 | if ( this.path !== undefined ) url = this.path + url;
|
45 |
|
46 | var scope = this;
|
47 |
|
48 | var cached = Cache.get( url );
|
49 |
|
50 | if ( cached !== undefined ) {
|
51 |
|
52 | scope.manager.itemStart( url );
|
53 |
|
54 | setTimeout( function () {
|
55 |
|
56 | if ( onLoad ) onLoad( cached );
|
57 |
|
58 | scope.manager.itemEnd( url );
|
59 |
|
60 | }, 0 );
|
61 |
|
62 | return cached;
|
63 |
|
64 | }
|
65 |
|
66 | fetch( url ).then( function ( res ) {
|
67 |
|
68 | return res.blob();
|
69 |
|
70 | } ).then( function ( blob ) {
|
71 |
|
72 | return createImageBitmap( blob, scope.options );
|
73 |
|
74 | } ).then( function ( imageBitmap ) {
|
75 |
|
76 | Cache.add( url, imageBitmap );
|
77 |
|
78 | if ( onLoad ) onLoad( imageBitmap );
|
79 |
|
80 | scope.manager.itemEnd( url );
|
81 |
|
82 | } ).catch( function ( e ) {
|
83 |
|
84 | if ( onError ) onError( e );
|
85 |
|
86 | scope.manager.itemEnd( url );
|
87 | scope.manager.itemError( url );
|
88 |
|
89 | } );
|
90 |
|
91 | },
|
92 |
|
93 | setCrossOrigin: function ( /* value */ ) {
|
94 |
|
95 | return this;
|
96 |
|
97 | },
|
98 |
|
99 | setPath: function ( value ) {
|
100 |
|
101 | this.path = value;
|
102 | return this;
|
103 |
|
104 | }
|
105 |
|
106 | };
|
107 |
|
108 | export { ImageBitmapLoader };
|