SelectionHelper.js 2.0 KB

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