I'm trying to use javascript to place an image overlay over an existing HTML image when the user clicks on it. I have that part working, but am wondering if it is possible to reposition the superimposed images when the window resizes so they maintain their position relative to the base image. I'd also like to scale the inserted images on window resize. I have a little codepen here: https://codepen.io/Matt-Denko/pen/abxLqrR.
window.addEventListener("load", function () {
document.getElementById('myImg').onclick = function(event) {
var i = new Image();
i.src = 'https://picsum.photos/30/30?grayscale';
i.style.position = "absolute";
var yOffset = event.clientY;
var xOffset = event.clientX;
// Apply offsets for CSS margins etc.
yOffset -= 148;
xOffset -= 40;
// Set image insertion coordinates
i.style.top = yOffset + 'px';
i.style.left = xOffset + 'px';
// Append the image to the DOM
document.getElementById('map1').appendChild(i);
}
});
.responsive {
position: relative;
max-width: 1200px;
width: 100%;
height: 100%;
}
<div class="container">
<div style="height: auto; padding: 3px 0 3px 0;">
<div class="card">
<div class="card_content">
<h1>Map PNG Testing</h1>
<div id="map1" class="responsive">
<img src="https://picsum.photos/seed/picsum/200/300" id="myImg" class="responsive">
</div>
</div>
</div>
</div>
</div>