Skip to content

Commit

Permalink
Defer window.PIXI.Ticker detection
Browse files Browse the repository at this point in the history
  • Loading branch information
guansss committed Jul 22, 2022
1 parent 4f6b891 commit 1090b9e
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/Live2DModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Live2DFactory } from '@/factory/Live2DFactory';
import { Renderer, Texture } from '@pixi/core';
import { Container } from '@pixi/display';
import { Matrix, ObservablePoint, Point, Rectangle } from '@pixi/math';
import { Ticker } from '@pixi/ticker';
import type { Ticker } from '@pixi/ticker';
import { InteractionMixin } from './InteractionMixin';
import { Live2DTransform } from './Live2DTransform';
import { applyMixins, logger } from './utils';
Expand All @@ -28,8 +28,9 @@ export interface Live2DModelOptions extends MotionManagerOptions {
const tempPoint = new Point();
const tempMatrix = new Matrix();

// a reference to Ticker class, defaults to window.PIXI.Ticker (when loaded by a <script> tag)
let TickerClass: typeof Ticker | undefined = (window as any).PIXI?.Ticker;
// a reference to Ticker class, defaults to window.PIXI.Ticker
type TickerClass = typeof Ticker;
let tickerRef: TickerClass | undefined;

export interface Live2DModel<IM extends InternalModel = InternalModel> extends InteractionMixin {}

Expand Down Expand Up @@ -88,8 +89,8 @@ export class Live2DModel<IM extends InternalModel = InternalModel> extends Conta
/**
* Registers the class of `PIXI.Ticker` for auto updating.
*/
static registerTicker(tickerClass: typeof Ticker): void {
TickerClass = tickerClass;
static registerTicker(tickerClass: TickerClass): void {
tickerRef = tickerClass;
}

/**
Expand Down Expand Up @@ -141,18 +142,20 @@ export class Live2DModel<IM extends InternalModel = InternalModel> extends Conta
}

set autoUpdate(autoUpdate: boolean) {
tickerRef ||= (window as any).PIXI?.Ticker;

if (autoUpdate) {
if (!this._destroyed) {
if (TickerClass) {
TickerClass.shared.add(this.onTickerUpdate, this);
if (tickerRef) {
tickerRef.shared.add(this.onTickerUpdate, this);

this._autoUpdate = true;
} else {
logger.warn(this.tag, 'No Ticker registered, please call Live2DModel.registerTicker(Ticker).');
}
}
} else {
TickerClass?.shared.remove(this.onTickerUpdate, this);
tickerRef?.shared.remove(this.onTickerUpdate, this);

this._autoUpdate = false;
}
Expand Down Expand Up @@ -316,7 +319,7 @@ export class Live2DModel<IM extends InternalModel = InternalModel> extends Conta
* An update callback to be added to `PIXI.Ticker` and invoked every tick.
*/
onTickerUpdate(): void {
this.update(TickerClass!.shared.deltaMS);
this.update(tickerRef!.shared.deltaMS);
}

/**
Expand Down

0 comments on commit 1090b9e

Please sign in to comment.