@doki-land/live2d 0.0.0

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/package.json ADDED
@@ -0,0 +1,39 @@
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"
21
+ }
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
+ }
package/src/index.ts ADDED
@@ -0,0 +1,149 @@
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
+ };
@@ -0,0 +1,30 @@
1
+ import { ModelOptions } from './ModelOptions.ts';
2
+
3
+ /**
4
+ * Live2D模型配置选项
5
+ */
6
+ export interface Live2dOptions {
7
+ /**
8
+ * 要渲染到的HTML元素ID
9
+ */
10
+ element_id: string;
11
+ /**
12
+ * 模型配置选项
13
+ */
14
+ models: ModelOptions[];
15
+ /**
16
+ * 是否自动适应模型大小
17
+ * @default true
18
+ */
19
+ auto_fit?: boolean;
20
+ /**
21
+ * 是否自动开始动画
22
+ * @default true
23
+ */
24
+ auto_motion?: boolean;
25
+ /**
26
+ * 是否启用鼠标跟踪
27
+ * @default true
28
+ */
29
+ mouse_tracking?: boolean;
30
+ }
@@ -0,0 +1,40 @@
1
+ export type ModelOptions = ModelMainOptions & ModelMobileAdaptation;
2
+ export interface ModelMobileAdaptation {
3
+ /**
4
+ * 移动端配置, 会覆盖 desktop 中的主配置
5
+ * @default null
6
+ */
7
+ mobile?: Partial<ModelOptions>;
8
+ }
9
+ export interface ModelMainOptions {
10
+ /**
11
+ * 模型JSON文件的路径
12
+ */
13
+ model_url: string;
14
+ position?: ModelPosition;
15
+ }
16
+
17
+ export interface ModelPosition {
18
+ /**
19
+ * 模型在画布中的缩放
20
+ */
21
+ scale?: number,
22
+ /**
23
+ * 模型在画布中的位置
24
+ */
25
+ anchor_x?: number;
26
+ /**
27
+ * 模型在画布中的位置
28
+ */
29
+ anchor_y?: number;
30
+ /**
31
+ * 画布宽度
32
+ * @default 300
33
+ */
34
+ width?: number;
35
+ /**
36
+ * 画布高度
37
+ * @default 300
38
+ */
39
+ height?: number;
40
+ }
@@ -0,0 +1,2 @@
1
+ export type { Live2dOptions } from './Live2dOptions.ts';
2
+ export type { ModelOptions, ModelPosition, ModelMobileAdaptation, ModelMainOptions } from './ModelOptions.ts';