0.2.0 • Published 28 days ago

react-image-label v0.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
28 days ago

A comprehensive component for tagging images. Check out demo 1 and demo 2 for some examples.

Features

  • Bounding Box (Rectangle, Square, and Circle), and Polygon Annotations
  • Add, Edit, Drag, and Delete Annotations
  • Zoom and Scale
  • Changing image on the fly
  • Raw or typed input/output

Usage

Install react-image-label using npm.

npm install react-image-label

Then you can just import the component and its hook:

import { ImageAnnotator, useImageAnnotator } from 'react-image-label';

and use it as below:

const { setHandles, annotator } = useImageAnnotator();
<ImageAnnotator
  setHandles={setHandles}
  naturalSize={true}
  imageUrl={'your-image-url'}
  onReady={annotator => { annotator.drawRectangle() }} />

Now you can draw rectangles on the image by dragging the left mouse button.

Props

The following props can be defined on ImageAnnotator:

PropTypeDescriptionDefault
imageUrl *stringUse a state for image url if you want to change it on the fly
shapesShape[] \| any[]Annotations being displayed on load
naturalSizebooleanTo show image in its natural sizefalse
widthnumberMaximum width without breaking the aspect ratio at initial loading (when naturalSize is false)
heightnumberMaximum height without breaking the aspect ratio at initial loading (when naturalSize is false)
discRadiusnumberThe radius of the green discs in edit mode5
onAddedShape => anyWhen an annotation is drawn (see Annotations with Categories)
onContextMenuShape => anyWhen an annotation is right-clicked (see Annotations with Categories)
onReadyAnnotatorHandles => anyWhen the component is mounted

(*) required props

Handles

You can access the handles using the annotator object as follows:

<button onClick={() => { annotator.drawCircle() }}>Draw Circle</button>

Below is a list of all handles:

PropTypeDescription
drawCircle() => voidAllows drawing circles by dragging the left mouse button
drawRectangle() => voidAllows drawing rectangles by dragging the left mouse button (keep the shift key to draw square)
drawPolygon() => voidAllows drawing polygons by clicking and double-clicking
stop() => voidStops draw/edit/drag mode
edit(id: number) => voidThe annotation identified by id can be edited and dragged
stopEdit() => voidStops editing and dragging
updateCategories(id: number, categories: string[]) => voidUpdates the categories associated with the annotation identified by id
zoom(factor: number) => voidMultiplies the dimensions by factor
getShapes() => Shape[]Gets all annotations
containerHTMLDivElementThe div wrapping the SVG

Annotations with Categories

To attach one or more categories to an annotation, utilize onAdded and onContextMenu props being called when an annotation is drawn and right-clicked, respectively. Use shape.categories to get the previous categories as string[]:

const showCategoriesDialog = (shape) => {
  console.log(shape.id) // 1
  console.log(shape.getCenterWithOffset()) // { X: 247.5, Y: 193 }
  console.log(shape.categories) // Array []
  // Show a category selection component
}

return (
  <ImageAnnotator
    onAdded={showCategoriesDialog}
    onContextMenu={showCategoriesDialog}
    ...
);

Finally, call annotator.updateCategories to update the categories of the annotation.

Contributing

  • Fork the project.
  • Make changes.
  • Run the project in development mode: npm run ladle.
  • Test your changes using annotator.stories.tsx or your own Stories (*.stories.tsx).
  • Update README with appropriate docs.
  • Commit and PR

Dependencies

The package is dependent on SVG.js through react-svgdotjs package. The following peer dependencies must be specified by your project in order to avoid version conflicts: react, react-dom. NPM will not automatically install these for you but it will show you a warning message with instructions on how to install them.

0.2.0

28 days ago

0.1.5

1 month ago

0.1.4

2 months ago

0.1.3

2 months ago

0.1.2

2 months ago

0.1.1

2 months ago

0.1.0

2 months ago

0.0.9

2 months ago

0.0.8

2 months ago

0.0.7

2 months ago

0.0.6

2 months ago

0.0.3

2 months ago

0.0.2

2 months ago

0.0.5

2 months ago

0.0.4

2 months ago

0.0.1

3 months ago

0.0.0

3 months ago