Skip to content

Commit 87fcdd1

Browse files
author
Michael Johnston
committed
Proper hit testing for translated layers.
Fixes Flipboard#22
1 parent 3c5a89a commit 87fcdd1

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

lib/hitTest.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ function hitTest (e, rootLayer, rootNode) {
2121
touchX -= rootNodeBox.left;
2222
touchY -= rootNodeBox.top;
2323
}
24-
return getLayerAtPoint(rootLayer, e.type, FrameUtils.make(touchX, touchY, 1, 1));
24+
return getLayerAtPoint(
25+
rootLayer,
26+
e.type,
27+
FrameUtils.make(touchX, touchY, 1, 1),
28+
rootLayer.translateX || 0,
29+
rootLayer.translateY || 0
30+
);
2531
}
2632

2733
/**
@@ -48,11 +54,11 @@ function getHitHandle (type) {
4854
/**
4955
* @private
5056
*/
51-
function getLayerAtPoint (root, type, point) {
57+
function getLayerAtPoint (root, type, point, tx, ty) {
5258
var layer = null;
5359
var hitHandle = getHitHandle(type);
5460
var sortedChildren;
55-
var hitFrame = root.frame;
61+
var hitFrame = FrameUtils.clone(root.frame);
5662

5763
// Early bail for non-visible layers
5864
if (typeof root.alpha === 'number' && root.alpha < 0.01) {
@@ -63,7 +69,13 @@ function getLayerAtPoint (root, type, point) {
6369
if (root.children) {
6470
sortedChildren = root.children.slice().reverse().sort(sortByZIndexDescending);
6571
for (var i=0, len=sortedChildren.length; i < len; i++) {
66-
layer = getLayerAtPoint(sortedChildren[i], type, point);
72+
layer = getLayerAtPoint(
73+
sortedChildren[i],
74+
type,
75+
point,
76+
tx + (root.translateX || 0),
77+
ty + (root.translateY || 0)
78+
);
6779
if (layer) {
6880
break;
6981
}
@@ -78,6 +90,15 @@ function getLayerAtPoint (root, type, point) {
7890
);
7991
}
8092

93+
// Check for x/y translation
94+
if (tx) {
95+
hitFrame.x += tx;
96+
}
97+
98+
if (ty) {
99+
hitFrame.y += ty;
100+
}
101+
81102
// No child layer at the given point. Try the parent layer.
82103
if (!layer && root[hitHandle] && FrameUtils.intersects(hitFrame, point)) {
83104
layer = root;

0 commit comments

Comments
 (0)