2.0.0 • Published 4 months ago

react-img-mapper v2.0.0

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

React Img Mapper

React Component to Highlight Interactive Zones in Images

Key Features:

  1. Actively maintained repository.
  2. Built with TypeScript.
  3. Fully compatible with Next.js.
  4. Optimized for smaller bundle size.
  5. Comprehensive documentation.
  6. Toggle and reset functionality for single and multiple highlighted areas.
  7. Access image properties (width, height, etc.) using the onLoad callback.
  8. Fully responsive image mapper.

Installation

Install the package via your preferred package manager:

# npm
npm install react-img-mapper

# yarn
yarn add react-img-mapper

# pnpm
pnpm install react-img-mapper

Demo & Examples

Live Demo: View Demo

To run the example locally:

  1. Clone the repository:
    git clone https://github.com/img-mapper/react-img-mapper.git
  2. Install dependencies and start the dev server:
    pnpm playground:install
    pnpm playground:dev
  3. Open localhost:3000 in your browser.

To build the project, run:

pnpm build

Usage

Here’s how to integrate react-img-mapper into your project:

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

const Mapper = () => {
  const url =
    'https://raw.githubusercontent.com/img-mapper/react-img-mapper/refs/heads/master/resources/example.jpg';
  const name = 'my-map';
  // GET JSON FROM BELOW URL AS AN EXAMPLE AND PUT IT HERE
  const areas =
    'https://raw.githubusercontent.com/img-mapper/react-img-mapper/refs/heads/master/resources/areas.json';

  return <ImageMapper src={url} name={name} areas={areas} />;
};

export default Mapper;

Properties

PropTypeDescriptionDefault
srcstringURL of the image to displayrequired
namestringThe name of the map, used to associate it with the image.required
areasarrayArray of area objects, please check Area Properties belowrequired
areaKeyNamestringKey name used to uniquely identify areasid
isMultiboolAllows multiple areas to be highlightedtrue
toggleboolEnables toggling highlights for selected areasfalse
activeboolEnables area listeners and highlightingtrue
disabledboolDisable highlighting, listeners, and removes area tagfalse
fillColorstringFill color of highlighted zonesrgba(255, 255, 255, 0.5)
strokeColorstringBorder color of highlighted zonesrgba(0, 0, 0, 0.5)
lineWidthnumberBorder thickness of highlighted zones1
imgWidthnumberOriginal width of the image0
widthnumber | func => numberImage width (can use a function for dynamic sizing)0
heightnumber | func => numberImage height (can use a function for dynamic sizing)0
naturalboolUse original dimensions for canvas and wrapperfalse
responsiveboolEnable responsiveness (requires parentWidth)false
parentWidthnumberMaximum width of the parent container for responsiveness0
containerPropsobjectProps for the container <div> elementnull
imgPropsobjectProps for the <img> elementnull
canvasPropsobjectProps for the <canvas> elementnull
mapPropsobjectProps for the <map> elementnull
areaPropsobjectProps for the <area> elementsnull

Callbacks

CallbackTriggered OnSignature
onChangeClicking an area(selectedArea, areas) => void
onImageClickClicking outside of mapped zones(event) => void
onImageMouseMoveMoving the mouse over the image(event) => void
onClickClicking a mapped zone(area, index, event) => void
onMouseDownMouse down on a mapped zone(area, index, event) => void
onMouseUpMouse up on a mapped zone(area, index, event) => void
onTouchStartTouching a mapped zone(area, index, event) => void
onTouchEndEnding a touch on a mapped zone(area, index, event) => void
onMouseMoveMoving the mouse over a mapped zone(area, index, event) => void
onMouseEnterHovering over a mapped zone(area, index, event) => void
onMouseLeaveLeaving a mapped zone(area, index, event) => void
onLoadImage loaded and canvas initialized(event, dimensions) => void

Methods

MethodDescription
getRefsRetrieves refs for the container, canvas, and image elements

Areas Properties

PropertyTypeDescriptionDefault
idstringA unique identifier for the area. If not provided, an index from the array is used. This can be customized using the areaKeyName property.based on areaKeyName
shapestringSpecifies the shape of the area: rect, circle, or poly.required
coordsarray of numberCoordinates defining the area based on its shape: rect: top-left-X, top-left-Y, bottom-right-X, bottom-right-Ycircle: center-X, center-Y, radiuspoly: List of points defining the polygon as point-X, point-Y, ...required
activeboolEnables or disables event listeners and highlighting for the area.true
disabledboolDisables all interactions, highlighting, and tag additions/removals for the area.false
hrefstringA target link for clicks on the area. If an onClick handler is provided, the href will not be triggered.undefined
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
preFillColorstringPre filled color of the highlighted zoneundefined

When triggered by an event handler, an area object includes the following additional properties:

PropertyTypeDescription
scaledCoordsarray of numberScaled coordinates adjusted based on the image's dimensions.
centerarray of numberThe center or centroid coordinates of the area, represented as [X, Y].

License

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

Copyright (c) 2025 Nisharg Shah

2.0.0-alpha.9

4 months ago

2.0.0-alpha.4

4 months ago

2.0.0-alpha.5

4 months ago

2.0.0-alpha.6

4 months ago

2.0.0

4 months ago

2.0.0-alpha.3

1 year ago

2.0.0-alpha.0

1 year ago

2.0.0-alpha.1

1 year ago

2.0.0-alpha.2

1 year ago

1.4.3

2 years ago

1.4.2

2 years ago

1.4.1

2 years ago

1.5.1

2 years ago

1.5.0

2 years ago

1.4.0

3 years ago

1.3.0

3 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.9

4 years ago

1.1.8

4 years ago

1.1.7

4 years ago

1.1.6

4 years ago

1.1.10

4 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.1

4 years ago

1.1.2

4 years ago

1.1.0

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.5.0

4 years ago

0.4.4

4 years ago

0.4.3

4 years ago

0.4.2

4 years ago

0.4.1

4 years ago

0.3.2

4 years ago

0.4.0

4 years ago

0.3.1

4 years ago

0.3.0

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.2.3

4 years ago

0.1.4

4 years ago

0.2.2

4 years ago

0.1.3

4 years ago

0.1.6

4 years ago

0.2.4

4 years ago

0.1.5

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago