How to remove labels from a renderer

So, I have been generating multiple labels by using vtkLabelWidget in my vtk js project after a button is clicked. Now, I want to remove those labels from the view, so far I have tried creating the following events:

  • Cleaning the SVG Helpers after a button is clicked.
  • Removing the widgets from the widgetManager.
  • Setting the renderer from widget manager to null.

Any clue on how to implement this? Is there a general method I can use to delete all of the labels from my renderer?

Did you check the example ?

Note that for performance reasons, you might want to consider the PolyLineWidget without showing lines.

Didn’t check it before, seems to work at first, thank you so much.

Nevertheless, now I am getting a warning message and after two or three seconds an error triggers after calling the method multiple times (let’s say, removing labels in order to add new ones):

RenderWindow.js:231 WARNING: Too many active WebGL contexts. Oldest context will be lost.
TypeError: Cannot set properties of null (setting ‘width’).

Any idea of what this is or could be related to?

Seems to be related to be related to Chromium’s WebGL context limits, just tried setting –max-active-webgl-contexts= to a higher value and seems to crash no so early. But it looks like if I generate new labels, the contexts are accumulated somehow. Do labels count as WebGL context?

That error is related to creating lots of vtkRenderWindows. I’d suggest using techniques to reduce the number of active WebGL contexts in your app, e.g. ManyRenderWindows.

Not sure if it’s due to creating lots of renderWindows since I am using a single fullScreenRenderWindow, and the error only occurs after calling the method which deletes my vtkLabelWidget’s for a third time. Still have not found the issue…

          document.getElementById('btnDel').addEventListener('click', () => {
            widgetManager.addWidget(labelWidget).reset()
            widgetManager.removeWidget(handle);
            svgCleanupCallbacks.get(handle);
            svgCleanupCallbacks.delete(handle);
            handleTextProps.delete(handle);
            handle = null;
})

Am I properly deleting the labels from the renderer? Or could it be related to something else?