react-pixi-fiber 2.0.0-rc.1 → 2.0.0-rc.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.
@@ -24057,26 +24057,51 @@ function useStageRenderer(props, appRef, canvasRef) {
24057
24057
 
24058
24058
  // Cleanup current PIXI.Application when unmounting
24059
24059
  return function cleanup() {
24060
- cleanupStage(appRef.current, STAGE_OPTIONS_UNMOUNT);
24060
+ // There is no PIXI.Application while waiting for a new canvas, see `useStageRerenderer`
24061
+ if (appRef.current != null) {
24062
+ cleanupStage(appRef.current, STAGE_OPTIONS_UNMOUNT);
24063
+ }
24061
24064
  };
24062
24065
  // eslint-disable-next-line react-hooks/exhaustive-deps
24063
24066
  }, []);
24064
24067
  }
24068
+
24069
+ // Returns `key` for the rendered canvas. It changes every time PIXI.Application has to be recreated,
24070
+ // so the new PIXI.Application gets a fresh canvas and WebGL context. The old PIXI.Application is
24071
+ // destroyed later (see `cleanupStage`) and destroying a renderer unbinds the current program
24072
+ // (PixiJS v6) or loses the context (PixiJS v7) of its canvas, which would break the new
24073
+ // PIXI.Application if it was sharing that canvas.
24065
24074
  function useStageRerenderer(props, appRef, canvasRef) {
24066
24075
  var prevProps = usePreviousProps(props);
24076
+ var _useState = React.useState(0),
24077
+ _useState2 = _slicedToArray(_useState, 2),
24078
+ canvasKey = _useState2[0],
24079
+ setCanvasKey = _useState2[1];
24067
24080
 
24068
24081
  // eslint-disable-next-line react-hooks/exhaustive-deps
24069
24082
  React.useLayoutEffect(function () {
24070
24083
  // This is first render, no need to do anything
24071
- if (!appRef.current || prevProps === emptyObject$1) return;
24072
- var app = props.app;
24084
+ if (prevProps === emptyObject$1) return;
24085
+ var app = props.app,
24086
+ options = props.options;
24073
24087
  if (app instanceof PIXI__namespace.Application) {
24074
24088
  // Update stage tree
24075
24089
  rerenderStage(appRef.current, prevProps, props);
24076
24090
  return;
24077
24091
  }
24078
- var options = props.options,
24079
- _props$options = props.options;
24092
+ var view = canvasRef.current;
24093
+
24094
+ // Previous update destroyed PIXI.Application and rendered a new canvas, create PIXI.Application on it
24095
+ if (appRef.current == null) {
24096
+ appRef.current = createPixiApplication(_objectSpread2({
24097
+ view: view
24098
+ }, options));
24099
+
24100
+ // Set initial properties
24101
+ renderStage(appRef.current, props);
24102
+ return;
24103
+ }
24104
+ var _props$options = props.options;
24080
24105
  _props$options.height;
24081
24106
  _props$options.width;
24082
24107
  var otherOptions = _objectWithoutProperties(_props$options, _excluded$1);
@@ -24084,22 +24109,27 @@ function useStageRerenderer(props, appRef, canvasRef) {
24084
24109
  _prevProps$options.height;
24085
24110
  _prevProps$options.width;
24086
24111
  var prevOtherOptions = _objectWithoutProperties(_prevProps$options, _excluded2$1);
24087
- var view = canvasRef.current;
24088
24112
 
24089
24113
  // We need to create new PIXI.Application when options other than dimensions
24090
24114
  // are changed because some renderer settings are immutable.
24091
24115
  if (!shallowEqual$1(otherOptions, prevOtherOptions)) {
24092
24116
  // Destroy PIXI.Application
24093
24117
  cleanupStage(appRef.current, STAGE_OPTIONS_RECREATE);
24118
+ if (options.view) {
24119
+ // Canvas passed in options as `view` is not ours to replace, reuse it
24120
+ appRef.current = createPixiApplication(_objectSpread2({
24121
+ view: view
24122
+ }, options));
24094
24123
 
24095
- // Create new PIXI.Application
24096
- // Canvas passed in options as `view` will be used if provided
24097
- appRef.current = createPixiApplication(_objectSpread2({
24098
- view: view
24099
- }, options));
24100
-
24101
- // Set initial properties
24102
- renderStage(appRef.current, props);
24124
+ // Set initial properties
24125
+ renderStage(appRef.current, props);
24126
+ } else {
24127
+ // Render a new canvas, PIXI.Application is created on it in the next update
24128
+ appRef.current = null;
24129
+ setCanvasKey(function (canvasKey) {
24130
+ return canvasKey + 1;
24131
+ });
24132
+ }
24103
24133
  } else {
24104
24134
  // Update stage tree
24105
24135
  rerenderStage(appRef.current, prevProps, props);
@@ -24107,6 +24137,7 @@ function useStageRerenderer(props, appRef, canvasRef) {
24107
24137
  resizeRenderer(appRef.current, prevProps, props);
24108
24138
  }
24109
24139
  });
24140
+ return canvasKey;
24110
24141
  }
24111
24142
  function createStageFunction() {
24112
24143
  var Stage = /*#__PURE__*/React.forwardRef(function Stage(props, ref) {
@@ -24132,7 +24163,7 @@ function createStageFunction() {
24132
24163
  // - useStageRenderer:
24133
24164
  // - is only called once
24134
24165
  // - is responsible for creating first PIXI.Application and destroying it when Stage is finally unmounted
24135
- useStageRerenderer(props, appRef, canvasRef);
24166
+ var canvasKey = useStageRerenderer(props, appRef, canvasRef);
24136
24167
  useStageRenderer(props, appRef, canvasRef);
24137
24168
 
24138
24169
  // Do not render anything if PIXI.Application was provided in props
@@ -24146,6 +24177,7 @@ function createStageFunction() {
24146
24177
  }
24147
24178
  var canvasProps = getCanvasProps(props);
24148
24179
  return /*#__PURE__*/React__default["default"].createElement("canvas", _extends({
24180
+ key: canvasKey,
24149
24181
  ref: canvasRef
24150
24182
  }, canvasProps));
24151
24183
  });
@@ -24167,6 +24199,11 @@ function createStageClass() {
24167
24199
  _this._app = createRef();
24168
24200
  // Store canvas if it was rendered
24169
24201
  _this._canvas = createRef();
24202
+ // Changes every time PIXI.Application has to be recreated, so it gets a fresh canvas.
24203
+ // See `getPixiApplication`.
24204
+ _this.state = {
24205
+ canvasKey: 0
24206
+ };
24170
24207
  return _this;
24171
24208
  }
24172
24209
  _inherits(Stage, _React$Component);
@@ -24185,6 +24222,9 @@ function createStageClass() {
24185
24222
  key: "componentDidUpdate",
24186
24223
  value: function componentDidUpdate(prevProps) {
24187
24224
  var app = this.getPixiApplication(this.props, prevProps);
24225
+
24226
+ // Waiting for a new canvas to be rendered, PIXI.Application is created in the next update
24227
+ if (app === null) return;
24188
24228
  this.renderStage(app, this.props);
24189
24229
  this.rerenderStage(app, this.props, prevProps);
24190
24230
 
@@ -24197,7 +24237,8 @@ function createStageClass() {
24197
24237
  var app = this.props.app;
24198
24238
 
24199
24239
  // Destroy PIXI.Application if it was not provided in props
24200
- if (!(app instanceof PIXI__namespace.Application)) {
24240
+ // There is no PIXI.Application while waiting for a new canvas, see `getPixiApplication`
24241
+ if (!(app instanceof PIXI__namespace.Application) && this._app.current != null) {
24201
24242
  cleanupStage(this._app.current, STAGE_OPTIONS_UNMOUNT);
24202
24243
  }
24203
24244
  }
@@ -24219,6 +24260,7 @@ function createStageClass() {
24219
24260
  }
24220
24261
  var canvasProps = getCanvasProps(this.props);
24221
24262
  return /*#__PURE__*/React__default["default"].createElement("canvas", _extends({
24263
+ key: this.state.canvasKey,
24222
24264
  ref: this._canvas
24223
24265
  }, canvasProps));
24224
24266
  }
@@ -24235,7 +24277,7 @@ function createStageClass() {
24235
24277
  }
24236
24278
  var view = this._canvas.current;
24237
24279
 
24238
- // Render stage for the first time
24280
+ // Render stage for the first time, or on a new canvas after PIXI.Application was destroyed
24239
24281
  if (this._app.current == null) {
24240
24282
  // Create new PIXI.Application
24241
24283
  // Canvas passed in options as `view` will be used if provided
@@ -24258,11 +24300,23 @@ function createStageClass() {
24258
24300
  // Destroy PIXI.Application
24259
24301
  cleanupStage(this._app.current, STAGE_OPTIONS_RECREATE);
24260
24302
 
24261
- // Create new PIXI.Application
24262
- // Canvas passed in options as `view` will be used if provided
24263
- return createPixiApplication(_objectSpread2({
24264
- view: view
24265
- }, options));
24303
+ // Canvas passed in options as `view` is not ours to replace, reuse it
24304
+ if (options.view) {
24305
+ return createPixiApplication(_objectSpread2({
24306
+ view: view
24307
+ }, options));
24308
+ }
24309
+
24310
+ // Destroying the old PIXI.Application later unbinds or loses the WebGL context of its canvas,
24311
+ // so render a new canvas and create PIXI.Application on it in the next update
24312
+ this._app.current = null;
24313
+ this.setState(function (_ref) {
24314
+ var canvasKey = _ref.canvasKey;
24315
+ return {
24316
+ canvasKey: canvasKey + 1
24317
+ };
24318
+ });
24319
+ return null;
24266
24320
  }
24267
24321
 
24268
24322
  // Return already existing Application otherwise