UNPKG

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