@charivo/render-live2d 0.2.1 → 0.2.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/dist/renderer.js CHANGED
@@ -1,3 +1,7 @@
1
+ import { subscribeBrowserLifecycle } from '@charivo/core';
2
+
3
+ // src/renderer.ts
4
+
1
5
  // CubismSdkForWeb-5-r.4/Framework/src/type/csmvector.ts
2
6
  var csmVector = class _csmVector {
3
7
  /**
@@ -14068,6 +14072,9 @@ var LAppGlManager = class {
14068
14072
  }
14069
14073
  return this.gl;
14070
14074
  }
14075
+ isContextLost() {
14076
+ return Boolean(this.gl?.isContextLost?.());
14077
+ }
14071
14078
  };
14072
14079
 
14073
14080
  // src/cubism/lapptexturemanager.ts
@@ -14320,8 +14327,13 @@ var Live2DRenderer = class _Live2DRenderer {
14320
14327
  canvas;
14321
14328
  host;
14322
14329
  model;
14330
+ lastModelPath;
14323
14331
  teardownResize;
14332
+ teardownBrowserLifecycle;
14324
14333
  animationFrameId;
14334
+ renderLoopPaused = false;
14335
+ recoveryPending = false;
14336
+ isRestoringContext = false;
14325
14337
  deviceToScreen = new CubismMatrix44();
14326
14338
  viewMatrix = new CubismViewMatrix();
14327
14339
  constructor(options) {
@@ -14334,6 +14346,8 @@ var Live2DRenderer = class _Live2DRenderer {
14334
14346
  this.initializeCubism();
14335
14347
  this.host = new CubismModelHost(this.canvas);
14336
14348
  this.host.initialize();
14349
+ this.bindCanvasLifecycle();
14350
+ this.bindPageLifecycle();
14337
14351
  this.resizeCanvas();
14338
14352
  this.teardownResize = setupResponsiveResize(
14339
14353
  this.canvas,
@@ -14343,6 +14357,7 @@ var Live2DRenderer = class _Live2DRenderer {
14343
14357
  }
14344
14358
  async loadModel(modelPath) {
14345
14359
  if (!this.host) throw new Error("Live2D renderer is not initialized");
14360
+ this.lastModelPath = modelPath;
14346
14361
  this.model?.release();
14347
14362
  const model = new LAppModel();
14348
14363
  await model.loadAssets(modelPath, this.host);
@@ -14404,6 +14419,8 @@ var Live2DRenderer = class _Live2DRenderer {
14404
14419
  this.model.setDragging(clamp(coords.x, -1, 1), clamp(coords.y, -1, 1));
14405
14420
  }
14406
14421
  async destroy() {
14422
+ this.unbindCanvasLifecycle();
14423
+ this.unbindPageLifecycle();
14407
14424
  if (this.animationFrameId) {
14408
14425
  cancelAnimationFrame(this.animationFrameId);
14409
14426
  this.animationFrameId = void 0;
@@ -14427,6 +14444,7 @@ var Live2DRenderer = class _Live2DRenderer {
14427
14444
  LAppPal.updateTime();
14428
14445
  }
14429
14446
  startRenderLoop() {
14447
+ this.renderLoopPaused = false;
14430
14448
  const loop = () => {
14431
14449
  this.animationFrameId = requestAnimationFrame(loop);
14432
14450
  this.renderFrame();
@@ -14434,7 +14452,7 @@ var Live2DRenderer = class _Live2DRenderer {
14434
14452
  loop();
14435
14453
  }
14436
14454
  renderFrame() {
14437
- if (!this.host) return;
14455
+ if (!this.host || this.renderLoopPaused || this.recoveryPending) return;
14438
14456
  const gl = this.host.getGlManager().getGl();
14439
14457
  LAppPal.updateTime();
14440
14458
  gl.clearColor(0, 0, 0, 0);
@@ -14459,7 +14477,7 @@ var Live2DRenderer = class _Live2DRenderer {
14459
14477
  this.model.draw(projection);
14460
14478
  }
14461
14479
  resizeCanvas() {
14462
- if (!this.canvas || !this.host) return;
14480
+ if (!this.canvas || !this.host || this.recoveryPending) return;
14463
14481
  const parent = this.canvas.parentElement;
14464
14482
  const rect = parent?.getBoundingClientRect();
14465
14483
  const width = Math.max(1, Math.floor(rect?.width ?? this.canvas.width));
@@ -14531,6 +14549,93 @@ var Live2DRenderer = class _Live2DRenderer {
14531
14549
  viewY: this.viewMatrix.invertTransformY(screenY)
14532
14550
  };
14533
14551
  }
14552
+ bindCanvasLifecycle() {
14553
+ this.canvas?.addEventListener("webglcontextlost", this.handleContextLost);
14554
+ this.canvas?.addEventListener(
14555
+ "webglcontextrestored",
14556
+ this.handleContextRestored
14557
+ );
14558
+ }
14559
+ unbindCanvasLifecycle() {
14560
+ this.canvas?.removeEventListener(
14561
+ "webglcontextlost",
14562
+ this.handleContextLost
14563
+ );
14564
+ this.canvas?.removeEventListener(
14565
+ "webglcontextrestored",
14566
+ this.handleContextRestored
14567
+ );
14568
+ }
14569
+ bindPageLifecycle() {
14570
+ if (this.teardownBrowserLifecycle) {
14571
+ return;
14572
+ }
14573
+ this.teardownBrowserLifecycle = subscribeBrowserLifecycle({
14574
+ onHidden: this.handleHidden,
14575
+ onPageHide: this.handlePageHide,
14576
+ onPageShow: this.handlePageShow,
14577
+ onVisible: this.handleVisible
14578
+ });
14579
+ }
14580
+ unbindPageLifecycle() {
14581
+ this.teardownBrowserLifecycle?.();
14582
+ this.teardownBrowserLifecycle = void 0;
14583
+ }
14584
+ handleContextLost = (event) => {
14585
+ event.preventDefault();
14586
+ this.recoveryPending = true;
14587
+ this.pauseRenderLoop();
14588
+ };
14589
+ handleContextRestored = () => {
14590
+ void this.restoreAfterContextLoss();
14591
+ };
14592
+ handleHidden = () => {
14593
+ this.pauseRenderLoop();
14594
+ };
14595
+ handleVisible = () => {
14596
+ this.resumeRenderLoop();
14597
+ };
14598
+ handlePageHide = () => {
14599
+ this.pauseRenderLoop();
14600
+ };
14601
+ handlePageShow = () => {
14602
+ this.resumeRenderLoop();
14603
+ };
14604
+ pauseRenderLoop() {
14605
+ this.renderLoopPaused = true;
14606
+ }
14607
+ resumeRenderLoop() {
14608
+ if (this.recoveryPending) {
14609
+ return;
14610
+ }
14611
+ this.renderLoopPaused = false;
14612
+ }
14613
+ async restoreAfterContextLoss() {
14614
+ if (!this.canvas || this.isRestoringContext) {
14615
+ return;
14616
+ }
14617
+ this.isRestoringContext = true;
14618
+ this.recoveryPending = true;
14619
+ try {
14620
+ this.model?.release();
14621
+ this.model = void 0;
14622
+ this.host?.dispose();
14623
+ this.host = new CubismModelHost(this.canvas);
14624
+ this.host.initialize();
14625
+ this.resizeCanvas();
14626
+ if (this.lastModelPath) {
14627
+ const model = new LAppModel();
14628
+ await model.loadAssets(this.lastModelPath, this.host);
14629
+ await model.waitUntilReady();
14630
+ this.model = model;
14631
+ }
14632
+ this.updateViewMatrices();
14633
+ this.recoveryPending = false;
14634
+ this.resumeRenderLoop();
14635
+ } finally {
14636
+ this.isRestoringContext = false;
14637
+ }
14638
+ }
14534
14639
  };
14535
14640
  function createLive2DRenderer(options) {
14536
14641
  return new Live2DRenderer(options);