It might be possible...and here's a totally untested and ugly attempt at it.
You mention cross-browser compatibility, so jQuery is useful here.
Here's the plan:
- Have jQuery listen for mouse events on the canvas.
- When fired, get the mouseX and mouseY.
- Get a reference to the jQuery event object.
- Get a set of jQuery objects containing each element at mouseX/mouseY.
- Use jQuery to trigger the event on each element in that set.
This is a possible starting point...and probably needs tweeking.
Here's some untested code for the click event:
// pre-calc the offsets of the canvas
var canvasOffset=$("#canvas").offset();
var offsetX=canvasOffset.left;
var offsetY=canvasOffset.top;
// on canvas click,
// get all elements under the click
// and trigger the click event on them
$("#mycanvas").click(function(e){
mouseX=parseInt(e.clientX-offsetX);
mouseY=parseInt(e.clientY-offsetY);
// get a referenct to the jq click event object
var mEvent=$.Event('click'); // added '.'
// get a set of all elements under mouseX/mouseY
var elements=elementsAtXY(mouseX,mouseY);
// trigger the event on each element under X/Y
elements.each(function(){
$(this).trigger(mEvent);
});
});
function elementsAtXY(x,y){
var $elements = $("body *").map(function() {
var $this = $(this);
var offset = $this.offset();
var mleft = offset.left;
var mtop = offset.top;
var height = $this.height();
var width = $this.width();
var mright = mleft + width;
var mbottom = mtop + height;
return (y>=mtop && y <=mbottom) && (x>=mleft && x<=mright) ? $this : null;
});
return $elements;
}
If the elements on your page don't move, you could improve performance by pre-calculating the position of each element instead of calculating it during each event.