How to detect area tag onClick in a Image Map in React
Asked Answered
M

2

6

I want to detect the onClick & onHover on different parts of a image.

<img src={physicalimage} id="physicalimage" usemap="#image-map"/>
   <map name="image-map">
   <area target="" onClick={this.consoleMessage("bpleft")} alt="bpleft" title="bpleft"  coords="223,201,269,254" shape="rect" />
   <area target="" onClick={this.consoleMessage("bpright")} alt="bpright" title="bpright" coords="60,199,101,259" shape="rect" />
   <area target="" onClick={this.consoleMessage("heart")} alt="heartrate"  title="heartrate" coords="169,124,219,183" shape="rect" />
</map>

I use react library for developing my website. I am not able to detect the onClick event. Are there any other alternative?

Michelle answered 15/2, 2018 at 5:22 Comment(4)
Can you try like onClick={this.consoleMessage("bpleft").bind(this)}?Unwilling
Bind them, you can do this onClick={()=> code} or (this is better) onClick={method} and method=() => {code}Cerelia
Binding them to the area needed is the best route to take in this case.Hutner
@Cerelia It worked me great. thanksMichelle
C
4

I suggested you to use a maintained library with the all feature of react-image-mapper with extra functionality like responsive

https://github.com/NishargShah/react-img-mapper

https://www.npmjs.com/package/react-img-mapper

Chenay answered 22/1, 2021 at 17:15 Comment(0)
P
2

I'm currently doing exactly the same myself and have stumbled across this Package: React Image Mapper

So far, it seems to do everything I need. Hopefully it's of use to someone else in the future.

Pressurize answered 9/3, 2020 at 9:29 Comment(1)
kindly do you have a code sample? I am not able to get the links(href) working.Fagen

© 2022 - 2024 — McMap. All rights reserved.