I have an array of elements which has some coordinates. On the basis of that coordinates, I am drawing the element in a single canvas. Now I want when the user clicks on any of the element it should trigger a function.
can anyone has an idea of how we can do it
//js part
drawLayout() {
const canvas = this.refs.canvas;
const ctx = canvas.getContext("2d");
data.forEach(item => {
ctx.beginPath();
ctx.lineWidth = "1";
ctx.strokeStyle = "red";
let X = (maxValueOfX - item.row) * width;
let Y = item.column * height;
ctx.fillText(item.name, X + 5, Y + 10);
ctx.rect(X, Y ,width, height)
ctx.stroke();
});
componentDidMount() {
this.drawLayout()
}
//html part
<div className="layoutContainer">
<canvas
ref="canvas"
width={width}
height={height}
style={{ backgroundColor: "#e1d4d4" }}
/>
</div>
Thanks