For the complete documentation index, see llms.txt. This page is also available as Markdown.

Custom Selection Models in WebViewer

Along with custom annotations, WebViewer also allows customizing the selection models. Selection models are the selection boxes that appear when annotations are selected. This is a good way to make your custom annotation more noticeable or with further customization. For example, if your custom annotation was a certain small size, then the selection model would highlight red until it was resized to a larger size, then it would change to green.

Creating the custom selection model class

As with all custom things, everything begins with a custom class. In this case, we will require a custom selection model class that the annotation class can provide for initialization and rendering.

const { Core } = instance;
const { Annotations } = Core;

class CustomRectangleSelectionModel extends Annotations.SelectionModel {
  constructor(annotation, canModify) {
    super(annotation, canModify);
    if (canModify) {
      const controlHandles = this.getControlHandles();
      // pass the vertex index to each control handle
      controlHandles.push(new Annotations.BoxControlHandle(10, 10, 1, 16));
      controlHandles.push(new Annotations.BoxControlHandle(10, 10, 2, 16));
      controlHandles.push(new Annotations.BoxControlHandle(10, 10, 4, 16));
      controlHandles.push(new Annotations.BoxControlHandle(10, 10, 1, 64));
      controlHandles.push(new Annotations.BoxControlHandle(10, 10, 2, 64));
      controlHandles.push(new Annotations.BoxControlHandle(10, 10, 4, 64));
      controlHandles.push(new Annotations.BoxControlHandle(10, 10, 1, 32));
      controlHandles.push(new Annotations.BoxControlHandle(10, 10, 4, 32));
    }
  }
}

The horizontal and vertical offsets used to initialize BoxControlHandle uses specific values to help determine which point of the box it represents.Horizontal Alignment

  • Left: 1

  • Center: 2

  • Right: 4

Vertical Alignment

  • Top: 16

  • Middle: 32

  • Bottom: 64

As just mentioned, the annotation class needs to provide the selection model as a class as the selection model will be initialized at a later point (when a selection is actually made). The selection model can be set on the selectionModel property on the annotation class. This can be done in two ways:

First, we could set it directly in the constructor if you are working with your own annotation class.

Second, you could also set it directly through the annotation class prototype.

Changing selection testing

A selection model's main purpose is to determine the area, body, or model in which you can select and interact with an annotation. This is determined through the testSelection function on the selection model. This can be overriden with custom logic if you have any or if your custom annotation has an irregular shape.

Defining how it renders

Similar to how annotations render with a draw function, selection models render with drawSelectionOutline.

Changing existing selection testing and rendering

It is also possible to change the existing behavior of existing selection models without having to create a custom class and/or extending from them. This is done with the setCustomHandlers API.

An object containing the custom handler functions that will override the existing behavior must be provided with the class being targeted.

Next steps

Read the full tutorial on how to create a custom triangle annotation and see how much further you can customize with WebViewer!

Last updated

Was this helpful?