Skip to content

Using NiiVue directly

View as Markdown

The components cover showing images. For everything else, such as drawing, meshes or saving the scene, use the NiiVue instance behind a viewer. All of the NiiVue guides apply to it.

Getting the instance

const nv = useNiivue(scene, 0)
useEffect(() => {
if (nv) nv.isColorbarVisible = true
}, [nv])

useNiivue returns undefined until the viewer exists, so check for it before using it.

Listening for events

useSceneEvent(scene, 'locationChange', (viewerIndex, location) => {
console.log(viewerIndex, location)
})

The hook unsubscribes when the component unmounts. Other events include viewerCreated, viewerRemoved, imageLoaded, volumeAdded, volumeRemoved and error.

Sharing a scene between components

Wrap your components in NvSceneProvider, and any of them can reach the scene with useSceneContext, without passing it down as a prop:

function AddButton() {
const scene = useSceneContext()
return <button onClick={() => scene.addViewer()}>Add viewer</button>
}
export function App() {
const { scene } = useScene()
return (
<NvSceneProvider scene={scene}>
<AddButton />
<NvScene scene={scene} initialLayout="2x2" style={{ height: 600 }} />
</NvSceneProvider>
)
}