Skip to content

ImageAnnotator

The ImageAnnotator is the main entry point into the Annotorious API when using the @annotorious/annotorious package. An object of this type is returned when you call the createImageAnnotator method.

const anno = createImageAnnotator('sample-image');

Instance Fields

element

The HTML element where the Annotorious instance is rendered.

  • Type: HTMLDivElement

Instance Methods

addAnnotation

anno.addAnnotation(annotation);

Add an annotation programmatically.

ArgumentType
annotationImageAnnotation

cancelSelected

anno.cancelSelected();

Programmatically cancel the current selection, if any.

canRedo

const canRedo = anno.canRedo();

Tests if there are any re-doable user actions in the undo stack.

Returns: boolean

canUndo

const canUndo = anno.canUndo();

Tests if there are any undoable user actions in the undo stack.

Returns: boolean

clearAnnotations

anno.clearAnnotations();

Delete all annotations from the image.

destroy

anno.destroy();

Destroy this instance of Annotorious, removing the annotation layer from the image.

getAnnotationById

const annotation = anno.getAnnotationById(id);

Returns the annotation with the specified ID.

ArgumentTypeValue
idstringthe annotation ID

Returns: ImageAnnotation | undefined

getAnnotations

const all = anno.getAnnotations();

Returns: ImageAnnotation[]

getSelected

const selected = anno.getSelected();

Returns the currently selected annotations.

Returns: ImageAnnotation[]

getUser

const currentUser = anno.getUser();

Returns: User

loadAnnotations

const loaded = await anno.loadAnnotations(url, replace = false)

Loads annotations from a JSON file at a given URL. The method returns a promise of the fetched annotations, in case you want to perform an action after the they have loaded. The optional replace parameter controls whether the new annotations should replace the current annotations, erasing all existing ones, or get added to them.

ArgumentTypeValueDefault
urlstringthe URL to a JSON annotation file
replacebooleanOptional: set to true to clear the annotation canvas before adding the new annotations.false

Returns: Promise<ImageAnnotation[]>

listDrawingTools

const tools = anno.listDrawingTools();

Returns a list of the available drawing tools, including those from registered drawing tool plugins.

Returns: string[]

redo

anno.redo();

Programmatically redo the last undone user action.

removeAnnotation

const removedAnnotation = anno.removeAnnotation(annotationOrId);

Removes the specified annotation from the image.

ArgumentTypeValue
annotationOrIdImageAnnotation | stringThe annotation object or the annotation ID to remove

Returns: ImageAnnotation | undefined

setAnnotations

anno.setAnnotations(annotations, replace = false);

Sets the annotations for the image, optionally replacing any existing ones.

ArgumentTypeValue
annotationsE[]The new set of annotations to display
replaceboolean (optional)If true, the new annotations will replace any existing ones. Default is false.

setFilter

anno.setFilter(filter);

Sets a filter function to control which annotations are displayed.

ArgumentTypeValue
filterFilter | undefinedA function that returns true for annotations that should be displayed. Pass undefined to remove the filter.

setSelected

anno.setSelected(arg, editable?);

Programmatically sets the currently selected annotation(s).

ArgumentTypeValue
argstring | string[] | undefinedThe ID(s) of the annotation(s) to select, or undefined to clear the selection.
editableboolean (optional)If true, the selected annotation(s) will be editable. If the argument is omitted, the current UserSelectAction is applied.

setStyle

anno.setStyle(style);

Sets the drawing style for new annotations.

ArgumentTypeValue
styleDrawingStyleExpression | undefinedAn object that defines the style properties for new annotations. Pass undefined to reset to the default style.

setUser

anno.setUser(user);

Sets the current user for the Annotorious instance.

ArgumentType
userUser

setUserSelectAction

anno.setUserSelectAction(action);

Sets the action to be performed when the user selects an annotation.

ArgumentTypeValue
actionUserSelectActionExpressionAn object that defines the action to be performed on annotation selection.

setVisible

anno.setVisible(visible);

Sets whether the Annotorious instance should be visible or not.

ArgumentTypValue
visiblebooleantrue to make the annotation layer visible, false to hide it.

undo

anno.undo();

Programmatically undo the last user action.

updateAnnotation

const updatedAnnotation = anno.updateAnnotation(annotation);

Updates an existing annotation with new data.

ArgumentTypeValue
annotationImageAnnotationThe updated annotation object.

Returns: ImageAnnotation - the previous version of the annotation.

on

anno.on(event, callback);

Subscribes to a lifecycle event. See the list of available events in the LifecycleEvents type.

ArgumentTypeValue
eventTThe name of the event to subscribe to, e.g. 'annotatioUpdated'.
callbackLifecycleEvents<E>[T]The callback function to be executed when the event is triggered.

off

anno.off(event, callback);

Unsubscribes from a lifecycle event.

ArgumentTypeValue
eventTThe name of the event to unsubscribe from, e.g. 'annotatioUpdated'.
callbackLifecycleEvents<E>[T]The callback function to be removed from the event.