Annotation Rects & Quads
Annotations take up a certain amount of space on a document page. Their "body" in the document is commonly represented as a rectangle or an array of rectangles. These rectangles are provided as either a Rect or Quad object in WebViewer.
Most annotations will use a Rect as their body, whereas Quad objects are typically for annotations that mark up text. With a Quad, you can convert it to a Rect and vice-versa. With a Rect, you can get more information from the shape such as its dimensions or checking for intersections with other rectangles.
Rect
const { Core } = instance;
const { Annotations, Math } = Core;
const rectAnnot = new Annotations.RectangleAnnotation({
PageNumber: 1,
X: 0,
Y: 0,
Width: 100,
Height: 50,
});
const rect = rectAnnot.getRect();
const width = rect.getWidth();
rect.translate(5, 5); // Move right and down by 5 pixels
rect.x2 *= 2;
rectAnnot.setRect(rect);Collisions
There are two APIs for collision detection. You can use either intersect or contains with another rectangle to check if the two rectangle bodies overlap. The difference, however, is that contains strictly checks whether one rectangle is inside another.
TransformationBuilder
Occasionally, you may need to transform a rectangle a certain way or perhaps you need a special transformation for your custom annotation. In this case, you can transform a Rect with a Matrix.
However, matrices are difficult to work with and are not easily recognizable. WebViewer provides a TransformationBuilder to assist in building a transformation out. The operations you set with the TransformationBuilder is in the order you would perform them (not in reverse order).
Rect.getCenterPoint Rect.transform TransformationBuilder TransformationBuilder.translate TransformationBuilder.rotate TransformationBuilder.getFinalTransform
If you want to use the transformation matrix with the Canvas context, you can use the toTransform API on the Matrix to get an array of values to be used with setTransform on the context.
Quad
Quads are commonly found with annotations that mark up text. In many of the cases, these annotations, like HighlightAnnotation, contain a list of Quad objects representing the lines of text that are marked.
Conversion between Rect
Quad objects are limited in functionality, but you can convert them to a Rect where you will gain access to its APIs. After you are done, you can convert it back to a Quad.
To convert it back:
Next steps
See how Rect and Quad objects are used in some of our annotation guides, specifically RectangleAnnotation and HighlightAnnotation!
Last updated
Was this helpful?