hammerjs
Version:
A javascript library for multi-touch gestures
78 lines (67 loc) • 1.79 kB
JavaScript
var PointerEvent = Hammer.PointerEvent = {
/**
* holds all pointers
* @type {Object}
*/
pointers: {},
/**
* get a list of pointers
* @returns {Array} touchlist
*/
getTouchList: function getTouchList() {
var touchlist = [];
// we can use forEach since pointerEvents only is in IE10
Utils.each(this.pointers, function(pointer){
touchlist.push(pointer);
});
return touchlist;
},
/**
* update the position of a pointer
* @param {String} type EVENT_END
* @param {Object} pointerEvent
*/
updatePointer: function updatePointer(type, pointerEvent) {
if(type == EVENT_END) {
delete this.pointers[pointerEvent.pointerId];
}
else {
pointerEvent.identifier = pointerEvent.pointerId;
this.pointers[pointerEvent.pointerId] = pointerEvent;
}
// it's save to use Object.keys, since pointerEvents are only in newer browsers
return Object.keys(this.pointers).length;
},
/**
* check if ev matches pointertype
* @param {String} pointerType POINTER_MOUSE
* @param {PointerEvent} ev
*/
matchType: function matchType(pointerType, ev) {
if(!ev.pointerType) {
return false;
}
var pt = ev.pointerType
, types = {};
types[POINTER_MOUSE] = (pt === POINTER_MOUSE);
types[POINTER_TOUCH] = (pt === POINTER_TOUCH);
types[POINTER_PEN] = (pt === POINTER_PEN);
return types[pointerType];
},
/**
* get events
*/
getEvents: function getEvents() {
return [
'pointerdown MSPointerDown',
'pointermove MSPointerMove',
'pointerup pointercancel MSPointerUp MSPointerCancel'
];
},
/**
* reset the list
*/
reset: function resetList() {
this.pointers = {};
}
};