0.10.24 • Published 5 years ago

react-image-polygon-annotation v0.10.24

Weekly downloads
5
License
MIT
Repository
github
Last release
5 years ago

React Image Annotation

An infinitely customizable image annotation library built on React

Annotation demo

Installation

npm install --save react-image-annotation
# or
yarn add react-image-annotation

Usage

export default class Simple extends Component {
  state = {
    annotations: [],
    annotation: {}
  }

  onChange = (annotation) => {
    this.setState({ annotation })
  }

  onSubmit = (annotation) => {
    const { geometry, data } = annotation

    this.setState({
      annotation: {},
      annotations: this.state.annotations.concat({
        geometry,
        data: {
          ...data,
          id: Math.random()
        }
      })
    })
  }

  render () {
    return (
      <Root>
        <Annotation
          src={img}
          alt='Two pebbles anthropomorphized holding hands'

          annotations={this.state.annotations}

          type={this.state.type}
          value={this.state.annotation}
          onChange={this.onChange}
          onSubmit={this.onSubmit}
        />
      </Root>
    )
  }
}

Props

PropDescriptionDefault
srcImage src attribute
altImage alt attribute
annotationsArray of annotations
valueAnnotation object currently being created. See annotation object
onChangeonChange handler for annotation object
onSubmitonSubmit handler for annotation object
typeSelector type. See custom shapesRECTANGLE
selectorsAn array of selectors. See adding custom selector logic[RectangleSelector, PointSelector, OvalSelector]
activeAnnotationsArray of annotations that will be passed as 'active' (active highlight and shows content)
activeAnnotationComparatorMethod to compare annotation and activeAnnotation item (from props.activeAnnotations). Return true if it's the annotations are equal(a, b) => a === b
disableAnnotationSet to true to disable creating of annotations (note that no callback methods will be called if this is true)false
disableSelectorSet to true to not render Selectorfalse
disableEditorSet to true to not render Editorfalse
disableOverlaySet to true to not render Overlayfalse
renderSelectorFunction that renders Selector ComponentSee custom components
renderEditorFunction that renders Editor ComponentSee custom components
renderHighlightFunction that renders Highlight ComponentSee custom components
renderContentFunction that renders ContentSee custom components
renderOverlayFunction that renders OverlaySee custom components
onMouseUponMouseUp handler on annotation target
onMouseDownonMouseDown handler on annotation target
onMouseMoveonMouseMove handler on annotation target
onClickonClick handler on annotation target

Annotation object

An Annotation object is an object that conforms to the object shape

({
  selection: T.object, // temporary object for selector logic
  geometry: T.shape({ // geometry data for annotation
    type: T.string.isRequired // type is used to resolve Highlighter/Selector renderer
  }),
  // auxiliary data object for application.
  // Content data can be stored here (text, image, primary key, etc.)
  data: T.object
})

Using custom components

Annotation supports renderProps for almost every internal component.

This allows you to customize everything about the the look of the annotation interface, and you can even use canvas elements for performance or more complex interaction models.

  • renderSelector - used for selecting annotation area (during annotation creation)
  • renderEditor - appears after annotation area has been selected (during annotation creation)
  • renderHighlight - used to render current annotations in the annotation interface. It is passed an object that contains the property active, which is true if the mouse is hovering over the higlight
  • renderComponent - auxiliary component that appears when mouse is hovering over the highlight. It is passed an object that contains the annotation being hovered over. { annotation }
  • renderOverlay - Component overlay for Annotation (i.e. 'Click and Drag to Annotate')

You can view the default renderProps here

Note: You cannot use :hover selectors in css for components returned by renderSelector and renderHighlight. This is due to the fact that Annotation places DOM layers on top of these components, preventing triggering of :hover

Using custom shapes

Annotation supports three shapes by default, RECTANGLE, POINT and OVAL.

You can switch the shape selector by passing the appropriate type as a property. Default shape TYPEs are accessible on their appropriate selectors:

import {
  PointSelector,
  RectangleSelector,
  OvalSelector
} from 'react-image-annotation/lib/selectors'

<Annotation
  type={PointSelector.TYPE}
/>

Adding custom selector logic

This is an Advanced Topic

The Annotation API allows support for custom shapes that use custom logic such as polygon or freehand selection. This is done by defining your own selection logic and passing it as a selector in the selectors property.

Selectors are objects that must have the following properties:

  • TYPE - string that uniquely identifies this selector (i.e. RECTANGLE)
  • intersects - method that returns true if the mouse point intersects with the annotation geometry
  • area - method that calculates and returns the area of the annotation geometry
  • methods - object that can contain various listener handlers (onMouseUp, onMouseDown, onMouseMove, onClick). These listener handlers are called when triggered in the annotation area. These handlers must be reducer-like methods - returning a new annotation object depending on the change of the method

You can view a defined RectangleSelector here

Connecting selector logic to Redux/MobX

First see Selectors

You can use Selector methods to connect these method logic to your stores. This is due to the fact that selector methods function as reducers, returning new state depending on the event.

Note that it is not necessary to connect the selector logic with redux/mobx. Connecting the annotation and annotations state is more than enough for most use cases.

License

MIT

0.10.24

5 years ago

0.10.22

5 years ago

0.10.21

5 years ago

0.10.20

5 years ago

0.10.19

5 years ago

0.10.18

5 years ago

0.10.17

5 years ago

0.10.16

5 years ago

0.10.15

5 years ago

0.10.14

5 years ago

0.10.13

5 years ago

0.10.12

5 years ago

0.10.11

5 years ago

0.10.10

5 years ago

0.10.9

5 years ago

0.10.8

5 years ago

0.10.7

5 years ago

0.10.6

5 years ago

0.10.5

5 years ago

0.10.4

5 years ago

0.10.3

6 years ago

0.10.2

6 years ago

0.10.1

6 years ago

0.10.0

6 years ago

0.9.7

6 years ago