NitroUI
Use the same canvas in a NitroUI app with nitro-ui-tgfx.
NitroUI apps draw with nitro-ui-tgfx. It shares the
engine, the components, the hooks and the imperative Scene API with
react-native-tgfx, so the rest of these docs apply, with the differences below.
nitro-ui-tgfx is not on npm yet. Until it is, use it from this repository:
apps/nitro-ui-example depends on it as a workspace package.
Install
bun add nitro-ui-tgfx
bun run nitro-ui preparenitro-ui-tgfx needs NitroUI 0.2. NitroUI finds the package through the app's
dependencies, so app.json needs no modules entry. Rebuild the native app
after prepare.
Draw
import { Canvas, Circle, Rect } from 'nitro-ui-tgfx'
export function Hello() {
return (
<Canvas layout={{ flex: 1 }} style={{ background: 0xf2f2f7ff }}>
<Rect x={20} y={20} width={120} height={80} color="#0A84FF" />
<Circle cx={200} cy={140} r={40} color="#FF9500" />
</Canvas>
)
}Differences from React Native
- View props.
Canvastakes NitroUI'slayoutandstyleprops instead of React Native'sstyle. Astyle.backgroundcolor is drawn into the scene, asbackgroundColoris on React Native. - The native view.
TgfxNativeViewhands out its methods throughrefinstead ofhybridRef. - No worklets yet. NitroUI has no worklet runtime and no Reanimated, so
useClock,createClock,useVideo,createVideoPlayer,useCameraFrameandcreateCameraFrameSinkthrow an error that says so. Shared values as props are drawn with the value they have when the canvas commits, and a shared valueviewportis ignored.loadVideoworks: callupdateFrame()from the JS thread while the video plays. - No numeric assets yet. A NitroUI import is an
ImageLoaderor a font name, never a number, so the loaders refuse numeric asset ids. Pass a file path, a URL, adata:URI or base64 instead. - Unmounting. A canvas frees its GPU resources when its native view is collected or the JavaScript runtime reloads, not when it unmounts.
- Press locations. NitroUI's gestures do not report a location yet, so
useCanvasPressneeds the caller to supplynativeEvent.locationXandlocationY.
Android
tgfx renders with Vulkan by default. Add TgfxVulkan=OFF to the app's
android/gradle.properties to use OpenGL ES instead. To build the GPU device
and the font table before the first canvas mounts, call Tgfx.prewarm() from
your NitroActivity before super.onCreate. On iOS, set TgfxPrewarm to
YES in the app's Info.plist.