Skip to content

Commit 91421a4

Browse files
committed
PointerEvent: Polyfill offsetX/Y as getter
WIP: No idea how to write a unit test for this, so for now it only extends the simple sample. Tested with the sample on Chrome, Firefox and Safari (all OSX) and Android 5 (BrowserStack) and iOS 9.1 (iOS Simulator). Fixes jquery-archive#217
1 parent 222f046 commit 91421a4

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

samples/simple/index.html

+7-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
height: 300px;
2222
width: 300px;
2323
text-align: center;
24+
position: relative;
25+
top: 10px;
26+
left: 10px;
27+
margin-bottom: 10px;
2428
}
2529
#enterleave {
2630
background-color: blue;
@@ -34,7 +38,6 @@
3438
position: relative;
3539
}
3640
#output {
37-
width: 300px;
3841
height: 150px;
3942
overflow: scroll;
4043
white-space: pre;
@@ -72,7 +75,9 @@
7275
find('capture', 'output', 'enterleave');
7376
events.forEach(function(en) {
7477
capture.addEventListener(en, function(inEvent) {
75-
appendOutput(inEvent.type + ' [' + inEvent.pointerId + ']');
78+
appendOutput(inEvent.type + ' [' + inEvent.pointerId + ']' +
79+
' pageX/offsetX ' + inEvent.pageX + '/' + inEvent.offsetX +
80+
' pageY/offsetY ' + inEvent.pageY + '/' + inEvent.offsetY);
7681
});
7782
});
7883
enterleave.addEventListener('pointerenter', function(e) {

src/PointerEvent.js

+29
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,35 @@ function PointerEvent(inType, inDict) {
8787
e.pointerType = inDict.pointerType || '';
8888
e.hwTimestamp = inDict.hwTimestamp || 0;
8989
e.isPrimary = inDict.isPrimary || false;
90+
91+
var rect;
92+
function boundingClientRect() {
93+
if (rect) {
94+
return rect;
95+
}
96+
return (rect = inDict.target.getBoundingClientRect());
97+
}
98+
99+
// CSSOM View Module's extension to mouse events
100+
if (inDict.offsetX === undefined) {
101+
Object.defineProperty(e, 'offsetX', {
102+
get: function() {
103+
return e.pageX - boundingClientRect().left;
104+
}
105+
});
106+
} else {
107+
e.offsetX = inDict.offsetX;
108+
}
109+
if (inDict.offsetY === undefined) {
110+
Object.defineProperty(e, 'offsetY', {
111+
get: function() {
112+
return e.pageY - boundingClientRect().top;
113+
}
114+
});
115+
} else {
116+
e.offsetY = inDict.offsetY;
117+
}
118+
90119
return e;
91120
}
92121

0 commit comments

Comments
 (0)