ViewportControls.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import {Viewport} from "../Viewport.js";
  2. import {Pointer} from "../input/Pointer.js";
  3. import {Vector2} from "../math/Vector2.js";
  4. /**
  5. * Viewport controls are used to allow the user to control the viewport.
  6. *
  7. * The user controls the viewport using pointer input (e.g. mouse, touchscreen)
  8. *
  9. * @class
  10. * @param {Viewport} viewport
  11. */
  12. function ViewportControls(viewport)
  13. {
  14. /**
  15. * Viewport being controlled by this object.
  16. *
  17. * @type {Viewport}
  18. */
  19. this.viewport = viewport;
  20. /**
  21. * Button used to drag and viewport around.
  22. *
  23. * On touch enabled devices the touch event is represented as a LEFT button.
  24. *
  25. * @type {number}
  26. */
  27. this.dragButton = Pointer.RIGHT;
  28. /**
  29. * Button used to rotate the viewport.
  30. *
  31. * @type {number}
  32. */
  33. this.rotateButton = Pointer.MIDDLE;
  34. /**
  35. * Is set to true allow the viewport to be scalled.
  36. *
  37. * Scaling is performed using the pointer scroll.
  38. *
  39. * @type {boolean}
  40. */
  41. this.allowScale = true;
  42. /**
  43. * Flag to indicate if the viewport should automatically be recentered.
  44. *
  45. * This will cause the viewport center property to be automatically set based on an heuristic defined by the user.
  46. *
  47. * @type {number}
  48. */
  49. this.recenterViewport = ViewportControls.RECENTER_POINTER;
  50. /**
  51. * If true allows the viewport to be rotated.
  52. *
  53. * Rotation is performed by holding the RIGHT and LEFT pointer buttons and rotating around the initial point.
  54. *
  55. * @type {boolean}
  56. */
  57. this.allowRotation = true;
  58. /**
  59. * Value of the initial point of rotation if the viewport is being rotated.
  60. *
  61. * Is the value of the pointer position when the rotation starts.
  62. *
  63. * Is set to null when the viewport is not being rotated.
  64. *
  65. * @type {Vector2 | null}
  66. */
  67. this.rotationPoint = null;
  68. /**
  69. * Initial rotation of the viewport.
  70. *
  71. * Is set to the current rotation of the viewport when the rotation starts.
  72. *
  73. * @type {number}
  74. */
  75. this.rotationInitial = 0;
  76. }
  77. /**
  78. * Viewport is not automatically recentered.
  79. *
  80. * The center point can be set manually by the developer.
  81. *
  82. * @type {number}
  83. */
  84. ViewportControls.RECENTER_NONE = 0;
  85. /**
  86. * Recenter the viewport automatically to the canvas.
  87. *
  88. * This will ensure that rotation and scaling will not cause the viewport to move around.
  89. *
  90. * @type {number}
  91. */
  92. ViewportControls.RECENTER_CANVAS = 1;
  93. /**
  94. * Viewport should automatically center on the pointer position.
  95. *
  96. * The viewport will simulataniously move to the pointer position while scalling.
  97. *
  98. * For some application its easier to focus the target if the viewport moves to the pointer location while scalling.
  99. *
  100. * @type {number}
  101. */
  102. ViewportControls.RECENTER_POINTER = 2;
  103. /**
  104. * Update the viewport controls using the pointer object.
  105. *
  106. * Should be called every frame before rendering.
  107. *
  108. * @param {Pointer} pointer Pointer used to control the viewport.
  109. */
  110. ViewportControls.prototype.update = function(pointer)
  111. {
  112. // Scale
  113. if(this.allowScale && pointer.wheel !== 0)
  114. {
  115. // Compute the pointer position in world space before applying the new scale
  116. var pointerWorld = null;
  117. if(this.recenterViewport === ViewportControls.RECENTER_POINTER && pointer.canvas !== null)
  118. {
  119. pointerWorld = this.viewport.inverseMatrix.transformPoint(pointer.position);
  120. }
  121. var scale = pointer.wheel * 1e-3 * this.viewport.scale;
  122. this.viewport.scale -= scale;
  123. this.viewport.matrixNeedsUpdate = true;
  124. // Recenter viewport on pointer only when zooming
  125. if(pointerWorld !== null)
  126. {
  127. this.viewport.center.copy(pointerWorld);
  128. this.viewport.matrixNeedsUpdate = true;
  129. }
  130. }
  131. // Rotation
  132. if(this.allowRotation && pointer.buttonPressed(this.rotateButton))
  133. {
  134. // Rotation pivot
  135. if(this.rotationPoint === null)
  136. {
  137. this.rotationPoint = pointer.position.clone();
  138. this.rotationInitial = this.viewport.rotation;
  139. }
  140. else
  141. {
  142. var point = pointer.position.clone();
  143. point.sub(this.rotationPoint);
  144. this.viewport.rotation = this.rotationInitial + point.angle();
  145. this.viewport.matrixNeedsUpdate = true;
  146. }
  147. return;
  148. } else {
  149. this.rotationPoint = null;
  150. }
  151. // Drag
  152. if(pointer.buttonPressed(this.dragButton))
  153. {
  154. this.viewport.position.add(pointer.delta);
  155. this.viewport.matrixNeedsUpdate = true;
  156. }
  157. if (pointer.canvas === null) {
  158. return;
  159. }
  160. // Center viewport on canvas
  161. if (this.recenterViewport === ViewportControls.RECENTER_CANVAS) {
  162. var centerWorld = new Vector2(pointer.canvas.width / 2.0, pointer.canvas.height / 2.0);
  163. centerWorld = this.viewport.inverseMatrix.transformPoint(centerWorld);
  164. this.viewport.center.copy(centerWorld);
  165. this.viewport.matrixNeedsUpdate = true;
  166. }
  167. };
  168. export {ViewportControls};