1 | {"version":3,"file":"ViewSystem.mjs","sources":["../../src/view/ViewSystem.ts"],"sourcesContent":["import { extensions, ExtensionType } from '@pixi/extensions';\nimport { Rectangle } from '@pixi/math';\nimport { settings } from '@pixi/settings';\n\nimport type { ExtensionMetadata } from '@pixi/extensions';\nimport type { ICanvas } from '@pixi/settings';\nimport type { IRenderer } from '../IRenderer';\nimport type { ISystem } from '../system/ISystem';\n\n/**\n * Options for the view system.\n * @memberof PIXI\n */\nexport interface ViewSystemOptions\n{\n /**\n * The canvas to use as the view. If omitted, a new canvas will be created.\n * @memberof PIXI.IRendererOptions\n */\n view?: ICanvas;\n /**\n * The width of the renderer's view.\n * @memberof PIXI.IRendererOptions\n */\n width?: number;\n /**\n * The height of the renderer's view.\n * @memberof PIXI.IRendererOptions\n */\n height?: number;\n /**\n * The resolution / device pixel ratio of the renderer.\n * @memberof PIXI.IRendererOptions\n */\n resolution?: number;\n /**\n * Whether the CSS dimensions of the renderer's view should be resized automatically.\n * @memberof PIXI.IRendererOptions\n */\n autoDensity?: boolean;\n}\n\n/**\n * The view system manages the main canvas that is attached to the DOM.\n * This main role is to deal with how the holding the view reference and dealing with how it is resized.\n * @memberof PIXI\n */\nexport class ViewSystem implements ISystem<ViewSystemOptions, boolean>\n{\n /** @ignore */\n static defaultOptions: ViewSystemOptions = {\n /**\n * {@link PIXI.IRendererOptions.width}\n * @default 800\n * @memberof PIXI.settings.RENDER_OPTIONS\n */\n width: 800,\n /**\n * {@link PIXI.IRendererOptions.height}\n * @default 600\n * @memberof PIXI.settings.RENDER_OPTIONS\n */\n height: 600,\n /**\n * {@link PIXI.IRendererOptions.resolution}\n * @type {number}\n * @default PIXI.settings.RESOLUTION\n * @memberof PIXI.settings.RENDER_OPTIONS\n */\n resolution: undefined,\n /**\n * {@link PIXI.IRendererOptions.autoDensity}\n * @default false\n * @memberof PIXI.settings.RENDER_OPTIONS\n */\n autoDensity: false,\n };\n\n /** @ignore */\n static extension: ExtensionMetadata = {\n type: [\n ExtensionType.RendererSystem,\n ExtensionType.CanvasRendererSystem\n ],\n name: '_view',\n };\n\n private renderer: IRenderer;\n\n /**\n * The resolution / device pixel ratio of the renderer.\n * @member {number}\n * @default PIXI.settings.RESOLUTION\n */\n public resolution: number;\n\n /**\n * Measurements of the screen. (0, 0, screenWidth, screenHeight).\n *\n * Its safe to use as filterArea or hitArea for the whole stage.\n * @member {PIXI.Rectangle}\n */\n public screen: Rectangle;\n\n /**\n * The canvas element that everything is drawn to.\n * @member {PIXI.ICanvas}\n */\n public element: ICanvas;\n\n /**\n * Whether CSS dimensions of canvas view should be resized to screen dimensions automatically.\n * @member {boolean}\n */\n public autoDensity: boolean;\n\n constructor(renderer: IRenderer)\n {\n this.renderer = renderer;\n }\n\n /**\n * initiates the view system\n * @param {PIXI.ViewOptions} options - the options for the view\n */\n init(options: ViewSystemOptions): void\n {\n this.screen = new Rectangle(0, 0, options.width, options.height);\n\n this.element = options.view || settings.ADAPTER.createCanvas() as ICanvas;\n\n this.resolution = options.resolution || settings.RESOLUTION;\n\n this.autoDensity = !!options.autoDensity;\n }\n\n /**\n * Resizes the screen and canvas to the specified dimensions.\n * @param desiredScreenWidth - The new width of the screen.\n * @param desiredScreenHeight - The new height of the screen.\n */\n resizeView(desiredScreenWidth: number, desiredScreenHeight: number): void\n {\n this.element.width = Math.round(desiredScreenWidth * this.resolution);\n this.element.height = Math.round(desiredScreenHeight * this.resolution);\n\n const screenWidth = this.element.width / this.resolution;\n const screenHeight = this.element.height / this.resolution;\n\n this.screen.width = screenWidth;\n this.screen.height = screenHeight;\n\n if (this.autoDensity)\n {\n this.element.style.width = `${screenWidth}px`;\n this.element.style.height = `${screenHeight}px`;\n }\n\n /**\n * Fired after view has been resized.\n * @event PIXI.Renderer#resize\n * @param {number} screenWidth - The new width of the screen.\n * @param {number} screenHeight - The new height of the screen.\n */\n this.renderer.emit('resize', screenWidth, screenHeight);\n this.renderer.runners.resize.emit(this.screen.width, this.screen.height);\n }\n\n /**\n * Destroys this System and optionally removes the canvas from the dom.\n * @param {boolean} [removeView=false] - Whether to remove the canvas from the DOM.\n */\n destroy(removeView: boolean): void\n {\n // ka boom!\n if (removeView)\n {\n this.element.parentNode?.removeChild(this.element);\n }\n\n this.renderer = null;\n this.element = null;\n this.screen = null;\n }\n}\n\nextensions.add(ViewSystem);\n"],"names":[],"mappings":";;;AA+CO,MAAM,WACb;AAAA,EAoEI,YAAY,UACZ;AACI,SAAK,WAAW;AAAA,EACpB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,KAAK,SACL;AACI,SAAK,SAAS,IAAI,UAAU,GAAG,GAAG,QAAQ,OAAO,QAAQ,MAAM,GAE/D,KAAK,UAAU,QAAQ,QAAQ,SAAS,QAAQ,aAAa,GAE7D,KAAK,aAAa,QAAQ,cAAc,SAAS,YAEjD,KAAK,cAAc,CAAC,CAAC,QAAQ;AAAA,EACjC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,WAAW,oBAA4B,qBACvC;AACI,SAAK,QAAQ,QAAQ,KAAK,MAAM,qBAAqB,KAAK,UAAU,GACpE,KAAK,QAAQ,SAAS,KAAK,MAAM,sBAAsB,KAAK,UAAU;AAEhE,UAAA,cAAc,KAAK,QAAQ,QAAQ,KAAK,YACxC,eAAe,KAAK,QAAQ,SAAS,KAAK;AAEhD,SAAK,OAAO,QAAQ,aACpB,KAAK,OAAO,SAAS,cAEjB,KAAK,gBAEL,KAAK,QAAQ,MAAM,QAAQ,GAAG,WAAW,MACzC,KAAK,QAAQ,MAAM,SAAS,GAAG,YAAY,OAS/C,KAAK,SAAS,KAAK,UAAU,aAAa,YAAY,GACtD,KAAK,SAAS,QAAQ,OAAO,KAAK,KAAK,OAAO,OAAO,KAAK,OAAO,MAAM;AAAA,EAC3E;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,QAAQ,YACR;AAEQ,kBAEA,KAAK,QAAQ,YAAY,YAAY,KAAK,OAAO,GAGrD,KAAK,WAAW,MAChB,KAAK,UAAU,MACf,KAAK,SAAS;AAAA,EAClB;AACJ;AAzIa,WAGF,iBAAoC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAMvC,OAAO;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAMP,QAAQ;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOR,YAAY;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAMZ,aAAa;AACjB;AA7BS,WAgCF,YAA+B;AAAA,EAClC,MAAM;AAAA,IACF,cAAc;AAAA,IACd,cAAc;AAAA,EAClB;AAAA,EACA,MAAM;AACV;AAqGJ,WAAW,IAAI,UAAU;"} |