I have an image like this:
I want it so that when I click the transparent parts, the click should go through to the underlying element, but when I click a n
0
means it's
transparent."none"
and retrieve the underneath element using Document.elementFromPoint"click"
event on the underneath element"auto"
for all overlaysvar ctx = document.createElement("canvas").getContext("2d");
$('#logo').on("mousedown", function(event) {
// Get click coordinates
var x = event.pageX - this.offsetLeft,
y = event.pageY - this.offsetTop,
w = ctx.canvas.width = this.width,
h = ctx.canvas.height = this.height,
alpha;
// Draw image to canvas
// and read Alpha channel value
ctx.drawImage(this, 0, 0, w, h);
alpha = ctx.getImageData(x, y, 1, 1).data[3]; // [0]R [1]G [2]B [3]A
// If pixel is transparent,
// retrieve the element underneath and trigger it's click event
if( alpha===0 ) {
this.style.pointerEvents = "none";
$(document.elementFromPoint(event.clientX, event.clientY)).trigger("click");
this.style.pointerEvents = "auto";
} else {
console.log("LOGO clicked!");
}
});
$("#green").on("click", function(){
console.log("Green image clicked!");
});
img{border:1px solid #000;}
img + img{position:absolute;top:0; left:0;}