VTK windows not showing until I move mouse

Probably a simple question, but some of my windows are not showing anything until I move/click my mouse over them. It could be due to the setup that I have the code in, but the code below shows a snippet.

What am I forgetting / missing (if anything)?

Setup of Renderer etc.

// Where the VTK will be placed in the HTML
document.getElementById(htmlTag).innerHTML = "";
const container = document.querySelector('#' + htmlTag)

// VTK renderWindow/renderer
this.renderWindow = vtk.Rendering.Core.vtkRenderWindow.newInstance();
this.renderer = vtk.Rendering.Core.vtkRenderer.newInstance();
this.renderWindow.addRenderer(this.renderer);

// WebGL.OpenGL impl
this.openGLRenderWindow = vtk.Rendering.OpenGL.vtkRenderWindow.newInstance();
this.openGLRenderWindow.setContainer(container);
const { width, height } = container.getBoundingClientRect();
this.openGLRenderWindow.setSize(width, height);
this.renderWindow.addView(this.openGLRenderWindow);

// Interactor
this.interactor = vtk.Rendering.Core.vtkRenderWindowInteractor.newInstance();
this.style = vtk.Interaction.Style.vtkInteractorStyleTrackballCamera.newInstance();

this.interactor.setInteractorStyle(this.style)

this.interactor.setView(this.openGLRenderWindow)
this.interactor.initialize();
this.interactor.bindEvents(container);

if (isImage){
    this.renderer.setBackground([245, 245, 245])
}
this.renderer.resetCamera()
this.interactor.initialize()

Setting up the image

actor = vtk.Rendering.Core.vtkImageSlice.newInstance();
mapper = vtk.Rendering.Core.vtkImageMapper.newInstance();

this.renderer.addActor(actor)

mapper.setInputData(image_array);
mapper.setKSlice(30);
actor.setMapper(mapper);

this.renderWindow.render()

Two possible issues:

  • when adding the actor, be sure to call resetCamera() afterwards so that resetCamera() can take into consideration the bounds of all actors in the scene.
  • Ensure that you are calling render() when you add data, and not on some user event (e.g. mousemove)
1 Like