Show:
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. *
  5. * @license MPL 2.0
  6. * @copyright Famous Industries, Inc. 2014
  7. */
  8.  
  9. /* Modified work copyright © 2015-2016 David Valdman */
  10.  
  11. define(function(require, exports, module) {
  12. var TwoFingerInput = require('../inputs/TwoFingerInput');
  13. var OptionsManager = require('../core/OptionsManager');
  14.  
  15. /**
  16. * Detects two-finger pinching motion and emits `start`, `update` and
  17. * `end` events with the payload data:
  18. *
  19. * `value` - Distance between the two touches
  20. * `delta` - Differential in successive distances
  21. * `velocity` - Relative velocity between two touches
  22. * `displacement` - Total accumulated displacement
  23. * `center` - Midpoint between the two touches
  24. * `touchIds` - Array of DOM event touch identifiers
  25. *
  26. * @example
  27. *
  28. * var pinchInput = new PinchInput();
  29. *
  30. * pinchInput.subscribe(Engine) // listens on `window` events
  31. *
  32. * pinchInput.on('start', function(payload){
  33. * console.log('start', payload);
  34. * });
  35. *
  36. * pinchInput.on('update', function(payload){
  37. * console.log('update', payload);
  38. * });
  39. *
  40. * pinchInput.on('end', function(payload){
  41. * console.log('end', payload);
  42. * });
  43. *
  44. * @class PinchInput
  45. * @extends Inputs.TwoFingerInput
  46. * @uses Core.OptionsManager
  47. * @constructor
  48. * @param options {Object} Options
  49. * @param [options.scale=1] {Number} Scale the response to pinch
  50. */
  51. function PinchInput(options) {
  52. TwoFingerInput.call(this);
  53.  
  54. this.options = OptionsManager.setOptions(this, options);
  55.  
  56. this._displacement = 0;
  57. this._previousDistance = 0;
  58. }
  59.  
  60. PinchInput.prototype = Object.create(TwoFingerInput.prototype);
  61. PinchInput.prototype.constructor = PinchInput;
  62.  
  63. PinchInput.DEFAULT_OPTIONS = {
  64. scale : 1
  65. };
  66.  
  67. PinchInput.prototype._startUpdate = function _startUpdate(event) {
  68. var center = TwoFingerInput.calculateCenter(this.posA, this.posB);
  69. this._previousDistance = TwoFingerInput.calculateDistance(this.posA, this.posB);
  70.  
  71. this._displacement = 0;
  72.  
  73. this._eventOutput.emit('start', {
  74. count: event.touches.length,
  75. touchIds: [this.touchAId, this.touchBId],
  76. value: this._previousDistance,
  77. center: center
  78. });
  79. };
  80.  
  81. PinchInput.prototype._moveUpdate = function _moveUpdate(diffTime) {
  82. var currDist = TwoFingerInput.calculateDistance(this.posA, this.posB);
  83. var center = TwoFingerInput.calculateCenter(this.posA, this.posB);
  84.  
  85. var scale = this.options.scale;
  86. var delta = scale * (currDist - this._previousDistance);
  87. var velocity = delta / diffTime;
  88.  
  89. this._displacement += delta;
  90.  
  91. this._eventOutput.emit('update', {
  92. delta : delta,
  93. velocity: velocity,
  94. value: currDist,
  95. displacement: this._displacement,
  96. center: center,
  97. touchIds: [this.touchAId, this.touchBId]
  98. });
  99.  
  100. this._previousDistance = currDist;
  101. };
  102.  
  103. module.exports = PinchInput;
  104. });
  105.