UNPKG

5.05 kBSource Map (JSON)View Raw
1{"version":3,"file":"GenerateTextureSystem.mjs","sources":["../../src/renderTexture/GenerateTextureSystem.ts"],"sourcesContent":["import { extensions, ExtensionType } from '@pixi/extensions';\nimport { Matrix, Transform } from '@pixi/math';\nimport { RenderTexture } from './RenderTexture';\n\nimport type { MSAA_QUALITY, SCALE_MODES } from '@pixi/constants';\nimport type { ExtensionMetadata } from '@pixi/extensions';\nimport type { Rectangle } from '@pixi/math';\nimport type { IRenderableContainer, IRenderableObject, IRenderer } from '../IRenderer';\nimport type { ISystem } from '../system/ISystem';\n\nconst tempTransform = new Transform();\n\n// TODO could this just be part of extract?\nexport interface IGenerateTextureOptions\n{\n /** The scale mode of the texture. Optional, defaults to `PIXI.BaseTexture.defaultOptions.scaleMode`. */\n scaleMode?: SCALE_MODES;\n /** The resolution / device pixel ratio of the texture being generated. Optional defaults to Renderer resolution. */\n resolution?: number;\n /**\n * The region of the displayObject, that shall be rendered,\n * if no region is specified, defaults to the local bounds of the displayObject.\n */\n region?: Rectangle;\n /** The number of samples of the frame buffer. */\n multisample?: MSAA_QUALITY;\n}\n\n/**\n * System that manages the generation of textures from the renderer.\n * @memberof PIXI\n */\nexport class GenerateTextureSystem implements ISystem\n{\n /** @ignore */\n static extension: ExtensionMetadata = {\n type: [\n ExtensionType.RendererSystem,\n ExtensionType.CanvasRendererSystem\n ],\n name: 'textureGenerator',\n };\n\n renderer: IRenderer;\n\n private readonly _tempMatrix: Matrix;\n\n constructor(renderer: IRenderer)\n {\n this.renderer = renderer;\n\n this._tempMatrix = new Matrix();\n }\n\n /**\n * A Useful function that returns a texture of the display object that can then be used to create sprites\n * This can be quite useful if your displayObject is complicated and needs to be reused multiple times.\n * @param displayObject - The displayObject the object will be generated from.\n * @param {IGenerateTextureOptions} options - Generate texture options.\n * @returns a shiny new texture of the display object passed in\n */\n generateTexture(displayObject: IRenderableObject, options?: IGenerateTextureOptions): RenderTexture\n {\n const { region: manualRegion, ...textureOptions } = options || {};\n\n const region = manualRegion || (displayObject as IRenderableContainer).getLocalBounds(null, true);\n\n // minimum texture size is 1x1, 0x0 will throw an error\n if (region.width === 0) region.width = 1;\n if (region.height === 0) region.height = 1;\n\n const renderTexture = RenderTexture.create(\n {\n width: region.width,\n height: region.height,\n ...textureOptions,\n });\n\n this._tempMatrix.tx = -region.x;\n this._tempMatrix.ty = -region.y;\n\n const transform = displayObject.transform;\n\n displayObject.transform = tempTransform;\n\n this.renderer.render(displayObject, {\n renderTexture,\n transform: this._tempMatrix,\n skipUpdateTransform: !!displayObject.parent,\n blit: true,\n });\n\n displayObject.transform = transform;\n\n return renderTexture;\n }\n\n destroy(): void\n {\n // ka boom!\n }\n}\n\nextensions.add(GenerateTextureSystem);\n"],"names":[],"mappings":";;;;AAUA,MAAM,aAAA,GAAgB,IAAI,SAAU,EAAA,CAAA;AAsB7B,MAAM,qBACb,CAAA;AAAA,EAcI,YAAY,QACZ,EAAA;AACI,IAAA,IAAA,CAAK,QAAW,GAAA,QAAA,CAAA;AAEhB,IAAK,IAAA,CAAA,WAAA,GAAc,IAAI,MAAO,EAAA,CAAA;AAAA,GAClC;AAAA,EASA,eAAA,CAAgB,eAAkC,OAClD,EAAA;AACI,IAAA,MAAM,EAAE,MAAA,EAAQ,YAAiB,EAAA,GAAA,cAAA,EAAA,GAAmB,WAAW,EAAC,CAAA;AAEhE,IAAA,MAAM,MAAS,GAAA,YAAA,IAAiB,aAAuC,CAAA,cAAA,CAAe,MAAM,IAAI,CAAA,CAAA;AAGhG,IAAA,IAAI,OAAO,KAAU,KAAA,CAAA;AAAG,MAAA,MAAA,CAAO,KAAQ,GAAA,CAAA,CAAA;AACvC,IAAA,IAAI,OAAO,MAAW,KAAA,CAAA;AAAG,MAAA,MAAA,CAAO,MAAS,GAAA,CAAA,CAAA;AAEzC,IAAM,MAAA,aAAA,GAAgB,cAAc,MAChC,CAAA;AAAA,MACI,OAAO,MAAO,CAAA,KAAA;AAAA,MACd,QAAQ,MAAO,CAAA,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