Skip to content

Commit 4b4ae27

Browse files
committed
Fix touch implementation
1 parent e1f60bd commit 4b4ae27

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

src/components/dragelement/index.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
'use strict';
1111

12+
var mouseOffset = require('mouse-event-offset');
13+
1214
var Plotly = require('../../plotly');
1315
var Lib = require('../../lib');
1416

@@ -71,8 +73,9 @@ dragElement.init = function init(options) {
7173
// so that others can look at and modify them
7274
gd._dragged = false;
7375
gd._dragging = true;
74-
startX = e.clientX;
75-
startY = e.clientY;
76+
var offset = pointerOffset(e)
77+
startX = offset[0];
78+
startY = offset[1];
7679
initialTarget = e.target;
7780

7881
newMouseDownTime = (new Date()).getTime();
@@ -90,18 +93,25 @@ dragElement.init = function init(options) {
9093

9194
dragCover = coverSlip();
9295

96+
9397
dragCover.onmousemove = onMove;
98+
dragCover.ontouchmove = onMove;
9499
dragCover.onmouseup = onDone;
95100
dragCover.onmouseout = onDone;
101+
dragCover.ontouchend = onDone;
102+
103+
// var moveEvent = new TouchEvent('touchstart', e)
104+
// dragCover.dispatchEvent(moveEvent)
96105

97106
dragCover.style.cursor = window.getComputedStyle(options.element).cursor;
98107

99108
return Lib.pauseEvent(e);
100109
}
101110

102111
function onMove(e) {
103-
var dx = e.clientX - startX,
104-
dy = e.clientY - startY,
112+
var offset = pointerOffset(e)
113+
var dx = offset[0] - startX,
114+
dy = offset[1] - startY,
105115
minDrag = options.minDrag || constants.MINDRAG;
106116

107117
if(Math.abs(dx) < minDrag) dx = 0;
@@ -118,8 +128,10 @@ dragElement.init = function init(options) {
118128

119129
function onDone(e) {
120130
dragCover.onmousemove = null;
131+
dragCover.ontouchmove = null;
121132
dragCover.onmouseup = null;
122133
dragCover.onmouseout = null;
134+
dragCover.ontouchend = null;
123135
Lib.removeElement(dragCover);
124136

125137
if(!gd._dragging) {
@@ -143,12 +155,13 @@ dragElement.init = function init(options) {
143155
e2 = new MouseEvent('click', e);
144156
}
145157
catch(err) {
158+
var offset = pointerOffset(e)
146159
e2 = document.createEvent('MouseEvents');
147160
e2.initMouseEvent('click',
148161
e.bubbles, e.cancelable,
149162
e.view, e.detail,
150163
e.screenX, e.screenY,
151-
e.clientX, e.clientY,
164+
offset[0], offset[1],
152165
e.ctrlKey, e.altKey, e.shiftKey, e.metaKey,
153166
e.button, e.relatedTarget);
154167
}
@@ -164,6 +177,7 @@ dragElement.init = function init(options) {
164177
}
165178

166179
options.element.onmousedown = onStart;
180+
options.element.ontouchstart = onStart;
167181
options.element.style.pointerEvents = 'all';
168182
};
169183

@@ -191,3 +205,10 @@ function finishDrag(gd) {
191205
gd._dragging = false;
192206
if(gd._replotPending) Plotly.plot(gd);
193207
}
208+
209+
function pointerOffset(e) {
210+
return mouseOffset(
211+
e.changedTouches && e.changedTouches[0] || e,
212+
document.body
213+
)
214+
}

0 commit comments

Comments
 (0)