ViewportControls.js 4.2 KB

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