-
Notifications
You must be signed in to change notification settings - Fork 747
Expand file tree
/
Copy pathPreloadQueue.html
More file actions
224 lines (185 loc) · 5.08 KB
/
PreloadQueue.html
File metadata and controls
224 lines (185 loc) · 5.08 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<!DOCTYPE html>
<html>
<head>
<title>PreloadJS: Queue Example</title>
<link href="../_assets/css/shared.css" rel="stylesheet" type="text/css"/>
<link href="../_assets/css/examples.css" rel="stylesheet" type="text/css"/>
<script src="../_assets/js/examples.js"></script>
<style type="text/css">
#template {
display: none;
}
#mainProgress {
width: auto;
height: 20px;
float: none;
position: relative;
}
#mainProgress LABEL {
position: absolute;
}
.item {
height: 170px;
width: 226px;
border: 1px solid #eee;
background-color: #ddd;
padding: 2px;
float: left;
position: relative;
text-align: center;
}
.item .progress {
width: 0;
height: 20px;
background-color: #9c9;
bottom: 0;
}
.complete {
}
.error {
background-color: #FAA;
}
.complete DIV, .error DIV {
display: none;
}
</style>
</head>
<body onload="init()">
<header class="PreloadJS">
<h1>Preload Queue</h1>
<p>Click "load another" to add another image the the overall queue. As
images are added, the preload progress reflects
the overall loaded progress. Click "load all" to queue everything at
once.
Note that when loading images and sounds locally, you need to ensure
that PreloadJS uses tag loading to avoid
cross-origin errors.</p>
</header>
<div id="container" class="content">
<input type="button" id="loadAnotherBtn" value="Load Another"
disabled="disabled"/>
<input type="button" id="loadAllBtn" value="Load All" disabled="disabled"/>
<input type="button" id="reloadBtn" value="Reset"/>
<div id="mainProgress" class="item">
<label>Overall Progress</label>
<div class="progress"></div>
</div>
<hr/>
<!-- New items will get placed here -->
</div>
<!-- Item Template. This is cloned whenever we want a new one. -->
<div id="template" class="item">
<div class="progress"></div>
</div>
<script type="text/javascript" src="../lib/preloadjs-NEXT.js"></script>
<!-- We also provide hosted minified versions of all CreateJS libraries.
http://code.createjs.com -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" id="editable">
var map = {};
var preload;
var loader;
var manifest;
var w = 226; // Item width
var h = 170; // Item height
function init() {
$("#loadAnotherBtn").click(loadAnother);
$("#loadAllBtn").click(loadAll);
$("#reloadBtn").click(reload);
reload();
}
// Reset everything
function reload() {
// If there is an open preload queue, close it.
if (preload != null) {
preload.close();
}
// Reset the UI
$("#reloadBtn").css("display", "none");
$(".box").remove();
$("#mainProgress .progress").width(0);
$("#loadAnotherBtn").attr("disabled", null);
$("#loadAllBtn").attr("disabled", null);
// Push each item into our manifest
manifest = [
"image0.jpg",
"image1.jpg",
"image2.jpg",
"image3.jpg",
"Autumn.png",
"BlueBird.png",
"Nepal.jpg",
"Texas.jpg"
];
// Create a preloader. There is no manifest added to it up-front, we will add items on-demand.
preload = new createjs.LoadQueue(true, "../_assets/art/");
// Use this instead to use tag loading
//preload = new createjs.LoadQueue(false);
preload.on("fileload", handleFileLoad);
preload.on("progress", handleOverallProgress);
preload.on("fileprogress", handleFileProgress);
preload.on("error", handleFileError);
preload.setMaxConnections(5);
}
function stop() {
if (preload != null) {
preload.close();
}
}
function loadAll() {
while (manifest.length > 0) {
loadAnother();
}
}
function loadAnother() {
// Get the next manifest item, and load it
var item = manifest.shift();
preload.loadFile(item);
// If we have no more items, disable the UI.
if (manifest.length == 0) {
$("#loadAnotherBtn").attr("disabled", "disabled");
$("#loadAllBtn").attr("disabled", "disabled");
$("#reloadBtn").css("display", "inline");
}
// Create a new loader display item
var div = $("#template").clone();
div.attr("id", ""); // Wipe out the ID
div.addClass("box")
$("#container").append(div);
map[item] = div; // Store a reference to each item by its src
}
// File complete handler
function handleFileLoad(event) {
var div = map[event.item.id];
div.addClass("complete");
// Get a reference to the loaded image (<img/>)
var img = event.result;
// Resize it to fit inside the item
var r = img.width / img.height;
var ir = w / h
if (r > ir) {
img.width = w;
img.height = w / r;
} else {
img.height = h;
img.width = h;
}
div.append(img); // Add it to the DOM
}
// File progress handler
function handleFileProgress(event) {
var div = map[event.item.id]; // Lookup the related item
div.children("DIV").width(event.progress * div.width()); // Set the width the progress.
}
// Overall progress handler
function handleOverallProgress(event) {
$("#mainProgress > .progress").width(preload.progress * $("#mainProgress").width());
}
// An error happened on a file
function handleFileError(event) {
var div = map[event.item.id];
div.addClass("error");
}
</script>
</body>
</html>