-
Notifications
You must be signed in to change notification settings - Fork 14
/
application.ts
49 lines (41 loc) · 1.11 KB
/
application.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import type { InternalModel, Live2DModel } from 'pixi-live2d-display';
import type { Application as ApplicationType } from 'pixi.js';
import type { PixiModule } from '../types/index.js';
export class Application {
app?: ApplicationType;
constructor(
// private options: DefaultOptions,
private PIXI: PixiModule
) {}
mount(canvasElement: HTMLCanvasElement, stageElement: HTMLElement, model?: Live2DModel<InternalModel>): void {
if (!this.app) {
this.app = new this.PIXI.Application({
view: canvasElement,
resolution: 2,
autoStart: true,
autoDensity: true,
backgroundAlpha: 0,
resizeTo: stageElement
});
}
if (model) {
this.clearAppStage();
this.app.stage.addChild(model);
} else {
console.error('挂载模型失败');
}
}
unMount(): void {
this.clearAppStage();
}
// 清除舞台中所有模型
clearAppStage(): void {
const childLen = this.app?.stage.children.length || 0;
if (childLen > 0) {
this.app!.stage.removeChildren(0);
}
}
resize(): void {
this.app?.resize();
}
}