Class: Loader

PIXI.Loader

The new loader, extends Resource Loader by Chad Engler: https://github.com/englercj/resource-loader

const loader = PIXI.Loader.shared; // PixiJS exposes a premade instance for you to use.
//or
const loader = new PIXI.Loader(); // you can also create your own if you want

const sprites = {};

// Chainable `add` to enqueue a resource
loader.add('bunny', 'data/bunny.png')
      .add('spaceship', 'assets/spritesheet.json');
loader.add('scoreFont', 'assets/score.fnt');

// Chainable `pre` to add a middleware that runs for each resource, *before* loading that resource.
// This is useful to implement custom caching modules (using filesystem, indexeddb, memory, etc).
loader.pre(cachingMiddleware);

// Chainable `use` to add a middleware that runs for each resource, *after* loading that resource.
// This is useful to implement custom parsing modules (like spritesheet parsers, spine parser, etc).
loader.use(parsingMiddleware);

// The `load` method loads the queue of resources, and calls the passed in callback called once all
// resources have loaded.
loader.load((loader, resources) => {
    // resources is an object where the key is the name of the resource loaded and the value is the resource object.
    // They have a couple default properties:
    // - `url`: The URL that the resource was loaded from
    // - `error`: The error that happened when trying to load (if any)
    // - `data`: The raw data that was loaded
    // also may contain other properties based on the middleware that runs.
    sprites.bunny = new PIXI.TilingSprite(resources.bunny.texture);
    sprites.spaceship = new PIXI.TilingSprite(resources.spaceship.texture);
    sprites.scoreFont = new PIXI.TilingSprite(resources.scoreFont.texture);
});

// throughout the process multiple signals can be dispatched.
loader.onProgress.add(() => {}); // called once per loaded/errored file
loader.onError.add(() => {}); // called once per errored file
loader.onLoad.add(() => {}); // called once per loaded file
loader.onComplete.add(() => {}); // called once when the queued resources all load.

new PIXI.Loader (baseUrl, concurrency)

Name Type Attributes Default Description
baseUrl string <optional>
''

The base url for all resources loaded by this loader.

concurrency number <optional>
10

The number of resources to load concurrently.

See:

Extends

  • ResourceLoader

Members

PIXI.Loader.shared PIXI.Loader static

A premade instance of the loader that can be used to load resources.

Dispatched when completely loaded all resources.

Dispatched once per errored resource.

Dispatched once per loaded resource.

Dispatched once per loaded or errored resource.

Dispatched when the loader begins to loading process.

Type Definitions

ICallbackID object

ILoaderSignal object

Properties:
Name Type Description
add ISignalCallback

Register callback

detach ISignalDetach

Detach specific callback by ID

once ISignalCallback

Register oneshot callback

ISignalCallback

Name Type Attributes Description
callback Function

Callback function

context object <optional>

Context

Returns:
Type Description
ICallbackID
  • CallbackID

ISignalDetach

Name Type Description
id ICallbackID

CallbackID returned by add/once methods

loaderMiddleware

Callback

Name Type Description
resource PIXI.LoaderResource
next Function

Methods

PIXI.Loader.off (event) Deprecated`` : since 5.0.0 static

Use the corresponding signal, e.g., event start`` is signal onStart`.

Name Type Description
event any

PIXI.Loader.on (event) Deprecated`` : since 5.0.0 static

Use the corresponding signal, e.g., event start`` is signal onStart`.

Name Type Description
event any

PIXI.Loader.once (event) Deprecated`` : since 5.0.0 static

Use the corresponding signal, e.g., event start`` is signal onStart`.

Name Type Description
event any

PIXI.Loader.registerPlugin (plugin) PIXI.Loader static

Adds a Loader plugin for the global shared loader and all new Loader instances created.

Name Type Description
plugin PIXI.ILoaderPlugin

The plugin to add

Returns:
Type Description
PIXI.Loader Reference to PIXI.Loader for chaining

destroy () void

Destroy the loader, removes references.