1 var bindEvents, buttonIsDown, coordsForTouchEvent, position;
3 coordsForTouchEvent = function(el, e) {
5 tx = e.changedTouches[0].clientX;
6 ty = e.changedTouches[0].clientY;
7 p = el.getBoundingClientRect();
8 return [tx - p.left, ty - p.top];
11 position = function(el, e) {
13 p = el.getBoundingClientRect();
15 left: e.clientX - p.left,
16 top: e.clientY - p.top
20 buttonIsDown = function(e) {
21 if (e.buttons != null) {
22 return e.buttons === 1;
28 module.exports = bindEvents = function(lc, canvas, panWithKeyboard) {
29 var listener, mouseMoveListener, mouseUpListener, touchEndListener, touchMoveListener, unsubs;
30 if (panWithKeyboard == null) {
31 panWithKeyboard = false;
34 mouseMoveListener = (function(_this) {
38 p = position(canvas, e);
39 return lc.pointerMove(p.left, p.top);
42 mouseUpListener = (function(_this) {
46 canvas.onselectstart = function() {
49 p = position(canvas, e);
50 lc.pointerUp(p.left, p.top);
51 document.removeEventListener('mousemove', mouseMoveListener);
52 document.removeEventListener('mouseup', mouseUpListener);
53 return canvas.addEventListener('mousemove', mouseMoveListener);
56 canvas.addEventListener('mousedown', (function(_this) {
59 if (e.target.tagName.toLowerCase() !== 'canvas') {
64 canvas.onselectstart = function() {
67 p = position(canvas, e);
68 lc.pointerDown(p.left, p.top);
69 canvas.removeEventListener('mousemove', mouseMoveListener);
70 document.addEventListener('mousemove', mouseMoveListener);
71 return document.addEventListener('mouseup', mouseUpListener);
74 touchMoveListener = function(e) {
76 return lc.pointerMove.apply(lc, coordsForTouchEvent(canvas, e));
78 touchEndListener = function(e) {
80 lc.pointerUp.apply(lc, coordsForTouchEvent(canvas, e));
81 document.removeEventListener('touchmove', touchMoveListener);
82 document.removeEventListener('touchend', touchEndListener);
83 return document.removeEventListener('touchcancel', touchEndListener);
85 canvas.addEventListener('touchstart', function(e) {
86 if (e.target.tagName.toLowerCase() !== 'canvas') {
90 if (e.touches.length === 1) {
91 lc.pointerDown.apply(lc, coordsForTouchEvent(canvas, e));
92 document.addEventListener('touchmove', touchMoveListener);
93 document.addEventListener('touchend', touchEndListener);
94 return document.addEventListener('touchcancel', touchEndListener);
96 return lc.pointerMove.apply(lc, coordsForTouchEvent(canvas, e));
99 if (panWithKeyboard) {
100 console.warn("Keyboard panning is deprecated.");
101 listener = function(e) {
115 return lc.repaintAllLayers();
117 document.addEventListener('keydown', listener);
118 unsubs.push(function() {
119 return document.removeEventListener(listener);
123 var f, i, len, results;
125 for (i = 0, len = unsubs.length; i < len; i++) {