-
I write an SVG Layer that shows points and lines inside an element on top of the maplibre canvas. How can I forward events to the map so that I still can zoom, pan, ... it? I turned pointer-events off in css and on for the element. No the problem is mitigated, but i would like to use map events even when on the svg elements. I managed to forward the wheel event sucessfullt but am stuck with the other events. My code for mouse events looks like
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Doesn't the events automatically pass through? Wouldn't you need to call stopPropagation for them not to pass? |
Beta Was this translation helpful? Give feedback.
-
Thought like the same but actually events do not pass through. I have
I fixed my code, the missing thing was
Thanks for your immediate reply. attachment: Why Mouse Events Don’t Automatically Pass Through Your SVG to the MapWhen you place an SVG element on top of a MapLibre (or Mapbox GL) map, the mouse events (clicks, drags, etc.) are blocked by the SVG and don’t reach the map underneath. This happens due to:
Solutions to Forward Events to the Map1. Make the SVG "Transparent" to Events (Best for Overlays)svg {
pointer-events: none; /* Lets clicks pass through to the map */
}
2. Forward Specific Events Manually (If SVG Needs Interactivity)const svgElement = document.querySelector('svg');
const map = /* Your MapLibre instance */;
svgElement.addEventListener('mousedown', (e) => {
// 1. Handle SVG logic (if needed)
console.log('SVG clicked, but forwarding to map');
// 2. Clone and dispatch the event on the map’s canvas
const mapCanvas = map.getCanvas();
const clonedEvent = new MouseEvent(e.type, {
bubbles: true,
clientX: e.clientX,
clientY: e.clientY,
// Include other props (button, shiftKey, etc.)
});
mapCanvas.dispatchEvent(clonedEvent);
});
3. Hybrid Approach (Conditional Passthrough)svg.click-through {
pointer-events: none;
} // Enable/disable passthrough dynamically
svgElement.classList.toggle('click-through', true);
Why This Happens in MapLibre/Mapbox GL
Final Recommendation
|
Beta Was this translation helpful? Give feedback.
Thought like the same but actually events do not pass through. I have
<svg>
with pointer-events: none, so no pass through necessary or happens<circle>
with pointer-events: auto; event forwarding needed to allow zoom, pan,.. while on elementI fixed my code, the missing thing was
Thanks for your immediate reply.
attachment:
Why Mouse Events Don’t Automatically Pass Through Your SVG to the Map
When you place an SVG element on top of a MapLibre (or Mapbox GL) map, the mouse events (clicks, drags, etc.) are blocked by the SVG and don’t reach the map underneath. This happens due to:
Event Bubbling & Default Behavior