1 | import { ExtensionType, extensions } from '@pixi/extensions';
|
2 | import { Rectangle } from '@pixi/math';
|
3 | import { settings } from '@pixi/settings';
|
4 |
|
5 | class ViewSystem {
|
6 | constructor(renderer) {
|
7 | this.renderer = renderer;
|
8 | }
|
9 | init(options) {
|
10 | this.screen = new Rectangle(0, 0, options.width, options.height);
|
11 | this.element = options.view || settings.ADAPTER.createCanvas();
|
12 | this.resolution = options.resolution || settings.RESOLUTION;
|
13 | this.autoDensity = !!options.autoDensity;
|
14 | }
|
15 | resizeView(desiredScreenWidth, desiredScreenHeight) {
|
16 | this.element.width = Math.round(desiredScreenWidth * this.resolution);
|
17 | this.element.height = Math.round(desiredScreenHeight * this.resolution);
|
18 | const screenWidth = this.element.width / this.resolution;
|
19 | const screenHeight = this.element.height / this.resolution;
|
20 | this.screen.width = screenWidth;
|
21 | this.screen.height = screenHeight;
|
22 | if (this.autoDensity) {
|
23 | this.element.style.width = `${screenWidth}px`;
|
24 | this.element.style.height = `${screenHeight}px`;
|
25 | }
|
26 | this.renderer.emit("resize", screenWidth, screenHeight);
|
27 | this.renderer.runners.resize.emit(this.screen.width, this.screen.height);
|
28 | }
|
29 | destroy(removeView) {
|
30 | if (removeView) {
|
31 | this.element.parentNode?.removeChild(this.element);
|
32 | }
|
33 | this.renderer = null;
|
34 | this.element = null;
|
35 | this.screen = null;
|
36 | }
|
37 | }
|
38 | ViewSystem.defaultOptions = {
|
39 | width: 800,
|
40 | height: 600,
|
41 | resolution: settings.RESOLUTION,
|
42 | autoDensity: false
|
43 | };
|
44 | ViewSystem.extension = {
|
45 | type: [
|
46 | ExtensionType.RendererSystem,
|
47 | ExtensionType.CanvasRendererSystem
|
48 | ],
|
49 | name: "_view"
|
50 | };
|
51 | extensions.add(ViewSystem);
|
52 |
|
53 | export { ViewSystem };
|
54 |
|