As a novice following a tutorial, I am learning how to modify the color of a specific area within an image using various color options.
I can successfully change one area, but I am struggling with implementing changes for other areas.
My goal is to click on the desired region, have its border highlighted via CSS, and then alter the color using the available options.
What would be the most effective approach to achieve this? Would it be appropriate to utilize a switch statement along with onclick to select the particular image area?
const overlay = document.getElementById("product-a");
var el = document.getElementsByClassName("color");
for (var i = 0; i < el.length; i++) {
el[i].onclick = changeColor;
}
function changeColor(e) {
let hex = e.target.getAttribute("data-hex");
overlay.style.fill = hex;
}
body,
html {
margin: 0;
padding: 0;
height: 100%;
width: 100%;
}
#container {
height: 200px;
width: 200px;
}
#product-svg {
position: relative;
z-index: 2;
background-size: 100%;
background-repeat: no-repeat;
background-position: 50%;
mix-blend-mode: multiply;
}
path {
fill: #CCCCCC;
}
#background-image {
position: absolute;
top: 0;
left: 0;
height: 200px;
width: 200px;
height: auto;
z-index: 1;
}
.colors {
display: flex;
position: fixed;
bottom: 2em;
right: 2em;
z-index: 3;
}
.color {
height: 36px;
width: 36px;
margin-left: 0.5em;
border-radius: 18px;
box-shadow: 0px 4px 10px rgba(0,0,0,0.3);
border: 2px solid #aaa;
cursor: pointer;
}
<div id="container">
<svg id="product-svg" viewBox="0 0 744 1074">
<path id="product-a" d="M51 207.5L51 348L686 348L686 67L51 67L51 207.5Z" />
<path id="product-b" d="M51 544.5L51 685L686 685L686 404L51 404L51 544.5Z" />
<path id="product-c" d="M51 883.5L51 1024L686 1024L686 743L51 743L51 883.5Z" />
</svg>
<img id="background-image" src="boxes.jpg" alt="">
</div>
<div class="colors">
<div class="color" style="background-color: #ff0000" data-hex="#ff0000"></div>
<div class="color" style="background-color: #ffff33" data-hex="#ffff33"></div>
<div class="color" style="background-color: #3399ff" data-hex="#3399ff"></div>
</div>