一个用于 vanilla JavaScript、jQuery、Vue 和 React 的图像标注插件
A JavaScript image annotation plugin that creates Flickr-like comment annotations on images. Users can draw rectangular regions on images, add text notes, and persist annotations via callbacks or AJAX.
Works standalone (vanilla JS), or with jQuery, React, or Vue. Framework adapters are tree-shakeable — only the one you import gets bundled.
npm install annotate-imagejQuery, React, and Vue are optional peer dependencies. Install only what you use:
# For jQuery projects
npm install jquery
# For React projects
npm install react react-dom
# For Vue projects
npm install vue…import { annotate } from 'annotate-image';
import 'annotate-image/css';
const instance = annotate(document.getElementById('myImage'), {
editable: true,
notes: [],
});$(function() {
$('#myImage').annotateImage({
editable: true,
notes: [/* ... */],
});
});
// Destroy
$('#myImage').annotateImage('destroy');Or as an ES module:
import 'annotate-image/jquery';
import 'annotate-image/css';import { useRef } from 'react';
import { AnnotateImage } from 'annotate-image/react';
import type { AnnotateImageRef } from 'annotate-image/react';
import 'annotate-image/css';
function App() {
const ref = useRef(null);
return (
<>
console.log(notes)}
onSave={(note) => console.log('Saved:', note)}
onDelete={(note) => console.log('Deleted:', note)}
/>
ref.current?.add()}>Add Note
console.log(ref.current?.getNotes())}>
Get Notes
);
}
console.log(notes)"
@save="(note) => console.log('Saved:', note)"
@delete="(note) => console.log('Deleted:', note)"
/>
Add Note
Get Notes
annotate(img, options?)Creates an annotation layer on an image element. Returns an AnnotateImage instance.
| Option | Type | Default | Description |
|---|---|---|---|
editable |
boolean |
true |
Enable annotation editing |
notes |
AnnotationNote[] |
[] |
Initial annotations |
api |
AnnotateApi |
— | Server persistence (see below) |
onChange |
(notes: NoteData[]) => void |
— | Called after any notes mutation |
onSave |
(note: NoteData) => void |
— | Called after a note is saved |
onDelete |
(note: NoteData) => void |
— | Called after a note is deleted |
onLoad |
(notes: NoteData[]) => void |
— | Called after notes are loaded |
onError |
(ctx: AnnotateErrorContext) => void |
— | Called on API errors (defaults to console.error) |
autoResize |
boolean |
true |
Re-scale annotations when the container resizes |
theme |
string |
— | CSS theme name; sets data-theme on the canvas for variable scoping |
AnnotateApiEach field accepts a URL string (default fetch behavior) or a function for full control. Omit api entirely for static-only mode.
{
load?: string | (() => Promise),
save?: string | ((note: NoteData) => Promise),
delete?: string | ((note: NoteData) => Promise),
}| Method | Returns | Description |
|---|---|---|
add() |
boolean |
Enter add-note mode. Returns false if already editing. |
clear() |
void |
Remove all annotations. |
destroy() |
void |
Tear down completely. Idempotent. |
getNotes() |
NoteData[] |
Current annotations (internal fields stripped). |
load() |
void |
Rebuild views from the current notes array. |
// Full annotation (includes internal fields)
interface AnnotationNote {
id: string;
top: number;
left: number;
width: number;
height: number;
text: string;
editable: boolean;
}
// Annotation data passed to callbacks (no internal fields)
type NoteData = Omit;AnnotateImageProps)| Prop | Type | Default | Description |
|---|---|---|---|
src |
string |
— | Image URL (required) |
width |
number |
— | Image width in pixels |
height |
number |
— | Image height in pixels |
notes |
AnnotationNote[] |
[] |
Initial annotations |
editable |
boolean |
true |
Enable editing |
onChange |
(notes: NoteData[]) => void |
— | Notes changed |
onSave |
(note: NoteData) => void |
— | Note saved |
onDelete |
(note: NoteData) => void |
— | Note deleted |
onLoad |
(notes: NoteData[]) => void |
— | Notes loaded |
onError |
(ctx: AnnotateErrorContext) => void |
— | Error occurred |
autoResize |
boolean |
true |
Re-scale on container resize |
AnnotateImageRef)| Method | Returns | Description |
|---|---|---|
add() |
void |
Enter add-note mode |
clear() |
void |
Remove all annotations |
destroy() |
void |
Tear down the instance |
getNotes() |
NoteData[] |
Get current annotations |
| Prop | Type | Default | Description |
|---|---|---|---|
src |
String |
— | Image URL (required) |
width |
Number |
— | Image width in pixels |
height |
Number |
— | Image height in pixels |
notes |
AnnotationNote[] |
— | Initial annotations |
editable |
Boolean |
true |
Enable editing |
autoResize |
Boolean |
true |
Re-scale on container resize |
| Event | Payload | Description |
|---|---|---|
change |
NoteData[] |
Notes changed |
save |
NoteData |
Note saved |
delete |
NoteData |
Note deleted |
load |
NoteData[] |
Notes loaded |
error |
AnnotateErrorContext |
Error occurred |
| Method | Returns | Description |
|---|---|---|
add() |
void |
Enter add-note mode |
clear() |
void |
Remove all annotations |
destroy() |
void |
Tear down the instance |
getNotes() |
NoteData[] |
Get current annotations |
notes |
Ref |
Reactive current notes |
The plugin automatically detects the rendered image size and scales annotations accordingly. Annotation coordinates are always stored in natural (original) image pixels, so the same data works regardless of display size.
max-width: 500px) are respected automaticallywidth="400") work as expectedResizeObserver — annotations reposition when the container resizesautoResize: false to disable dynamic resizing (annotations scale once at initialization)// Works automatically — no configuration needed
annotate(document.getElementById('myImage'), {
notes: [/* coordinates in natural image pixels */],
});
// Disable dynamic resizing
annotate(document.getElementById('myImage'), {
autoResize: false,
notes: [/* ... */],
});The plugin uses CSS custom properties for all visual styling. Override these variables in your CSS to create custom themes.
theme optionSet theme in options to add a data-theme attribute to the canvas element, enabling CSS scoping:
annotate(document.getElementById('myImage'), {
theme: 'dark',
notes: [/* ... */],
});.image-annotate-canvas[data-theme="dark"] {
--image-annotate-area-border: rgba(255, 255, 255, 0.7);
--image-annotate-area-bg: rgba(100, 149, 237, 0.15);
--image-annotate-note-bg: #2a2a3e;
--image-annotate-note-text: #e0e0f0;
--image-annotate-note-radius: 4px;
--image-annotate-note-shadow: 0 2px 8px rgba(0, 0, 0, 0.4);
/* ... see demo for full example */
}Multiple instances on the same page can use different themes.
Button icons (save, delete, cancel) use CSS mask-image, so their color automatically follows --image-annotate-button-text. Dark themes just need to set the text color — no icon overrides required.
Set on .image-annotate-canvas:
| Variable | Default | Description |
|---|---|---|
--image-annotate-canvas-border |
none |
Border around the canvas |
--image-annotate-font-family |
Verdana, sans-serif |
Font for notes and buttons |
--image-annotate-font-size |
12px |
Font size for notes and buttons |
--image-annotate-area-border |
#000 |
Annotation rectangle border color |
--image-annotate-area-inner-border |
#fff |
Annotation inner border color |
--image-annotate-area-bg |
transparent |
Annotation rectangle fill |
--image-annotate-area-border-width |
1px |
Annotation border thickness |
--image-annotate-area-border-style |
solid |
Annotation border style |
--image-annotate-area-radius |
0 |
Annotation border-radius |
--image-annotate-hover-color |
yellow |
Hover border color (read-only) |
--image-annotate-hover-editable-color |
#00ad00 |
Hover border color (editable) |
--image-annotate-hover-bg |
transparent |
Hover background fill |
--image-annotate-note-bg |
#e7ffe7 |
Tooltip background |
--image-annotate-note-border |
#397f39 |
Tooltip border |
--image-annotate-note-text |
#000 |
Tooltip text color |
--image-annotate-note-radius |
0 |
Tooltip border-radius |
--image-annotate-note-shadow |
none |
Tooltip box-shadow |
--image-annotate-note-max-width |
300px |
Tooltip max width |
--image-annotate-edit-bg |
#fffee3 |
Edit form background |
--image-annotate-edit-border |
#000 |
Edit form border |
--image-annotate-edit-radius |
0 |
Edit form border-radius |
--image-annotate-edit-shadow |
none |
Edit form box-shadow |
--image-annotate-edit-max-width |
300px |
Edit form max width |
--image-annotate-button-bg |
#fff |
Button background |
--image-annotate-button-bg-hover |
#eee |
Button hover background |
--image-annotate-button-border |
#ccc |
Button border |
--image-annotate-button-text |
#000 |
Button text and icon color |
--image-annotate-add-bg |
rgba(0,0,0,0.4) |
Add Note button background |
--image-annotate-add-bg-hover |
rgba(0,0,0,0.6) |
Add Note button hover background |
--image-annotate-add-border |
1px solid rgba(255,255,255,0.5) |
Add Note button border |
--image-annotate-add-radius |
4px |
Add Note button border-radius |
Each entry point (annotate-image, annotate-image/jquery, annotate-image/react, annotate-image/vue) is a separate bundle. Importing one does not pull in the others. Unused framework adapters are excluded automatically by bundlers that support package exports.
The onError callback receives a context object:
interface AnnotateErrorContext {
type: 'load' | 'save' | 'delete';
error: Error;
note?: AnnotationNote;
}If no onError callback is provided, errors are logged to the console.
The plugin supports keyboard navigation:
role="button" and tabindex attributesTry the live examples online, or run the demo server locally:
npm run demoThis opens a browser at http://localhost:8080/demo/index.html with links to demos including static annotations, AJAX loading, vanilla JS, React, Vue, multiple instances, and CSS theming.
npm install
npm run build # Type-check, bundle, minify
npm run build:check # Type-check only
npm test暂无开放 Issues,或尚未同步最近议题。