Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to handle touch events? #12

Closed
tracycollins opened this issue Nov 2, 2017 · 8 comments
Closed

How to handle touch events? #12

tracycollins opened this issue Nov 2, 2017 · 8 comments

Comments

@tracycollins
Copy link

tracycollins commented Nov 2, 2017

I can't figure out how to get touch events (using the d3.js library) to work.

Is there a way to tell panzoom to not ignore touch events?

@anvaka
Copy link
Owner

anvaka commented Nov 2, 2017

Are you trying to enable panzoom for existing d3 visualization? Or do you want to disable touch events?

@tracycollins
Copy link
Author

tracycollins commented Nov 2, 2017

I want to use touch events with d3.

I have panzoom enabled for an svg g element "svgG" and then want to detect touch events on child elements (cirles and text) of the g element ("nodeCircles")

In the code snippet below, the .on("click", nodeClick) works, but the .on("touchStart", nodeClick) does not.

var svgG = svgMain.append("g").attr("id", "svgG");

var panzoomElement = document.getElementById("svgG");
panzoom(panzoomElement, { zoomSpeed: 0.030 });

var nodeSvgGroup = svgG.append("svg:g").attr("id", "nodeSvgGroup");

var nodeCircles = nodeSvgGroup.selectAll("circle").data(nodes, function(d){ return d.nodeId; });

nodeCircles.enter().append("circle").attr("nodeId", function(d) { return d.nodeId; })
 .on("click", nodeClick)
 .on("touchstart", nodeClick);

@anvaka
Copy link
Owner

anvaka commented Nov 28, 2017

I see. Might be relevant to #11 and comments in this thread. Unless @odai-alali or someone else beats me to it, I can give a callback approach a try later this week or next.

@anvaka anvaka closed this as completed in 1af4927 Mar 15, 2018
@anvaka
Copy link
Owner

anvaka commented Mar 15, 2018

Just pushed the changes. Should be available in version 4.2.0. Snippet from the readme:

panzoom(document.getElementById('g4'), {
  onTouch: function(e) {
    // `e` - is current touch event.

    return false; // tells the library to not preventDefault.
  }
});

Please let me know if this does not work.

@tracycollins
Copy link
Author

Yes, it works! Thanks!

Unrelated, is there a way to programmatically control panzoom? For instance, something like:

panzoom.setZoomFactor(2.0);

@tracycollins
Copy link
Author

I guess I don't completely understand handling events, because now that touch events work, zooming with a mouse (actually touchpad on macbook) now does not.

How can I get both touch events on iOS and zooming with a mouse to work?

@anvaka
Copy link
Owner

anvaka commented Apr 10, 2018

@tracycollins sorry - I missed this one.

To set zoom level from code, you'd need to call panzoom.zoomAbs(clientX, clientY, zoomFactor)

As for broken zoom with mouse - can you share a jsbin with me?

@tracycollins
Copy link
Author

The mouse zoom seems to have fixed itself. Not sure if something I did fixed it.

Anyway...

I'm not sure how to use the code snippet to set zoom level. I tried and got the error:
"panzoom.zoomAbs is not a function"

This is how I'm using it:

var zoomFactor = 1.0; var panzoomElement = document.getElementById("svgTreemapLayoutArea"); panzoom(panzoomElement, { zoomSpeed: 0.030 }); panzoom.zoomAbs(0.5*width, 0.5*height, zoomFactor);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants