-
Notifications
You must be signed in to change notification settings - Fork 14
/
live2d.ts
79 lines (69 loc) · 2.11 KB
/
live2d.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import type { InternalModel, Live2DModel } from 'pixi-live2d-display';
// import { MotionPreloadStrategy } from 'pixi-live2d-display';
import { MotionPreloadStrategy, WindowSizeType } from '../constants/index.js';
import type { DefaultOptions, Live2DModelType, ModelOptions } from '../types/index.js';
import { getWindowSizeType } from '../utils/index.js';
export class Models {
model?: Live2DModel<InternalModel>; // 当前模型实例
private currentModelIndex = 0;
constructor(private options: DefaultOptions) {}
get modelIndex(): number {
return this.currentModelIndex;
}
set modelIndex(index: number) {
this.currentModelIndex = index;
}
get currentModelOptions(): ModelOptions {
return this.options.models[this.modelIndex];
}
create(Live2dModel: Live2DModelType): Promise<void> {
return new Promise((resolve, reject) => {
this.model = Live2dModel.fromSync(this.currentModelOptions.path, {
motionPreload: (this.currentModelOptions.motionPreloadStrategy as MotionPreloadStrategy) || MotionPreloadStrategy.IDLE,
onError: reject
});
this.model.once('load', resolve);
});
}
// 设置模型
settingModel(): void {
switch (getWindowSizeType()) {
case WindowSizeType.mobile:
this.setPosition(...(this.currentModelOptions.mobilePosition || []));
this.setScale(this.currentModelOptions.mobileScale);
break;
case WindowSizeType.pc:
this.setPosition(...(this.currentModelOptions.position || []));
this.setScale(this.currentModelOptions.scale);
break;
}
}
// initializeStyle(): void {
// this.setScale();
// this.setPosition();
// }
// 模型尺寸
get modelSize(): { width: number; height: number } {
return {
width: this.model?.width || 0,
height: this.model?.height || 0
};
}
/**
* 设置缩放比例
* @param x
* @param y
*/
setScale(value: number = 0.1): void {
this.model?.scale.set(value, value);
}
/**
* 设置位置
* @param x
* @param y
*/
setPosition(x = 0, y = 0): void {
this.model!.x = x;
this.model!.y = y;
}
}