useAnnotator
Access the annotator instance for programmatic control of annotations.
Signature
Section titled “Signature”function useAnnotator(): Annotator | null;Returns
Section titled “Returns”Returns the Annotator instance, or null if not yet initialized.
import { useAnnotator } from "annota";import { useEffect } from "react";
function MyComponent() { const annotator = useAnnotator();
useEffect(() => { if (!annotator) return;
// Add an annotation programmatically annotator.addAnnotation({ id: "my-annotation", shape: { type: "point", geometry: { x: 100, y: 100 }, }, }); }, [annotator]);
return null;}Annotator Methods
Section titled “Annotator Methods”Annotation Management
Section titled “Annotation Management”// Add annotationannotator.addAnnotation(annotation: Annotation): void;
// Add multiple annotationsannotator.addAnnotations(annotations: Annotation[]): void;
// Update annotationannotator.updateAnnotation(id: string, updates: Partial<Annotation>): void;
// Delete annotationannotator.deleteAnnotation(id: string): void;
// Get annotation by IDannotator.getAnnotation(id: string): Annotation | undefined;
// Get all annotationsannotator.getAnnotations(): Annotation[];Selection
Section titled “Selection”// Select annotationsannotator.setSelected(ids: string[]): void;
// Clear selectionannotator.clearSelection(): void;
// Get selected annotationsannotator.getSelected(): Annotation[];Events
Section titled “Events”// Listen to eventsannotator.on(event: string, handler: Function): void;
// Remove listenerannotator.off(event: string, handler: Function): void;
// Emit eventannotator.emit(event: string, data: any): void;History
Section titled “History”// Undo last actionannotator.undo(): void;
// Redo last undone actionannotator.redo(): void;
// Check if undo is availableannotator.canUndo(): boolean;
// Check if redo is availableannotator.canRedo(): boolean;Example: Batch Operations
Section titled “Example: Batch Operations”function BatchOperations() { const annotator = useAnnotator();
const createMultiple = () => { if (!annotator) return;
const annotations = Array.from({ length: 10 }, (_, i) => ({ id: `point-${i}`, shape: { type: "point" as const, geometry: { x: i * 50, y: i * 50 }, }, }));
annotator.addAnnotations(annotations); };
const deleteAll = () => { if (!annotator) return;
const all = annotator.getAnnotations(); all.forEach(ann => annotator.deleteAnnotation(ann.id)); };
return ( <div> <button onClick={createMultiple}>Create 10 Points</button> <button onClick={deleteAll}>Delete All</button> </div> );}Example: Event Handling
Section titled “Example: Event Handling”function EventLogger() { const annotator = useAnnotator();
useEffect(() => { if (!annotator) return;
const handleCreate = (ann: Annotation) => { console.log("Created:", ann.id); };
annotator.on("createAnnotation", handleCreate); return () => annotator.off("createAnnotation", handleCreate); }, [annotator]);
return null;}See Also
Section titled “See Also”- useAnnotations — Get all annotations reactively
- useSelection — Access selection state
- Events Guide — Learn about the event system