1.5.1 • Published 5 months ago

react-img-mapper v1.5.1

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

React Img Mapper

React Component to highlight interactive zones in images

1. Promise to be maintained this repository
2. Built in TypeScript
3. Compatible with Next.js
4. Decreased size of bundled
5. Awesome Documentation
6. Selected area will stay highlighted ( Single & Multiple ) with toggle and reset feature
7. Image Reference in Width, Height and onLoad function to access image properties
8. Responsive Image Mapper

Installation

Package: react-img-mapper

# via npm
npm install react-img-mapper --save

# via yarn
yarn add react-img-mapper

Demo & Examples

Live Demo: Demo

To run the example locally

git clone https://github.com/img-mapper/react-docs.git
npm install
npm start

Then open localhost:3000 in a browser.

If you want to change something and want to make a build file, you just need to run npm run build

Usage

Import the component as you normally do, and add it wherever you like in your JSX views as below:

import React from 'react';
import ImageMapper from 'react-img-mapper';

const Mapper = props => {
  const URL =
    'https://raw.githubusercontent.com/img-mapper/react-docs/master/src/assets/example.jpg';
  const MAP = {
    name: 'my-map',
    // GET JSON FROM BELOW URL AS AN EXAMPLE AND PUT IT HERE
    areas: 'https://raw.githubusercontent.com/img-mapper/react-docs/master/src/assets/example.json',
  };

  return <ImageMapper src={URL} map={MAP} />;
};

export default Mapper;

Properties

PropsTypeDescriptionDefault
srcstringImage source urlrequired
mapobjectMapping objectrequired
areaKeyNamestringunique key name of the jsonid
isMultiboolEnable/Disable multiple highlight areas by clicking on the specific areatrue
toggleboolEnable/Disable toggle selected highlighted areafalse
activeboolEnable/Disable listeners and highlightingtrue
disabledboolEnable/Disable listeners, highlighting and add/remove area tag from the UIfalse
fillColorstringFill color of the highlighted zonergba(255, 255, 255, 0.5)
strokeColorstringBorder color of the highlighted zonergba(0, 0, 0, 0.5)
lineWidthnumberBorder thickness of the highlighted zone1
imgWidthnumberOriginal image width0
widthnumber | func => numberImage width, in function you will get image reference object0
heightnumber | func => numberImage height, in function you will get image reference object0
naturalboolGive the original dimensions ( height & width ) to canvas and image wrapperfalse
responsiveboolresponsive map in all resolution ( for enable it you need to specify parentWidth )false
parentWidthnumberparent max width for responsive0

Properties Callback

Props callbacksCalled onsignature
onChangeArea click listener(selectedArea: MapArea, areas: MapArea[]) => void
onImageClickClick outside of a zone in image(event: ImageEvent) => void
onImageMouseMoveMoving mouse on the image itself(event: ImageEvent) => void
onClickClick on a zone in image(area: MapArea, index: number, event: AreaEvent) => void
onMouseDownClicks any button of the mouse on a zone in image(area: MapArea, index: number, event: AreaEvent) => void
onMouseUpReleases left click of the mouse on a zone in image(area: MapArea, index: number, event: AreaEvent) => void
onTouchStartStart to touch the zone in image(area: MapArea, index: number, event: TouchEvent) => void
onTouchEndReleases touch from the zone in image(area: MapArea, index: number, event: TouchEvent) => void
onMouseMoveMoving mouse on a zone in image(area: MapArea, index: number, event: AreaEvent) => void
onMouseEnterHovering a zone in image(area: MapArea, index: number, event: AreaEvent) => void
onMouseLeaveLeaving a zone in image(area: MapArea, index: number, event: AreaEvent) => void
onLoadImage loading and canvas initialization completed(event: HTMLImageElement, dimensions: WidthHeight) => void

Methods

MethodDescription
getRefsGet container, canvas and image refs

Map Properties

A map is an object describing highlighted areas in the image.

Its structure is similar to the HTML syntax of mapping:

  • map: (object) Object to describe highlighted zones
    • name: (string) Name of the map, used to bind to the image.
    • areas: (array) Array of area objects - area: (object) Shaped like below
PropertyTypeDescriptionDefault
idstringUniquely identify an area. An index in an array is used if this value is not provided. You can set it with areaKeyName propertybased on areaKeyName
shapestringEither rect, circle or polyrequired
coordsarray of numberCoordinates delimiting the zone according to the specified shape: rect: top-left-X,top-left-Y,bottom-right-X,bottom-right-Ycircle: center-X,center-Y,radiuspoly: Every point in the polygon path as point-X,point-Y,...required
activestringEnable/Disable area listeners and highlightingtrue
disabledstringEnable/Disable area listeners, highlighting and add/remove area tag from the UIfalse
hrefstringTarget link for a click in the zone (note that if you provide an onClick prop, href will be prevented)undefined
fillColorstringFill color of the highlighted zonergba(255, 255, 255, 0.5)
strokeColorstringBorder color of the highlighted zonergba(0, 0, 0, 0.5)
lineWidthstringBorder thickness of the highlighted zone1
preFillColorstringPre filled color of the highlighted zoneundefined

When received from an event handler, an area is extended with the following properties:

PropertytypeDescription
scaledCoordsarray of numberScaled coordinates
centerarray of numberCoordinates positioning the center or centroid of the area: [X, Y]

License

Distributed with an MIT License. See LICENSE.txt for more details!

Copyright (c) 2024 Nisharg Shah

2.0.0-alpha.3

5 months ago

2.0.0-alpha.0

5 months ago

2.0.0-alpha.1

5 months ago

2.0.0-alpha.2

5 months ago

1.4.3

1 year ago

1.4.2

1 year ago

1.4.1

1 year ago

1.5.1

1 year ago

1.5.0

1 year ago

1.4.0

2 years ago

1.3.0

2 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.1.9

3 years ago

1.1.8

3 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.1.10

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.1

3 years ago

1.1.2

3 years ago

1.1.0

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

0.5.0

3 years ago

0.4.4

3 years ago

0.4.3

3 years ago

0.4.2

3 years ago

0.4.1

3 years ago

0.3.2

3 years ago

0.4.0

3 years ago

0.3.1

3 years ago

0.3.0

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.2.3

3 years ago

0.1.4

3 years ago

0.2.2

3 years ago

0.1.3

3 years ago

0.1.6

3 years ago

0.2.4

3 years ago

0.1.5

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago