@pixi/compressed-textures
Version:
Loaders for compressed texture file formats
1 lines • 4.1 kB
Source Map (JSON)
{"version":3,"file":"BlobResource.mjs","sources":["../../src/resources/BlobResource.ts"],"sourcesContent":["import { BufferResource, ViewableBuffer } from '@pixi/core';\n\nimport type { BufferType, IBufferResourceOptions } from '@pixi/core';\n\n/**\n * Constructor options for BlobResource.\n * @memberof PIXI\n */\nexport interface IBlobResourceOptions extends IBufferResourceOptions\n{\n autoLoad?: boolean;\n}\n\n/**\n * Resource that fetches texture data over the network and stores it in a buffer.\n * @class\n * @extends PIXI.Resource\n * @memberof PIXI\n */\nexport abstract class BlobResource extends BufferResource\n{\n /** The URL of the texture file. */\n protected origin: string | null;\n\n /** The viewable buffer on the data. */\n protected buffer: ViewableBuffer | null;\n\n protected loaded: boolean;\n\n /**\n * Promise when loading.\n * @default null\n */\n private _load: Promise<this>;\n\n /**\n * @param source - The buffer/URL of the texture file.\n * @param {PIXI.IBlobResourceOptions} [options]\n * @param {boolean} [options.autoLoad=false] - Whether to fetch the data immediately;\n * you can fetch it later via {@link PIXI.BlobResource#load}.\n * @param {number} [options.width=1] - The width in pixels.\n * @param {number} [options.height=1] - The height in pixels.\n * @param {1|2|4|8} [options.unpackAlignment=4] - The alignment of the pixel rows.\n */\n constructor(source: string | BufferType, options: IBlobResourceOptions = { width: 1, height: 1, autoLoad: true })\n {\n let origin: string | null;\n let data: BufferType;\n\n if (typeof source === 'string')\n {\n origin = source;\n data = new Uint8Array();\n }\n else\n {\n origin = null;\n data = source;\n }\n\n super(data, options);\n\n this.origin = origin;\n this.buffer = data ? new ViewableBuffer(data) : null;\n\n this._load = null;\n this.loaded = false;\n\n // Allow autoLoad = \"undefined\" still load the resource by default\n if (this.origin !== null && options.autoLoad !== false)\n {\n this.load();\n }\n if (this.origin === null && this.buffer)\n {\n this._load = Promise.resolve(this);\n this.loaded = true;\n this.onBlobLoaded(this.buffer.rawBinaryData);\n }\n }\n\n protected onBlobLoaded(_data: ArrayBuffer): void\n {\n // TODO: Override this method\n }\n\n /** Loads the blob */\n load(): Promise<this>\n {\n if (this._load)\n {\n return this._load;\n }\n\n this._load = fetch(this.origin)\n .then((response) => response.blob())\n .then((blob) => blob.arrayBuffer())\n .then((arrayBuffer) =>\n {\n this.data = new Uint32Array(arrayBuffer);\n this.buffer = new ViewableBuffer(arrayBuffer);\n this.loaded = true;\n\n this.onBlobLoaded(arrayBuffer);\n this.update();\n\n return this;\n });\n\n return this._load;\n }\n}\n"],"names":[],"mappings":";AAmBO,MAAe,qBAAqB,eAC3C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAwBI,YAAY,QAA6B,UAAgC,EAAE,OAAO,GAAG,QAAQ,GAAG,UAAU,MAC1G;AACI,QAAI,QACA;AAEA,WAAO,UAAW,YAElB,SAAS,QACT,OAAO,IAAI,WAAW,MAItB,SAAS,MACT,OAAO,SAGX,MAAM,MAAM,OAAO,GAEnB,KAAK,SAAS,QACd,KAAK,SAAS,OAAO,IAAI,eAAe,IAAI,IAAI,MAEhD,KAAK,QAAQ,MACb,KAAK,SAAS,IAGV,KAAK,WAAW,QAAQ,QAAQ,aAAa,MAE7C,KAAK,KAAA,GAEL,KAAK,WAAW,QAAQ,KAAK,WAE7B,KAAK,QAAQ,QAAQ,QAAQ,IAAI,GACjC,KAAK,SAAS,IACd,KAAK,aAAa,KAAK,OAAO,aAAa;AAAA,EAEnD;AAAA,EAEU,aAAa,OACvB;AAAA,EAEA;AAAA;AAAA,EAGA,OACA;AACQ,WAAA,KAAK,QAEE,KAAK,SAGhB,KAAK,QAAQ,MAAM,KAAK,MAAM,EACzB,KAAK,CAAC,aAAa,SAAS,MAAM,EAClC,KAAK,CAAC,SAAS,KAAK,YAAY,CAAC,EACjC,KAAK,CAAC,iBAEH,KAAK,OAAO,IAAI,YAAY,WAAW,GACvC,KAAK,SAAS,IAAI,eAAe,WAAW,GAC5C,KAAK,SAAS,IAEd,KAAK,aAAa,WAAW,GAC7B,KAAK,OAAO,GAEL,KACV,GAEE,KAAK;AAAA,EAChB;AACJ;"}