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

Add d3.pointers. #255

Merged
merged 7 commits into from
Aug 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -680,14 +680,24 @@ If *parameters* is a function, it is evaluated for each selected element, in ord

<a name="pointer" href="#pointer">#</a> d3.<b>pointer</b>(<i>event</i>[, <i>target</i>]) · [Source](https://github.com/d3/d3-selection/blob/master/src/pointer.js)

Returns a two-element array of numbers [*x*, *y*] representing the coordinates of the specified *event* relative to the specified *target*. If *target* is not specified, it defaults to *event*.currentTarget. (The *event* may also be a [touch](https://www.w3.org/TR/touch-events/#touch-interface).)
Returns a two-element array of numbers [*x*, *y*] representing the coordinates of the specified *event* relative to the specified *target*. *event* can be a [UIEvent](https://developer.mozilla.org/en-US/docs/Web/API/UIEvent) (MouseEvent or TouchEvent), a [PointerEvent](https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent), a custom event holding a UIEvent as *event*.sourceEvent, or a [touch](https://www.w3.org/TR/touch-events/#touch-interface).

If *target* is not specified, it defaults to the source event’s currentTarget property, if available.

If the *target* is an SVG element, the event’s coordinates are transformed using the [inverse](https://www.w3.org/TR/geometry-1/#dom-dommatrixreadonly-inverse) of the [screen coordinate transformation matrix](https://www.w3.org/TR/SVG/types.html#__svg__SVGGraphicsElement__getScreenCTM).

If the *target* is an HTML element, the event’s coordinates are translated relative to the top-left corner of the *target*’s [bounding client rectangle](https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect). (As such, the coordinate system can only be translated relative to the client coordinates. See also [GeometryUtils](https://www.w3.org/TR/cssom-view-1/#the-geometryutils-interface).)

Otherwise, [*event*.pageX, *event*.pageY] is returned.

In the case of touch events, the coordinates of the first touch are returned. To handle multitouch interactions, see [pointers](#pointers) instead.

<a name="pointers" href="#pointers">#</a> d3.<b>pointers</b>(<i>event</i>[, <i>target</i>]) · [Source](https://github.com/d3/d3-selection/blob/master/src/pointers.js)

Returns an array [[*x0*, *y0*], [*x1*, *y1*]…] of coordinates of the specified *event*’s pointer locations relative to the specified *target*. For mouse, stylus or single touch events, [*x0*, *y0*] is equivalent to d3.pointer(event, target). In the case of multi-touch events, the returned array contains a pair of coordinates for each of the touches.

If *target* is not specified, it defaults to the source event’s currentTarget property, if available.

### Control Flow

For advanced usage, selections provide methods for custom control flow.
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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 pointers} from "./pointers.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
6 changes: 5 additions & 1 deletion src/pointer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export default function(event, node = event.currentTarget) {
import sourceEvent from "./sourceEvent.js";

export default function(event, node) {
event = sourceEvent(event);
Fil marked this conversation as resolved.
Show resolved Hide resolved
if (node === undefined) node = event.currentTarget;
if (node) {
var svg = node.ownerSVGElement || node;
if (svg.createSVGPoint) {
Expand Down
11 changes: 11 additions & 0 deletions src/pointers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import pointer from "./pointer.js";
import sourceEvent from "./sourceEvent.js";

export default function(events, node) {
if (events.target) { // i.e., instanceof Event, not TouchList or iterable
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I’d prefer events instanceof Event here, but our current custom events in d3-drag etc. don’t extend Event, so that isn’t possible. It might be possible to change them to extend Event, although for all I know, that may introduce other problems.

events = sourceEvent(events);
if (node === undefined) node = events.currentTarget;
events = events.touches || [events];
Fil marked this conversation as resolved.
Show resolved Hide resolved
}
return Array.from(events, event => pointer(event, node));
}
5 changes: 5 additions & 0 deletions src/sourceEvent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default function(event) {
let sourceEvent;
while (sourceEvent = event.sourceEvent) event = sourceEvent;
return event;
}