- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
import type { ExtensionMetadata } from '@pixi/core';
import { ExtensionType, Texture } from '@pixi/core';
import { LoaderResource } from './LoaderResource';
/**
* Loader plugin for handling Texture resources.
* @memberof PIXI
*/
export class TextureLoader
{
/** @ignore */
static extension: ExtensionMetadata = ExtensionType.Loader;
/** Handle SVG elements a text, render with SVGResource. */
public static add(): void
{
LoaderResource.setExtensionLoadType('svg', LoaderResource.LOAD_TYPE.XHR);
LoaderResource.setExtensionXhrType('svg', LoaderResource.XHR_RESPONSE_TYPE.TEXT);
}
/**
* Called after a resource is loaded.
* @see PIXI.Loader.loaderMiddleware
* @param resource
* @param {Function} next
*/
public static use(resource: LoaderResource, next: (...args: any[]) => void): void
{
// create a new texture if the data is an Image object
if (resource.data && (resource.type === LoaderResource.TYPE.IMAGE || resource.extension === 'svg'))
{
const { data, url, name, metadata } = resource;
Texture.fromLoader(data, url, name, metadata).then((texture) =>
{
resource.texture = texture;
next();
})
// TODO: handle errors in Texture.fromLoader
// so we can pass them to the Loader
.catch(next);
}
else
{
next();
}
}
}