Skip to content

Commit d1b812b

Browse files
committed
Enable pan/zoom touch interactions
1 parent ab8b03e commit d1b812b

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/plots/gl2d/camera.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
var mouseChange = require('mouse-change');
1313
var mouseWheel = require('mouse-wheel');
14+
var mouseOffset = require('mouse-event-offset');
1415
var cartesianConstants = require('../cartesian/constants');
1516

1617
module.exports = createCamera;
@@ -55,7 +56,23 @@ function createCamera(scene) {
5556
return false;
5657
}
5758

58-
result.mouseListener = mouseChange(element, function(buttons, x, y) {
59+
result.mouseListener = mouseChange(element, handleInteraction);
60+
61+
// enable simple touch interactions
62+
element.addEventListener('touchstart', function(ev) {
63+
var xy = mouseOffset(ev.changedTouches[0], element);
64+
handleInteraction(0, xy[0], xy[1]);
65+
handleInteraction(1, xy[0], xy[1]);
66+
});
67+
element.addEventListener('touchmove', function(ev) {
68+
var xy = mouseOffset(ev.changedTouches[0], element);
69+
handleInteraction(1, xy[0], xy[1]);
70+
});
71+
element.addEventListener('touchend', function() {
72+
handleInteraction(0, result.lastPos[0], result.lastPos[1]);
73+
});
74+
75+
function handleInteraction(buttons, x, y) {
5976
var dataBox = scene.calcDataBox(),
6077
viewBox = plot.viewBox;
6178

@@ -235,7 +252,7 @@ function createCamera(scene) {
235252

236253
result.lastPos[0] = x;
237254
result.lastPos[1] = y;
238-
});
255+
}
239256

240257
result.wheelListener = mouseWheel(element, function(dx, dy) {
241258
var dataBox = scene.calcDataBox(),

0 commit comments

Comments
 (0)