@doki-land/live2d 0.0.0 → 0.0.2

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 ADDED
@@ -0,0 +1,7 @@
1
+ declare module "cubism2" {
2
+ export = Live2D;
3
+ }
4
+
5
+ declare module "cubism5" {
6
+ export = Live2DCubismCore;
7
+ }
package/package.json CHANGED
@@ -1,39 +1,45 @@
1
1
  {
2
- "name": "@doki-land/live2d",
3
- "version": "0.0.0",
4
- "keywords": [
5
- "live2d",
6
- "pixi",
7
- "typescript"
8
- ],
9
- "description": "Live2D TypeScript runtime based on pixi-live2d-display",
10
- "author": "Aster <192608617@qq.com>",
11
- "license": "LGPL-3.0-or-later",
12
- "type": "module",
13
- "main": "dist/index.umd.js",
14
- "module": "dist/index.es.js",
15
- "types": "dist/index.d.ts",
16
- "exports": {
17
- ".": {
18
- "require": "./dist/index.umd.js",
19
- "import": "./dist/index.es.js",
20
- "types": "./dist/index.d.ts"
2
+ "name": "@doki-land/live2d",
3
+ "version": "0.0.2",
4
+ "keywords": [
5
+ "live2d",
6
+ "pixi",
7
+ "typescript"
8
+ ],
9
+ "description": "Live2D TypeScript runtime based on pixi-live2d-display",
10
+ "author": "Aster <192608617@qq.com>",
11
+ "license": "LGPL-3.0-or-later",
12
+ "type": "module",
13
+ "main": "dist/l2d.umd.js",
14
+ "module": "dist/l2d.esm.js",
15
+ "types": "src/index.ts",
16
+ "exports": {
17
+ ".": {
18
+ "require": "./dist/l2d.umd.js",
19
+ "import": "./dist/l2d.esm.js",
20
+ "types": "./src/index.ts"
21
+ }
22
+ },
23
+ "files": [
24
+ "dist",
25
+ "lib",
26
+ "src"
27
+ ],
28
+ "publishConfig": {
29
+ "registry": "https://registry.npmjs.org",
30
+ "access": "public"
31
+ },
32
+ "scripts": {
33
+ "dev": "vite",
34
+ "build": "vite build",
35
+ "preview": "vite preview"
36
+ },
37
+ "dependencies": {
38
+ "@pixi/core": "^7.4.3",
39
+ "@pixi/display": "^7.4.3",
40
+ "@pixi/interaction": "^6.5.10",
41
+ "@pixi/ticker": "^7.4.3",
42
+ "pixi-live2d-display-lipsyncpatch": "0.5.0-ls-7",
43
+ "pixi.js": "^7.4.3"
21
44
  }
22
- },
23
- "files": [
24
- "dist",
25
- "lib",
26
- "src"
27
- ],
28
- "dependencies": {
29
- "@pixi/interaction": "6.*",
30
- "@pixi/ticker": "6.*",
31
- "pixi-live2d-display": "^0.4.0",
32
- "pixi.js": "6.*"
33
- },
34
- "scripts": {
35
- "dev": "vite",
36
- "build": "vite build",
37
- "preview": "vite preview"
38
- }
39
- }
45
+ }
package/readme.md ADDED
@@ -0,0 +1,19 @@
1
+ ## Doki land live2d renderer
2
+
3
+
4
+
5
+ ## Using in html by module
6
+
7
+ ```html
8
+ <script type="module">
9
+ const cdn = 'https://cdn.jsdelivr.net/npm/@doki-land/live2d@latest/dist/l2d.esm.js';
10
+ const { createLive2dModel, initializeLive2D } = await import(cdn);
11
+ initializeLive2D();
12
+ await createLive2dModel({
13
+ element_id: 'live2d-canvas',
14
+ models: [
15
+ {}
16
+ ]
17
+ });
18
+ </script>
19
+ ```
package/src/index.ts CHANGED
@@ -1,149 +1,141 @@
1
- import { Live2DModel, MotionManager } from 'pixi-live2d-display';
2
- import * as PIXI from 'pixi.js';
3
- import { Live2dOptions } from './types';
4
- // @ts-ignore
5
- import * as Cubism2 from '../lib/cubism2.min.js';
6
- // @ts-ignore
7
- import * as Cubism5 from '../lib/cubism5.min.js';
8
- import { Ticker, TickerPlugin } from '@pixi/ticker';
9
- import { InteractionManager } from '@pixi/interaction';
10
-
11
- export * from './types';
12
- export { Live2DModel, MotionManager, PIXI };
13
-
14
-
15
- /**
16
- * 创建Live2D模型
17
- * @param options 模型配置选项
18
- * @returns Promise<Live2DModel> 加载完成的Live2D模型实例
19
- */
20
- export async function createLive2dModel(options: Live2dOptions): Promise<Live2DModel> {
21
- const {
22
- models,
23
- element_id,
24
- auto_fit = true,
25
- auto_motion = true,
26
- mouse_tracking = true
27
- } = options;
28
-
29
- // 初始化PIXI应用
30
- const app = new PIXI.Application({
31
- view: document.getElementById(element_id) as HTMLCanvasElement,
32
- width: 300,
33
- height: 300,
34
- backgroundAlpha: 0,
35
- autoDensity: true,
36
- antialias: true
37
- });
38
-
39
- // 加载模型
40
- const model = await Live2DModel.from(models[0]);
41
-
42
- // 添加模型到舞台
43
- app.stage.addChild(model);
44
-
45
- // 自动适应大小
46
- if (auto_fit) {
47
- const scale = Math.min(300 / model.width, 300 / model.height);
48
- model.scale.set(scale);
49
-
50
- // 居中显示
51
- model.x = (300 - model.width * scale) / 2;
52
- model.y = (300 - model.height * scale) / 2;
53
- }
54
-
55
- // 启用鼠标跟踪
56
- if (mouse_tracking) {
57
- enableMouseTracking(model, app.view);
58
- }
59
-
60
- // 自动开始动画
61
- if (auto_motion) {
62
- await startIdleAnimation(model);
63
- }
64
-
65
- return model;
66
- }
67
-
68
- /**
69
- * 启用鼠标跟踪功能
70
- * @param model Live2D模型实例
71
- * @param canvas 画布元素
72
- */
73
- function enableMouseTracking(model: Live2DModel, canvas: HTMLCanvasElement) {
74
- // 跟踪鼠标移动
75
- document.addEventListener('mousemove', (event) => {
76
- const rect = canvas.getBoundingClientRect();
77
- const centerX = rect.left + rect.width / 2;
78
- const centerY = rect.top + rect.height / 2;
79
-
80
- // 计算鼠标位置相对于画布中心的偏移
81
- const mouseX = (event.clientX - centerX) / rect.width;
82
- const mouseY = (event.clientY - centerY) / rect.height;
83
-
84
- // 更新模型的视线方向
85
- if (model.internalModel) {
86
- model.internalModel.focusController?.focus(mouseX, mouseY);
87
- }
88
- });
89
- }
90
-
91
- /**
92
- * 开始空闲动画
93
- * @param model Live2D模型实例
94
- */
95
- async function startIdleAnimation(model: Live2DModel) {
96
- // 尝试播放内置的动作组
97
- try {
98
- // 获取模型支持的动作组
99
- const motions = model.internalModel?.motionManager.definitions;
100
-
101
- if (motions) {
102
- // 优先使用Idle动作组
103
- if (motions.idle) {
104
- await model.motion('idle');
105
- } else if (motions.tap_body) {
106
- // 如果没有idle动作,尝试使用tap_body动作
107
- await model.motion('tap_body');
108
- } else {
109
- // 使用第一个可用的动作组
110
- const firstMotionGroup = Object.keys(motions)[0];
111
- if (firstMotionGroup) {
112
- await model.motion(firstMotionGroup);
113
- }
114
- }
115
- }
116
- } catch (error) {
117
- console.warn('Failed to start idle animation:', error);
118
- }
119
- }
120
-
121
- /**
122
- * 加载Cubism SDK
123
- * 在使用Live2D功能前必须调用此函数
124
- * @param cubismCore 可选的CubismCore对象,如果在非浏览器环境中使用,需要传入
125
- */
126
- export async function initializeLive2D(cubismCore?: any) {
127
- // Live2DModel 注册 Ticker
128
- Live2DModel.registerTicker(Ticker);
129
- // Application 注册 Ticker
130
- PIXI.extensions.add(TickerPlugin)
131
- // 注册 InteractionManager 以支持 Live2D 模型的自动交互
132
- PIXI.extensions.add(InteractionManager);
133
- // 注册Cubism SDK
134
- // if (typeof window !== 'undefined' && window.Live2DCubismCore) {
135
- // Live2DModel.registerCubismCore(Cubism2);
136
- // } else if (cubismCore) {
137
- // Live2DModel.registerCubismCore(Cubism5);
138
- // } else {
139
- // console.warn('Live2DCubismCore is not loaded. Please include the Cubism SDK.');
140
- // }
141
- }
142
-
143
- // 导出默认对象
144
- export default {
145
- createLive2dModel,
146
- initializeLive2D,
147
- Live2DModel,
148
- MotionManager
149
- };
1
+ import '../lib/cubism2.min.js';
2
+ import '../lib/cubism5.min.js';
3
+ import * as PIXI from 'pixi.js';
4
+ import { Live2dOptions } from './types';
5
+ import { Ticker, TickerPlugin } from '@pixi/ticker';
6
+ import { InteractionManager } from '@pixi/interaction';
7
+ import { Live2DModel, MotionManager, SoundManager } from 'pixi-live2d-display-lipsyncpatch';
8
+
9
+ export { MotionManager, SoundManager, Live2DModel, PIXI, type Live2dOptions };
10
+
11
+
12
+ /**
13
+ * 创建Live2D模型
14
+ * @param options 模型配置选项
15
+ * @returns Promise<Live2DModel> 加载完成的Live2D模型实例
16
+ */
17
+ export async function createLive2dModel(options: Live2dOptions): Promise<Live2DModel> {
18
+ const {
19
+ models,
20
+ element_id,
21
+ auto_fit = true,
22
+ auto_motion = true,
23
+ mouse_tracking = true
24
+ } = options;
25
+
26
+ // 初始化PIXI应用
27
+ const app = new PIXI.Application({
28
+ view: document.getElementById(element_id) as HTMLCanvasElement,
29
+ width: 300,
30
+ height: 300,
31
+ backgroundAlpha: 0,
32
+ autoDensity: true,
33
+ antialias: true
34
+ });
35
+
36
+ // 加载模型
37
+ const model = await Live2DModel.from(models[0]);
38
+
39
+ // 添加模型到舞台
40
+ app.stage.addChild(model);
41
+
42
+ // 自动适应大小
43
+ if (auto_fit) {
44
+ const scale = Math.min(300 / model.width, 300 / model.height);
45
+ model.scale.set(scale);
46
+
47
+ // 居中显示
48
+ model.x = (300 - model.width * scale) / 2;
49
+ model.y = (300 - model.height * scale) / 2;
50
+ }
51
+
52
+ // 启用鼠标跟踪
53
+ if (mouse_tracking) {
54
+ enableMouseTracking(model, app.view);
55
+ }
56
+
57
+ // 自动开始动画
58
+ if (auto_motion) {
59
+ await startIdleAnimation(model);
60
+ }
61
+
62
+ return model;
63
+ }
64
+
65
+ /**
66
+ * 启用鼠标跟踪功能
67
+ * @param model Live2D模型实例
68
+ * @param canvas 画布元素
69
+ */
70
+ function enableMouseTracking(model: Live2DModel, canvas: HTMLCanvasElement) {
71
+ // 跟踪鼠标移动
72
+ document.addEventListener('mousemove', (event) => {
73
+ const rect = canvas.getBoundingClientRect();
74
+ const centerX = rect.left + rect.width / 2;
75
+ const centerY = rect.top + rect.height / 2;
76
+
77
+ // 计算鼠标位置相对于画布中心的偏移
78
+ const mouseX = (event.clientX - centerX) / rect.width;
79
+ const mouseY = (event.clientY - centerY) / rect.height;
80
+
81
+ // 更新模型的视线方向
82
+ if (model.internalModel) {
83
+ model.internalModel.focusController?.focus(mouseX, mouseY);
84
+ }
85
+ });
86
+ }
87
+
88
+ /**
89
+ * 开始空闲动画
90
+ * @param model Live2D模型实例
91
+ */
92
+ async function startIdleAnimation(model: Live2DModel) {
93
+ // 尝试播放内置的动作组
94
+ try {
95
+ // 获取模型支持的动作组
96
+ const motions = model.internalModel?.motionManager.definitions;
97
+
98
+ if (motions) {
99
+ // 优先使用Idle动作组
100
+ if (motions.idle) {
101
+ await model.motion('idle');
102
+ } else if (motions.tap_body) {
103
+ // 如果没有idle动作,尝试使用tap_body动作
104
+ await model.motion('tap_body');
105
+ } else {
106
+ // 使用第一个可用的动作组
107
+ const firstMotionGroup = Object.keys(motions)[0];
108
+ if (firstMotionGroup) {
109
+ await model.motion(firstMotionGroup);
110
+ }
111
+ }
112
+ }
113
+ } catch (error) {
114
+ console.warn('Failed to start idle animation:', error);
115
+ }
116
+ }
117
+
118
+ /**
119
+ * 加载Cubism SDK
120
+ * 在使用Live2D功能前必须调用此函数
121
+ * @param cubism2 可选的CubismCore对象,如果在非浏览器环境中使用,需要传入
122
+ * @param cubism5
123
+ */
124
+ export async function initializeLive2D(cubism2?: any, cubism5?: any) {
125
+ // 首先初始化 Cubism2 SDK
126
+ // console.log('sdk2:', Cubism2);
127
+ // console.log('sdk5:', Cubism5);
128
+ // @ts-ignore
129
+ // window.Live2D = cubism2 || Cubism2;
130
+ // 然后初始化 Cubism5 SDK
131
+ // @ts-ignore
132
+ // window.Live2DCubismCore = cubism5 || Cubism5().Live2DCubismCore;
133
+
134
+ // 最后初始化 PIXI 相关功能
135
+ //Live2DModel 注册 Ticker
136
+ Live2DModel.registerTicker(Ticker);
137
+ // 为 Application 注册 Ticker
138
+ PIXI.extensions.add(TickerPlugin);
139
+ // 注册 InteractionManager 以支持 Live2D 模型的自动交互
140
+ PIXI.extensions.add(InteractionManager);
141
+ }
@@ -1,17 +1,17 @@
1
- import { ModelOptions } from './ModelOptions.ts';
1
+ import { ModelOptions } from './ModelOptions';
2
2
 
3
3
  /**
4
4
  * Live2D模型配置选项
5
5
  */
6
6
  export interface Live2dOptions {
7
- /**
8
- * 要渲染到的HTML元素ID
9
- */
10
- element_id: string;
11
7
  /**
12
8
  * 模型配置选项
13
9
  */
14
10
  models: ModelOptions[];
11
+ /**
12
+ * 要渲染到的HTML元素ID
13
+ */
14
+ element_id?: string;
15
15
  /**
16
16
  * 是否自动适应模型大小
17
17
  * @default true
@@ -1,2 +1,2 @@
1
- export type { Live2dOptions } from './Live2dOptions.ts';
2
- export type { ModelOptions, ModelPosition, ModelMobileAdaptation, ModelMainOptions } from './ModelOptions.ts';
1
+ export type { Live2dOptions } from './Live2dOptions';
2
+ export type { ModelOptions, ModelPosition, ModelMobileAdaptation, ModelMainOptions } from './ModelOptions';