-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
preload.js
59 lines (52 loc) · 1.57 KB
/
preload.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
(function () {
'use strict';
// v1.1.0
// preload images prior to story startup
function preloadImage (url, cb) {
var img = new Image();
img.onload = cb;
img.onerror = cb;
img.src = url;
}
function preloadAll (urls, cb) {
var loaded = 0;
var toBeLoaded = urls.length;
if (toBeLoaded === 0) {
throw new Error("No URLs to preload!");
}
urls.forEach( function (url) {
preloadImage( url, function () {
loaded++;
if (loaded === toBeLoaded) {
cb();
}
});
});
}
function isInit () {
return State.length <= 0;
}
function preload () {
var list = [].slice.call(arguments).flat(Infinity);
var id = isInit() ? LoadScreen.lock() : false;
preloadAll( list, function () {
if (id) {
LoadScreen.unlock(id);
}
});
return id;
}
setup.preload = preload;
setup.preload.force = false;
/* <<preload imageUrls...>> */
Macro.add('preload', {
handler : function () {
if (!isInit() && !setup.preload.force) {
return this.error("Attempting to preload images outside of `StoryInit` or similar can cause performance issues. Set `setup.preload.force` to `true` if you want to do it anyway.");
}
preload(this.args.flat(Infinity).filter( function (url) {
return typeof url === 'string';
}));
}
});
}());