Files

1 line
15 KiB
Plaintext
Raw Permalink Normal View History

2025-01-04 00:34:03 +01:00
{"version":3,"file":"Extract.mjs","sources":["../src/Extract.ts"],"sourcesContent":["import { extensions, ExtensionType, FORMATS, Rectangle, RenderTexture, utils } from '@pixi/core';\n\nimport type { ExtensionMetadata, ICanvas, ISystem, Renderer } from '@pixi/core';\nimport type { DisplayObject } from '@pixi/display';\n\nconst TEMP_RECT = new Rectangle();\nconst BYTES_PER_PIXEL = 4;\n\nexport interface IExtract\n{\n image(target?: DisplayObject | RenderTexture, format?: string, quality?: number,\n frame?: Rectangle): Promise<HTMLImageElement>;\n base64(target?: DisplayObject | RenderTexture, format?: string, quality?: number,\n frame?: Rectangle): Promise<string>;\n canvas(target?: DisplayObject | RenderTexture, frame?: Rectangle): ICanvas;\n pixels(target?: DisplayObject | RenderTexture, frame?: Rectangle): Uint8Array | Uint8ClampedArray;\n}\n\n/**\n * This class provides renderer-specific plugins for exporting content from a renderer.\n * For instance, these plugins can be used for saving an Image, Canvas element or for exporting the raw image data (pixels).\n *\n * Do not instantiate these plugins directly. It is available from the `renderer.extract` property.\n * @example\n * import { Application, Graphics } from 'pixi.js';\n *\n * // Create a new application (extract will be auto-added to renderer)\n * const app = new Application();\n *\n * // Draw a red circle\n * const graphics = new Graphics()\n * .beginFill(0xFF0000)\n * .drawCircle(0, 0, 50);\n *\n * // Render the graphics as an HTMLImageElement\n * const image = await app.renderer.extract.image(graphics);\n * document.body.appendChild(image);\n * @memberof PIXI\n */\n\nexport class Extract implements ISystem, IExtract\n{\n /** @ignore */\n static extension: ExtensionMetadata = {\n name: 'extract',\n type: ExtensionType.RendererSystem,\n };\n\n private renderer: Renderer | null;\n\n /** Does the renderer have alpha and are its color channels stored premultipled by the alpha channel? */\n private _rendererPremultipliedAlpha: boolean;\n\n /**\n * @param renderer - A reference to the current renderer\n */\n constructor(renderer: Renderer)\n {\n this.renderer = renderer;\n this._rendererPremultipliedAlpha = false;\n }\n\n protected contextChange(): void\n {\n const attributes = this.renderer?.gl.getContextAttributes();\n\n this._rendererPremultipliedAlpha = !!(attributes && attributes.alpha && attributes.premultipliedAlpha);\n }\n\n /**\n * Will return a HTML Image of the target\n * @param target - A displayObject or renderTexture\n * to convert. If left empty will use the main renderer\n * @param format - Image format, e.g. \"image/jpeg\" or \"image/webp\".\n * @param quality - JPEG or Webp compression from 0 to 1. Default is 0.92.\n * @param frame - The frame the extraction is restricted to.\n * @returns - HTML Image of the target\n */\n public async image(target?: DisplayObject | RenderTexture, format?: string, quality?: number,\n frame?: Rectangle): Promise<HTMLImageElement>\n {\n const image = new Image();\n\n image.src = await this.base64(target, format, quality, frame);\n\n return image;\n }\n\n /**\n * Will return a base64 encoded string of this target. It works by calling\n * `Extract.canvas` and then running toDataURL on that.\n * @param target - A displayObject or renderTexture\n * to convert. If left empty will use the main renderer\n * @param format - Image format, e.g. \"image/jpeg\" or \"image/webp\".\n * @param quality - JPEG or Webp compression from 0 to 1. Default is 0.92.\n * @param frame - The frame the extraction is restricted to.\n * @returns - A base64 encoded string of the texture.\n */\n public async base64(target?: DisplayObject | RenderTexture, format?: string, quality?: number,\n frame?: Rectangle): Promise<string>\n {\n const canvas = this.canvas(target, frame);\n\n if (canvas