var guac_mouse = this;
/**
+ * The distance a two-finger touch must move per scrollwheel event, in
+ * pixels.
+ */
+ this.scrollThreshold = 20 * (window.devicePixelRatio || 1);
+
+ /**
+ * The maximum number of milliseconds to wait for a touch to end for the
+ * gesture to be considered a click.
+ */
+ this.clickTimingThreshold = 250;
+
+ /**
+ * The maximum number of pixels to allow a touch to move for the gesture to
+ * be considered a click.
+ */
+ this.clickMoveThreshold = 10 * (window.devicePixelRatio || 1);
+
+ /**
* The current mouse state. The properties of this state are updated when
* mouse events fire. This state object is also passed in as a parameter to
* the handler of any mouse events.
e.returnValue = false;
}
- function moveMouse(pageX, pageY) {
+ function moveMouse(clientX, clientY) {
- guac_mouse.currentState.x = pageX - element.offsetLeft;
- guac_mouse.currentState.y = pageY - element.offsetTop;
+ guac_mouse.currentState.x = clientX - element.offsetLeft;
+ guac_mouse.currentState.y = clientY - element.offsetTop;
// This is all JUST so we can get the mouse position within the element
var parent = element.offsetParent;
- while (parent) {
-
- if (parent.offsetLeft)
- guac_mouse.currentState.x -= parent.offsetLeft;
-
- if (parent.offsetTop)
- guac_mouse.currentState.y -= parent.offsetTop;
+ while (parent && !(parent === document.body)) {
+ guac_mouse.currentState.x -= parent.offsetLeft - parent.scrollLeft;
+ guac_mouse.currentState.y -= parent.offsetTop - parent.scrollTop;
parent = parent.offsetParent;
}
+ // Offset by document scroll amount
+ var documentScrollLeft = document.body.scrollLeft || document.documentElement.scrollLeft;
+ var documentScrollTop = document.body.scrollTop || document.documentElement.scrollTop;
+
+ guac_mouse.currentState.x -= parent.offsetLeft - documentScrollLeft;
+ guac_mouse.currentState.y -= parent.offsetTop - documentScrollTop;
+
if (guac_mouse.onmousemove)
guac_mouse.onmousemove(guac_mouse.currentState);
cancelEvent(e);
- moveMouse(e.pageX, e.pageY);
+ moveMouse(e.clientX, e.clientY);
}, false);
element.addEventListener("touchend", function(e) {
- // If we're handling a gesture AND this is the last touch
- if (gesture_in_progress && e.touches.length == 1) {
+ cancelEvent(e);
- cancelEvent(e);
+ // If we're handling a gesture AND this is the last touch
+ if (gesture_in_progress && e.touches.length == 0) {
var time = new Date().getTime();
}
// If single tap detected (based on time and distance)
- if (time - last_touch_time <= 250 && pixels_moved < 10) {
+ if (time - last_touch_time <= guac_mouse.clickTimingThreshold
+ && pixels_moved < guac_mouse.clickMoveThreshold) {
// Fire button down event
guac_mouse.currentState[button] = true;
if (guac_mouse.onmouseup)
guac_mouse.onmouseup(guac_mouse.currentState);
- // Allow mouse events now that touching is over
+ // Gesture now over
gesture_in_progress = false;
-
- }, 250);
+
+ }, guac_mouse.clickTimingThreshold);
}
+ // If we're not waiting to see if this is a click, stop gesture
+ if (!click_release_timeout)
+ gesture_in_progress = false;
+
}
}, false);
element.addEventListener("touchstart", function(e) {
+ cancelEvent(e);
+
// Track number of touches, but no more than three
- touch_count = Math.max(e.touches.length, 3);
+ touch_count = Math.min(e.touches.length, 3);
+
+ // Clear timeout, if set
+ if (click_release_timeout) {
+ window.clearTimeout(click_release_timeout);
+ click_release_timeout = null;
+ }
// Record initial touch location and time for touch movement
// and tap gestures
- if (e.touches.length == 1) {
-
- cancelEvent(e);
+ if (!gesture_in_progress) {
// Stop mouse events while touching
gesture_in_progress = true;
- // Clear timeout, if set
- if (click_release_timeout) {
- window.clearTimeout(click_release_timeout);
- click_release_timeout = null;
- }
-
// Record touch location and time
var starting_touch = e.touches[0];
- last_touch_x = starting_touch.screenX;
- last_touch_y = starting_touch.screenY;
+ last_touch_x = starting_touch.clientX;
+ last_touch_y = starting_touch.clientY;
last_touch_time = new Date().getTime();
pixels_moved = 0;
// Get change in touch location
var touch = e.touches[0];
- var delta_x = touch.screenX - last_touch_x;
- var delta_y = touch.screenY - last_touch_y;
+ var delta_x = touch.clientX - last_touch_x;
+ var delta_y = touch.clientY - last_touch_y;
// Track pixels moved
pixels_moved += Math.abs(delta_x) + Math.abs(delta_y);
- // Update mouse location
- guac_mouse.currentState.x += delta_x;
- guac_mouse.currentState.y += delta_y;
+ // If only one touch involved, this is mouse move
+ if (touch_count == 1) {
- // Prevent mouse from leaving screen
+ // Calculate average velocity in Manhatten pixels per millisecond
+ var velocity = pixels_moved / (new Date().getTime() - last_touch_time);
- if (guac_mouse.currentState.x < 0)
- guac_mouse.currentState.x = 0;
- else if (guac_mouse.currentState.x >= element.offsetWidth)
- guac_mouse.currentState.x = element.offsetWidth - 1;
+ // Scale mouse movement relative to velocity
+ var scale = 1 + velocity;
- if (guac_mouse.currentState.y < 0)
- guac_mouse.currentState.y = 0;
- else if (guac_mouse.currentState.y >= element.offsetHeight)
- guac_mouse.currentState.y = element.offsetHeight - 1;
+ // Update mouse location
+ guac_mouse.currentState.x += delta_x*scale;
+ guac_mouse.currentState.y += delta_y*scale;
- // Fire movement event, if defined
- if (guac_mouse.onmousemove)
- guac_mouse.onmousemove(guac_mouse.currentState);
+ // Prevent mouse from leaving screen
+
+ if (guac_mouse.currentState.x < 0)
+ guac_mouse.currentState.x = 0;
+ else if (guac_mouse.currentState.x >= element.offsetWidth)
+ guac_mouse.currentState.x = element.offsetWidth - 1;
+
+ if (guac_mouse.currentState.y < 0)
+ guac_mouse.currentState.y = 0;
+ else if (guac_mouse.currentState.y >= element.offsetHeight)
+ guac_mouse.currentState.y = element.offsetHeight - 1;
- // Update touch location
- last_touch_x = touch.screenX;
- last_touch_y = touch.screenY;
+ // Fire movement event, if defined
+ if (guac_mouse.onmousemove)
+ guac_mouse.onmousemove(guac_mouse.currentState);
+
+ // Update touch location
+ last_touch_x = touch.clientX;
+ last_touch_y = touch.clientY;
+
+ }
+
+ // Interpret two-finger swipe as scrollwheel
+ else if (touch_count == 2) {
+
+ // If change in location passes threshold for scroll
+ if (Math.abs(delta_y) >= guac_mouse.scrollThreshold) {
+
+ // Decide button based on Y movement direction
+ var button;
+ if (delta_y > 0) button = "down";
+ else button = "up";
+
+ // Fire button down event
+ guac_mouse.currentState[button] = true;
+ if (guac_mouse.onmousedown)
+ guac_mouse.onmousedown(guac_mouse.currentState);
+
+ // Fire button up event
+ guac_mouse.currentState[button] = false;
+ if (guac_mouse.onmouseup)
+ guac_mouse.onmouseup(guac_mouse.currentState);
+
+ // Only update touch location after a scroll has been
+ // detected
+ last_touch_x = touch.clientX;
+ last_touch_y = touch.clientY;
+
+ }
+
+ }
}, false);