ViewportControls.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 cente ron the pointer position.
  36. *
  37. * The viewport will simulataniously move to the pointer position while scalling.
  38. *
  39. * For some application its easier to focus the target if the viewport moves to the pointer location while scalling.
  40. *
  41. * @type {boolean}
  42. */
  43. this.centerOnPointer = false;
  44. /**
  45. * Flag to recenter the viewport automatically to the canvas.
  46. *
  47. * This will ensure that rotation and scaling will not cause the viewport to move around.
  48. *
  49. * @type {boolean}
  50. * @default true
  51. */
  52. this.centerOnCanvas = true;
  53. /**
  54. * If true allows the viewport to be rotated.
  55. *
  56. * Rotation is performed by holding the RIGHT and LEFT pointer buttons and rotating around the initial point.
  57. *
  58. * @type {boolean}
  59. */
  60. this.allowRotation = true;
  61. /**
  62. * Value of the initial point of rotation if the viewport is being rotated.
  63. *
  64. * Is the value of the pointer position when the rotation starts.
  65. *
  66. * Is set to null when the viewport is not being rotated.
  67. *
  68. * @type {Vector2 | null}
  69. */
  70. this.rotationPoint = null;
  71. /**
  72. * Initial rotation of the viewport.
  73. *
  74. * Is set to the current rotation of the viewport when the rotation starts.
  75. *
  76. * @type {number}
  77. */
  78. this.rotationInitial = 0;
  79. }
  80. /**
  81. * Update the viewport controls using the pointer object.
  82. *
  83. * Should be called every frame before rendering.
  84. *
  85. * @param {Pointer} pointer Pointer used to control the viewport.
  86. */
  87. ViewportControls.prototype.update = function(pointer)
  88. {
  89. // Scale
  90. if(this.allowScale && pointer.wheel !== 0)
  91. {
  92. var scale = pointer.wheel * 1e-3 * this.viewport.scale;
  93. this.viewport.scale -= scale;
  94. this.viewport.matrixNeedsUpdate = true;
  95. }
  96. // Rotation
  97. if(this.allowRotation && pointer.buttonPressed(Pointer.RIGHT) && pointer.buttonPressed(Pointer.LEFT))
  98. {
  99. // Rotation pivot
  100. if(this.rotationPoint === null)
  101. {
  102. this.rotationPoint = pointer.position.clone();
  103. this.rotationInitial = this.viewport.rotation;
  104. }
  105. else
  106. {
  107. var point = pointer.position.clone();
  108. point.sub(this.rotationPoint);
  109. this.viewport.rotation = this.rotationInitial + point.angle();
  110. this.viewport.matrixNeedsUpdate = true;
  111. }
  112. } else {
  113. this.rotationPoint = null;
  114. }
  115. // Drag
  116. if(pointer.buttonPressed(this.dragButton))
  117. {
  118. this.viewport.position.add(pointer.delta);
  119. this.viewport.matrixNeedsUpdate = true;
  120. }
  121. // Automtical center the viewport.
  122. if (this.centerOnCanvas && pointer.canvas !== null) {
  123. var centerWorld = new Vector2(pointer.canvas.width / 2.0, pointer.canvas.height / 2.0);
  124. centerWorld = this.viewport.inverseMatrix.transformPoint(centerWorld);
  125. this.viewport.center.copy(centerWorld);
  126. this.viewport.matrixNeedsUpdate = true;
  127. }
  128. // Center on pointer
  129. else if(this.centerOnPointer && pointer.canvas !== null)
  130. {
  131. var pointerWorld = this.viewport.inverseMatrix.transformPoint(pointer.position)
  132. this.viewport.center.copy(pointerWorld);
  133. this.viewport.matrixNeedsUpdate = true;
  134. }
  135. };
  136. export {ViewportControls};