SelectionHelper.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { Vector2 } from 'three';
  2. /**
  3. * A helper for {@link SelectionBox}.
  4. *
  5. * It visualizes the current selection box with a `div` container element.
  6. *
  7. * @three_import import { SelectionHelper } from 'three/addons/interactive/SelectionHelper.js';
  8. */
  9. class SelectionHelper {
  10. /**
  11. * Constructs a new selection helper.
  12. *
  13. * @param {(WebGPURenderer|WebGLRenderer)} renderer - The renderer.
  14. * @param {string} cssClassName - The CSS class name of the `div`.
  15. */
  16. constructor( renderer, cssClassName ) {
  17. /**
  18. * The visualization of the selection box.
  19. *
  20. * @type {HTMLDivElement}
  21. */
  22. this.element = document.createElement( 'div' );
  23. this.element.classList.add( cssClassName );
  24. this.element.style.pointerEvents = 'none';
  25. /**
  26. * A reference to the renderer.
  27. *
  28. * @type {(WebGPURenderer|WebGLRenderer)}
  29. */
  30. this.renderer = renderer;
  31. /**
  32. * Whether the mouse or pointer is pressed down.
  33. *
  34. * @type {boolean}
  35. * @default false
  36. */
  37. this.isDown = false;
  38. /**
  39. * Whether helper is enabled or not.
  40. *
  41. * @type {boolean}
  42. * @default true
  43. */
  44. this.enabled = true;
  45. // private
  46. this._startPoint = new Vector2();
  47. this._pointTopLeft = new Vector2();
  48. this._pointBottomRight = new Vector2();
  49. this._onPointerDown = function ( event ) {
  50. if ( this.enabled === false ) return;
  51. this.isDown = true;
  52. this._onSelectStart( event );
  53. }.bind( this );
  54. this._onPointerMove = function ( event ) {
  55. if ( this.enabled === false ) return;
  56. if ( this.isDown ) {
  57. this._onSelectMove( event );
  58. }
  59. }.bind( this );
  60. this._onPointerUp = function ( ) {
  61. if ( this.enabled === false ) return;
  62. this.isDown = false;
  63. this._onSelectOver();
  64. }.bind( this );
  65. this.renderer.domElement.addEventListener( 'pointerdown', this._onPointerDown );
  66. this.renderer.domElement.addEventListener( 'pointermove', this._onPointerMove );
  67. this.renderer.domElement.addEventListener( 'pointerup', this._onPointerUp );
  68. }
  69. /**
  70. * Call this method if you no longer want use to the controls. It frees all internal
  71. * resources and removes all event listeners.
  72. */
  73. dispose() {
  74. this.renderer.domElement.removeEventListener( 'pointerdown', this._onPointerDown );
  75. this.renderer.domElement.removeEventListener( 'pointermove', this._onPointerMove );
  76. this.renderer.domElement.removeEventListener( 'pointerup', this._onPointerUp );
  77. this.element.remove(); // in case disposal happens while dragging
  78. }
  79. // private
  80. _onSelectStart( event ) {
  81. this.element.style.display = 'none';
  82. this.renderer.domElement.parentElement.appendChild( this.element );
  83. this.element.style.left = event.clientX + 'px';
  84. this.element.style.top = event.clientY + 'px';
  85. this.element.style.width = '0px';
  86. this.element.style.height = '0px';
  87. this._startPoint.x = event.clientX;
  88. this._startPoint.y = event.clientY;
  89. }
  90. _onSelectMove( event ) {
  91. this.element.style.display = 'block';
  92. this._pointBottomRight.x = Math.max( this._startPoint.x, event.clientX );
  93. this._pointBottomRight.y = Math.max( this._startPoint.y, event.clientY );
  94. this._pointTopLeft.x = Math.min( this._startPoint.x, event.clientX );
  95. this._pointTopLeft.y = Math.min( this._startPoint.y, event.clientY );
  96. this.element.style.left = this._pointTopLeft.x + 'px';
  97. this.element.style.top = this._pointTopLeft.y + 'px';
  98. this.element.style.width = ( this._pointBottomRight.x - this._pointTopLeft.x ) + 'px';
  99. this.element.style.height = ( this._pointBottomRight.y - this._pointTopLeft.y ) + 'px';
  100. }
  101. _onSelectOver() {
  102. this.element.remove();
  103. }
  104. }
  105. export { SelectionHelper };