Graphics Reference
In-Depth Information
Getting ready
Before looking at the steps in this recipe, you will need to create a number of stand-
ard callbacks that can be used by all the different loaders. These callbacks are used
to inform you when a resource is loaded, when loading fails and, if available, the pro-
gress of the current request.
So for loading resources, we need to define three different callbacks:
• The
onload
callback: Whenever a resource is loaded, this callback will be
called with the loaded resource as an argument.
• The
onprogress
callback: Some loaders provide progress during the load-
ing of a resource. At specific intervals, this callback will be called to inform
you how much of the resource has been loaded.
• The
onerror
callback: If something goes wrong during the loading of the re-
source, this callback is used to inform you about the error that occurred.
For all the recipes dealing with asynchronous loading, we'll use the same set of
loaders. These loaders just output some information to the console, but you can of
course customize these callbacks for your specific use case.
First, we define the
onLoadCallback
function, which is called when a resource is
loaded:
function onLoadCallback(loaded) {
// just output the length for arrays and
binary blobs
if (loaded.length) {
console.log("Loaded", loaded.length);
} else {
console.log("Loaded", loaded);
}
}
As you can see from the function definition, we just output the passed argument to
the console. The other two callbacks,
onProgressCallback
and
onErrorCall-
back
, work exactly in the same manner as they are presented: