import React from 'react' import type { ViewProps } from 'react-native' import { getHostComponent } from 'react-native-nitro-modules' // Generated by `nitrogen` from src/specs/ThorView.nitro.ts import ThorViewConfig from '../nitrogen/generated/shared/json/ThorViewConfig.json' import type { ThorBackend, ThorShape, ThorViewMethods, ThorViewProps, } from './specs/ThorView.nitro' const NativeThorView = getHostComponent( 'ThorView', () => ThorViewConfig ) /** * The raw native host component. It takes a `shapes` array prop directly (no * children), so most apps should just use `` instead. * * For the off-JS-thread fast path (high-frequency updates from a worklet — e.g. * a reanimated `useFrameCallback` or a camera frame processor), do NOT animate * the `shapes` prop with reanimated `useAnimatedProps`: Nitro views require * JSI-backed props and reanimated commits `folly::dynamic`, which aborts * natively in `RawValue::castValue`. Instead grab the view's HybridObject via * `hybridRef` and call its imperative {@link ThorViewMethods.drawShapes} method * from the worklet — it rasterizes on the calling thread, never touching JS. * See `apps/example/src/WorkletDemo.tsx`. */ export const ThorCanvasNative = NativeThorView export interface CanvasProps extends ViewProps { children?: React.ReactNode /** Rendering backend; defaults to 'cpu'. 'gpu' uses ThorVG's GL engine (Android). */ backend?: ThorBackend } /** * The ThorVG-backed drawing surface. Render shape components as children: * ```tsx * * * * * ``` */ export function Canvas({ children, backend, ...viewProps }: CanvasProps): React.JSX.Element { // Single pass: collect shape descriptors into one array (no intermediate // arrays from toArray/map/filter, no per-shape spread objects). const shapes: ThorShape[] = [] React.Children.forEach(children, (child) => { const shape = childToShape(child) if (shape !== null) shapes.push(shape) }) return } function childToShape(child: React.ReactNode): ThorShape | null { if (!React.isValidElement(child)) { return null } const componentType = child.type as { shapeType?: ThorShape['type'] } const shapeType = componentType?.shapeType if (shapeType === undefined) { return null } const p = child.props as Record const fill = p.fill as string | undefined const stroke = p.stroke as string | undefined const strokeWidth = p.strokeWidth as number | undefined switch (shapeType) { case 'rect': return { type: 'rect', x: p.x as number | undefined, y: p.y as number | undefined, width: p.width as number | undefined, height: p.height as number | undefined, rx: p.rx as number | undefined, ry: p.ry as number | undefined, fill, stroke, strokeWidth, } case 'circle': return { type: 'circle', cx: p.cx as number | undefined, cy: p.cy as number | undefined, radius: p.radius as number | undefined, fill, stroke, strokeWidth, } case 'line': return { type: 'line', x1: p.x1 as number | undefined, y1: p.y1 as number | undefined, x2: p.x2 as number | undefined, y2: p.y2 as number | undefined, fill, stroke, strokeWidth, } case 'polygon': return { type: 'polygon', points: (p.points as number[] | undefined) ?? [], closed: p.closed as boolean | undefined, fill, stroke, strokeWidth, } default: return null } }