asset-loader

npm version Version License

icon

Description

The asset loader is a package used for preloading assets for your app or game. The idea is this:

Usage

Preloading

Instantiate the loader, then preload all assets you will eventually need.

const loader = new Loader();
loader.load(url1);
loader.load(url2);
loader.load(url3);
//...

Note that loader.load return a promise, but we don’t put await because we’re not waiting for those to finish.

Fetch asset on demand

Then your app will need some assets immediately:

const assetToShow = await loader.getUrl(url);
const img = new Image();
img.src = assetToShow;

The URL you’re passing to img.src will be a blob URL, which is shown immediately if the asset is in memory, or will wait for the await otherwise. Since you’ll be progressively downloading all assets, they will eventually all be available immediately.

Configure

You can change the configuration when instantiating the loader:

const loader = new Loader({
  waitBetweenLoader: 1000,  //  default 10
  retries: 5,               //  default 3
  maxParallelLoad: 5,       //  default 3
});

Demo

Below is a demo on @dobuki/asset-loader being used to load all assets within a Github repo.

Click here to view example.