1.4.1 • Published 2 years ago

@mohamadtsn/react-img-mapper v1.4.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

React Img Mapper

React Component to highlight interactive zones in images

This repository is based on react-image-mapper but with some enhancements

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

Installation

Package: react-img-mapper

NPM

npm install react-img-mapper --save        

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
    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
mapstringMapping description{ name: generated, areas: }
containerRefrefhelp to reset selected highlighted areanull
areaKeyNamestringdefault unique key name ( required for stayHighlighted )id
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
widthnumber | funcImage width, in function you will get image reference object0
heightnumber | funcImage height, in function you will get image reference object0
activeboolEnable/Disable highlightingtrue
disabledboolEnable/Disable area listeners and highlightingfalse
imgWidthnumberOriginal image width0
naturalboolGive the original dimensions ( height & width ) to canvas and image wrapperfalse
stayHighlightedboolYou can see the highlighted area after clicking on the particular areafalse
stayMultiHighlightedboolYou can see the multiple highlighted area after clicking on the particular areafalse
toggleHighlightedboolYou can toggle selected highlighted areafalse
rerenderPropsarrayspecify rerenderProps property, if you want to rerender your map with different property[]
responsiveboolresponsive map in all resolution ( for enable it you need to specify parentWidth )false
parentWidthnumberparent max width for responsive0

Properties Callback

Props callbacksCalled onsignature
onLoadImage loading and canvas initialization completed(imageRef: obj, parentDimensions: { width, height }): void
onClickClick on a zone in image(area: obj, index: num, event): void
onMouseEnterHovering a zone in image(area: obj, index: num, event): void
onMouseLeaveLeaving a zone in image(area: obj, index: num, event): void
onMouseMoveMoving mouse on a zone in image(area: obj, index: num, event): void
onMouseDownClicks any button of the mouse on a zone in image(area: obj, index: num, event): void
onMouseUpReleases left click of the mouse on a zone in image(area: obj, index: num, event): void
onTouchStartStart to touch the zone in image(area: obj, index: num, event): void
onTouchEndReleases touch from the zone in image(area: obj, index: num, event): void
onImageClickClick outside of a zone in image(event): void
onImageMouseMoveMoving mouse on the image itself(event): void

Methods

MethodDescription
clearHighlightedAreaClear all selected highlighted area from your map

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 propertyindex
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,...requiredactivestringEnable/Disable highlightingtrue
disabledstringEnable/Disable area listeners and highlightingfalse
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) 2021 Nisharg Shah