Skip to content

Commit 1a392a7

Browse files
committed
Fix for broken touch handling (move/press/click on mobiles)
1 parent aa554a3 commit 1a392a7

File tree

1 file changed

+65
-30
lines changed

1 file changed

+65
-30
lines changed

virtualsky.js

+65-30
Original file line numberDiff line numberDiff line change
@@ -1381,27 +1381,60 @@ VirtualSky.prototype.createSky = function(){
13811381
ctx.fillText(loading,(ctx.wide-ctx.measureText(loading).width)/2,(this.tall-fs)/2);
13821382
ctx.fill();
13831383

1384-
var contextMenuHandler = (!this.callback.contextmenu) ? undefined : {
1385-
longPressStart: function(x,y){
1386-
contextMenuHandler.longPressStop();
1387-
this.longPressTimer = window.setTimeout(function() {
1388-
this.longPressTimer = undefined;
1389-
this.dragging = false;
1390-
this.x = "";
1391-
this.y = "";
1392-
this.theta = "";
1393-
contextMenuHandler.click(x,y);
1394-
}, 400 /** 400ms for long press */);
1384+
var touchClickHandler = (!this.callback.contextmenu && !this.callback.click) ? undefined : {
1385+
// Indicate an immobile press is occuring. It will lead to context menu or click depending on its duration
1386+
clickActive: false,
1387+
clickDone: false,
1388+
// Timer that differentiate between long/short press
1389+
longPressTimer: undefined,
1390+
1391+
clickStart: function(e){
1392+
e.originalEvent.preventDefault();
1393+
touchClickHandler.clickCancel();
1394+
touchClickHandler.clickActive = true;
1395+
touchClickHandler.clickDone = false;
1396+
touchClickHandler.initialTouchEvent = e;
1397+
if (this.callback.contextmenu) {
1398+
touchClickHandler.longPressTimer = window.setTimeout(function() {
1399+
touchClickHandler.clickActive = false;
1400+
touchClickHandler.longPressTimer = undefined;
1401+
this.dragging = false;
1402+
this.x = "";
1403+
this.y = "";
1404+
this.theta = "";
1405+
1406+
if (this.callback.contextmenu) {
1407+
touchClickHandler.clickDone = true;
1408+
this.callback.contextmenu.call(e.data.sky, e);
1409+
}
1410+
}.bind(this), 400 /** 400ms for long press */);
1411+
}
1412+
}.bind(this),
1413+
1414+
clickEnd: function(e) {
1415+
if (touchClickHandler.clickActive) {
1416+
var initialTouchEvent = touchClickHandler.initialTouchEvent;
1417+
touchClickHandler.clickCancel();
1418+
1419+
if(e.data.sky.callback.click){
1420+
touchClickHandler.clickDone = true;
1421+
e.data.sky.callback.click.call(initialTouchEvent.data.sky, initialTouchEvent);
1422+
}
1423+
}
13951424
}.bind(this),
1396-
longPressStop: function(){
1397-
if (this.longPressTimer !== undefined) {
1398-
window.clearTimeout(this.longPressTimer);
1399-
this.longPressTimer = undefined;
1425+
1426+
clickCancel: function(){
1427+
touchClickHandler.clickActive = false;
1428+
touchClickHandler.initialTouchEvent = undefined;
1429+
if (touchClickHandler.longPressTimer !== undefined) {
1430+
window.clearTimeout(touchClickHandler.longPressTimer);
1431+
touchClickHandler.longPressTimer = undefined;
14001432
}
1401-
}.bind(this)
1433+
return !touchClickHandler.clickDone;
1434+
}.bind(this),
14021435
};
14031436

1404-
function getXYProperties(e,sky){
1437+
getXYProperties =function (e,sky){
14051438
e.matched = sky.whichPointer(e.x,e.y);
14061439
var skyPos = sky.xy2radec(e.x,e.y);
14071440
if(skyPos){
@@ -1415,6 +1448,11 @@ VirtualSky.prototype.createSky = function(){
14151448
e.y = o.pageY - el.offset().top - window.scrollY;
14161449
return getXYProperties(e,sky);
14171450
}
1451+
function getTouchXY(sky,o,el,e) {
1452+
e.x = o.touches[0].pageX - el.offset().left - window.scrollX;
1453+
e.y = o.touches[0].pageY - el.offset().top - window.scrollY;
1454+
return getXYProperties(e,sky);
1455+
}
14181456

14191457
S("#"+this.idinner).on('click',{sky:this},function(e){
14201458
e.data.sky.debug('click');
@@ -1496,7 +1534,11 @@ VirtualSky.prototype.createSky = function(){
14961534
if(typeof s.callback.mouseenter=="function") s.callback.mouseenter.call(s);
14971535
}).on('touchmove',{sky:this},function(e){
14981536
e.preventDefault();
1499-
if(contextMenuHandler) contextMenuHandler.longPressStop();
1537+
if(touchClickHandler) {
1538+
if (!touchClickHandler.clickCancel()) {
1539+
return;
1540+
}
1541+
}
15001542
var s = e.data.sky;
15011543
var x = e.originalEvent.touches[0].pageX;
15021544
var y = e.originalEvent.touches[0].pageY;
@@ -1529,25 +1571,18 @@ VirtualSky.prototype.createSky = function(){
15291571
}).on('touchstart',{sky:this},function(e){
15301572
e.data.sky.debug('touchstart');
15311573
e.data.sky.dragging = true;
1532-
if(contextMenuHandler){
1533-
var x = e.originalEvent.touches[0].pageX;
1534-
var y = e.originalEvent.touches[0].pageY;
1535-
x = x - this.offset().left - window.scrollX;
1536-
y = y - this.offset().top - window.scrollY;
1537-
contextMenuHandler.longPressStart(x, y);
1538-
if(e.data.sky.callback.click){
1539-
e.x = x;
1540-
e.y = y;
1541-
e.data.sky.callback.click.call(e.data.sky,getXYProperties(e,e.data.sky));
1542-
}
1574+
if(touchClickHandler){
1575+
touchClickHandler.clickStart(getTouchXY(e.data.sky,e.originalEvent,this,e));
15431576
}
15441577
}).on('touchend',{sky:this},function(e){
15451578
e.data.sky.debug('touchend');
15461579
e.data.sky.dragging = false;
15471580
e.data.sky.x = "";
15481581
e.data.sky.y = "";
15491582
e.data.sky.theta = "";
1550-
if(contextMenuHandler) contextMenuHandler.longPressStop();
1583+
if(touchClickHandler) {
1584+
touchClickHandler.clickEnd(e);
1585+
}
15511586
}).on((isEventSupported('mousewheel') ? 'mousewheel' : 'wheel'),{sky:this},function(e) {
15521587
e.preventDefault();
15531588
e.data.sky.debug('mousewheel');

0 commit comments

Comments
 (0)