UNPKG

pixi.js

Version:

<p align="center"> <a href="https://pixijs.com" target="_blank" rel="noopener noreferrer"> <img height="150" src="https://files.pixijs.download/branding/pixijs-logo-transparent-dark.svg?v=1" alt="PixiJS logo"> </a> </p> <br/> <p align="center">

1 lines 6.71 kB
{"version":3,"file":"BackgroundSystem.mjs","sources":["../../../../../src/rendering/renderers/shared/background/BackgroundSystem.ts"],"sourcesContent":["import { Color } from '../../../../color/Color';\nimport { ExtensionType } from '../../../../extensions/Extensions';\nimport { warn } from '../../../../utils/logging/warn';\n\nimport type { ColorSource, RgbaArray } from '../../../../color/Color';\nimport type { System } from '../system/System';\n/**\n * Options for the background system.\n * @category rendering\n * @advanced\n */\nexport interface BackgroundSystemOptions\n{\n /**\n * The background color used to clear the canvas. See {@link ColorSource} for accepted color values.\n * @default 'black'\n */\n backgroundColor: ColorSource;\n /** Alias for `backgroundColor` */\n background?: ColorSource\n /**\n * Transparency of the background color, value from `0` (fully transparent) to `1` (fully opaque).\n * This value determines whether the canvas is initialized with alpha transparency support.\n * Note: This cannot be changed after initialization. If set to `1`, the canvas will remain opaque,\n * even if a transparent background color is set later.\n * @default 1\n */\n backgroundAlpha?: number;\n /**\n * Whether to clear the canvas before new render passes.\n * @default true\n */\n clearBeforeRender?: boolean;\n}\n\n/**\n * The background system manages the background color and alpha of the main view.\n * @category rendering\n * @advanced\n */\nexport class BackgroundSystem implements System<BackgroundSystemOptions>\n{\n /** @ignore */\n public static extension = {\n type: [\n ExtensionType.WebGLSystem,\n ExtensionType.WebGPUSystem,\n ExtensionType.CanvasSystem,\n ],\n name: 'background',\n priority: 0,\n } as const;\n\n /** default options used by the system */\n public static defaultOptions: BackgroundSystemOptions = {\n /**\n * {@link WebGLOptions.backgroundAlpha}\n * @default 1\n */\n backgroundAlpha: 1,\n /**\n * {@link WebGLOptions.backgroundColor}\n * @default 0x000000\n */\n backgroundColor: 0x0,\n /**\n * {@link WebGLOptions.clearBeforeRender}\n * @default true\n */\n clearBeforeRender: true,\n };\n\n /**\n * This sets if the CanvasRenderer will clear the canvas or not before the new render pass.\n * If the scene is NOT transparent PixiJS will use a canvas sized fillRect operation every\n * frame to set the canvas background color. If the scene is transparent PixiJS will use clearRect\n * to clear the canvas every frame. Disable this by setting this to false. For example, if\n * your game has a canvas filling background image you often don't need this set.\n */\n public clearBeforeRender: boolean;\n\n private readonly _backgroundColor: Color;\n\n constructor()\n {\n this.clearBeforeRender = true;\n\n this._backgroundColor = new Color(0x000000);\n\n this.color = this._backgroundColor; // run bg color setter\n this.alpha = 1;\n }\n\n /**\n * initiates the background system\n * @param options - the options for the background colors\n */\n public init(options: BackgroundSystemOptions): void\n {\n options = { ...BackgroundSystem.defaultOptions, ...options };\n\n this.clearBeforeRender = options.clearBeforeRender;\n this.color = options.background || options.backgroundColor || this._backgroundColor; // run bg color setter\n this.alpha = options.backgroundAlpha;\n\n this._backgroundColor.setAlpha(options.backgroundAlpha);\n }\n\n /** The background color to fill if not transparent */\n get color(): Color\n {\n return this._backgroundColor;\n }\n\n set color(value: ColorSource)\n {\n // #if _DEBUG\n\n const incoming = Color.shared.setValue(value);\n\n if (incoming.alpha < 1 && this._backgroundColor.alpha === 1)\n {\n warn(\n 'Cannot set a transparent background on an opaque canvas. '\n + 'To enable transparency, set backgroundAlpha < 1 when initializing your Application.'\n );\n }\n // #endif\n this._backgroundColor.setValue(value);\n }\n\n /** The background color alpha. Setting this to 0 will make the canvas transparent. */\n get alpha(): number\n {\n return this._backgroundColor.alpha;\n }\n\n set alpha(value: number)\n {\n this._backgroundColor.setAlpha(value);\n }\n\n /** The background color as an [R, G, B, A] array. */\n get colorRgba(): RgbaArray\n {\n return this._backgroundColor.toArray() as RgbaArray;\n }\n\n /**\n * destroys the background system\n * @internal\n */\n public destroy(): void\n {\n // No cleanup required\n }\n}\n"],"names":[],"mappings":";;;;;AAwCO,MAAM,iBAAA,GAAN,MAAM,iBAAA,CACb;AAAA,EA0CI,WAAA,GACA;AACI,IAAA,IAAA,CAAK,iBAAA,GA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