Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
809 additions
and
487 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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, childLen - 1); | ||
} | ||
} | ||
|
||
resize(): void { | ||
this.app?.resize(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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; | ||
} | ||
} |
Oops, something went wrong.