In order to customize an element with a specific class name, you can simply assign the class name selected
to the desired path element.
For better organization, I recommend dividing your design into two layers: the background layer containing images, and the foreground layer with hex shapes (which can also be hexagonal). You can place a hex shape at x=0, y=0 and use it as a universal clip-path for all background images. In this example, I have added a blue-colored image as the background of path id="13", masked with a clip-path that matches the hex shape.
To handle interactions, I set up an event listener for the parent element (g
element with id="parent"
) and check if the correct element has been clicked on. Currently, the opacity is taking precedence over the red fill, but you can modify this as needed.
let parent = document.getElementById('parent');
parent.addEventListener('click', e => {
if(e.target.nodeName == 'path'){
// this is a hex
e.target.classList.add('selected');
}
});
<div class="board">
<svg viewBox="0 0 3400 3673" xmlns="http://www.w3.org/2000/svg">
<defs>
<clipPath id="hex">
<path d="m1278 376 211-365h422l211 365-211 365h-422z"/>
</clipPath>
</defs>
<style>
g#parent path {fill: #bbb}
path.selected {fill: red}
path.selected {opacity: 0}
</style>
<g id="images">
<image id="img13" x="1200" y="0" height="1000" width="1000" clip-path="url(#hex)" href="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyMDAiIGhlaWdodD0iMjAwIj4KPHJlY3QgZmlsbD0iYmx1ZSIgd2lkdGg9IjIwMCIgaGVpZ2h0PSIyMDAiIHg9IjAiIHk9IjAiLz4KPC9zdmc+"/>
</g>
<g id="parent" stroke="#fff" stroke-width="20">
<path id="13" d="m1278 376 211-365h422l211 365-211 365h-422z"/>
<path id="22" d="m646 741 211-365h422l211 365-211 365h-422z"/>
<path id="24" d="m1911 741 211-365h422l211 365-211 365h-422z"/>
<path id="31" d="m14 1106 211-365h422l211 365-211 365h-422z"/>
<path id="33" d="m1278 1106 211-365h422l211 365-211 365h-422z"/>
<path id="35" d="m2543 1106 211-365h422l211 365-211 365h-422z"/>
<path id="42" d="m646 1471 211-365h422l211 365-211 365h-422z"/>
<path id="44" d="m1911 1471 211-365h422l211 365-211 365h-422z"/>
<path id="51" d="m14 1836 211-365h422l211 365-211 365h-422z"/>
<path id="53" d="m1278 1836 211-365h422l211 365-211 365h-422z"/>
<path id="55" d="m2543 1836 211-365h422l211 365-211 365h-422z"/>
<path id="62" d="m646 2201 211-365h422l211 365-211 365h-422z"/>
<path id="64" d="m1911 2201 211-365h422l211 365-211 365h-422z"/>
<path id="71" d="m14 2566 211-365h422l211 365-211 365h-422z"/>
<path id="73" d="m1278 2566 211-365h422l211 365-211 365h-422z"/>
<path id="75" d="m2543 2566 211-365h422l211 365-211 365h-422z"/>
<path id="82" d="m646 2932 211-365h422l211 365-211 365h-422z"/>
<path id="84" d="m1911 2932 211-365h422l211 365-211 365h-422z"/>
<path id="93" d="m1278 3297 211-365h422l211 365-211 365h-422z"/>
</g>
</svg>
</div>