Skip to content

Commit

Permalink
- fix d3.pointer for touch events
Browse files Browse the repository at this point in the history
- add d3.pointers for multitouch events

closes #245
  • Loading branch information
Fil committed May 8, 2020
1 parent 0a67d22 commit 5017419
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,10 @@ If the *target* is an HTML element, the event’s coordinates are translated rel
Otherwise, [*event*.pageX, *event*.pageY] is returned.
<a name="pointers" href="#pointers">#</a> d3.<b>pointers</b>(<i>event</i>[, <i>target</i>]) [<>](https://github.com/d3/d3-selection/blob/master/src/pointer.js "Source")
A generalization of [pointer](#pointer) for multitouch events. Returns the *x* and *y* coordinates of the specified *event*’s touches relative to the specified *target*, as an array of two-element arrays of numbers [[*x1, y1*], [*x2, y2*], …]. If *target* is not specified, it defaults to *event*.currentTarget.
### Control Flow
For advanced usage, selections provide methods for custom control flow.
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export {default as local} from "./local.js";
export {default as matcher} from "./matcher.js";
export {default as namespace} from "./namespace.js";
export {default as namespaces} from "./namespaces.js";
export {default as pointer} from "./pointer.js";
export {default as pointer, pointers} from "./pointer.js";
export {default as select} from "./select.js";
export {default as selectAll} from "./selectAll.js";
export {default as selection} from "./selection/index.js";
Expand Down
7 changes: 6 additions & 1 deletion src/pointer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export default function(event, node = event.currentTarget) {
export default function pointer(event, node = event.currentTarget) {
if (event instanceof TouchEvent) event = event.touches[0];
if (node) {
var svg = node.ownerSVGElement || node;
if (svg.createSVGPoint) {
Expand All @@ -14,3 +15,7 @@ export default function(event, node = event.currentTarget) {
}
return [event.pageX, event.pageY];
}

export function pointers(event, node = event.currentTarget) {
return Array.from(event.touches || [event]).map(e => pointer(e, node));
}

0 comments on commit 5017419

Please sign in to comment.