Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
38
rated 0 times [  42] [ 4]  / answers: 1 / hits: 18088  / 8 Years ago, tue, may 24, 2016, 12:00:00

I have a full-screen transparent canvas covering my web page. On the canvas, I render an interactive object. There's a problem: elements (e.g. links) below the canvas do not respond to mouse clicks.



The obvious solution, which I would normally use, is to apply pointer-events: none to the canvas. This will allow clicks to pass through. However, this doesn't work in this situation, because I want the interactive object to be clickable.



So here's what I want to do:

The canvas should retain mouse-click events. IF the event is NOT over the interactive object, it should pass the event to the elements on the other side of the page.



How can I do this?


More From » jquery

 Answers
48

Found a really nice solution that I thought I should share in case anybody else has the same question.



I used pointer-events: none on the canvas. I set canvas.onclick and canvas.onmousemove like I normally would; however, pointer events are disabled so nothing happens. I bypassed the disabled pointer events like this:



document.addEventListener('click', function() {
canvas.onclick();
});
window.onmousemove = function() {
canvas.onmousemove();
}
// etc.


So far, mouse events will be received by both the web page AND the canvas.



Now, in my interactive program, I included a simple little function called mouseOver which returns true if the mouse is hovering over the interactive object. I modified window.onmousemove like this:



window.onmousemove = function() {
canvas.onmousemove();
if (mouseOver()) {
canvas.style[pointer-events] = auto;
} else {
canvas.style[pointer-events] = none;
}};


This prevents mouse events from going through to the web page, allowing interaction with the object without webpage interference.


[#62051] Saturday, May 21, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
turnerf

Total Points: 620
Total Questions: 101
Total Answers: 109

Location: French Polynesia
Member since Tue, Jul 7, 2020
4 Years ago
turnerf questions
;