
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
react-img-markup
Advanced tools
React pattern based library for creating markup on images.

Install package:
npm i react-img-markup
or
yarn add react-img-markup
*Note:
react-img-markupsupports a hooks API and a render-props model API. Scroll futher down the page if you're looking for the render-props documentation.
New to react-img-markup version 2, hooks api allow for a more easily controllable pattern that does not rely on component nesting.
Import useImgMarkup hook and ImgMarkupCanvas component from library:
import { useImgMarkup, ImgMarkupCanvas } from 'react-img-markup';
Call the useImgMarkup hook to initialize the logic for your image annotations, destructure bind from the return value of the hook.
const { bind } = useImgMarkup()
Insert component ImgMarkupCanvas into your React JSX and insert bind using spread operator syntax. This will assign the hook logic to the correct instance of ImgMarkupCanvas.
<ImgMarkupCanvas {...bind} />
Now pass the image src directly to ImgMarkupCanvas via imgSrc prop.
<ImgMarkupCanvas imgSrc={src} {...bind} />
Voila! Your image markup should now work with the default settings, though there is a lot more you can customize! Next we will show how the useImgMarkup hook allows us to use state values and setters to set different configurations for annotation- in this example, changing the active color.
Destructure activeColor and setActiveColor from the useImgMarkup hook.
const {
bind,
activeColor,
setActiveColor,
} = useImgMarkup()
You can now use these destructured properties from useImgMarkup to control image markup colors via a standard React state-controlled pattern.
<>
<ImgMarkupCanvas imgSrc={src} {...bind} />
<select
value={activeColor}
onChange={(e) => setActiveColor(e.target.value)}
>
<option value='red'>red</option>
<option value='green'>green</option>
<option value='#0af'>blue</option>
<option value='orange'>orange</option>
</select>
</>
That's the basics! I will give a full and detailed list of the rest of the features react-img-markup supports below. More in-depth examples will be provided in this project's repository.
useImgMarkup hook argumentsonSave (func, optional) - callback invoked when image is saved. Will pass the uri as parameter: (uri) => {}.onImgLoad (func, optional) - callback invoked when image has loaded: () => {}.encoderType (string, optional) - determines the format the file will be saved as. default is "jpg" but also accepts "png".encoderOptions (number, optional) - A number between 0 and 1 indicating image quality on save. The default is 0.8.defaultValues (obj, optional) - pass options to give ImgMarkup default state values when component mounts.
type (string) - value for markup type. Includes: "rect", "ellipsis", "line", "arrow", and "text". Default: "rect".color (string) - value for markup color. Can be any CSS color name, hex, or rgba. Default: "red"strokeWidth (number) - value for markup stroke width. Accepts any numerical value. Default: 2fontSize (number) - value for markup font size. Accepts any numerical value. Default: 20text (string) - value for default text shown to user when text element is first created. Default: ""useImgMarkup hook return valuesactiveType (string) - state. The active markup type. Returned values can be: "rect", "ellipsis", "line", "arrow", and "text".activeColor (string) - state. The markup active color state.activeStrokeWidth (number) - state. The markup active stroke width.activeFontSize (number) - state. The markup active stroke font size.setActiveType (func) - sets the activeType state. State settable options are: "rect", "ellipsis", "line", "arrow", and "text".setActiveColor (func) - sets the activeColor state. Accepts string values.setActiveStrokeWidth (func) - sets the activeStrokeWidth state. Accepts number values.setActiveFontSize (func) - sets the activeFontSize state. Accepts number values.undo (func) - will undo the last markup element the user created.deletePath (func) - pass a path id to delete desired path: deletePath(id).save (func) - saves the image with the markup. Will pass the uri to onSave when this function is called. Will also return uri if asynchronously await the call.activePathId (string) - state. The id of activePath. Will be assigned null if no paths are active.imgMarkupModifiers (obj) - react ref. Assign this ref to an element that wraps your modification elements to not lose the selected state when changing shape/text settings.paths (obj) - state. Exposing the exact path data to the user.ImgMarkupCanvas propsimgSrc (string, required) - the src of the image you would like to add markup to.imgStyles (obj, optional) - styles passed to the image element.This pattern replicates the original render-props API structure of react-img-markup v1. If you're looking to upgrade to v2 and want to make as few changes as possible, this is the way.
Import ImgMarkupRenderProp component from library.
import { ImgMarkupRenderProp } from 'react-img-markup';
Insert the ImgMarkupRenderProp component inside your JSX, passing your image src to the prop imgSrc.
<ImgMarkupRenderProp imgSrc={src} />
By passing a function as a child of ImgMarkupRenderProp, you get access to properties of ImgMarkupRenderProp via the child's callback function parameters. In this case, our active color state value and setter.
<ImgMarkupRenderProp imgSrc={src}>
{({ activeColor, setActiveColor }) => (
<select value={activeColor} onChange={(e) => setActiveColor(e.target.value)}>
<option value='red'>red</option>
<option value='green'>green</option>
<option value='blue'>blue</option>
<option value='orange'>orange</option>
</select>
)}
</ImgMarkupRenderProp>
ImgMarkupRenderProp propsimgSrc (string, required) - the src of the image you would like to add markup to.imgStyles (obj, optional) - styles passed to the image.onSave (func, optional) - callback invoked when image is saved. Will pass the uri as parameter: (uri) => {}.onImgLoad (func, optional) - callback invoked when image has loaded: () => {}.encoderType (string, optional) - determines the format the file will be saved as. default is "jpg" but also accepts "png".encoderOptions (number, optional) - A number between 0 and 1 indicating image quality on save. The default is 0.8.defaultValues (obj, optional) - pass options to give ImgMarkup default state values when component mounts.
type (string) - value for markup type. Includes: "rect", "ellipsis", "line", "arrow", and "text". Default: "rect"color (string) - value for markup color. Can be any CSS color name, hex, or rgba. Default: "red"strokeWidth (number) - value for markup stroke width. Accepts any numerical value. Default: 2fontSize (number) - value for markup font size. Accepts any numerical value. Default: 20text (string) - value for default text shown to user when text element is first created. Default: ""ImgMarkupRenderProp render prop parameter valuesactiveType (string) - state. The active markup type. Returned values can be: "rect", "ellipsis", "line", "arrow", and "text".activeColor (string) - state. The markup active color state.activeStrokeWidth (number) - state. The markup active stroke width.activeFontSize (number) - state. The markup active stroke font size.setActiveType (func) - sets the activeType state. State settable options are: "rect", "ellipsis", "line", "arrow", and "text".setActiveColor (func) - sets the activeColor state. Accepts string values.setActiveStrokeWidth (func) - sets the activeStrokeWidth state. Accepts number values.setActiveFontSize (func) - sets the activeFontSize state. Accepts number values.undo (func) - will undo the last markup element the user created.deletePath (func) - pass a path id to delete desired path: deletePath(id).save (func) - saves the image with the markup. Will pass the uri to onSave when this function is called. Will also return uri if asynchronously await the call.activePathId (string) - state. The id of activePath. Will be assigned `null`` if no paths are active.imgMarkupModifiers (obj) - react ref. Assign this ref to an element that wraps your modification elements to not lose the selected state when changing shape/text settings.paths (obj) - state. Exposing the exact path data to the user.git clone
npm i --legacy-peer-deps
npm run start
This library was built using react-npm-package-boilerplate as a boilerplate.
FAQs
Library for adding markup to images using shapes and text
The npm package react-img-markup receives a total of 476 weekly downloads. As such, react-img-markup popularity was classified as not popular.
We found that react-img-markup demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.