- 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
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
import type { Resource } from '@pixi/core';
import { ViewableBuffer, BufferResource } from '@pixi/core';
interface IBlobOptions
{
autoLoad?: boolean;
width: number;
height: number;
}
/**
* Resource that fetches texture data over the network and stores it in a buffer.
* @class
* @extends PIXI.Resource
* @memberof PIXI
*/
export abstract class BlobResource extends BufferResource
{
protected origin: string;
protected buffer: ViewableBuffer;
protected loaded: boolean;
/**
* @param {string} source - the URL of the texture file
* @param {PIXI.IBlobOptions} options
* @param {boolean}[options.autoLoad] - whether to fetch the data immediately;
* you can fetch it later via {@link BlobResource#load}
* @param {boolean}[options.width] - the width in pixels.
* @param {boolean}[options.height] - the height in pixels.
*/
constructor(source: string | Uint8Array | Uint32Array | Float32Array,
options: IBlobOptions = { width: 1, height: 1, autoLoad: true })
{
let origin: string;
let data: Uint8Array | Uint32Array | Float32Array;
if (typeof source === 'string')
{
origin = source;
data = new Uint8Array();
}
else
{
origin = null;
data = source;
}
super(data, options);
/**
* The URL of the texture file
* @member {string}
*/
this.origin = origin;
/**
* The viewable buffer on the data
* @member {ViewableBuffer}
*/
// HINT: BlobResource allows "null" sources, assuming the child class provides an alternative
this.buffer = data ? new ViewableBuffer(data) : null;
// Allow autoLoad = "undefined" still load the resource by default
if (this.origin && options.autoLoad !== false)
{
this.load();
}
if (data && data.length)
{
this.loaded = true;
this.onBlobLoaded(this.buffer.rawBinaryData);
}
}
protected onBlobLoaded(_data: ArrayBuffer): void
{
// TODO: Override this method
}
/** Loads the blob */
async load(): Promise<Resource>
{
const response = await fetch(this.origin);
const blob = await response.blob();
const arrayBuffer = await blob.arrayBuffer();
this.data = new Uint32Array(arrayBuffer);
this.buffer = new ViewableBuffer(arrayBuffer);
this.loaded = true;
this.onBlobLoaded(arrayBuffer);
this.update();
return this;
}
}