1.2.1 • Published 5 months ago

react-img-annotation v1.2.1

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

react-img-annotation

This is a package that helps with annotating data and viewing annotations.

It supports creating rectangles and attaching labels/fields to said rectangles.

NOTE:

This package was previously implemented differently using fabricjs, now it has been migrated to konva. If you want to use the previous implementation you can use version 1.1.61

Dependencies

for this library you need konva, react-konva, react-konva-utils, react-icons

which you can install using npm install konva react-konva react-konva-utils react-icons

Examples

There are two main components in this package: AnnotationsEditor and AnnotationsViewer here is how to use AnnotationsEditor:

import { useState } from "react";
import { AnnotationsEditor } from "react-img-annotation";
import "./App.css";
const initialRectangles = [
  {
    x: 10,
    y: 10,
    width: 100,
    height: 100,
    fill: "transparent",
    id: "rect1",
  },
  {
    x: 150,
    y: 150,
    width: 300,
    height: 100,
    fill: "transparent",
    id: "rect2",
  },
];

const App = () => {
  const [rectangles, setRectangles] = useState(initialRectangles);
  const [selectedId, selectShape] = useState(null);

  const checkDeselect = (e) => {
    // deselect when clicked on empty area
    const clickedOnEmpty = e.target === e.target.getStage();
    if (clickedOnEmpty) {
      selectShape(null);
    }
  };

  return (
    <>
      <AnnotationsEditor
        // initial annotations
        annotations={[]}
        onChange={(newattr) => {
          setRectangles((rectangles) =>
            rectangles.map((rect) => {
              if (rect.id === newattr.id) {
                return { ...rect, ...newattr };
              }
              return rect;
            })
          );
        }}
        onAddAnnotation={(annotation) => {
          setRectangles((rectangles) => [...rectangles, annotation]);
        }}
        onDeleteAnnotation={(deletedId) => {
          console.log(deletedId);
        }}
        onFieldSelectChange={(change) => {
          console.log(change);
        }}
        onAnnotationSelected={(shapeProps) => {
          console.log(shapeProps);
        }}
        options={[
          { label: "field one", value: "3" },
          { label: "field two", value: "4" },
        ]}
        image={"url/to/image"}
        width={1144}
        height={643}
        disabledOptions={["field one"]}
        highlightedAnnotations={[]}
        // default rect styling, check konva rect for all props
        defaultAnnotationStyle={{
          stroke: "red",
          fill: "transparent",
        }}
        // higlighted rect styling, check konva rect for all props
        highlightedAnnotationStyle={{
          stroke: "blue",
          fill: "transparent",
        }}
        // transformer styling, check konva transformer for all props
        transformerStyle={{ anchorFill: "green", borderStroke: "red" }}
        showLabels={true} // shows labels on top of the rectangles
        rectLabelPositionFunc={(selectedOptions, currentPos) => {
          // control position of labels (default is on top)
          return {
            top: 25,
            left: 25,
          };
        }}
      />
    </>
  );
};

export default App;

Here is how to use AnnotationsViewer:

import { useState } from "react";
import "./App.css";
import { AnnotationsViewer } from "react-img-annotation";
const initialRectangles = [
  {
    x: 10,
    y: 10,
    width: 100,
    height: 100,
    fill: "transparent",
    id: "rect1",
    confidence: 0.2,
    fields: [
      { name: "rect1 field1", confidence: 0.32323 },
      { name: "rect1 field2", confidence: 0.3123123 },
      { name: "rect1 field3", confidence: 0.35435 },
      { name: "rect1 field4", confidence: 0.344 },
    ],
  },
  {
    x: 150,
    y: 150,
    width: 300,
    height: 100,
    fill: "transparent",
    id: "rect2",
    confidence: 0.9,
    fields: [{ name: "rect2 field1", confidence: 0.9 }],
  },
];

const App = () => {
  const [rectangles, setRectangles] = useState(initialRectangles);

  return (
    <>
      <AnnotationsViewer
        annotations={rectangles}
        image={"url/to/image"}
        width={1144}
        height={643}
        onAnnotationClick={(annotation) => console.log(annotation)}
      />
    </>
  );
};

export default App;

Style customization:

Many parts of the package are customizable through classNames:

classNamedescriptionnotes
rect-edit-labels-containercontrols styling for labels container in editor rectanglesdo not change position, top or left.
rect-edit-labelcontrols styling for labels
rect-edit-menu-buttonmenu button styling
rect-edit-delete-buttondelete button styling
rect-view-labellabel styling in viewer rectangles
fields-select-placeholderstyling for fields select placehlder
fields-select-placeholder-inputstyling for select input
fields-select-dropdownstyling for select dropdown
fields-select-no-optionsstyling for when there are no options
fields-select-optionstyling for a single option

License

MIT © mhmodo10

1.2.1

5 months ago

1.2.0

12 months ago

1.1.63

12 months ago

1.1.62

12 months ago

1.1.56

12 months ago

1.1.55

1 year ago

1.1.54

1 year ago

1.1.59

12 months ago

1.1.58

12 months ago

1.1.57

12 months ago

1.1.61

12 months ago

1.1.60

12 months ago

1.1.29

1 year ago

1.1.28

1 year ago

1.1.30

1 year ago

1.1.34

1 year ago

1.1.33

1 year ago

1.1.32

1 year ago

1.1.31

1 year ago

1.1.38

1 year ago

1.1.37

1 year ago

1.1.36

1 year ago

1.1.35

1 year ago

1.1.39

1 year ago

1.1.41

1 year ago

1.1.40

1 year ago

1.1.45

1 year ago

1.1.44

1 year ago

1.1.43

1 year ago

1.1.42

1 year ago

1.1.49

1 year ago

1.1.48

1 year ago

1.1.47

1 year ago

1.1.46

1 year ago

1.1.52

1 year ago

1.1.51

1 year ago

1.1.50

1 year ago

1.1.53

1 year ago

1.1.16

1 year ago

1.1.15

1 year ago

1.1.19

1 year ago

1.1.18

1 year ago

1.1.17

1 year ago

1.1.23

1 year ago

1.1.22

1 year ago

1.1.21

1 year ago

1.1.27

1 year ago

1.1.26

1 year ago

1.1.25

1 year ago

1.1.24

1 year ago

1.1.14

2 years ago

1.1.13

2 years ago

1.1.12

2 years ago

1.1.11

2 years ago

1.1.10

2 years ago

1.1.9

2 years ago

1.1.8

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago