load-asset
A simple Promise-based asset loader with cross browser support down to IE 11. Ideal for use with async/await syntax. Uses fetch
where possible, otherwise falls back to XMLHTTPRequest
.
Tries to infer loader type from extension, but you can also specify a type
for other extensions – see Loaders for details.
- Image tag (png, jpg, svg, etc)
- Audio tag (mp3, ogg, etc)
- Video tag (mp4, etc)
- JSON (json)
- Text (txt)
- Binary (bin)
- Blob
const load = ; { // Load a single <img> asset const image = await ; // Print the image width and height after loading console;}
Handles loading a single asset, or multiple assets in parallel. You can use a named object map for convenience, provide a progress
callback for visual updates, and optionally continue loading if individual resources error out.
Note: You will need to polyfill Promise for older browsers.
Full Example
const load = ; { // Load markdown file as text string const readme = await ; console; // Load a list of named assets in parallel const urls = 'a.png' 'data.json' 'other.txt' ; const items = await loadallurls; console; // Load a map of named assets in parallel // But use 'any' instead of 'all' so that errors/404s // do not stop loading the rest of the assets. const assets = await load; console; // <img> tag console; // JSON data}
Install
Use npm to install.
npm install load-asset --save
Usage
asset = load(item)
Loads the item
asset, which is either a string URL or an object with properties, and returns an asset
Promise which will resolve to the loaded object (such as HTMLVideoElement, Audio or Image tags).
If item
is an object, you can specify a list of options like so:
url
(required) - the URL to the assettype
- the loader type name, can be'image'
,'audio'
, etc (see full list below). If not specified, it will be inferred from file extension if possible. If no loader is found by the extension, or no extension exists, the returned Promise will reject with an error.
Loaders may have individual options that can also be passed down into them, such as crossOrigin
on Image:
const image = await ;
You can also pass a function to type
if you have a custom loader, see test/custom-type.js for an example loading the Wavefront OBJ format. This way you can integrate other game/app-specific asset types into a single sequence of progress events.
assets = load.all(items, [progress])
This loads an array or map of items
, loading them all in parallel, and returns the result when all are finished loading.
If you specify an array, the returned Promise resolves to an array of loaded values corresponding to the same order as the items
. Alternatively, you can specify an object to receive back a map with named values.
{ // Resolves to an array of images const images = await loadall 'a.png' 'b.png' ; console; // Resolves to an object mapping const assets = await loadall normal: 'foo/normal.png' diffuse: 'foo/diffuse.png' ; console;}
You can optionally specify a progress
callback function which gets triggered after each item is loaded, passed with an event
object:
count
- number of items that have been loadedtotal
- number of total items in the queueprogress
- a number between 0 (exclusive) and 1 (inclusive) of load completiontarget
- an object or string representing the asset descriptor to loadvalue
- the loaded value for this target
Note: With this function, if one asset fails to load, the loading will stop and reject early.
assets = load.any(items, [progress])
The same as load.all
, except that any errors in loading (for e.g. 404 assets) will be caught and resolved to null
, so that other assets can continue loading.
If a resource did not load, an additional error
property will be added to the progress
callback event for that resource.
Example:
{ // Resolves to an array of images const results = await load; // Filter out any images that couldn't load const images = results; // ... do something ...}
Nesting
If you pass a Promise to the load funtion, it will simply be returned. This allows for nesting features, for example:
loaderall tiles: loaderall 'a.png' 'b.png' 'c.png' group: loaderall iconA: 'bar.png' iconB: 'foo.png' ;
Loaders
All loaders, their inferred extensions, and any additional options are detailed below. You can specify a type
option, such as 'image'
or 'blob'
, to override the extension lookup.
'image'
Extensions: jpg, jpeg, svg, png, gif, webp, bmp, tga, tif, apng, wbpm, ico
Resolves to an Image tag. You can also pass crossOrigin
in options for images, e.g. { crossOrigin: 'Anonymous' }
.
'json'
Extensions: json
Resolves to parsed JSON data.
'text'
Extensions: txt
Resolves to a text string.
'audio'
/ 'video'
Audio Extensions: mp3, ogg, wav, midi, and more
Video Extensions: mp4, m4v, mov, mkv, mpeg, and more
See here for all inferred audio and video extensions.
Additional options for audio
& video
types:
crossOrigin
- e.g. 'Anonymous'event
- the load event to wait for, default'canplay'
, can also be:'canplaythrough'
,'loadeddata'
,'loadedmetadata'
- Additional media properties: volume, preload, playbackRate, muted, currentTime, controls, autoPlay
'binary'
Extensions: bin
Resolves to an ArrayBuffer.
'blob'
Not associated with any file extensions.
License
MIT, see LICENSE.md for details.