fingers.js 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556
  1. /*! Fingers.js - v1.0.5 - 2014-09-03
  2. * https://github.com/paztis/fingers.js
  3. *
  4. * Copyright (c) 2014 Jérôme HENAFF <jerome.henaff@gmail.com>;
  5. * Licensed under the MIT license */
  6. (function(window, undefined) {
  7. 'use strict';
  8. var Fingers = function Fingers(pElement) {
  9. return new Fingers.Instance(pElement);
  10. };
  11. Fingers.__extend = function(obj) {
  12. Array.prototype.slice.call(arguments, 1).forEach(function(source) {
  13. if (source) {
  14. for (var prop in source) {
  15. obj[prop] = source[prop];
  16. }
  17. }
  18. });
  19. return obj;
  20. };
  21. /**
  22. * @module fingers
  23. *
  24. * @class Utils
  25. */
  26. var Utils = {
  27. DIRECTION: {
  28. UP: 'up',
  29. DOWN: 'down',
  30. LEFT: 'left',
  31. RIGHT: 'right'
  32. },
  33. GROW: {
  34. IN: 'in',
  35. OUT: 'out'
  36. },
  37. getVelocity: function(deltaTime, deltaPos) {
  38. return Math.abs(deltaPos / deltaTime) || 0;
  39. },
  40. getOrientedVelocity: function(deltaTime, deltaPos) {
  41. return (deltaPos / deltaTime) || 0;
  42. },
  43. getAngle: function(x, y) {
  44. return Math.atan2(x, y);
  45. },
  46. getDirection: function(deltaX, deltaY) {
  47. if(Math.abs(deltaX) >= Math.abs(deltaY)) {
  48. return (deltaX > 0) ? this.DIRECTION.RIGHT : this.DIRECTION.LEFT;
  49. }
  50. else {
  51. return (deltaY > 0) ? this.DIRECTION.DOWN : this.DIRECTION.UP;
  52. }
  53. },
  54. isVertical: function isVertical(direction) {
  55. return direction === this.DIRECTION.UP || direction === this.DIRECTION.DOWN;
  56. },
  57. getDistance: function(x, y) {
  58. return Math.sqrt((x * x) + (y * y));
  59. }
  60. };
  61. Fingers.Utils = Utils;
  62. /**
  63. * @module fingers
  64. *
  65. * @class CacheArray
  66. * @constructor
  67. * @return {CacheArray}
  68. */
  69. var CacheArray = function() {
  70. this._cache = [];
  71. };
  72. CacheArray.prototype = {
  73. _cache: null,
  74. isCachedValue: function(pIndex) {
  75. return (this._cache[pIndex] !== undefined);
  76. },
  77. getCachedValue: function(pIndex) {
  78. return this._cache[pIndex];
  79. },
  80. setCachedValue: function(pIndex, pValue) {
  81. this._cache[pIndex] = pValue;
  82. },
  83. clearCachedValue: function(pIndex) {
  84. delete this._cache[pIndex];
  85. },
  86. clearCache: function() {
  87. this._cache.length = 0;
  88. },
  89. getCachedValueOrUpdate: function(pIndex, pUpdateF, pUpdateContext) {
  90. var cacheValue = this.getCachedValue(pIndex);
  91. if(cacheValue === undefined) {
  92. cacheValue = pUpdateF.call(pUpdateContext);
  93. this.setCachedValue(pIndex, cacheValue);
  94. }
  95. return cacheValue;
  96. }
  97. };
  98. Fingers.CacheArray = CacheArray;
  99. /**
  100. * @module fingers
  101. */
  102. /**
  103. * create new fingers instance
  104. * all methods should return the instance itself, so it is chainable.
  105. *
  106. * @class Instance
  107. * @constructor
  108. * @param {HTMLElement} pElement
  109. * @return {Instance}
  110. */
  111. var Instance = function(pElement) {
  112. this._init(pElement);
  113. };
  114. Instance.HAS_TOUCHEVENTS = ('ontouchstart' in window);
  115. Instance.IS_MOBILE = /mobile|tablet|ip(ad|hone|od)|android|silk/i.test(navigator.userAgent);
  116. Instance.LISTEN_TOUCH_EVENTS = (Instance.HAS_TOUCHEVENTS && Instance.IS_MOBILE);
  117. /**
  118. * @property FINGER_MAP
  119. * @type {Object.<Number>, Finger>}
  120. */
  121. Instance.FINGER_MAP = {};
  122. Instance.prototype = {
  123. /**
  124. * @property element
  125. * @type {HTMLElement}
  126. */
  127. element: null,
  128. /**
  129. * @property fingerList
  130. * @type {Array.<Finger>}
  131. */
  132. fingerList: null,
  133. /**
  134. * @property fingerCreatedMap
  135. * @type {Object.<Number>, Finger>}
  136. */
  137. fingerCreatedMap: null,
  138. /**
  139. * @property fingerIdList
  140. * @type {Array.<Gesture>}
  141. */
  142. gestureList: null,
  143. /*---- INIT ----*/
  144. _init: function(pElement) {
  145. this.element = pElement;
  146. this.fingerList = [];
  147. this.fingerCreatedMap = {};
  148. this.gestureList = [];
  149. this.startListening();
  150. },
  151. getElement: function() {
  152. return this.element;
  153. },
  154. /*---- gestures ----*/
  155. getGestures: function() {
  156. return this.gestureList;
  157. },
  158. addGesture: function(PGestureClass, pOptions) {
  159. var gesture = new PGestureClass(pOptions);
  160. this.gestureList.push(gesture);
  161. return gesture;
  162. },
  163. removeGesture: function(pGesture) {
  164. pGesture.removeAllHandlers();
  165. var index = this.gestureList.indexOf(pGesture);
  166. this.gestureList.splice(index, 1);
  167. },
  168. removeAllGestures: function() {
  169. for(var i= 0, size=this.gestureList.length; i<size; i++) {
  170. this.gestureList[i].removeAllHandlers();
  171. }
  172. this.gestureList.length = 0;
  173. },
  174. /*---- Native event listening ----*/
  175. startListening: function() {
  176. if(this._stopListeningF === null) {
  177. var _this = this;
  178. if(Instance.LISTEN_TOUCH_EVENTS) {
  179. var onTouchStartF = this._onTouchStart.bind(this);
  180. var onTouchMoveF = this._onTouchMove.bind(this);
  181. var onTouchEndF = this._onTouchEnd.bind(this);
  182. var onTouchCancelF = this._onTouchCancel.bind(this);
  183. this.element.addEventListener("touchstart", onTouchStartF);
  184. this.element.addEventListener("touchmove", onTouchMoveF);
  185. this.element.addEventListener("touchend", onTouchEndF);
  186. this.element.addEventListener("touchcancel", onTouchCancelF);
  187. this._stopListeningF = function() {
  188. _this.element.removeEventListener("touchstart", onTouchStartF);
  189. _this.element.removeEventListener("touchmove", onTouchMoveF);
  190. _this.element.removeEventListener("touchend", onTouchEndF);
  191. _this.element.removeEventListener("touchcancel", onTouchCancelF);
  192. };
  193. }
  194. else {
  195. this._onMouseMoveF = this._onMouseMove.bind(this);
  196. this._onMouseUpF = this._onMouseUp.bind(this);
  197. var onMouseDownF = this._onMouseDown.bind(this);
  198. this.element.addEventListener("mousedown", onMouseDownF);
  199. this._stopListeningF = function() {
  200. _this.element.removeEventListener("mousedown", onMouseDownF);
  201. document.removeEventListener("mousemove", this._onMouseMoveF);
  202. document.removeEventListener("mouseup", this._onMouseUpF);
  203. };
  204. }
  205. }
  206. },
  207. _stopListeningF: null,
  208. stopListening: function() {
  209. if(this._stopListeningF !== null) {
  210. this._removeAllFingers(Date.now());
  211. this._stopListeningF();
  212. this._stopListeningF = null;
  213. }
  214. },
  215. /*-------- Touch events ----*/
  216. _onTouchStart: function(pTouchEvent) {
  217. var touch;
  218. for(var i= 0, size=pTouchEvent.changedTouches.length; i<size; i++) {
  219. touch = pTouchEvent.changedTouches[i];
  220. this._createFinger(touch.identifier, pTouchEvent.timeStamp, touch.pageX, touch.pageY);
  221. }
  222. },
  223. _onTouchMove: function(pTouchEvent) {
  224. var touch;
  225. for(var i= 0, size=pTouchEvent.changedTouches.length; i<size; i++) {
  226. touch = pTouchEvent.changedTouches[i];
  227. this._updateFingerPosition(touch.identifier, pTouchEvent.timeStamp, touch.pageX, touch.pageY);
  228. }
  229. pTouchEvent.preventDefault();
  230. },
  231. _onTouchEnd: function(pTouchEvent) {
  232. for(var i= 0, size=pTouchEvent.changedTouches.length; i<size; i++) {
  233. this._removeFinger(pTouchEvent.changedTouches[i].identifier, pTouchEvent.timeStamp);
  234. }
  235. },
  236. _onTouchCancel: function(pTouchEvent) {
  237. //Security to prevent chrome bugs
  238. var finger;
  239. for(var i= 0, size=pTouchEvent.changedTouches.length; i<size; i++) {
  240. finger = Instance.FINGER_MAP[pTouchEvent.changedTouches[i].identifier];
  241. if(finger !== undefined && this._getFingerPosition(finger) !== -1) {
  242. //Remove all fingers
  243. this._removeAllFingers(pTouchEvent.timeStamp);
  244. break;
  245. }
  246. }
  247. },
  248. /*-------- Mouse events ----*/
  249. _onMouseDown: function(pMouseEvent) {
  250. if(pMouseEvent.button === 0) {
  251. //Prevention against alert popups that loose mouse finger reference
  252. var finger = Instance.FINGER_MAP[pMouseEvent.button];
  253. if(finger === undefined || this._getFingerPosition(finger) === -1) {
  254. document.addEventListener("mousemove", this._onMouseMoveF);
  255. document.addEventListener("mouseup", this._onMouseUpF);
  256. this._createFinger(pMouseEvent.button, pMouseEvent.timeStamp, pMouseEvent.pageX, pMouseEvent.pageY);
  257. pMouseEvent.preventDefault();
  258. }
  259. }
  260. },
  261. _onMouseMoveF: null,
  262. _onMouseMove: function(pMouseEvent) {
  263. if(pMouseEvent.button === 0) {
  264. this._updateFingerPosition(pMouseEvent.button, pMouseEvent.timeStamp, pMouseEvent.pageX, pMouseEvent.pageY);
  265. }
  266. },
  267. _onMouseUpF: null,
  268. _onMouseUp: function(pMouseEvent) {
  269. //In all cases, remove listener
  270. document.removeEventListener("mousemove", this._onMouseMoveF);
  271. document.removeEventListener("mouseup", this._onMouseUpF);
  272. this._removeFinger(0, pMouseEvent.timeStamp);
  273. },
  274. /*---- Fingers ----*/
  275. _createFinger: function(pFingerId, pTimestamp, pX, pY) {
  276. var finger;
  277. if(Instance.FINGER_MAP[pFingerId] === undefined) {
  278. finger = new Finger(pFingerId, pTimestamp, pX, pY);
  279. Instance.FINGER_MAP[pFingerId] = finger;
  280. this.fingerCreatedMap[pFingerId] = finger;
  281. }
  282. else {
  283. finger = Instance.FINGER_MAP[pFingerId];
  284. }
  285. this.fingerList.push(finger);
  286. finger.nbListeningInstances++;
  287. for(var i=0, size=this.gestureList.length; i<size; i++) {
  288. this.gestureList[i]._onFingerAdded(finger, this.fingerList);
  289. }
  290. },
  291. _removeFinger: function(pFingerId, pTimestamp) {
  292. var finger = Instance.FINGER_MAP[pFingerId];
  293. if(finger !== undefined) {
  294. this.fingerList.splice(this._getFingerPosition(finger), 1);
  295. delete this.fingerCreatedMap[finger.id];
  296. finger.nbListeningInstances--;
  297. //Only last one can remove a finger
  298. if(finger.nbListeningInstances === 0) {
  299. finger._setEndP(pTimestamp);
  300. delete Instance.FINGER_MAP[finger.id];
  301. finger._clearHandlerObjects();
  302. }
  303. }
  304. },
  305. _removeAllFingers: function(pTimestamp) {
  306. var list = this.fingerList.splice(0);
  307. for(var i= 0, size=list.length; i<size; i++) {
  308. this._removeFinger(list[i].id, pTimestamp);
  309. }
  310. },
  311. _updateFingerPosition: function(pFingerId, pTimestamp, pX, pY) {
  312. //Only creator can update a finger
  313. var finger = this.fingerCreatedMap[pFingerId];
  314. if(finger !== undefined) {
  315. finger._setCurrentP(pTimestamp, pX, pY);
  316. }
  317. },
  318. /*---- utils ----*/
  319. _getFingerPosition: function(pFinger) {
  320. return this.fingerList.indexOf(pFinger);
  321. }
  322. };
  323. Fingers.Instance = Instance;
  324. /**
  325. * @module fingers
  326. *
  327. * @class Finger
  328. * @constructor
  329. * @param {Number} pId
  330. * @param {Number} pTimestamp
  331. * @param {Number} pX
  332. * @param {Number} pY
  333. * @return {Finger}
  334. */
  335. var Finger = function(pId, pTimestamp, pX, pY) {
  336. this.id = pId;
  337. this.state = Finger.STATE.ACTIVE;
  338. this._handlerList = [];
  339. this.startP = new Position(pTimestamp, pX, pY);
  340. this.previousP = new Position(pTimestamp, pX, pY);
  341. this.currentP = new Position(pTimestamp, pX, pY);
  342. this._cacheArray = new CacheArray();
  343. };
  344. var CACHE_INDEX_CREATOR = 0;
  345. Finger.cacheIndexes = {
  346. deltaTime: CACHE_INDEX_CREATOR++,
  347. totalTime: CACHE_INDEX_CREATOR++,
  348. deltaX: CACHE_INDEX_CREATOR++,
  349. deltaY: CACHE_INDEX_CREATOR++,
  350. deltaDistance: CACHE_INDEX_CREATOR++,
  351. totalX: CACHE_INDEX_CREATOR++,
  352. totalY: CACHE_INDEX_CREATOR++,
  353. totalDistance: CACHE_INDEX_CREATOR++,
  354. deltaDirection: CACHE_INDEX_CREATOR++,
  355. totalDirection: CACHE_INDEX_CREATOR++,
  356. velocityX: CACHE_INDEX_CREATOR++,
  357. velocityY: CACHE_INDEX_CREATOR++,
  358. velocity: CACHE_INDEX_CREATOR++,
  359. velocityAverage: CACHE_INDEX_CREATOR++,
  360. orientedVelocityX: CACHE_INDEX_CREATOR++,
  361. orientedVelocityY: CACHE_INDEX_CREATOR++
  362. };
  363. Finger.STATE = {
  364. ACTIVE: "active",
  365. REMOVED: "removed"
  366. };
  367. Finger.CONSTANTS = {
  368. inactivityTime: 100
  369. };
  370. Finger.prototype = {
  371. /**
  372. * @property id
  373. * @type {Number}
  374. */
  375. id: null,
  376. state: null,
  377. startP: null,
  378. previousP: null,
  379. currentP: null,
  380. nbListeningInstances: 0,
  381. _cacheArray: null,
  382. _handlerList: null,
  383. _handlerListSize: 0,
  384. _addHandlerObject: function(pHandlerObject) {
  385. this._handlerList.push(pHandlerObject);
  386. this._handlerListSize = this._handlerList.length;
  387. },
  388. _removeHandlerObject: function(pHandlerObject) {
  389. var index = this._handlerList.indexOf(pHandlerObject);
  390. this._handlerList.splice(index, 1);
  391. this._handlerListSize = this._handlerList.length;
  392. },
  393. _clearHandlerObjects: function() {
  394. this._handlerList.length = 0;
  395. this._handlerListSize = 0;
  396. },
  397. _setCurrentP: function(pTimestamp, pX, pY, pForceSetter) {
  398. if(this.getX() != pX || this.getY() != pY || pForceSetter) { //Prevent chrome multiple events for same position (radiusX, radiusY)
  399. this._cacheArray.clearCache();
  400. this.previousP.copy(this.currentP);
  401. this.currentP.set(pTimestamp, pX, pY);
  402. for(var i= 0; i<this._handlerListSize; i++) {
  403. this._handlerList[i]._onFingerUpdate(this);
  404. }
  405. }
  406. },
  407. _setEndP: function(pTimestamp) {
  408. //Only update if end event is not "instant" with move event
  409. if((pTimestamp - this.getTime()) > Finger.CONSTANTS.inactivityTime) {
  410. this._setCurrentP(pTimestamp, this.getX(), this.getY(), true);
  411. }
  412. this.state = Finger.STATE.REMOVED;
  413. var handlerList = this._handlerList.slice(0);
  414. for(var i= 0; i<handlerList.length; i++) {
  415. handlerList[i]._onFingerRemoved(this);
  416. }
  417. },
  418. /*---- time ----*/
  419. getTime: function() {
  420. return this.currentP.timestamp;
  421. },
  422. getDeltaTime: function() {
  423. return this._cacheArray.getCachedValueOrUpdate(Finger.cacheIndexes.deltaTime, this._getDeltaTime, this);
  424. },
  425. _getDeltaTime: function() {
  426. return this.currentP.timestamp - this.previousP.timestamp;
  427. },
  428. getTotalTime: function() {
  429. return this._cacheArray.getCachedValueOrUpdate(Finger.cacheIndexes.totalTime, this._getTotalTime, this);
  430. },
  431. _getTotalTime: function() {
  432. return this.currentP.timestamp - this.startP.timestamp;
  433. },
  434. getInactivityTime: function() {
  435. var delta = Date.now() - this.currentP.timestamp;
  436. return (delta > Finger.CONSTANTS.inactivityTime) ? delta : 0;
  437. },
  438. /*---- position ----*/
  439. getX: function() {
  440. return this.currentP.x;
  441. },
  442. getY: function() {
  443. return this.currentP.y;
  444. },
  445. /*---- distance ----*/
  446. getDeltaX: function() {
  447. return this._cacheArray.getCachedValueOrUpdate(Finger.cacheIndexes.deltaX, this._getDeltaX, this);
  448. },
  449. _getDeltaX: function() {
  450. return this.currentP.x - this.previousP.x;
  451. },
  452. getDeltaY: function() {
  453. return this._cacheArray.getCachedValueOrUpdate(Finger.cacheIndexes.deltaY, this._getDeltaY, this);
  454. },
  455. _getDeltaY: function() {
  456. return this.currentP.y - this.previousP.y;
  457. },
  458. getDeltaDistance: function() {
  459. return this._cacheArray.getCachedValueOrUpdate(Finger.cacheIndexes.deltaDistance, this._getDeltaDistance, this);
  460. },
  461. _getDeltaDistance: function() {
  462. return Utils.getDistance(this.getDeltaX(), this.getDeltaY());
  463. },
  464. getTotalX: function() {
  465. return this._cacheArray.getCachedValueOrUpdate(Finger.cacheIndexes.totalX, this._getTotalX, this);
  466. },
  467. _getTotalX: function() {
  468. return this.currentP.x - this.startP.x;
  469. },
  470. getTotalY: function() {
  471. return this._cacheArray.getCachedValueOrUpdate(Finger.cacheIndexes.totalY, this._getTotalY, this);
  472. },
  473. _getTotalY: function() {
  474. return this.currentP.y - this.startP.y;
  475. },
  476. getDistance: function() {
  477. return this._cacheArray.getCachedValueOrUpdate(Finger.cacheIndexes.totalDistance, this._getDistance, this);
  478. },
  479. _getDistance: function() {
  480. return Utils.getDistance(this.getTotalX(), this.getTotalY());
  481. },
  482. /*---- direction ----*/
  483. getDeltaDirection: function() {
  484. return this._cacheArray.getCachedValueOrUpdate(Finger.cacheIndexes.deltaDirection, this._getDeltaDirection, this);
  485. },
  486. _getDeltaDirection: function() {
  487. return Utils.getDirection(this.getDeltaX(), this.getDeltaY());
  488. },
  489. getDirection: function() {
  490. return this._cacheArray.getCachedValueOrUpdate(Finger.cacheIndexes.totalDirection, this._getDirection, this);
  491. },
  492. _getDirection: function() {
  493. return Utils.getDirection(this.getTotalX(), this.getTotalY());
  494. },
  495. /*---- velocity ----*/
  496. getVelocityX: function() {
  497. return this._cacheArray.getCachedValueOrUpdate(Finger.cacheIndexes.velocityX, this._getVelocityX, this);
  498. },
  499. _getVelocityX: function() {
  500. return Utils.getVelocity(this.getDeltaTime(), this.getDeltaX());
  501. },
  502. getVelocityY: function() {
  503. return this._cacheArray.getCachedValueOrUpdate(Finger.cacheIndexes.velocityY, this._getVelocityY, this);
  504. },
  505. _getVelocityY: function() {
  506. return Utils.getVelocity(this.getDeltaTime(), this.getDeltaY());
  507. },
  508. getVelocity: function() {
  509. return this._cacheArray.getCachedValueOrUpdate(Finger.cacheIndexes.velocity, this._getVelocity, this);
  510. },
  511. _getVelocity: function() {
  512. return Utils.getVelocity(this.getDeltaTime(), this.getDeltaDistance());
  513. },
  514. getVelocityAverage: function() {
  515. return this._cacheArray.getCachedValueOrUpdate(Finger.cacheIndexes.velocityAverage, this._getVelocity, this);
  516. },
  517. _getVelocityAverage: function() {
  518. return Utils.getVelocity(this.getTotalTime(), this.getDistance());
  519. },
  520. getOrientedVelocityX: function() {
  521. return this._cacheArray.getCachedValueOrUpdate(Finger.cacheIndexes.orientedVelocityX, this._getOrientedVelocityX, this);
  522. },
  523. _getOrientedVelocityX: function() {
  524. return Utils.getOrientedVelocity(this.getDeltaTime(), this.getDeltaX());
  525. },
  526. getOrientedVelocityY: function() {
  527. return this._cacheArray.getCachedValueOrUpdate(Finger.cacheIndexes.orientedVelocityY, this._getOrientedVelocityY, this);
  528. },
  529. _getOrientedVelocityY: function() {
  530. return Utils.getOrientedVelocity(this.getDeltaTime(), this.getDeltaY());
  531. }
  532. };
  533. Fingers.Finger = Finger;
  534. var Position = function(pTimestamp, pX, pY) {
  535. this.set(pTimestamp, pX, pY);
  536. };
  537. Position.prototype = {
  538. /**
  539. * @property timestamp
  540. * @type {Number}
  541. */
  542. timestamp: null,
  543. /**
  544. * @property x
  545. * @type {Number}
  546. */
  547. x: null,
  548. /**
  549. * @property y
  550. * @type {Number}
  551. */
  552. y: null,
  553. set: function(pTimestamp, pX, pY) {
  554. this.timestamp = pTimestamp;
  555. this.x = pX;
  556. this.y = pY;
  557. },
  558. copy: function(pPosition) {
  559. this.timestamp = pPosition.timestamp;
  560. this.x = pPosition.x;
  561. this.y = pPosition.y;
  562. }
  563. };
  564. Fingers.Position = Position;
  565. /**
  566. * @module fingers
  567. *
  568. * @class FingerUtils
  569. */
  570. var FingerUtils = {
  571. getFingersAngle: function(pFinger1, pFinger2) {
  572. return Utils.getAngle(pFinger2.currentP.x - pFinger1.currentP.x, pFinger2.currentP.y - pFinger1.currentP.y);
  573. },
  574. getFingersDistance: function(pFinger1, pFinger2) {
  575. return Utils.getDistance(pFinger2.currentP.x - pFinger1.currentP.x, pFinger2.currentP.y - pFinger1.currentP.y);
  576. },
  577. getFingersCenter: function(pFinger1, pFinger2) {
  578. return {
  579. x: Math.round((pFinger1.currentP.x + pFinger2.currentP.x) / 2),
  580. y: Math.round((pFinger1.currentP.y + pFinger2.currentP.y) / 2)
  581. };
  582. },
  583. getMultipleFingersCenter: function(pFinger1, pFinger2, pFinger3, pFinger4, pFinger5) {
  584. var center = {
  585. x: 0,
  586. y: 0
  587. };
  588. var size = arguments.length;
  589. for(var i= 0; i<size; i++) {
  590. center.x += arguments[i].currentP.x;
  591. center.y += arguments[i].currentP.y;
  592. }
  593. center.x = Math.round(center.x / size);
  594. center.y = Math.round(center.y / size);
  595. return center;
  596. }
  597. };
  598. Fingers.FingerUtils = FingerUtils;
  599. /**
  600. * @module fingers
  601. *
  602. * @class Gesture
  603. * @constructor
  604. * @param {Object} pOptions
  605. * @param {Object} pDefaultOptions
  606. * @return {Gesture}
  607. */
  608. var Gesture = function(pOptions, pDefaultOptions) {
  609. this.options = Fingers.__extend({}, pDefaultOptions || {}, pOptions || {});
  610. this._handlerList = [];
  611. this.listenedFingers = [];
  612. };
  613. Gesture.EVENT_TYPE = {
  614. instant: "instant",
  615. start: "start",
  616. end: "end",
  617. move: "move"
  618. };
  619. Gesture.prototype = {
  620. options: null,
  621. _handlerList: null,
  622. _handlerListSize: 0,
  623. isListening: false,
  624. listenedFingers: null,
  625. /*---- Handlers ----*/
  626. addHandler: function(pHandler) {
  627. this._handlerList.push(pHandler);
  628. this._handlerListSize++;
  629. return this;
  630. },
  631. removeHandler: function(pHandler) {
  632. var index = this._handlerList.indexOf(pHandler);
  633. this._handlerList.splice(index, 1);
  634. this._handlerListSize--;
  635. return this;
  636. },
  637. removeAllHandlers: function() {
  638. this._handlerList.length = 0;
  639. this._handlerListSize = 0;
  640. return this;
  641. },
  642. fire: function(pType, pData) {
  643. for(var i=0; i<this._handlerListSize; i++) {
  644. this._handlerList[i](pType, pData, this.listenedFingers);
  645. }
  646. },
  647. /*---- Fingers events ----*/
  648. _onFingerAdded: function(pNewFinger, pFingerList) { /*To Override*/ },
  649. _onFingerUpdate: function(pFinger) { /*To Override*/ },
  650. _onFingerRemoved: function(pFinger) { /*To Override*/ },
  651. /*---- Actions ----*/
  652. _addListenedFingers: function(pFinger1, pFinger2, pFinger3) {
  653. for(var i= 0, size=arguments.length; i<size; i++) {
  654. this._addListenedFinger(arguments[i]);
  655. }
  656. },
  657. _addListenedFinger: function(pFinger) {
  658. this.listenedFingers.push(pFinger);
  659. pFinger._addHandlerObject(this);
  660. if(!this.isListening) {
  661. this.isListening = true;
  662. }
  663. },
  664. _removeListenedFingers: function(pFinger1, pFinger2, pFinger3) {
  665. for(var i= 0, size=arguments.length; i<size; i++) {
  666. this._removeListenedFinger(arguments[i]);
  667. }
  668. },
  669. _removeListenedFinger: function(pFinger) {
  670. pFinger._removeHandlerObject(this);
  671. var index = this.listenedFingers.indexOf(pFinger);
  672. this.listenedFingers.splice(index, 1);
  673. if(this.listenedFingers.length === 0) {
  674. this.isListening = false;
  675. }
  676. },
  677. _removeAllListenedFingers: function() {
  678. var finger;
  679. for(var i= 0, size=this.listenedFingers.length; i<size; i++) {
  680. finger = this.listenedFingers[i];
  681. finger._removeHandlerObject(this);
  682. }
  683. this.listenedFingers.length = 0;
  684. this.isListening = false;
  685. },
  686. /*---- Utils ----*/
  687. isListenedFinger: function(pFinger) {
  688. return (this.isListening && this.getListenedPosition(pFinger) > -1);
  689. },
  690. getListenedPosition: function(pFinger) {
  691. return this.listenedFingers.indexOf(pFinger);
  692. }
  693. };
  694. Fingers.Gesture = Gesture;
  695. Fingers.gesture = {
  696. };
  697. /**
  698. * @module gestures
  699. *
  700. * @class Drag
  701. * @constructor
  702. * @param {Object} pOptions
  703. * @return {Drag}
  704. */
  705. var Drag = (function (_super) {
  706. function Drag(pOptions) {
  707. _super.call(this, pOptions);
  708. }
  709. Fingers.__extend(Drag.prototype, _super.prototype, {
  710. _onFingerAdded: function(pNewFinger, pFingerList) {
  711. if(!this.isListening) {
  712. this._addListenedFinger(pNewFinger);
  713. this.fire(_super.EVENT_TYPE.start, null);
  714. }
  715. },
  716. _onFingerUpdate: function(pFinger) {
  717. this.fire(_super.EVENT_TYPE.move, null);
  718. },
  719. _onFingerRemoved: function(pFinger) {
  720. this.fire(_super.EVENT_TYPE.end, null);
  721. this._removeAllListenedFingers();
  722. }
  723. });
  724. return Drag;
  725. })(Fingers.Gesture);
  726. Fingers.gesture.Drag = Drag;
  727. /**
  728. * @module gestures
  729. *
  730. * @class Hold
  731. * @constructor
  732. * @param {Object} pOptions
  733. * @return {Swipe}
  734. */
  735. var Hold = (function (_super) {
  736. var DEFAULT_OPTIONS = {
  737. nbFingers: 1,
  738. disanceThreshold: 10,
  739. duration: 500
  740. };
  741. function Hold(pOptions) {
  742. _super.call(this, pOptions, DEFAULT_OPTIONS);
  743. this._onHoldTimeLeftF = this._onHoldTimeLeft.bind(this);
  744. }
  745. Fingers.__extend(Hold.prototype, _super.prototype, {
  746. timer: null,
  747. _onFingerAdded: function(pNewFinger, pFingerList) {
  748. if(!this.isListening && pFingerList.length >= this.options.nbFingers) {
  749. for(var i=0; i<this.options.nbFingers; i++) {
  750. this._addListenedFinger(pFingerList[i]);
  751. }
  752. clearTimeout(this.timer);
  753. this.timer = setTimeout(this._onHoldTimeLeftF, this.options.duration);
  754. }
  755. },
  756. _onFingerUpdate: function(pFinger) {
  757. var size = this.listenedFingers.length;
  758. for(var i= 0; i<size; i++) {
  759. if(this.listenedFingers[i].getDistance() > this.options.disanceThreshold) {
  760. this._onHoldCancel();
  761. break;
  762. }
  763. }
  764. },
  765. _onFingerRemoved: function(pFinger) {
  766. this._onHoldCancel();
  767. },
  768. _onHoldTimeLeftF: null,
  769. _onHoldTimeLeft: function() {
  770. this.fire(_super.EVENT_TYPE.instant, null);
  771. },
  772. _onHoldCancel: function() {
  773. clearTimeout(this.timer);
  774. this._removeAllListenedFingers();
  775. }
  776. });
  777. return Hold;
  778. })(Fingers.Gesture);
  779. Fingers.gesture.Hold = Hold;
  780. /**
  781. * @module gestures
  782. *
  783. * @class Pinch
  784. * @constructor
  785. * @param {Object} pOptions
  786. * @return {Pinch}
  787. */
  788. var Pinch = (function (_super) {
  789. var DEFAULT_OPTIONS = {
  790. pinchInDetect: 0.6,
  791. pinchOutDetect: 1.4
  792. };
  793. function Pinch(pOptions) {
  794. _super.call(this, pOptions, DEFAULT_OPTIONS);
  795. this.data = {
  796. grow: null,
  797. scale: 1
  798. };
  799. }
  800. Fingers.__extend(Pinch.prototype, _super.prototype, {
  801. _startDistance: 0,
  802. data: null,
  803. _onFingerAdded: function(pNewFinger, pFingerList) {
  804. if(!this.isListening && pFingerList.length >= 2) {
  805. this._addListenedFingers(pFingerList[0], pFingerList[1]);
  806. this._startDistance = this._getFingersDistance();
  807. }
  808. },
  809. _onFingerUpdate: function(pFinger) {},
  810. _onFingerRemoved: function(pFinger) {
  811. var newDistance = this._getFingersDistance();
  812. var scale = newDistance / this._startDistance;
  813. if(scale <= this.options.pinchInDetect || scale >= this.options.pinchOutDetect) {
  814. this.data.grow = (scale > 1) ? Utils.GROW.OUT : Utils.GROW.IN;
  815. this.data.scale = scale;
  816. this.fire(_super.EVENT_TYPE.instant, this.data);
  817. }
  818. this._removeAllListenedFingers();
  819. },
  820. _getFingersDistance: function() {
  821. var finger1P = this.listenedFingers[0].currentP;
  822. var finger2P = this.listenedFingers[1].currentP;
  823. return Fingers.Utils.getDistance(finger2P.x - finger1P.x, finger2P.y - finger1P.y);
  824. }
  825. });
  826. return Pinch;
  827. })(Fingers.Gesture);
  828. Fingers.gesture.Pinch = Pinch;
  829. /**
  830. * @module gestures
  831. *
  832. * @class Raw
  833. * @constructor
  834. * @param {Object} pOptions
  835. * @return {Raw}
  836. */
  837. var Raw = (function (_super) {
  838. var DEFAULT_OPTIONS = {
  839. nbMaxFingers: Number.MAX_VALUE
  840. };
  841. function Raw(pOptions) {
  842. _super.call(this, pOptions, DEFAULT_OPTIONS);
  843. }
  844. Fingers.__extend(Raw.prototype, _super.prototype, {
  845. _onFingerAdded: function(pNewFinger, pFingerList) {
  846. if(this.listenedFingers.length < this.options.nbMaxFingers) {
  847. this._addListenedFinger(pNewFinger);
  848. this.fire(_super.EVENT_TYPE.start, pNewFinger);
  849. }
  850. },
  851. _onFingerUpdate: function(pFinger) {
  852. this.fire(_super.EVENT_TYPE.move, pFinger);
  853. },
  854. _onFingerRemoved: function(pFinger) {
  855. this.fire(_super.EVENT_TYPE.end, pFinger);
  856. this._removeListenedFinger(pFinger);
  857. }
  858. });
  859. return Raw;
  860. })(Fingers.Gesture);
  861. Fingers.gesture.Raw = Raw;
  862. /**
  863. * @module gestures
  864. *
  865. * @class Swipe
  866. * @constructor
  867. * @param {Object} pOptions
  868. * @return {Swipe}
  869. */
  870. var Swipe = (function (_super) {
  871. var DEFAULT_OPTIONS = {
  872. nbFingers: 1,
  873. swipeVelocityX: 0.6,
  874. swipeVelocityY: 0.6
  875. };
  876. function Swipe(pOptions) {
  877. _super.call(this, pOptions, DEFAULT_OPTIONS);
  878. this.data = {
  879. direction: null,
  880. velocity: 0
  881. };
  882. }
  883. Fingers.__extend(Swipe.prototype, _super.prototype, {
  884. data: null,
  885. _onFingerAdded: function(pNewFinger, pFingerList) {
  886. if(!this.isListening && pFingerList.length >= this.options.nbFingers) {
  887. for(var i=0; i<this.options.nbFingers; i++) {
  888. this._addListenedFinger(pFingerList[i]);
  889. }
  890. }
  891. },
  892. _onFingerUpdate: function(pFinger) {
  893. },
  894. _onFingerRemoved: function(pFinger) {
  895. var isSameDirection = true;
  896. var direction = this.listenedFingers[0].getDeltaDirection();
  897. var maxVelocityX = 0;
  898. var maxVelocityY = 0;
  899. var size = this.listenedFingers.length;
  900. for(var i= 0; i<size; i++) {
  901. isSameDirection = isSameDirection && (direction === this.listenedFingers[i].getDeltaDirection());
  902. maxVelocityX = Math.max(maxVelocityX, this.listenedFingers[i].getVelocityX());
  903. maxVelocityY = Math.max(maxVelocityY, this.listenedFingers[i].getVelocityY());
  904. }
  905. if(isSameDirection &&
  906. (maxVelocityX > this.options.swipeVelocityX || maxVelocityY > this.options.swipeVelocityY)) {
  907. this.data.direction = direction;
  908. this.data.velocity = (maxVelocityX > this.options.swipeVelocityX) ? maxVelocityX : maxVelocityY;
  909. this.fire(_super.EVENT_TYPE.instant, this.data);
  910. }
  911. this._removeAllListenedFingers();
  912. }
  913. });
  914. return Swipe;
  915. })(Fingers.Gesture);
  916. Fingers.gesture.Swipe = Swipe;
  917. /**
  918. * @module gestures
  919. *
  920. * @class Tap
  921. * @constructor
  922. * @param {Object} pOptions
  923. * @return {Swipe}
  924. */
  925. var Tap = (function (_super) {
  926. var DEFAULT_OPTIONS = {
  927. nbFingers: 1,
  928. nbTapMin: 0,
  929. nbTapMax: Number.MAX_VALUE,
  930. tapInterval: 400,
  931. maxDistanceMoving: Number.MAX_VALUE
  932. };
  933. function Tap(pOptions) {
  934. _super.call(this, pOptions, DEFAULT_OPTIONS);
  935. this.data = {
  936. nbTap: 0,
  937. lastTapTimestamp: 0
  938. };
  939. }
  940. Fingers.__extend(Tap.prototype, _super.prototype, {
  941. data: null,
  942. _onFingerAdded: function(pNewFinger, pFingerList) {
  943. if(!this.isListening && pFingerList.length >= this.options.nbFingers) {
  944. if((pNewFinger.getTime() - this.data.lastTapTimestamp) > this.options.tapInterval) {
  945. this._clearTap();
  946. }
  947. for(var i=0; i<this.options.nbFingers; i++) {
  948. this._addListenedFinger(pFingerList[i]);
  949. }
  950. }
  951. },
  952. _onFingerUpdate: function(pFinger) {
  953. },
  954. _onFingerRemoved: function(pFinger) {
  955. this._removeAllListenedFingers();
  956. if(pFinger.getTotalTime() < this.options.tapInterval &&
  957. pFinger.getDistance() < this.options.maxDistanceMoving) {
  958. this.data.lastTapTimestamp = pFinger.getTime();
  959. this.data.nbTap++;
  960. if(this.data.nbTap >= this.options.nbTapMin && this.data.nbTap <= this.options.nbTapMax) {
  961. this.fire(_super.EVENT_TYPE.instant, this.data);
  962. }
  963. }
  964. },
  965. _clearTap: function() {
  966. this.data.lastTapTimestamp = 0;
  967. this.data.nbTap = 0;
  968. }
  969. });
  970. return Tap;
  971. })(Fingers.Gesture);
  972. Fingers.gesture.Tap = Tap;
  973. /**
  974. * @module gestures
  975. *
  976. * @class Transform
  977. * @constructor
  978. * @param {Object} pOptions
  979. * @param {Function} pHandler
  980. * @return {Transform}
  981. */
  982. var Transform = (function (_super) {
  983. var DEFAULT_OPTIONS = {
  984. rotation: true,
  985. scale: true
  986. };
  987. function Transform(pOptions) {
  988. _super.call(this, pOptions, DEFAULT_OPTIONS);
  989. this.data = {
  990. totalRotation: 0,
  991. deltaRotation: 0,
  992. totalScale: 1,
  993. deltaScale: 1
  994. };
  995. }
  996. Fingers.__extend(Transform.prototype, _super.prototype, {
  997. _startAngle: 0,
  998. _lastAngle: 0,
  999. _startDistance: 0,
  1000. _lastDistance: 0,
  1001. data: null,
  1002. _onFingerAdded: function(pNewFinger, pFingerList) {
  1003. if(!this.isListening && pFingerList.length >= 2) {
  1004. this._addListenedFingers(pFingerList[0], pFingerList[1]);
  1005. if(this.options.rotation) {
  1006. this._lastAngle = this._getFingersAngle();
  1007. this._startAngle = this._lastAngle;
  1008. this.data.totalRotation = 0;
  1009. this.data.deltaRotation = 0;
  1010. }
  1011. if(this.options.scale) {
  1012. this._lastDistance = this._getFingersDistance();
  1013. this._startDistance = this._lastDistance;
  1014. this.data.totalScale = 1;
  1015. this.data.deltaScale = 1;
  1016. }
  1017. this.fire(_super.EVENT_TYPE.start, this.data);
  1018. }
  1019. },
  1020. _onFingerUpdate: function(pFinger) {
  1021. if(this.options.rotation) {
  1022. var newAngle = this._getFingersAngle();
  1023. this.data.totalRotation = this._startAngle - newAngle;
  1024. this.data.deltaRotation = this._lastAngle - newAngle;
  1025. this._lastAngle = newAngle;
  1026. }
  1027. if(this.options.scale) {
  1028. var newDistance = this._getFingersDistance();
  1029. this.data.totalScale = newDistance / this._startDistance;
  1030. this.data.deltaScale = newDistance / this._lastDistance;
  1031. this._lastDistance = newDistance;
  1032. }
  1033. this.fire(_super.EVENT_TYPE.move, this.data);
  1034. },
  1035. _onFingerRemoved: function(pFinger) {
  1036. this.fire(_super.EVENT_TYPE.end, this.data);
  1037. this._removeAllListenedFingers();
  1038. },
  1039. _getFingersAngle: function() {
  1040. return Fingers.FingerUtils.getFingersAngle(this.listenedFingers[0], this.listenedFingers[1]);
  1041. },
  1042. _getFingersDistance: function() {
  1043. return Fingers.FingerUtils.getFingersDistance(this.listenedFingers[0], this.listenedFingers[1]);
  1044. }
  1045. });
  1046. return Transform;
  1047. })(Fingers.Gesture);
  1048. Fingers.gesture.Transform = Transform;
  1049. /**
  1050. * @module gestures
  1051. *
  1052. * @class Rotate
  1053. * @constructor
  1054. * @param {Object} pOptions
  1055. * @param {Function} pHandler
  1056. * @return {Rotate}
  1057. */
  1058. var Rotate = (function (_super) {
  1059. function Rotate(pOptions) {
  1060. pOptions = pOptions || {};
  1061. pOptions.rotation = true;
  1062. pOptions.scale = false;
  1063. _super.call(this, pOptions);
  1064. }
  1065. Fingers.__extend(Rotate.prototype, _super.prototype);
  1066. return Rotate;
  1067. })(Transform);
  1068. Fingers.gesture.Rotate = Rotate;
  1069. /**
  1070. * @module gestures
  1071. *
  1072. * @class Scale
  1073. * @constructor
  1074. * @param {Object} pOptions
  1075. * @return {Pinch}
  1076. */
  1077. var Scale = (function (_super) {
  1078. function Scale(pOptions) {
  1079. pOptions = pOptions || {};
  1080. pOptions.rotation = false;
  1081. pOptions.scale = true;
  1082. _super.call(this, pOptions);
  1083. }
  1084. Fingers.__extend(Scale.prototype, _super.prototype);
  1085. return Scale;
  1086. })(Transform);
  1087. Fingers.gesture.Scale = Scale;
  1088. /**
  1089. * @module gestures
  1090. *
  1091. * @class ZoneHover
  1092. * @constructor
  1093. * @param {Object} pOptions
  1094. * @return {ZoneHover}
  1095. */
  1096. var ZoneHover = (function (_super) {
  1097. var DEFAULT_OPTIONS = {
  1098. };
  1099. function ZoneHover(pOptions) {
  1100. _super.call(this, pOptions, DEFAULT_OPTIONS);
  1101. this._zoneList = [];
  1102. this._zoneMap = {};
  1103. }
  1104. ZoneHover.TYPE = {
  1105. enter: "enter",
  1106. leave: "leave"
  1107. };
  1108. ZoneHover.LAST_ZONE_ID = 0;
  1109. Fingers.__extend(ZoneHover.prototype, _super.prototype, {
  1110. _zoneList: null,
  1111. _zoneMap: null,
  1112. _zoneSize: 0,
  1113. _onFingerAdded: function(pNewFinger, pFingerList) {
  1114. if(this.listenedFingers.length === 0) {
  1115. this._addListenedFinger(pNewFinger);
  1116. for(var i=0; i<this._zoneSize; i++) {
  1117. this._checkZone(this._zoneList[i], pNewFinger);
  1118. }
  1119. }
  1120. },
  1121. _onFingerUpdate: function(pFinger) {
  1122. for(var i=0; i<this._zoneSize; i++) {
  1123. this._checkZone(this._zoneList[i], pFinger);
  1124. }
  1125. },
  1126. _onFingerRemoved: function(pFinger) {
  1127. var zone;
  1128. for(var i=0; i<this._zoneSize; i++) {
  1129. zone = this._zoneList[i];
  1130. if(this._zoneMap[zone.id] === true) {
  1131. this._fireLeaveZone(zone);
  1132. }
  1133. }
  1134. this._removeListenedFinger(pFinger);
  1135. },
  1136. /**
  1137. * @typedef Zone
  1138. * @type {Object}
  1139. * @property {number} id
  1140. * @property {number} left
  1141. * @property {number} right
  1142. * @property {number} top
  1143. * @property {number} bottom
  1144. */
  1145. /**
  1146. * @param {Zone} pZone
  1147. */
  1148. addZone: function(pZone) {
  1149. if(this._zoneList.indexOf(pZone) === -1) {
  1150. if(pZone.id === undefined) {
  1151. pZone.id = ZoneHover.LAST_ZONE_ID++;
  1152. }
  1153. this._zoneList.push(pZone);
  1154. this._zoneMap[pZone.id] = false;
  1155. this._zoneSize++;
  1156. }
  1157. return this;
  1158. },
  1159. /**
  1160. * @param {Zone} pZone
  1161. */
  1162. removeZone: function(pZone) {
  1163. var index = this._zoneList.indexOf(pZone);
  1164. if(index !== -1) {
  1165. this._zoneList.splice(index, 1);
  1166. delete this._zoneMap[pZone.id];
  1167. this._zoneSize--;
  1168. }
  1169. return this;
  1170. },
  1171. getHoveredZones: function() {
  1172. var enteredZones = [];
  1173. var zone;
  1174. for(var i=0; i<this._zoneSize; i++) {
  1175. zone = this._zoneList[i];
  1176. if(this._zoneMap[zone.id] === true) {
  1177. enteredZones.push(zone);
  1178. }
  1179. }
  1180. return enteredZones;
  1181. },
  1182. _checkZone: function(pZone, pFinger) {
  1183. var isInZone = this._isInZone(pZone, pFinger.getX(), pFinger.getY());
  1184. if(this._zoneMap[pZone.id] === false && isInZone) {
  1185. this._zoneMap[pZone.id] = true;
  1186. this._fireEnterZone(pZone);
  1187. }
  1188. else if(this._zoneMap[pZone.id] === true && !isInZone) {
  1189. this._zoneMap[pZone.id] = false;
  1190. this._fireLeaveZone(pZone);
  1191. }
  1192. },
  1193. _fireEnterZone: function(pZone) {
  1194. this.fire(_super.EVENT_TYPE.instant, {
  1195. type: ZoneHover.TYPE.enter,
  1196. zone: pZone
  1197. });
  1198. },
  1199. _fireLeaveZone: function(pZone) {
  1200. this.fire(_super.EVENT_TYPE.instant, {
  1201. type: ZoneHover.TYPE.leave,
  1202. zone: pZone
  1203. });
  1204. },
  1205. _isInZone: function(pZone, pX, pY) {
  1206. return (pX >= pZone.left &&
  1207. pX <= pZone.right &&
  1208. pY >= pZone.top &&
  1209. pY <= pZone.bottom);
  1210. }
  1211. });
  1212. return ZoneHover;
  1213. })(Fingers.Gesture);
  1214. Fingers.gesture.ZoneHover = ZoneHover;
  1215. /**
  1216. * @module fingers
  1217. */
  1218. // AMD export
  1219. if(typeof define == 'function' && define.amd) {
  1220. define(function() {
  1221. return Fingers;
  1222. });
  1223. // commonjs export
  1224. } else if(typeof module !== 'undefined' && module.exports) {
  1225. module.exports = Fingers;
  1226. // browser export
  1227. } else {
  1228. window.Fingers = Fingers;
  1229. }
  1230. })(window);