SelectionHelper.js 3.4 KB

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