react-native-wgpu 0.4.1 → 0.4.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/README.md +28 -14
- package/lib/commonjs/Canvas.js +9 -14
- package/lib/commonjs/Canvas.js.map +1 -1
- package/lib/module/Canvas.js +10 -16
- package/lib/module/Canvas.js.map +1 -1
- package/lib/typescript/lib/commonjs/Canvas.d.ts +5 -1
- package/lib/typescript/lib/commonjs/Canvas.d.ts.map +1 -1
- package/lib/typescript/lib/module/Canvas.d.ts +6 -1
- package/lib/typescript/lib/module/Canvas.d.ts.map +1 -1
- package/lib/typescript/src/Canvas.d.ts +4 -2
- package/lib/typescript/src/Canvas.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/Canvas.tsx +16 -22
package/README.md
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
# React Native WebGPU
|
|
2
2
|
|
|
3
|
-
React Native implementation of WebGPU using [Dawn](https://dawn.googlesource.com/dawn).
|
|
4
|
-
This is currently a technical preview for early adopters.
|
|
3
|
+
React Native implementation of WebGPU using [Dawn](https://dawn.googlesource.com/dawn).
|
|
5
4
|
|
|
6
|
-
React Native WebGPU requires React Native 0.81 or newer
|
|
5
|
+
React Native WebGPU requires React Native 0.81 or newer. It doesn't support the legacy architecture.
|
|
7
6
|
|
|
8
7
|
## Installation
|
|
9
8
|
|
|
@@ -13,7 +12,16 @@ Please note that the package name is `react-native-wgpu`.
|
|
|
13
12
|
npm install react-native-wgpu
|
|
14
13
|
```
|
|
15
14
|
|
|
16
|
-
|
|
15
|
+
## With Expo
|
|
16
|
+
|
|
17
|
+
Expo provides a React Native WebGPU template that works with React Three Fiber.
|
|
18
|
+
The works on iOS, Android, and Web.
|
|
19
|
+
|
|
20
|
+
```
|
|
21
|
+
npx create-expo-app@latest -e with-webgpu
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
https://github.com/user-attachments/assets/efbd05f8-4ce0-46c2-919c-03e1095bc8ac
|
|
17
25
|
|
|
18
26
|
Below are some examples from the [example app](/apps/example/).
|
|
19
27
|
|
|
@@ -38,13 +46,6 @@ We also provide prebuilt binaries for visionOS and macOS.
|
|
|
38
46
|
|
|
39
47
|
https://github.com/user-attachments/assets/2d5c618e-5b15-4cef-8558-d4ddf8c70667
|
|
40
48
|
|
|
41
|
-
## Diagnostics
|
|
42
|
-
|
|
43
|
-
Two diagnostic screens were added to the example app so you can reproduce the issues highlighted in the code review:
|
|
44
|
-
|
|
45
|
-
- `Async Runner Starvation` (`apps/example/src/Diagnostics/AsyncStarvation.tsx`) shows that a zero-delay `setTimeout` never fires before `device.createRenderPipelineAsync()` resolves because the event loop is kept busy. Launch the example app and open the screen from the home list to observe the stalled timer.
|
|
46
|
-
- `Device Lost Promise Hang` (`apps/example/src/Diagnostics/DeviceLostHang.tsx`) forces a synthetic device loss by calling the native `forceLossForTesting()` helper. On the current build the `device.lost` promise remains pending indefinitely, confirming that the loss callback is never delivered once pumping stops.
|
|
47
|
-
|
|
48
49
|
## Usage
|
|
49
50
|
|
|
50
51
|
Usage is identical to Web.
|
|
@@ -158,8 +159,8 @@ From there you will be able to run the example app properly.
|
|
|
158
159
|
|
|
159
160
|
## Similarities and Differences with the Web
|
|
160
161
|
|
|
161
|
-
The API has been designed to be completely symmetric with the Web.
|
|
162
|
-
For instance, you can access the WebGPU context synchronously, as well as the canvas size.
|
|
162
|
+
The API has been designed to be completely symmetric with the Web.
|
|
163
|
+
For instance, you can access the WebGPU context synchronously, as well as the canvas size.
|
|
163
164
|
Pixel density and canvas resizing are handled exactly like on the Web as well.
|
|
164
165
|
|
|
165
166
|
```tsx
|
|
@@ -173,7 +174,7 @@ ctx.canvas.height = ctx.canvas.clientHeight * PixelRatio.get();
|
|
|
173
174
|
|
|
174
175
|
### Frame Scheduling
|
|
175
176
|
|
|
176
|
-
In React Native, we want to keep frame presentation as a manual operation as we plan to provide more advanced rendering options that are React Native specific.
|
|
177
|
+
In React Native, we want to keep frame presentation as a manual operation as we plan to provide more advanced rendering options that are React Native specific.
|
|
177
178
|
This means that when you are ready to present a frame, you need to call `present` on the context.
|
|
178
179
|
|
|
179
180
|
```tsx
|
|
@@ -184,6 +185,19 @@ device.queue.submit([commandEncoder.finish()]);
|
|
|
184
185
|
context.present();
|
|
185
186
|
```
|
|
186
187
|
|
|
188
|
+
### Canvas Transparency
|
|
189
|
+
|
|
190
|
+
On Android, the `alphaMode` property is ignored when configuring the canvas.
|
|
191
|
+
To have a transparent canvas by default, use the `transparent` property.
|
|
192
|
+
|
|
193
|
+
```tsx
|
|
194
|
+
return (
|
|
195
|
+
<View style={style.container}>
|
|
196
|
+
<Canvas ref={ref} style={style.webgpu} transparent />
|
|
197
|
+
</View>
|
|
198
|
+
);
|
|
199
|
+
```
|
|
200
|
+
|
|
187
201
|
### External Textures
|
|
188
202
|
|
|
189
203
|
This module provides a `createImageBitmap` function that you can use in `copyExternalImageToTexture`.
|
package/lib/commonjs/Canvas.js
CHANGED
|
@@ -14,11 +14,11 @@ let CONTEXT_COUNTER = 1;
|
|
|
14
14
|
function generateContextId() {
|
|
15
15
|
return CONTEXT_COUNTER++;
|
|
16
16
|
}
|
|
17
|
-
const Canvas =
|
|
18
|
-
onLayout: _onLayout,
|
|
17
|
+
const Canvas = ({
|
|
19
18
|
transparent,
|
|
19
|
+
ref,
|
|
20
20
|
...props
|
|
21
|
-
}
|
|
21
|
+
}) => {
|
|
22
22
|
const viewRef = (0, _react.useRef)(null);
|
|
23
23
|
const [contextId, _] = (0, _react.useState)(() => generateContextId());
|
|
24
24
|
(0, _react.useImperativeHandle)(ref, () => ({
|
|
@@ -33,16 +33,10 @@ const Canvas = exports.Canvas = /*#__PURE__*/(0, _react.forwardRef)(({
|
|
|
33
33
|
if (!viewRef.current) {
|
|
34
34
|
throw new Error("[WebGPU] Cannot get context before mount");
|
|
35
35
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
size = viewRef.current.getBoundingClientRect();
|
|
41
|
-
} else {
|
|
42
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
43
|
-
// @ts-expect-error
|
|
44
|
-
size = viewRef.current.unstable_getBoundingClientRect();
|
|
45
|
-
}
|
|
36
|
+
// getBoundingClientRect became stable in RN 0.83
|
|
37
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
38
|
+
const view = viewRef.current;
|
|
39
|
+
const size = "getBoundingClientRect" in view ? view.getBoundingClientRect() : view.unstable_getBoundingClientRect();
|
|
46
40
|
return RNWebGPU.MakeWebGPUCanvasContext(contextId, size.width, size.height);
|
|
47
41
|
}
|
|
48
42
|
}));
|
|
@@ -56,5 +50,6 @@ const Canvas = exports.Canvas = /*#__PURE__*/(0, _react.forwardRef)(({
|
|
|
56
50
|
contextId: contextId,
|
|
57
51
|
transparent: !!transparent
|
|
58
52
|
}));
|
|
59
|
-
}
|
|
53
|
+
};
|
|
54
|
+
exports.Canvas = Canvas;
|
|
60
55
|
//# sourceMappingURL=Canvas.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_react","_interopRequireWildcard","require","_reactNative","_WebGPUViewNativeComponent","_interopRequireDefault","e","__esModule","default","t","WeakMap","r","n","o","i","f","__proto__","has","get","set","hasOwnProperty","call","Object","defineProperty","getOwnPropertyDescriptor","_extends","assign","bind","arguments","length","apply","CONTEXT_COUNTER","generateContextId","Canvas","
|
|
1
|
+
{"version":3,"names":["_react","_interopRequireWildcard","require","_reactNative","_WebGPUViewNativeComponent","_interopRequireDefault","e","__esModule","default","t","WeakMap","r","n","o","i","f","__proto__","has","get","set","hasOwnProperty","call","Object","defineProperty","getOwnPropertyDescriptor","_extends","assign","bind","arguments","length","apply","CONTEXT_COUNTER","generateContextId","Canvas","transparent","ref","props","viewRef","useRef","contextId","_","useState","useImperativeHandle","getContextId","getNativeSurface","RNWebGPU","getContext","contextName","Error","current","view","size","getBoundingClientRect","unstable_getBoundingClientRect","MakeWebG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ACA;MACA,MAAME,IAAI,GAAGb,OAAO,CAACY,OAAc;MACnC,MAAME,IAAI,GACR,uBAAuB,IAAID,IAAI,GAC3BA,IAAI,CAACE,qBAAqB,CAAC,CAAC,GAC5BF,IAAI,CAACG,8BAA8B,CAAC,CAAC;MAC3C,OAAOR,QAAQ,CAACS,uBAAuB,CACrCf,SAAS,EACTY,IAAI,CAACI,KAAK,EACVJ,IAAI,CAACK,MACP,CAAC;IACH;EACF,CAAC,CAAC,CAAC;EAEH,oBACExD,MAAA,CAAAQ,OAAA,CAAAiD,aAAA,CAACtD,YAAA,CAAAuD,IAAI,EAAAjC,QAAA;IAACkC,WAAW,EAAE,KAAM;IAACxB,GAAG,EAAEE;EAAQ,GAAKD,KAAK,gBAC/CpC,MAAA,CAAAQ,OAAA,CAAAiD,aAAA,CAACrD,0BAAA,CAAAI,OAAgB;IACfoD,KAAK,EAAE;MAAEC,IAAI,EAAE;IAAE,CAAE;IACnBtB,SAAS,EAAEA,SAAU;IACrBL,WAAW,EAAE,CAAC,CAACA;EAAY,CAC5B,CACG,CAAC;AAEX,CAAC;AAAC4B,OAAA,CAAA7B,MAAA,GAAAA,MAAA","ignoreList":[]}
|
package/lib/module/Canvas.js
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
|
|
2
|
-
import React, {
|
|
3
|
-
import {
|
|
2
|
+
import React, { useImperativeHandle, useRef, useState } from "react";
|
|
3
|
+
import { View } from "react-native";
|
|
4
4
|
import WebGPUNativeView from "./WebGPUViewNativeComponent";
|
|
5
5
|
let CONTEXT_COUNTER = 1;
|
|
6
6
|
function generateContextId() {
|
|
7
7
|
return CONTEXT_COUNTER++;
|
|
8
8
|
}
|
|
9
|
-
export const Canvas =
|
|
10
|
-
onLayout: _onLayout,
|
|
9
|
+
export const Canvas = ({
|
|
11
10
|
transparent,
|
|
11
|
+
ref,
|
|
12
12
|
...props
|
|
13
|
-
}
|
|
13
|
+
}) => {
|
|
14
14
|
const viewRef = useRef(null);
|
|
15
15
|
const [contextId, _] = useState(() => generateContextId());
|
|
16
16
|
useImperativeHandle(ref, () => ({
|
|
@@ -25,16 +25,10 @@ export const Canvas = /*#__PURE__*/forwardRef(({
|
|
|
25
25
|
if (!viewRef.current) {
|
|
26
26
|
throw new Error("[WebGPU] Cannot get context before mount");
|
|
27
27
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
size = viewRef.current.getBoundingClientRect();
|
|
33
|
-
} else {
|
|
34
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
35
|
-
// @ts-expect-error
|
|
36
|
-
size = viewRef.current.unstable_getBoundingClientRect();
|
|
37
|
-
}
|
|
28
|
+
// getBoundingClientRect became stable in RN 0.83
|
|
29
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
30
|
+
const view = viewRef.current;
|
|
31
|
+
const size = "getBoundingClientRect" in view ? view.getBoundingClientRect() : view.unstable_getBoundingClientRect();
|
|
38
32
|
return RNWebGPU.MakeWebGPUCanvasContext(contextId, size.width, size.height);
|
|
39
33
|
}
|
|
40
34
|
}));
|
|
@@ -48,5 +42,5 @@ export const Canvas = /*#__PURE__*/forwardRef(({
|
|
|
48
42
|
contextId: contextId,
|
|
49
43
|
transparent: !!transparent
|
|
50
44
|
}));
|
|
51
|
-
}
|
|
45
|
+
};
|
|
52
46
|
//# sourceMappingURL=Canvas.js.map
|
package/lib/module/Canvas.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["React","
|
|
1
|
+
{"version":3,"names":["React","useImperativeHandle","useRef","useState","View","WebGPUNativeView","CONTEXT_COUNTER","generateContextId","Canvas","transparent","ref","props","viewRef","contextId","_","getContextId","getNativeSurface","RNWebGPU","getContext","contextName","Error","current","view","size","getBoundingClientRect","unstable_getBoundingClientRect","MakeWebGPUCanvasContext","width","height","createElement","_extends","collapsable","style","flex"],"sourceRoot":"../../src","sources":["Canvas.tsx"],"mappings":";AAAA,OAAOA,KAAK,IAAIC,mBAAmB,EAAEC,MAAM,EAAEC,QAAQ,QAAQ,OAAO;AAEpE,SAASC,IAAI,QAAQ,cAAc;AAEnC,OAAOC,gBAAgB,MAAM,6BAA6B;AAE1D,IAAIC,eAAe,GAAG,CAAC;AACvB,SAASC,iBAAiBA,CAAA,EAAG;EAC3B,OAAOD,eAAe,EAAE;AAC1B;AA0CA,OAAO,MAAME,MAAM,GAAGA,CAAC;EAAEC,WAAW;EAAEC,GAAG;EAAE,GAAGC;AAAmB,CAAC,KAAK;EACrE,MAAMC,OAAO,GAAGV,MAAM,CAAC,IAAI,CAAC;EAC5B,MAAM,CAACW,SAAS,EAAEC,CAAC,CAAC,GAAGX,QAAQ,CAAC,MAAMI,iBAAiB,CAAC,CAAC,CAAC;EAC1DN,mBAAmB,CAACS,GAAG,EAAE,OAAO;IAC9BK,YAAY,EAAEA,CAAA,KAAMF,SAAS;IAC7BG,gBAAgB,EAAEA,CAAA,KAAM;MACtB,OAAOC,QAAQ,CAACD,gBAAgB,CAACH,SAAS,CAAC;IAC7C,CAAC;IACDK,UAAUA,CAACC,WAAqB,EAA0B;MACxD,IAAIA,WAAW,KAAK,QAAQ,EAAE;QAC5B,MAAM,IAAIC,KAAK,CAAC,iCAAiCD,WAAW,EAAE,CAAC;MACjE;MACA,IAAI,CAACP,OAAO,CAACS,OAAO,EAAE;QACpB,MAAM,IAAID,KAAK,CAAC,0CAA0C,CAAC;MAC7D;MACA;MACA;MACA,MAAME,IAAI,GAAGV,OAAO,CAACS,OAAc;MACnC,MAAME,IAAI,GACR,uBAAuB,IAAID,IAAI,GAC3BA,IAAI,CAACE,qBAAqB,CAAC,CAAC,GAC5BF,IAAI,CAACG,8BAA8B,CAAC,CAAC;MAC3C,OAAOR,QAAQ,CAACS,uBAAuB,CACrCb,SAAS,EACTU,IAAI,CAACI,KAAK,EACVJ,IAAI,CAACK,MACP,CAAC;IACH;EACF,CAAC,CAAC,CAAC;EAEH,oBACE5B,KAAA,CAAA6B,aAAA,CAACzB,IAAI,EAAA0B,QAAA;IAACC,WAAW,EAAE,KAAM;IAACrB,GAAG,EAAEE;EAAQ,GAAKD,KAAK,gBAC/CX,KAAA,CAAA6B,aAAA,CAACxB,gBAAgB;IACf2B,KAAK,EAAE;MAAEC,IAAI,EAAE;IAAE,CAAE;IACnBpB,SAAS,EAAEA,SAAU;IACrBJ,WAAW,EAAE,CAAC,CAACA;EAAY,CAC5B,CACG,CAAC;AAEX,CAAC","ignoreList":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Canvas.d.ts","sourceRoot":"","sources":["../../../commonjs/Canvas.js"],"names":[],"mappings":""}
|
|
1
|
+
{"version":3,"file":"Canvas.d.ts","sourceRoot":"","sources":["../../../commonjs/Canvas.js"],"names":[],"mappings":";AAgBA;;;;QAoCC"}
|
|
@@ -1,3 +1,8 @@
|
|
|
1
|
-
export
|
|
1
|
+
export function Canvas({ transparent, ref, ...props }: {
|
|
2
|
+
[x: string]: any;
|
|
3
|
+
transparent: any;
|
|
4
|
+
ref: any;
|
|
5
|
+
}): React.CElement<import("react-native").ViewProps, View>;
|
|
6
|
+
import { View } from "react-native";
|
|
2
7
|
import React from "react";
|
|
3
8
|
//# sourceMappingURL=Canvas.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Canvas.d.ts","sourceRoot":"","sources":["../../../module/Canvas.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Canvas.d.ts","sourceRoot":"","sources":["../../../module/Canvas.js"],"names":[],"mappings":"AAQO;;;;2DAoCN;qBA1CoB,cAAc;kBAD0B,OAAO"}
|
|
@@ -26,8 +26,10 @@ export interface CanvasRef {
|
|
|
26
26
|
getContext(contextName: "webgpu"): RNCanvasContext | null;
|
|
27
27
|
getNativeSurface: () => NativeCanvas;
|
|
28
28
|
}
|
|
29
|
-
|
|
29
|
+
interface CanvasProps extends ViewProps {
|
|
30
30
|
transparent?: boolean;
|
|
31
|
-
|
|
31
|
+
ref?: React.Ref<CanvasRef>;
|
|
32
|
+
}
|
|
33
|
+
export declare const Canvas: ({ transparent, ref, ...props }: CanvasProps) => React.JSX.Element;
|
|
32
34
|
export {};
|
|
33
35
|
//# sourceMappingURL=Canvas.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Canvas.d.ts","sourceRoot":"","sources":["../../../src/Canvas.tsx"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"Canvas.d.ts","sourceRoot":"","sources":["../../../src/Canvas.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAgD,MAAM,OAAO,CAAC;AACrE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAU9C,OAAO,CAAC,MAAM,CAAC;IACb,IAAI,QAAQ,EAAE;QACZ,GAAG,EAAE,GAAG,CAAC;QACT,MAAM,EAAE,OAAO,CAAC;QAChB,gBAAgB,EAAE,CAAC,SAAS,EAAE,MAAM,KAAK,YAAY,CAAC;QACtD,uBAAuB,EAAE,CACvB,SAAS,EAAE,MAAM,EACjB,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,KACX,eAAe,CAAC;QACrB,YAAY,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,eAAe,GAAG,WAAW,KAAK,MAAM,CAAC;QACvE,iBAAiB,EAAE,OAAO,iBAAiB,CAAC;KAC7C,CAAC;CACH;AAED,KAAK,cAAc,GAAG,MAAM,CAAC;AAE7B,MAAM,WAAW,YAAY;IAC3B,OAAO,EAAE,cAAc,CAAC;IACxB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,MAAM,eAAe,GAAG,gBAAgB,GAAG;IAC/C,OAAO,EAAE,MAAM,IAAI,CAAC;CACrB,CAAC;AAEF,MAAM,WAAW,SAAS;IACxB,YAAY,EAAE,MAAM,MAAM,CAAC;IAC3B,UAAU,CAAC,WAAW,EAAE,QAAQ,GAAG,eAAe,GAAG,IAAI,CAAC;IAC1D,gBAAgB,EAAE,MAAM,YAAY,CAAC;CACtC;AAED,UAAU,WAAY,SAAQ,SAAS;IACrC,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,GAAG,CAAC,EAAE,KAAK,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;CAC5B;AAED,eAAO,MAAM,MAAM,GAAI,gCAAgC,WAAW,sBAuCjE,CAAC"}
|
package/package.json
CHANGED
package/src/Canvas.tsx
CHANGED
|
@@ -1,11 +1,6 @@
|
|
|
1
|
-
import React, {
|
|
2
|
-
forwardRef,
|
|
3
|
-
useImperativeHandle,
|
|
4
|
-
useRef,
|
|
5
|
-
useState,
|
|
6
|
-
} from "react";
|
|
1
|
+
import React, { useImperativeHandle, useRef, useState } from "react";
|
|
7
2
|
import type { ViewProps } from "react-native";
|
|
8
|
-
import {
|
|
3
|
+
import { View } from "react-native";
|
|
9
4
|
|
|
10
5
|
import WebGPUNativeView from "./WebGPUViewNativeComponent";
|
|
11
6
|
|
|
@@ -49,10 +44,12 @@ export interface CanvasRef {
|
|
|
49
44
|
getNativeSurface: () => NativeCanvas;
|
|
50
45
|
}
|
|
51
46
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
47
|
+
interface CanvasProps extends ViewProps {
|
|
48
|
+
transparent?: boolean;
|
|
49
|
+
ref?: React.Ref<CanvasRef>;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export const Canvas = ({ transparent, ref, ...props }: CanvasProps) => {
|
|
56
53
|
const viewRef = useRef(null);
|
|
57
54
|
const [contextId, _] = useState(() => generateContextId());
|
|
58
55
|
useImperativeHandle(ref, () => ({
|
|
@@ -67,16 +64,13 @@ export const Canvas = forwardRef<
|
|
|
67
64
|
if (!viewRef.current) {
|
|
68
65
|
throw new Error("[WebGPU] Cannot get context before mount");
|
|
69
66
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
// @ts-expect-error
|
|
78
|
-
size = viewRef.current.unstable_getBoundingClientRect();
|
|
79
|
-
}
|
|
67
|
+
// getBoundingClientRect became stable in RN 0.83
|
|
68
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
69
|
+
const view = viewRef.current as any;
|
|
70
|
+
const size =
|
|
71
|
+
"getBoundingClientRect" in view
|
|
72
|
+
? view.getBoundingClientRect()
|
|
73
|
+
: view.unstable_getBoundingClientRect();
|
|
80
74
|
return RNWebGPU.MakeWebGPUCanvasContext(
|
|
81
75
|
contextId,
|
|
82
76
|
size.width,
|
|
@@ -94,4 +88,4 @@ export const Canvas = forwardRef<
|
|
|
94
88
|
/>
|
|
95
89
|
</View>
|
|
96
90
|
);
|
|
97
|
-
}
|
|
91
|
+
};
|