0.0.15 • Published 4 years ago

@lauriegriffiths/react-image-mapper v0.0.15

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

react-image-mapper

React Component to highlight interactive zones in images

Demo & Examples

Live demo: coldiary.github.io/react-image-mapper

To build the example locally, run:

npm install
npm start

Then open localhost:8000 in a browser.

Installation

The easiest way to use react-image-mapper is to install it from NPM and include it in your own React build process (using Browserify, Webpack, etc).

You can also use the standalone build by including dist/react-image-mapper.js in your page. If you use this, make sure you have already included React, and it is available as a global variable.

npm install react-image-mapper --save

Usage

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

// ES5 require
var ImageMapper = require('react-image-mapper');

// ES6 import
import ImageMapper from 'react-image-mapper';

<ImageMapper src={IMAGE_URL} map={AREAS_MAP}/>

Properties

PropstypeDescriptiondefault
srcstringImage source urlrequired
mapstringMapping description{ name: generated, areas: [ ] }(see below)
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
widthnumberImage widthDisplayed width
heightnumberImage heightDisplayed height
activeboolEnable/Disable highlightingtrue
imgWidthnumberOriginal image widthnull
Props callbacksCalled onsignature
onLoadImage loading and canvas initialization completed(): 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
onClickClick on a 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

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 :
PropertytypeDescription
_idstringUniquely identify an area. Index in array is used if this value is not provided.
shapestringEither rect, circle or poly
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,...
hrefstringTarget link for a click in the zone (note that if you provide a onClick prop, href will be prevented)

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

PropertytypeDescription
scaledCoordsarray of numberScaled coordinates (see Dynamic Scaling below)
centerarray of numberCoordinates positionning the center or centroid of the area: [X, Y]

Dynamic scaling

When a parent component updates the width prop on <ImageMapper>, the area coordinates also have to be scaled. This can be accomplied by specifying both the new width and a constant imgWidth. imgWidth is the width of the original image. <ImageMapper> will calculate the new coordinates for each area. For example:

/* assume that image is actually 1500px wide */

// this will be a 1:1 scale, areas will be 3x bigger than they should be
<ImageMapper width={500} />

// this will be the same 1:1 scale, same problem with areas being too big
<ImageMapper width={500} imgWidth={500} />

// this will scale the areas to 1/3rd, they will now fit the 500px image on the screen
<ImageMapper width={500} imgWidth={1500} />

Development (src, lib and the build process)

NOTE: The source code for the component is in src. A transpiled CommonJS version (generated with Babel) is available in lib for use with node.js, browserify and webpack. A UMD bundle is also built to dist, which can be included without the need for any build system.

To build, watch and serve the examples (which will also watch the component source), run npm start. If you just want to watch changes to src and rebuild lib, run npm run watch (this is useful if you are working with npm link).

Notes & Contributions

This a component is still a work in progress.

If you encounter a bug of some kind, feel free to report the issue.

If you'd like to improve this code or ask/advise for any improvement, feel free to comment it as well.

License

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

Copyright (c) 2017 Coldiary.