Fetching contributors…
Cannot retrieve contributors at this time
225 lines (185 sloc) 5.08 KB
<!DOCTYPE html>
<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;
<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
Note that when loading images and sounds locally, you need to ensure
that PreloadJS uses tag loading to avoid
cross-origin errors.</p>
<div id="container" class="content">
<input type="button" id="loadAnotherBtn" value="Load Another"
<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>
<!-- New items will get placed here -->
<!-- Item Template. This is cloned whenever we want a new one. -->
<div id="template" class="item">
<div class="progress"></div>
<script type="text/javascript" src="../lib/preloadjs-NEXT.combined.js"></script>
<!-- We also provide hosted minified versions of all CreateJS libraries. -->
<script src=""></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() {
// Reset everything
function reload() {
// If there is an open preload queue, close it.
if (preload != null) {
// Reset the UI
$("#reloadBtn").css("display", "none");
$("#mainProgress .progress").width(0);
$("#loadAnotherBtn").attr("disabled", null);
$("#loadAllBtn").attr("disabled", null);
// Push each item into our manifest
manifest = [
// 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);
function stop() {
if (preload != null) {
function loadAll() {
while (manifest.length > 0) {
function loadAnother() {
// Get the next manifest item, and load it
var item = manifest.shift();
// 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
map[item] = div; // Store a reference to each item by its src
// File complete handler
function handleFileLoad(event) {
var div = map[];
// 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[]; // 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[];