{"version":3,"file":"ElementImageSource.mjs","sources":["../../src/html-source/ElementImageSource.ts"],"sourcesContent":["import { ExtensionType } from '../extensions/Extensions';\nimport { TextureSource } from '../rendering/renderers/shared/texture/sources/TextureSource';\n\nimport type { ExtensionMetadata } from '../extensions/Extensions';\nimport type { TextureSourceOptions } from '../rendering/renderers/shared/texture/sources/TextureSource';\nimport type { ElementImage } from './HTMLSourceTypes';\n\nfunction isElementImage(resource: unknown): resource is ElementImage\n{\n    // `ElementImage` is an experimental browser global; rely on `instanceof` against the real\n    // constructor when present. Duck-typing on `{ width, height, close }` would also accept\n    // `ImageBitmap`, which has its own dedicated uploader path.\n    const ElementImageCtor = (globalThis as { ElementImage?: new (...args: unknown[]) => ElementImage }).ElementImage;\n\n    return !!ElementImageCtor && resource instanceof ElementImageCtor;\n}\n\n/**\n * @experimental\n * Options for creating an {@link ElementImageSource}.\n * @example\n * ```ts\n * import { ElementImageSource } from 'pixi.js/html-source';\n *\n * // Manual lifetime (default): you call snapshot.close() yourself.\n * const source = new ElementImageSource({ resource: snapshot });\n *\n * // Let the source close the snapshot for you when it is destroyed.\n * const owned = new ElementImageSource({ resource: snapshot, autoClose: true });\n * ```\n * @see {@link ElementImageSource} For the texture source these options configure\n * @extends TextureSourceOptions\n * @category rendering\n * @advanced\n * @noInheritDoc\n */\nexport interface ElementImageSourceOptions extends TextureSourceOptions<ElementImage>\n{\n    /**\n     * Call {@link ElementImage.close} on the snapshot when this source is destroyed. Leave\n     * `false` when the same snapshot is shared with other sources or reused later, otherwise\n     * you risk a use-after-free.\n     * @default false\n     * @example\n     * ```ts\n     * const source = new ElementImageSource({ resource: snapshot, autoClose: true });\n     *\n     * source.destroy(); // snapshot.close() is called for you\n     * ```\n     */\n    autoClose?: boolean;\n}\n\n/**\n * @experimental\n * A texture source backed by an immutable {@link ElementImage} snapshot produced by the\n * experimental `captureElementImage()` API.\n *\n * This is the static counterpart to {@link HTMLSource}: there is no owning canvas, no `paint`\n * listener, and no repaint lifecycle. The snapshot's pixels never change, so the source is\n * ready the moment it is constructed. Most apps render live elements with {@link HTMLSource};\n * reach for `ElementImageSource` when you need a frozen copy that outlives its element or is\n * transferred around.\n *\n * > [!NOTE]\n * > This relies on an experimental browser proposal. An `ElementImage` passed to `Texture.from`\n * > resolves to an `ElementImageSource` only as a last resort (lowest texture-source priority);\n * > construct it explicitly when you need {@link ElementImageSourceOptions}.\n * @example\n * ```ts\n * import { Sprite } from 'pixi.js';\n * import { ElementImageSource } from 'pixi.js/html-source';\n * import type { HTMLSourceCanvas } from 'pixi.js/html-source';\n *\n * const canvas = app.canvas as HTMLSourceCanvas;\n * const snapshot = canvas.captureElementImage!(element);\n *\n * const source = new ElementImageSource({ resource: snapshot, autoClose: true });\n * const sprite = Sprite.from(source);\n *\n * app.stage.addChild(sprite);\n * ```\n * @example\n * ```ts\n * // Slice a frozen snapshot into sub-textures (e.g. a \"shatter\" effect that keeps\n * // shattering even after the original element is gone).\n * import { Rectangle, Texture } from 'pixi.js';\n *\n * const source = new ElementImageSource({ resource: snapshot });\n * const chunk = new Texture({\n *     source,\n *     frame: new Rectangle(0, 0, 64, 64),\n * });\n * ```\n * @see {@link ElementImageSourceOptions} For configuration options\n * @see {@link ElementImage} For the snapshot resource\n * @see {@link HTMLSource} For rendering a live, repainting element\n * @see {@link Sprite} For displaying the source on screen\n * @category rendering\n * @advanced\n */\nexport class ElementImageSource extends TextureSource<ElementImage>\n{\n    /**\n     * Registers the source with the {@link extensions} system at the lowest texture-source\n     * priority, so automatic detection only falls back to it when no other built-in source\n     * claims the resource.\n     */\n    public static extension: ExtensionMetadata = {\n        type: ExtensionType.TextureSource,\n        priority: -10,\n    };\n\n    /**\n     * Tests whether a resource is an {@link ElementImage} snapshot, used during automatic\n     * source detection (`Texture.from`, `TextureSource.from`).\n     * @param resource - The resource to test.\n     * @returns `true` if this source can handle the resource.\n     */\n    public static test(resource: any): resource is ElementImage\n    {\n        return isElementImage(resource);\n    }\n\n    /** The upload method for this texture. */\n    public uploadMethodId = 'html';\n\n    /** Snapshots are immutable, so the source is ready as soon as it is constructed. */\n    public readonly isReady = true;\n\n    private readonly _autoClose: boolean;\n\n    /**\n     * @param options - Options for creating the snapshot source. `resource` is required.\n     * @example\n     * ```ts\n     * const source = new ElementImageSource({\n     *     resource: snapshot, // an ElementImage from captureElementImage()\n     *     autoClose: true,    // close the snapshot when this source is destroyed\n     * });\n     * ```\n     */\n    constructor(options: ElementImageSourceOptions)\n    {\n        if (!options.resource)\n        {\n            throw new Error('[ElementImageSource] resource is required.');\n        }\n\n        super(options);\n\n        this._autoClose = options.autoClose === true;\n    }\n\n    /**\n     * The width of the snapshot in pixels, rounded up.\n     * @example\n     * ```ts\n     * const source = new ElementImageSource({ resource: snapshot });\n     *\n     * console.log(source.resourceWidth, source.resourceHeight);\n     * ```\n     */\n    public get resourceWidth(): number\n    {\n        return Math.ceil(this.resource.width);\n    }\n\n    /**\n     * The height of the snapshot in pixels, rounded up.\n     * @example\n     * ```ts\n     * const source = new ElementImageSource({ resource: snapshot });\n     *\n     * console.log(source.resourceWidth, source.resourceHeight);\n     * ```\n     */\n    public get resourceHeight(): number\n    {\n        return Math.ceil(this.resource.height);\n    }\n\n    /**\n     * Destroys the underlying texture source. When {@link ElementImageSourceOptions.autoClose}\n     * was set, also calls {@link ElementImage.close} on the snapshot.\n     * @example\n     * ```ts\n     * const source = new ElementImageSource({ resource: snapshot });\n     *\n     * source.destroy();\n     * snapshot.close(); // release the snapshot yourself unless autoClose was set\n     * ```\n     */\n    public destroy(): void\n    {\n        const snapshot = this.resource;\n\n        super.destroy();\n\n        if (this._autoClose && snapshot)\n        {\n            snapshot.close();\n        }\n    }\n}\n"],"names":[],"mappings":";;;;AAOA,SAAS,eAAe,QAAA,EACxB;AAII,EAAA,MAAM,mBAAoB,UAAA,CAA2E,YAAA;AAErG,EAAA,OAAO,CAAC,CAAC,gBAAA,IAAoB,QAAA,YAAoB,gBAAA;AACrD;AAsFO,MAAM,2BAA2B,aAAA,CACxC;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