@doki-land/live2d 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- declare module "cubism2" {
2
- export = Live2D;
3
- }
4
-
5
- declare module "cubism5" {
6
- export = Live2DCubismCore;
7
- }
1
+ declare module "cubism2" {
2
+ export = Live2D;
3
+ }
4
+
5
+ declare module "cubism5" {
6
+ export = Live2DCubismCore;
7
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@doki-land/live2d",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "keywords": [
5
5
  "live2d",
6
6
  "pixi",
package/src/index.ts CHANGED
@@ -8,7 +8,6 @@ import { Live2DModel, MotionManager, SoundManager } from 'pixi-live2d-display-li
8
8
 
9
9
  export { MotionManager, SoundManager, Live2DModel, PIXI, type Live2dOptions };
10
10
 
11
-
12
11
  /**
13
12
  * 创建Live2D模型
14
13
  * @param options 模型配置选项
@@ -16,18 +15,22 @@ export { MotionManager, SoundManager, Live2DModel, PIXI, type Live2dOptions };
16
15
  */
17
16
  export async function createLive2dModel(options: Live2dOptions): Promise<Live2DModel> {
18
17
  const {
18
+ element_id = 'live2d-canvas',
19
19
  models,
20
- element_id,
20
+ width = 300,
21
+ height = 300,
21
22
  auto_fit = true,
22
23
  auto_motion = true,
23
24
  mouse_tracking = true
24
25
  } = options;
25
-
26
+ // 创建画布
27
+ let element = findOrCreateCanvas(element_id)
26
28
  // 初始化PIXI应用
27
29
  const app = new PIXI.Application({
28
- view: document.getElementById(element_id) as HTMLCanvasElement,
29
- width: 300,
30
- height: 300,
30
+ view: element,
31
+ width: width,
32
+ height: height,
33
+ // resolution: 1,
31
34
  backgroundAlpha: 0,
32
35
  autoDensity: true,
33
36
  antialias: true
@@ -41,12 +44,12 @@ export async function createLive2dModel(options: Live2dOptions): Promise<Live2DM
41
44
 
42
45
  // 自动适应大小
43
46
  if (auto_fit) {
44
- const scale = Math.min(300 / model.width, 300 / model.height);
47
+ const scale = Math.min(width / model.width, height / model.height);
45
48
  model.scale.set(scale);
46
49
 
47
50
  // 居中显示
48
- model.x = (300 - model.width * scale) / 2;
49
- model.y = (300 - model.height * scale) / 2;
51
+ model.x = (width - model.width * scale) / 2;
52
+ model.y = (height - model.height * scale) / 2;
50
53
  }
51
54
 
52
55
  // 启用鼠标跟踪
@@ -62,6 +65,19 @@ export async function createLive2dModel(options: Live2dOptions): Promise<Live2DM
62
65
  return model;
63
66
  }
64
67
 
68
+ function findOrCreateCanvas(id: string): HTMLCanvasElement {
69
+ let canvas = document.getElementById(id) as HTMLCanvasElement;
70
+ if (!canvas) {
71
+ canvas = document.createElement('canvas');
72
+ canvas.id = id;
73
+ document.body.appendChild(canvas);
74
+ canvas.style.display = 'none';
75
+ canvas.style.position = 'fixed';
76
+ }
77
+ return canvas;
78
+ }
79
+
80
+
65
81
  /**
66
82
  * 启用鼠标跟踪功能
67
83
  * @param model Live2D模型实例
@@ -9,9 +9,20 @@ export interface Live2dOptions {
9
9
  */
10
10
  models: ModelOptions[];
11
11
  /**
12
- * 要渲染到的HTML元素ID
12
+ * 画布的ID
13
+ * @default 'live2d-canvas'
13
14
  */
14
- element_id?: string;
15
+ element_id?: string,
16
+ /**
17
+ * 画布的宽度
18
+ * @default 300
19
+ */
20
+ width?: number;
21
+ /**
22
+ * 画布的高度
23
+ * @default 300
24
+ */
25
+ height?: number;
15
26
  /**
16
27
  * 是否自动适应模型大小
17
28
  * @default true