5.0.2 • Published 4 years ago

musk-ox v5.0.2

Weekly downloads
21
License
MIT
Repository
github
Last release
4 years ago

NPM version Known Vulnerabilities npm NPM downloads Gitter

Table of Contents

Installation

MuskOx is shipped as an ES6 module.

To install MuskOx through npm, simply use the following command:

$ npm install musk-ox

Initialization

To use MuskOx, simply import the module:

import MuskOx from './path/to/muskox.js';

and after that, a new instance can be initialized like so:

const ox = new MuskOx();

As of 5.0.0 the single crossOrigin initialization parameter has been replaced with an options object:

paramtypedescriptiondefault
optionsObject
crossOriginstringA cross-origin property to set for all assets that use cross-origin.''
audioContextAudioContextA reference to an existing AudioContext to use if creating web audio assets. If one is not assigned then a new instance of an AudioContext will be used.new AudioContext()

The MuskOx asset loading system is split into three steps: defining assets to load, initiating the load, and retrieving loaded assets.

Signals and Events

Before we get into usage, it helps to know how Musk Ox's event system works. Instead of using traditional events, MuskOx uses signals which don't rely on event names but objects that dispatch the signal with any data needed.

To respond to a signal, you have to use the signal's add method and then declare the function that you want to run when the signal is dispatched.

Currently MuskOx has the following available events:

onLoad

The onLoad signal is dispatched each time an asset is loaded and it contains the data of the asset that was loaded.

const onLoadResponse = (asset) => {
  console.log(asset); // Asset is the asset that was most recently loaded.
};

ox.onLoad.add(onLoadResponse);

onComplete

The onComplete signal is dispatched when all of the assets are loaded.

const onCompleteResponse = () => {
};

ox.onComplete.add(onCompleteResponse);

onError

The onError signal is dispatched if an asset encounters an error while loading and it contains the asset and error that was thrown.

const onErrorResponse = (asset, err) => {
  console.log(asset); // Asset is the asset that had trouble loading.
  console.log(err); // Err is the error that was thrown while loading the asset.
};

ox.onError.add(onErrorResponse);

onProgress

The onProgress signal is dispatched when the loading progress changes and it contains the current load percentage.

const onProgressResponse = (percent) => {
  console.log(percent); // Percent is the current loading percentage.
};

ox.onProgress.add(onProgressResponse);

Step 1: Defining Assets to Load

The first step in using MuskOx involves defining the assets that need to be loaded. As of current, MuskOx supports loading images, audio, video, text, binary, and JSON assets. Assets defined during the loading phase are set to a queue that will process when the start method is called.

image

Adds an image asset to the load queue.

paramtypedescriptiondefault
keystringA unique key which will be used to reference this image asset by when using it
srcstringThe path to the image asset.
replacebooleanIf set to true, this image asset will replace an existing image asset with the same keyfalse
ox.image('star', './images/star.png');

audio

Adds an audio asset to the load queue.

paramtypedescriptiondefault
keystringA unique key which will be used to reference this audio asset by when using it
srcsstring or ArrayOne or more paths to the audio asset. Multiple paths can be passed as fallbacks in case the user's browser doesn't support the one or more of the formats
replacebooleanIf set to true, this audio asset will replace an existing audio asset with the same keyfalse

Using a single source:

ox.audio('podcast', './recordings/2019-01-01.m4a');

Using fallback sources:

ox.audio('podcast', ['./recordings/2019-01-01.m4a', './recordings/2019-01-01.wav']);

audioBuffer

Adds an audio asset to the load queue to load as an AudioBuffer.

paramtypedescriptiondefault
keystringA unique key which will be used to reference this audio buffer by when using it
srcstringThe path to the audio asset.
replacebooleanIf set to true, this audio buffer will replace an existing audio buffer with the same keyfalse
ox.audioBuffer('podcast', './recordings/2019-01-01.m4a');

video

Adds a video asset to the load queue.

paramtypedescriptiondefault
keystringA unique key which will be used to reference this video asset by when using it
srcsstring or ArrayOne or more paths to the video asset. Multiple paths can be passed as fallbacks in case the user's browser doesn't support the one or more of the formats
replacebooleanIf set to true, this video asset will replace an existing video asset with the same keyfalse

Using a single source:

ox.video('stream', './recordings/stardew-valley-1.m4a');

Using fallback sources:

ox.video('stream', ['./recordings/stardew-valley-1.mp4', './recordings/stardew-valley-1.webm']);

text

Adds a text asset to the load queue.

paramtypedescriptiondefault
keystringA unique key which will be used to reference this text asset by when using it
srcstringThe path to the text asset.
replacebooleanIf set to true, this text asset will replace an existing text asset with the same keyfalse
ox.text('bio', './documents/biography.txt');

binary

Adds a binary asset to the load queue. Any file provided to this will be turned into a binary format.

paramtypedescriptiondefault
keystringA unique key which will be used to reference this binary asset by when using it
srcstringThe path to the binary asset.
replacebooleanIf set to true, this binary asset will replace an existing binary asset with the same keyfalse
ox.binary('bio', './documents/biography.txt');

json

Adds a JSON asset to the load queue. The JSON will be stored as a parsed object.

paramtypedescriptiondefault
keystringA unique key which will be used to reference this JSON asset by when using it
srcstringThe path to the JSON asset.
replacebooleanIf set to true, this JSON asset will replace an existing JSON asset with the same keyfalse
ox.json('movies', './documents/favorite-movies.json');

arrayBuffer

Adds an asset to load as an array buffer.

paramtypedescriptiondefault
keystringA unique key which will be used to reference this array buffer by when using it
srcstringThe path to the asset.
replacebooleanIf set to true, this array buffer will replace an existing array buffer with the same keyfalse
ox.arrayBuffer('podcast', './podcasts/podcast-new.mp4');

Step 2: Start Loading

After defining all of the assets that need to be loaded, you have to tell MuskOx that you're ready for it to actually load all of the assets by calling the start method.

start

Starts the loading process and emits the load-complete event when finished.

ox.start();

Step 3: Using Loaded Assets

Since loading assets is an asynchronous action, you must wait until the load-complete event is emitted to ensure that all of the assets are loaded and ready to use.

Here is an example of how to listen to the load-complete event:

const complete = () => {

  // You can start using assets here.
  const star = ox.fetch.image('star');

  document.body.appendChild(star);

};

ox.onComplete.add(complete);

Retrieving assets from the cache is made possible through the fetch methods. By using the method that corresponds to the asset type and the asset key, you can easily retrieve any saved asset.

image

Get a saved image asset from the cache.

paramtypedescriptiondefault
keystringThe key assigned to the image asset when loading it
const star = ox.fetch.image('star');

audio

Get a saved audio asset from the cache.

paramtypedescriptiondefault
keystringThe key assigned to the audio asset when loading it
const podcast = ox.fetch.audio('podcast');

audioBuffer

Get a saved audio buffer from the cache.

paramtypedescriptiondefault
keystringThe key assigned to the audio buffer when loading it
const podcast = ox.fetch.audioBuffer('podcast');

video

Get a saved video asset from the cache.

paramtypedescriptiondefault
keystringThe key assigned to the video asset when loading it
const stream = ox.fetch.video('stream');

text

Get a saved text asset from the cache.

paramtypedescriptiondefault
keystringThe key assigned to the text asset when loading it
const bio = ox.fetch.text('bio');

binary

Get a saved binary asset from the cache.

paramtypedescriptiondefault
keystringThe key assigned to the binary asset when loading it
const bio = ox.fetch.binary('bio');

json

Get a saved JSON asset from the cache.

paramtypedescriptiondefault
keystringThe key assigned to the JSON asset when loading it
const movies = ox.fetch.json('movies');

arrayBuffer

Get a saved arrayBuffer from the cache.

paramtypedescriptiondefault
keystringThe key assigned to the array buffer when loading it
const podcast = ox.fetch.arrayBuffer('podcast');

Properties

There are also a few properties that are available:

assetsLoaded

Gets the number of assets that have been loaded so far.

assetsToLoad

Gets the number of assets that have yet to be loaded.

progress

Gets the current loading progress as a percentage.

License

MIT

5.0.2

4 years ago

5.0.1

4 years ago

5.0.0

4 years ago

4.1.1

4 years ago

4.1.0

4 years ago

4.0.2

4 years ago

4.0.1

4 years ago

4.0.0

5 years ago

3.1.2

5 years ago

3.1.0

5 years ago

3.0.0

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.3.0

6 years ago

0.2.0

6 years ago

0.1.0

6 years ago