question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

v4.0 - d3.zoom() prevents binding events on the SVG or it's elements

See original GitHub issue

After changing to the new v4.0 D3, seems like using d3.zoom() blocks other events somehow, like:

Calling this:

svg.call(d3.zoom()
     .scaleExtent([1, 40])
     .translateExtent([[-100, -100], [width + 90, height + 100]])
     .on("zoom", zoomed));

And then this:

document.querySelector('svg').addEventListener('mouseup', function(){ alert(1) })

Won’t alert anything.

Demo page

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
IPWright83commented, Mar 1, 2018

I came across this too, and have just played around a little. I was a bit confused by @mbostock’s comment regarding the d3.event.stopPropagation() because I couldn’t seem to see that working.

If however you go a little further, you can prevent the zoom kicking in when clicking on say a circle, by ensuring that you stopPropagation of the mousedown, preventing the zoom from ever triggering.

const zoom = d3.zoom().on("zoom", () => {
    console.log("zoomed");
});

d3.select("svg").call(zoom);

d3.select("svg")
    .selectAll("circle")
    .on("mousedown", () => {
        // Without this, clicking the circle will never trigger a mouseup, because zoom will handle mouseup
        d3.event.stopPropagation();
        console.log("mousedown");
    })
    .on("mouseup", () => {
        d3.event.stopPropagation();
        console.log("mouseup");
    });

using DOM:

<svg width="500" height="500">
  <circle cx="150" cy="150" r="50" />
</svg>
2reactions
mbostockcommented, Sep 29, 2016

Sorry, but no. I expect you can achieve your desired result without modifying the zoom implementation, but I don’t have time right now to make a more specific example for you other than the ones I have already linked (and are linked from the README).

If you want to allow the zoom gesture but still listen to a mouseup handled by the zoom behavior then again I recommend listening to the zoom behavior’s end event so that you can do something on the end of a zoom gesture (or specifically mouseup by looking at d3.event.sourceEvent).

Alternatively I expect you could still intercede on the mouseup event by using a capturing listener (passing true as the third argument to selection.on). You might need to listen to the window rather than the specific element you want to receive the mouseup, though, since capturing events bubble the other way.

Anyhow, I am afraid this is all the help I can provide. If you want further assistance please try Stack Overflow tag d3.js or the d3-js Google group. I am currently on vacation.

Read more comments on GitHub >

github_iconTop Results From Across the Web

d3.zoom() prevents binding some events except on the Window
The zoom adds a pan event, mousedown starts a pan (i.e. zoom start ) and mouseup ends a pan (i.e. zoom end )....
Read more >
D3 v4 Brush and Zoom on the same element (without mouse ...
Essentially it amounts to creating an SVG rect to specifically handle the zoom events. You can leave your brush behavior attached to an...
Read more >
4. Events, Interactivity, and Animation - D3 for the Impatient ...
When working with SVG, you can supply any element (as a Node ), and the function will calculate the coordinates relative to the...
Read more >
D3.js Tutorial: Mouse Events Handling - OctoPerf
Learn how to handle mouse events using D3.js to manipulate SVG graphics. d3.datum() and d3.mouse() explained via code and live examples.
Read more >
Simplest way to add zoom/pan on d3.js (version 3 and 4)
You just need to create a g element as first child of the SVG element and connect d3.behavior.zoom() behavior. var svg = d3.select("body") ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found