UNPKG

2.63 kBJavaScriptView Raw
1import { ExtensionType, extensions } from "@pixi/extensions";
2import { Rectangle } from "@pixi/math";
3import { settings } from "@pixi/settings";
4class ViewSystem {
5 constructor(renderer) {
6 this.renderer = renderer;
7 }
8 /**
9 * initiates the view system
10 * @param {PIXI.ViewOptions} options - the options for the view
11 */
12 init(options) {
13 this.screen = new Rectangle(0, 0, options.width, options.height), this.element = options.view || settings.ADAPTER.createCanvas(), this.resolution = options.resolution || settings.RESOLUTION, this.autoDensity = !!options.autoDensity;
14 }
15 /**
16 * Resizes the screen and canvas to the specified dimensions.
17 * @param desiredScreenWidth - The new width of the screen.
18 * @param desiredScreenHeight - The new height of the screen.
19 */
20 resizeView(desiredScreenWidth, desiredScreenHeight) {
21 this.element.width = Math.round(desiredScreenWidth * this.resolution), this.element.height = Math.round(desiredScreenHeight * this.resolution);
22 const screenWidth = this.element.width / this.resolution, screenHeight = this.element.height / this.resolution;
23 this.screen.width = screenWidth, this.screen.height = screenHeight, this.autoDensity && (this.element.style.width = `${screenWidth}px`, this.element.style.height = `${screenHeight}px`), this.renderer.emit("resize", screenWidth, screenHeight), this.renderer.runners.resize.emit(this.screen.width, this.screen.height);
24 }
25 /**
26 * Destroys this System and optionally removes the canvas from the dom.
27 * @param {boolean} [removeView=false] - Whether to remove the canvas from the DOM.
28 */
29 destroy(removeView) {
30 removeView && this.element.parentNode?.removeChild(this.element), this.renderer = null, this.element = null, this.screen = null;
31 }
32}
33ViewSystem.defaultOptions = {
34 /**
35 * {@link PIXI.IRendererOptions.width}
36 * @default 800
37 * @memberof PIXI.settings.RENDER_OPTIONS
38 */
39 width: 800,
40 /**
41 * {@link PIXI.IRendererOptions.height}
42 * @default 600
43 * @memberof PIXI.settings.RENDER_OPTIONS
44 */
45 height: 600,
46 /**
47 * {@link PIXI.IRendererOptions.resolution}
48 * @type {number}
49 * @default PIXI.settings.RESOLUTION
50 * @memberof PIXI.settings.RENDER_OPTIONS
51 */
52 resolution: void 0,
53 /**
54 * {@link PIXI.IRendererOptions.autoDensity}
55 * @default false
56 * @memberof PIXI.settings.RENDER_OPTIONS
57 */
58 autoDensity: !1
59}, /** @ignore */
60ViewSystem.extension = {
61 type: [
62 ExtensionType.RendererSystem,
63 ExtensionType.CanvasRendererSystem
64 ],
65 name: "_view"
66};
67extensions.add(ViewSystem);
68export {
69 ViewSystem
70};
71//# sourceMappingURL=ViewSystem.mjs.map