SelectionHelper.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import {
  2. Vector2
  3. } from '../../../build/three.module.js';
  4. class SelectionHelper {
  5. constructor( selectionBox, renderer, cssClassName ) {
  6. this.element = document.createElement( 'div' );
  7. this.element.classList.add( cssClassName );
  8. this.element.style.pointerEvents = 'none';
  9. this.renderer = renderer;
  10. this.startPoint = new Vector2();
  11. this.pointTopLeft = new Vector2();
  12. this.pointBottomRight = new Vector2();
  13. this.isDown = false;
  14. this.renderer.domElement.addEventListener( 'pointerdown', function ( event ) {
  15. this.isDown = true;
  16. this.onSelectStart( event );
  17. }.bind( this ) );
  18. this.renderer.domElement.addEventListener( 'pointermove', function ( event ) {
  19. if ( this.isDown ) {
  20. this.onSelectMove( event );
  21. }
  22. }.bind( this ) );
  23. this.renderer.domElement.addEventListener( 'pointerup', function ( event ) {
  24. this.isDown = false;
  25. this.onSelectOver( event );
  26. }.bind( this ) );
  27. }
  28. onSelectStart( event ) {
  29. this.renderer.domElement.parentElement.appendChild( this.element );
  30. this.element.style.left = event.clientX + 'px';
  31. this.element.style.top = event.clientY + 'px';
  32. this.element.style.width = '0px';
  33. this.element.style.height = '0px';
  34. this.startPoint.x = event.clientX;
  35. this.startPoint.y = event.clientY;
  36. }
  37. onSelectMove( event ) {
  38. this.pointBottomRight.x = Math.max( this.startPoint.x, event.clientX );
  39. this.pointBottomRight.y = Math.max( this.startPoint.y, event.clientY );
  40. this.pointTopLeft.x = Math.min( this.startPoint.x, event.clientX );
  41. this.pointTopLeft.y = Math.min( this.startPoint.y, event.clientY );
  42. this.element.style.left = this.pointTopLeft.x + 'px';
  43. this.element.style.top = this.pointTopLeft.y + 'px';
  44. this.element.style.width = ( this.pointBottomRight.x - this.pointTopLeft.x ) + 'px';
  45. this.element.style.height = ( this.pointBottomRight.y - this.pointTopLeft.y ) + 'px';
  46. }
  47. onSelectOver() {
  48. this.element.parentElement.removeChild( this.element );
  49. }
  50. }
  51. export { SelectionHelper };