I'm trying to implement a board game in React. I'm using area/map tags to detect clicks on the board. I want to place a prick/stone on the position of the area tag, when some logic happens. How can I place an img element on the position of the area tag in the board-img element?
I've tried this code, but it dosen't seem to work..
renderBoard() {
const board = this.state.gameState.board;
let positionSyle = {};
let square = this['w1'] ? this['w1'].getBoundingClientRect() : false;
if (square) {
{/**Find square w1 and get position its position */ }
positionSyle = {
top: square.top + 'px',
left: square.left + 'px',
}
}
return (
<React.Fragment>
{/**render board image + insert areas on board(squares) */}
<div>
<img src="board.png" useMap="#image-map" />
<map name="image-map">
<area ref={(c) => this['w1'] = c} target alt="w1" title="w1" coords="387,313,315,244" shape="rect" onClick={() => console.log('w1')} />
<area ref={(c) => this['w2'] = c} target alt="w2" title="w2" href coords="295,317,219,243" shape="rect" onClick={() => console.log('w2')} />
</map>
<img src={"w-stone.png"} style={{ ...positionSyle, position: 'absolute', width: '50px', height: '50px' }} />
</div>
</React.Fragment>
)
}
Screenshot of current code. The stone should be placed on the "w1" square. But it isn't..