Skip to content

Commit

Permalink
Check for TouchEvent class and prevent default actions
Browse files Browse the repository at this point in the history
  • Loading branch information
speigg committed Jun 3, 2017
1 parent 12b884d commit 8584cff
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/webvr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,25 @@ var kButtonWidthDp = 28;
var kTouchSlopFactor = 1.5;
CardboardUI.prototype.listen = function(optionsCallback, backCallback) {
var canvas = this.gl.canvas;
var hasTouchEventClass = typeof TouchEvent !== 'undefined';
this.listener = function(event:MouseEvent|TouchEvent) {
var midline = canvas.clientWidth / 2;
var buttonSize = kButtonWidthDp * kTouchSlopFactor;
var e:MouseEvent|Touch = hasTouchEventClass && event instanceof TouchEvent ?
event.changedTouches[0] :
<MouseEvent>event;
// Check to see if the user clicked on (or around) the gear icon
var e = event instanceof TouchEvent ? event.changedTouches[0] : event;
if (e.clientX > midline - buttonSize &&
e.clientX < midline + buttonSize &&
e.clientY > canvas.clientHeight - buttonSize) {
event.preventDefault();
event.stopImmediatePropagation();
optionsCallback(event);
}
// Check to see if the user clicked on (or around) the back icon
else if (e.clientX < buttonSize && e.clientY < buttonSize) {
event.preventDefault();
event.stopImmediatePropagation();
backCallback(event);
}
};
Expand Down

0 comments on commit 8584cff

Please sign in to comment.