# Assets

A promise based asset loader for loading images, audio, and data files.

    let { load } = kontra;

    load(
      'assets/imgs/character.png',
      'assets/data/tile_engine_basic.json',
      ['/audio/music.ogg', '/audio/music.mp3']
    ).then(function(assets) {
      // all assets have loaded
    }).catch(function(err) {
      // error loading an asset
    });

    import { load } from 'path/to/kontra.mjs';

    load(
      'assets/imgs/character.png',
      'assets/data/tile_engine_basic.json',
      ['/audio/music.ogg', '/audio/music.mp3']
    ).then(function(assets) {
      // all assets have loaded
    }).catch(function(err) {
      // error loading an asset
    });

    import { load } from 'kontra';

    load(
      'assets/imgs/character.png',
      'assets/data/tile_engine_basic.json',
      ['/audio/music.ogg', '/audio/music.mp3']
    ).then(function(assets) {
      // all assets have loaded
    }).catch(function(err) {
      // error loading an asset
    });

## audioAssets #

Object of all loaded audio assets by both file name and path. If the base audio path was set before the audio was loaded, the file name and path will not include the base audio path.

    let { load, setAudioPath, audioAssets } = kontra;

    load('/audio/music.ogg').then(function() {
      // Audio asset can be accessed by both
      // name: audioAssets['/audio/music']
      // path: audioAssets['/audio/music.ogg']
    });

    setAudioPath('/audio');
    load('sound.ogg').then(function() {
      // Audio asset can be accessed by both
      // name: audioAssets['sound']
      // path: audioAssets['sound.ogg']
    });

    import { load, setAudioPath, audioAssets } from 'path/to/kontra.mjs';

    load('/audio/music.ogg').then(function() {
      // Audio asset can be accessed by both
      // name: audioAssets['/audio/music']
      // path: audioAssets['/audio/music.ogg']
    });

    setAudioPath('/audio');
    load('sound.ogg').then(function() {
      // Audio asset can be accessed by both
      // name: audioAssets['sound']
      // path: audioAssets['sound.ogg']
    });

    import { load, setAudioPath, audioAssets } from 'kontra';

    load('/audio/music.ogg').then(function() {
      // Audio asset can be accessed by both
      // name: audioAssets['/audio/music']
      // path: audioAssets['/audio/music.ogg']
    });

    setAudioPath('/audio');
    load('sound.ogg').then(function() {
      // Audio asset can be accessed by both
      // name: audioAssets['sound']
      // path: audioAssets['sound.ogg']
    });

## dataAssets #

Object of all loaded data assets by both file name and path. If the base data path was set before the data was loaded, the file name and path will not include the base data path.

    let { load, setDataPath, dataAssets } = kontra;

    load('assets/data/file.txt').then(function() {
      // Audio asset can be accessed by both
      // name: dataAssets['assets/data/file']
      // path: dataAssets['assets/data/file.txt']
    });

    setDataPath('assets/data');
    load('info.json').then(function() {
      // Audio asset can be accessed by both
      // name: dataAssets['info']
      // path: dataAssets['info.json']
    });

    import { load, setDataPath, dataAssets } from 'path/to/kontra.mjs';

    load('assets/data/file.txt').then(function() {
      // Audio asset can be accessed by both
      // name: dataAssets['assets/data/file']
      // path: dataAssets['assets/data/file.txt']
    });

    setDataPath('assets/data');
    load('info.json').then(function() {
      // Audio asset can be accessed by both
      // name: dataAssets['info']
      // path: dataAssets['info.json']
    });

    import { load, setDataPath, dataAssets } from 'kontra';

    load('assets/data/file.txt').then(function() {
      // Audio asset can be accessed by both
      // name: dataAssets['assets/data/file']
      // path: dataAssets['assets/data/file.txt']
    });

    setDataPath('assets/data');
    load('info.json').then(function() {
      // Audio asset can be accessed by both
      // name: dataAssets['info']
      // path: dataAssets['info.json']
    });

## imageAssets #

Object of all loaded image assets by both file name and path. If the base image path was set before the image was loaded, the file name and path will not include the base image path.

    let { load, setImagePath, imageAssets } = kontra;

    load('assets/imgs/character.png').then(function() {
      // Image asset can be accessed by both
      // name: imageAssets['assets/imgs/character']
      // path: imageAssets['assets/imgs/character.png']
    });

    setImagePath('assets/imgs');
    load('character_walk_sheet.png').then(function() {
      // Image asset can be accessed by both
      // name: imageAssets['character_walk_sheet']
      // path: imageAssets['character_walk_sheet.png']
    });

    import { load, setImagePath, imageAssets } from 'path/to/kontra.mjs';

    load('assets/imgs/character.png').then(function() {
      // Image asset can be accessed by both
      // name: imageAssets['assets/imgs/character']
      // path: imageAssets['assets/imgs/character.png']
    });

    setImagePath('assets/imgs');
    load('character_walk_sheet.png').then(function() {
      // Image asset can be accessed by both
      // name: imageAssets['character_walk_sheet']
      // path: imageAssets['character_walk_sheet.png']
    });

    import { load, setImagePath, imageAssets } from 'kontra';

    load('assets/imgs/character.png').then(function() {
      // Image asset can be accessed by both
      // name: imageAssets['assets/imgs/character']
      // path: imageAssets['assets/imgs/character.png']
    });

    setImagePath('assets/imgs');
    load('character_walk_sheet.png').then(function() {
      // Image asset can be accessed by both
      // name: imageAssets['character_walk_sheet']
      // path: imageAssets['character_walk_sheet.png']
    });

## load(​urls) #

Load Image, Audio, or data files. Uses the loadImage, loadAudio, and loadData functions to load each asset type.

    let { load } = kontra;

    load(
      'assets/imgs/character.png',
      'assets/data/tile_engine_basic.json',
      ['/audio/music.ogg', '/audio/music.mp3']
    ).then(function(assets) {
      // all assets have loaded
    }).catch(function(err) {
      // error loading an asset
    });

    import { load } from 'path/to/kontra.mjs';

    load(
      'assets/imgs/character.png',
      'assets/data/tile_engine_basic.json',
      ['/audio/music.ogg', '/audio/music.mp3']
    ).then(function(assets) {
      // all assets have loaded
    }).catch(function(err) {
      // error loading an asset
    });

    import { load } from 'kontra';

    load(
      'assets/imgs/character.png',
      'assets/data/tile_engine_basic.json',
      ['/audio/music.ogg', '/audio/music.mp3']
    ).then(function(assets) {
      // all assets have loaded
    }).catch(function(err) {
      // error loading an asset
    });

### load Parameters

`urls`

  String or an Array of Strings. Comma separated list of asset urls to load.

### load Return value

A deferred promise. Resolves with all the loaded assets.

## loadAudio(​url) #

Load a single Audio asset. Supports loading multiple audio formats which the loader will use to load the first audio format supported by the browser in the order listed. Uses the base audio path to resolve the URL.

Once loaded, the asset will be accessible on the the audioAssets property. Since the loader determines which audio asset to load based on browser support, you should only reference the audio by its name and not by its file path since there's no guarantee which asset was loaded.

    let { loadAudio, audioAssets } = kontra;

    loadAudio([
      '/audio/music.mp3',
      '/audio/music.ogg'
    ]).then(function(audio) {

      // access audio by its name only (not by its .mp3 or .ogg path)
      audioAssets['/audio/music'].play();
    })

    import { loadAudio, audioAssets } from 'path/to/kontra.mjs';

    loadAudio([
      '/audio/music.mp3',
      '/audio/music.ogg'
    ]).then(function(audio) {

      // access audio by its name only (not by its .mp3 or .ogg path)
      audioAssets['/audio/music'].play();
    })

    import { loadAudio, audioAssets } from 'kontra';

    loadAudio([
      '/audio/music.mp3',
      '/audio/music.ogg'
    ]).then(function(audio) {

      // access audio by its name only (not by its .mp3 or .ogg path)
      audioAssets['/audio/music'].play();
    })

### loadAudio Parameters

`url`

  String. The URL to the Audio file.

### loadAudio Return value

A deferred promise. Promise resolves with the Audio.

## loadData(​url) #

Load a single Data asset. Uses the base data path to resolve the URL.

Once loaded, the asset will be accessible on the the dataAssets property.

    let { loadData } = kontra;

    loadData('assets/data/tile_engine_basic.json').then(function(data) {
      // data contains the parsed JSON data
    })

    import { loadData } from 'path/to/kontra.mjs';

    loadData('assets/data/tile_engine_basic.json').then(function(data) {
      // data contains the parsed JSON data
    })

    import { loadData } from 'kontra';

    loadData('assets/data/tile_engine_basic.json').then(function(data) {
      // data contains the parsed JSON data
    })

### loadData Parameters

`url`

  String. The URL to the Data file.

### loadData Return value

A deferred promise. Promise resolves with the contents of the file. If the file is a JSON file, the contents will be parsed as JSON.

## loadImage(​url) #

Load a single Image asset. Uses the base image path to resolve the URL.

Once loaded, the asset will be accessible on the the imageAssets property.

    let { loadImage } = kontra;

    loadImage('car.png').then(function(image) {
      console.log(image.src);  //=> 'car.png'
    })

    import { loadImage } from 'path/to/kontra.mjs';

    loadImage('car.png').then(function(image) {
      console.log(image.src);  //=> 'car.png'
    })

    import { loadImage } from 'kontra';

    loadImage('car.png').then(function(image) {
      console.log(image.src);  //=> 'car.png'
    })

### loadImage Parameters

`url`

  String. The URL to the Image file.

### loadImage Return value

A deferred promise. Promise resolves with the Image.

## setAudioPath(​path) #

Sets the base path for all audio assets. If a base path is set, all load calls for audio assets will prepend the base path to the URL.

    let { setAudioPath, load } = kontra;

    setAudioPath('/audio');
    load('music.ogg');  // loads '/audio/music.ogg'

    import { setAudioPath, load } from 'path/to/kontra.mjs';

    setAudioPath('/audio');
    load('music.ogg');  // loads '/audio/music.ogg'

    import { setAudioPath, load } from 'kontra';

    setAudioPath('/audio');
    load('music.ogg');  // loads '/audio/music.ogg'

### setAudioPath Parameters

`path`

  String. Base audio path.

## setDataPath(​path) #

Sets the base path for all data assets. If a base path is set, all load calls for data assets will prepend the base path to the URL.

    let { setDataPath, load } = kontra;

    setDataPath('/data');
    load('file.json');  // loads '/data/file.json'

    import { setDataPath, load } from 'path/to/kontra.mjs';

    setDataPath('/data');
    load('file.json');  // loads '/data/file.json'

    import { setDataPath, load } from 'kontra';

    setDataPath('/data');
    load('file.json');  // loads '/data/file.json'

### setDataPath Parameters

`path`

  String. Base data path.

## setImagePath(​path) #

Sets the base path for all image assets. If a base path is set, all load calls for image assets will prepend the base path to the URL.

    let { setImagePath, load } = kontra;

    setImagePath('/imgs');
    load('character.png');  // loads '/imgs/character.png'

    import { setImagePath, load } from 'path/to/kontra.mjs';

    setImagePath('/imgs');
    load('character.png');  // loads '/imgs/character.png'

    import { setImagePath, load } from 'kontra';

    setImagePath('/imgs');
    load('character.png');  // loads '/imgs/character.png'

### setImagePath Parameters

`path`

  String. Base image path.