Class: Loader

Tiny.loaders. Loader

继承自 Resource Loader 的 Loader 类。

const loader = new Tiny.loaders.Loader(); // 你也可以通过 `Loader` 类自己实例化一个
// 建议使用 Tiny.js 提供静态方法来获取 Loader 实例化对象
// const loader = Tiny.Loader;

// 声明资源文件
var resources = [
  'https://zos.alipayobjects.com/rmsportal/nJBojwdMJfUqpCWvwyoA.png',
  'https://zos.alipayobjects.com/rmsportal/kkroUtIawGrWrqOLRmjq.jpg',
  'https://zos.alipayobjects.com/rmsportal/jkgwjYSFHRkorsKaZbng.jpeg',
  'https://zos.alipayobjects.com/rmsportal/HAacythTETlcsPxEePbk.webp',
  'https://os.alipayobjects.com/rmsportal/atrIuwPurrBiNEyWNdQA.ogg'
];
//执行加载
loader.run({
  resources: resources,
  // 加载进度
  onProgress: function(per){
      console.log('percent:', per + '%');
  },
  // 单个资源加载完成后的回调
  onComplete: function(resourceLoader, resource){
      console.log(resource.url);
  },
  // 单个资源加载失败后的回调
  onError: function(errorMsg, resourceLoader, resource){
      console.log(errorMsg);
  },
  // 所有资源加载完成后的回调
  onAllComplete: function (resourceLoader, object) {
      console.log('all complete');
  }
});

new Tiny.loaders.Loader(baseUrl, concurrency)

Name Type Default Description
baseUrl string '' optional

The base url for all resources loaded by this loader.

concurrency number 10 optional

The number of resources to load concurrently.

See:

extends

  • module:resource-loader.resourceloader

methods

statictiny.loaders.loader.addtinymiddleware(fn)

Adds a default middleware to the Tiny loader.

Name Type Description
fn function

The middleware to add.

staticTiny.loaders.Loader.addTinyPreMiddleware(fn)

Adds a default middleware pre the Tiny loader.

Name Type Description
fn function

The middleware to add pre.

Version:
  • 1.1.7

add(name, url, options, cb){Tiny.loaders.Loader}

Adds a resource (or multiple resources) to the loader queue.

This function can take a wide variety of different parameters. The only thing that is always
required the url to load. All the following will work:

loader
    // normal param syntax
    .add('key', 'http://...', function () {})
    .add('http://...', function () {})
    .add('http://...')

    // object syntax
    .add({
        name: 'key1'
        url: 'data:image/png;base64,iV...Jggg=='
    }, function () {})
    .add({
        url: 'http://...'
    }, function () {})
    .add({
        name: 'key3',
        url: 'http://...'
        onComplete: function () {}
    })
    .add({
        url: 'https://...',
        onComplete: function () {},
        crossOrigin: true
    })

    // you can pass JSONObject
    .add({
        url: 'key4.json', // the name can be anything, but the extname must be .json
        metadata: {
          JSONObject: {"meta":{"image":"https://...","size":{"w":1730,"h":1158},"scale":"1"},"frames":{}},
          fallback: function () {}
        },
        xhrType: Tiny.loaders.Resource.XHR_RESPONSE_TYPE.JSONOBJECT,
    })

    // you can also pass an array of objects or urls or both
    .add([
        { name: 'key5', url: 'http://...', onComplete: function () {} },
        { url: 'http://...', onComplete: function () {} },
        'http://...',
        'data:image/png;base64,iV...Jggg=='
    ])

    // and you can use both params and options
    .add('key6', 'http://...', { crossOrigin: true }, function () {})
    .add('http://...', { crossOrigin: true }, function () {});
Name Type Description
name string optional

The name of the resource to load, if not passed the url is used.

url string optional

The url for this resource, relative to the baseUrl of this loader.

options object optional

The options for the load.

Name Type Default Description
crossOrigin boolean optional

Is this request cross-origin? Default is to determine automatically.

loadType Tiny.loaders.Resource.LOAD_TYPE XHR optional

How should this resource be loaded?「XHR, IMAGE, AUDIO, VIDEO

xhrType Tiny.loaders.Resource.XHR_RESPONSE_TYPE DEFAULT optional

How should the data being loaded be interpreted when using XH?「BLOB, BUFFER, DEFAULT, DOCUMENT, JSON, JSONOBJECT, TEXT

metadata object optional

Extra configuration for middleware and the Resource object.

Name Type Default Description
loadElement HTMLImageElement | HTMLAudioElement | HTMLVideoElement null optional

The element to use for loading, instead of creating one.

skipSource boolean false optional

Skips adding source(s) to the load element. This is useful if you want to pass in a loadElement that you already added load sources to.

JSONObject object optional

加载的资源如果是一个 JSON 对象,将对象在这里传入,同时传入 url 的格式为:*.jsonxhrType 的格式为:JSONOBJECT

fallback function optional

Spritesheet 解析识别时的兜底回调

cb function optional

Function to call when this specific resource completes loading.

Version:
  • 1.1.7
Returns:
Type Description
Tiny.loaders.Loader Returns itself.

执行加载

Name Type Description
opts.resources array

资源集合

opts.onProgress function

加载进度回调函数

opts.onComplete function

单个资源加载完成回调函数

opts.onError function

单个资源加载失败回调函数

opts.onAllComplete function

所有资源加载完成回调函数

Documentation generated by JSDoc 3.4.3 on Thu May 31 2018 14:40:22 GMT+0800 (CST)