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 prepare

nitro-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

App.tsx
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. Canvas takes NitroUI's layout and style props instead of React Native's style. A style.background color is drawn into the scene, as backgroundColor is on React Native.
  • The native view. TgfxNativeView hands out its methods through ref instead of hybridRef.
  • No worklets yet. NitroUI has no worklet runtime and no Reanimated, so useClock, createClock, useVideo, createVideoPlayer, useCameraFrame and createCameraFrameSink throw an error that says so. Shared values as props are drawn with the value they have when the canvas commits, and a shared value viewport is ignored. loadVideo works: call updateFrame() from the JS thread while the video plays.
  • No numeric assets yet. A NitroUI import is an ImageLoader or a font name, never a number, so the loaders refuse numeric asset ids. Pass a file path, a URL, a data: 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 useCanvasPress needs the caller to supply nativeEvent.locationX and locationY.

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.

On this page