PreloadJS Documentation
repository·master·Indexed 25 days ago
https://github.com/createjs/preloadjsPreloadJS is a library that provides a consistent API for preloading assets including images, sounds, JavaScript, fonts, JSON, and text data. It features automatic XHR detection with tag-based fallbacks, composite progress events, and a plugin system for integration with other libraries like SoundJS. The primary interface for managing preloading tasks is the LoadQueue class.
What's inside PreloadJS
- PreloadJS is a library designed to simplify asset preloading. It provides a consistent API for loading various file types, automatically detects XHR (XMLHttpRequest) availability with a fallback to tag-based loading, provides composite progress events, and features a plugin model for integration with other libraries like SoundJS.
Access PreloadJS classes via the createjs namespace
masterIn this version of PreloadJS, all class definitions are contained within the
createjsnamespace by default. Instead of instantiating classes directly (e.g.,new PreloadJS()), you must access them through the namespace (e.g.,new createjs.LoadQueue()).var bar = new createjs.LoadQueue();Remove the createjs namespace
masterTo remove the namespace entirely and make the libraries compatible with legacy content (such as Flash Pro Toolkit output for CreateJS v1.0), assign
windowto thecreatejsvariable before loading the libraries. This causes the classes to be defined directly on the globalwindowobject.<script> var createjs = window; // sets window as the createjs namespace </script> <script src="easeljs.js"></script>Configure Cross-Origin headers for remote assets
masterIf you are serving images or other assets from a different domain than your application, you must configure cross-origin headers on the asset server. This allows assets to be loaded with the
crossOrigin="Anonymous"attribute.To do this on a Linux/Unix server, use the contents of the provided
sample.htaccessfile. You can either rename this file to.htaccessin your asset directory or append its contents to your existing.htaccessfile.Access PreloadJS via CDN
masterTo benefit from faster load times and shared caching across sites, you can link to the PreloadJS libraries hosted on the CreateJS CDN instead of hosting them locally.
http://code.createjs.com/Shortcut the createjs namespace
masterIf you want to avoid typing the full
createjsprefix, you can create a shortcut by assigning thecreatejsobject to a different variable after the libraries have loaded.<script src="easeljs.js"></script> <script> var c = createjs; // creates a reference to the createjs namespace in "c" var foo = new c.Shape(); </script>Use ManifestLoader to load JSON manifests
masterThe
ManifestLoaderis used to load assets defined in a JSON manifest. A manifest is a JSON object with amanifestproperty containing an array of items. Items can be simple strings (paths) or objects specifyingsrc,id,type, orcallback(for JSONP).When a
ManifestLoadercompletes, the items it loaded are inherited by the parent loader (e.g., aLoadQueue), making them directly accessible.Note: To avoid conflicts with higher-priority loaders like
JSONLoader, you must explicitly set thetypeproperty of your manifest items tocreatejs.Types.MANIFEST.{ "path": "assets/", "manifest": [ "image.png", {"src": "image2.png", "id":"image2"}, {"src": "sub-manifest.json", "type":"manifest", "callback":"jsonCallback"} ] }Basic usage of LoadQueue
masterTo preload assets, instantiate a
createjs.LoadQueue. You can listen for thefileloadevent to handle individual files as they complete. UseloadFile()to start loading a specific file URL.var queue = new createjs.LoadQueue(false); queue.on("fileload", handleFileComplete); queue.loadFile('http://createjs.com/assets/images/png/createjs-badge-dark.png'); function handleFileComplete(event) { document.body.appendChild(event.result); }Select the appropriate PreloadJS library version
masterThe
libdirectory provides different versions of PreloadJS depending on your needs for stability, debugging, or deployment:- For stable production use: Use
preloadjs.js(unminified, for debugging) orpreloadjs.min.js(minified, for deployment). These represent the most recent tagged release. - For testing latest features: Use
preloadjs-NEXT.js(unminified) orpreloadjs-NEXT.min.js(minified). These contain the latest in-progress updates.
- For stable production use: Use
Monitor Loader progress
masterWhen using an
AbstractLoader(or its subclasses likeLoadQueue), you can monitor the loading progress of an individual item via theprogressproperty (a value between 0 and 1) and theprogressevent.var queue = new createjs.LoadQueue(); queue.loadFile("largeImage.png"); queue.on("progress", function(event) { console.log("Progress:", queue.progress, event.progress); });Promote superclass methods with `createjs.promote`
masterUse
createjs.promote(subclass, prefix)to create aliases for superclass methods that were overridden in the subclass. This allows calling superclass methods viaprefix_methodName(e.g.,MySuperClass_draw) without usingfunction.call, which improves performance. An alias for the constructor is also added asprefix_constructor.function ClassA(name) { this.name = name; } ClassA.prototype.greet = function() { return "Hello " + this.name; }; function ClassB(name, punctuation) { this.ClassA_constructor(name); this.punctuation = punctuation; } createjs.extend(ClassB, ClassA); ClassB.prototype.greet = function() { return this.ClassA_greet() + this.punctuation; }; createjs.promote(ClassB, "ClassA"); var foo = new ClassB("World", "!?!!"); console.log(foo.greet()); // Hello World!?!?!Add files and manifests to LoadQueue
masterYou can add files to the queue individually or in bulk using manifests. Files are appended to the end of the active queue.
- Single file: Use
loadFile()with a string path or an object. - Multiple files/Manifests: Use
loadManifest()with an array of files, a manifest definition object, or a path to a JSON manifest file.
If you pass
falseas theloadNowparameter (in methods that support it), the queue will pause. You can then call.load()to begin processing.- Single file: Use