-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathimageLoader.js
101 lines (88 loc) · 2.27 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
92
93
94
95
96
97
98
99
100
101
// can count required and retreived for a progresss bar indicator...
let didWork = false;
let useOrigin = null;
let pending = [];
export async function wait() {
await Promise.all( pending );
}
//------------------------ Image Library ---------------------------------
export function getImage(src) {
return new Promise( (res,rej)=>{
const i = new Image();
let resi;
let pi = new Promise( (res,rej)=>{
resi = i;
} );
if( src[0] !== '/' && src.substring(0,4)!='http' && useOrigin )
src = useOrigin + src;
i.crossOrigin = "Anonymous";
i.onload = ()=>{
const idx = pending.find( p=>p===pi );
if( idx >= 0 ) pending.splice( idx, 1 );
res( i );
};
i.onerror = ()=>{
const idx = pending.find( p=>p===pi );
if( idx >= 0 ) pending.splice( idx, 1 );
rej( src, i );
}
i.src = src;
pending.push(pi);
} );
}
export function getSound(src) {
return new Promise( (res,rej)=>{
var i = document.createElement( "AUDIO" );
if( src[0] !== '/' && src.substring(0,4)!='http' && useOrigin )
src = useOrigin + src;
i.crossOrigin = "Anonymous";
i.onload = ()=>{
res( i );
};
i.onerror = ()=>{
rej( src, i );
}
i.src = src;
} );
}
//------------------------ Image Library ---------------------------------
export function newSound(src) {
var i = document.createElement( "AUDIO" );
var timeout;
i.crossOrigin = "Anonymous";
i.onerror = (err)=>{
console.log( "ERROR:", err );
}
//if( pendingLoad.length ) { pendingLoad.push( {i:i,src:src} ); return i; }
//if( requiredImages.length ) { pendingLoad.push({i:i,src:src}); return i; }
i.src = src;
// requiredImages.push( i );
return i;
}
export function newVideo(src) {
var i = document.createElement( "video" );
var timeout;
i.crossOrigin = "Anonymous";
i.onload = ()=>{
//clearTimeout( timeout );
var pl = pendingLoad;
requiredImages.pop();
if( pl.length ) {
var pi = pl.shift();
requiredImages.push( pi.i );
console.log( "loading:", pi.src );
window.lastImage = pi.i;
pi.i.src = pi.src;
pendingLoad = pl;
//return;
}
console.log( "newVIdeo got message: ", requiredImages.length );
if( requiredImages.length == 0 ) doWork();
};
i.src = src;
//requiredImages.push( i );
return i;
}
export function setOrigin( origin ) {
useOrigin = origin;
}