ViewportControls.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. "use strict";
  2. import {Viewport} from "../Viewport.js";
  3. import {Pointer} from "../input/Pointer.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. this.viewport = viewport;
  16. /**
  17. * Button used to drag and viewport around.
  18. *
  19. * On touch enabled devices the touch event is represented as a LEFT button.
  20. */
  21. this.dragButton = Pointer.RIGHT;
  22. /**
  23. * Is set to true allow the viewport to be scalled.
  24. *
  25. * Scaling is performed using the pointer scroll.
  26. */
  27. this.allowScale = true;
  28. /**
  29. * Flag to indicate if the viewport should move when scalling.
  30. *
  31. * For some application its easier to focus the target if the viewport moves to the pointer location while scalling.
  32. */
  33. this.moveOnScale = true;
  34. /**
  35. * If true allows the viewport to be rotated.
  36. *
  37. * Rotation is performed by holding the RIGHT and LEFT pointer buttons and rotating around the initial point.
  38. */
  39. this.allowRotation = false;
  40. /**
  41. * Value of the initial point of rotation if the viewport is being rotated.
  42. *
  43. * Is set to null when the viewport is not being rotated.
  44. */
  45. this.rotationPoint = null;
  46. /**
  47. * Initial rotation of the viewport.
  48. */
  49. this.rotationInitial = 0;
  50. }
  51. /**
  52. * Update the viewport controls using the pointer object.
  53. *
  54. * Should be called every frame before rendering.
  55. *
  56. * @param {Pointer} pointer
  57. */
  58. ViewportControls.prototype.update = function(pointer)
  59. {
  60. if(this.allowScale && pointer.wheel !== 0)
  61. {
  62. this.viewport.scale -= pointer.wheel * 1e-3 * this.viewport.scale;
  63. if(this.moveOnScale)
  64. {
  65. var speed = pointer.wheel;
  66. var halfWidth = pointer.canvas.width / 2;
  67. var halfWeight = pointer.canvas.height / 2;
  68. this.viewport.position.x += ((pointer.position.x - halfWidth) / halfWidth) * speed;
  69. this.viewport.position.y += ((pointer.position.y - halfWeight) / halfWeight) * speed;
  70. }
  71. }
  72. if(this.allowRotation && pointer.buttonPressed(Pointer.RIGHT) && pointer.buttonPressed(Pointer.LEFT))
  73. {
  74. if(this.rotationPoint === null)
  75. {
  76. this.rotationPoint = pointer.position.clone();
  77. this.rotationInitial = this.viewport.rotation;
  78. }
  79. else
  80. {
  81. var pointer = pointer.position.clone();
  82. pointer.sub(this.rotationPoint);
  83. this.viewport.rotation = this.rotationInitial + pointer.angle();
  84. }
  85. }
  86. else
  87. {
  88. this.rotationPoint = null;
  89. if(pointer.buttonPressed(this.dragButton))
  90. {
  91. this.viewport.position.x += pointer.delta.x;
  92. this.viewport.position.y += pointer.delta.y;
  93. }
  94. }
  95. };
  96. export {ViewportControls};